DHEIVER commited on
Commit
66d57f2
1 Parent(s): e743d4a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -29
app.py CHANGED
@@ -3,12 +3,6 @@ from tensorflow.keras.models import load_model
3
  import cv2
4
  import numpy as np
5
 
6
- # Load the trained model
7
- model = load_model('FightOS_CNN_Models.h5')
8
-
9
- # Define the class labels
10
- class_labels = ['positive', 'negative']
11
-
12
  # Function to preprocess the image (resize, normalize, etc.)
13
  def preprocess_image(image):
14
  # Resize the image to 224x224
@@ -22,7 +16,7 @@ def preprocess_image(image):
22
 
23
  return preprocessed_image
24
 
25
- # Function to segment the image using Otsu's thresholding
26
  def segment_image(image):
27
  # Convert the image to grayscale
28
  grayscale_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
@@ -30,31 +24,38 @@ def segment_image(image):
30
  # Apply Otsu's thresholding
31
  _, thresholded_image = cv2.threshold(grayscale_image, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
32
 
33
- return thresholded_image
 
 
 
 
 
34
 
35
  # Function to make the classification prediction and return the segmented image
36
  def classify_pcos(image):
37
- try:
38
- # Preprocess the image (resize, normalize, etc.)
39
- preprocessed_image = preprocess_image(image)
40
-
41
- # Make the prediction using the model
42
- prediction = model.predict(preprocessed_image)
43
-
44
- # Get the predicted class
45
- class_index = np.argmax(prediction)
46
- class_label = class_labels[class_index]
47
-
48
- # Get the probability of each class
49
- probabilities = {label: round(float(prediction[0][i]), 2) for i, label in enumerate(class_labels)}
50
-
51
- # Segment the image
52
- segmented_image = segment_image(image)
53
-
54
- return class_label, probabilities, segmented_image
55
- except Exception as e:
56
- error_message = f"Error occurred: {str(e)}"
57
- return error_message, {}, None
 
 
58
 
59
  # Gradio app interface with multiple outputs
60
  iface = gr.Interface(
 
3
  import cv2
4
  import numpy as np
5
 
 
 
 
 
 
 
6
  # Function to preprocess the image (resize, normalize, etc.)
7
  def preprocess_image(image):
8
  # Resize the image to 224x224
 
16
 
17
  return preprocessed_image
18
 
19
+ # Function to segment the image using Otsu's thresholding and post-processing
20
  def segment_image(image):
21
  # Convert the image to grayscale
22
  grayscale_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
 
24
  # Apply Otsu's thresholding
25
  _, thresholded_image = cv2.threshold(grayscale_image, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
26
 
27
+ # Perform morphological operations for post-processing
28
+ kernel = np.ones((5, 5), np.uint8)
29
+ segmented_image = cv2.morphologyEx(thresholded_image, cv2.MORPH_OPEN, kernel, iterations=2)
30
+ segmented_image = cv2.morphologyEx(segmented_image, cv2.MORPH_CLOSE, kernel, iterations=2)
31
+
32
+ return segmented_image
33
 
34
  # Function to make the classification prediction and return the segmented image
35
  def classify_pcos(image):
36
+ # Preprocess the image (resize, normalize, etc.)
37
+ preprocessed_image = preprocess_image(image)
38
+
39
+ # Make the prediction using the model
40
+ prediction = model.predict(preprocessed_image)
41
+
42
+ # Get the predicted class
43
+ class_index = np.argmax(prediction)
44
+ class_label = class_labels[class_index]
45
+
46
+ # Get the probability of each class
47
+ probabilities = {label: round(float(prediction[0][i]), 2) for i, label in enumerate(class_labels)}
48
+
49
+ # Segment the image
50
+ segmented_image = segment_image(image)
51
+
52
+ return class_label, probabilities, segmented_image
53
+
54
+ # Load the trained model
55
+ model = load_model('FightOS_CNN_Models.h5')
56
+
57
+ # Define the class labels
58
+ class_labels = ['positive', 'negative']
59
 
60
  # Gradio app interface with multiple outputs
61
  iface = gr.Interface(