itzjunayed commited on
Commit
7bc6fbb
·
verified ·
1 Parent(s): a62a422

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -10
app.py CHANGED
@@ -14,21 +14,66 @@ def predict_segmentation(image):
14
  SIZE_X = 128
15
  SIZE_Y = 128
16
 
17
- img = cv2.resize(image, (SIZE_Y, SIZE_X))
18
- img = np.expand_dims(img, axis=2)
19
- img = normalize(img, axis=1)
20
 
21
- # Prepare image for prediction
22
- img = np.expand_dims(img, axis=0)
23
 
24
- # Predict
25
- prediction = model.predict(img)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  predicted_img = np.argmax(prediction, axis=3)[0, :, :]
27
 
28
- return predicted_img
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
 
30
- # Load the model
31
- model = tf.keras.models.load_model("model100.h5", custom_objects={'dice_coef': dice_coef})
32
 
33
  # Gradio Interface
34
  iface = gr.Interface(
 
14
  SIZE_X = 128
15
  SIZE_Y = 128
16
 
 
 
 
17
 
18
+ train_images = []
 
19
 
20
+ img = cv2.imread(image, 0)
21
+ img = cv2.resize(img, (SIZE_Y, SIZE_X))
22
+ train_images.append(img)
23
+
24
+ train_images = np.array(train_images)
25
+ train_images = np.expand_dims(train_images, axis=3)
26
+ train_images = normalize(train_images, axis=1)
27
+ X_test = train_images
28
+
29
+ custom_objects = {'dice_coef': dice_coef}
30
+ with tf.keras.utils.custom_object_scope(custom_objects):
31
+ model = tf.keras.models.load_model("model100.h5")
32
+
33
+
34
+ # Normalize the test image
35
+ test_img = X_test[0]
36
+ test_img_norm = test_img[:, :, 0][:, :, None]
37
+ test_img_input = np.expand_dims(test_img_norm, 0)
38
+
39
+ # Get the prediction
40
+ prediction = model.predict(test_img_input)
41
  predicted_img = np.argmax(prediction, axis=3)[0, :, :]
42
 
43
+ # Create an RGB image with a transparent background
44
+ rgba_img = np.zeros((predicted_img.shape[0], predicted_img.shape[1], 4))
45
+
46
+ # Define the color for the segmented area (e.g., red)
47
+ segmented_color = [1, 0, 0] # Red color in RGB
48
+
49
+ # Set the segmented area to the desired color
50
+ for i in range(3):
51
+ rgba_img[:, :, i] = np.where(predicted_img > 0, segmented_color[i], 0)
52
+
53
+ # Create an alpha channel: 1 where there is segmentation, 0 otherwise
54
+ alpha_channel = np.where(predicted_img > 0, 1, 0)
55
+ rgba_img[:, :, 3] = alpha_channel
56
+
57
+
58
+
59
+
60
+
61
+
62
+
63
+
64
+ # img = cv2.resize(image, (SIZE_Y, SIZE_X))
65
+ # img = np.expand_dims(img, axis=2)
66
+ # img = normalize(img, axis=1)
67
+
68
+ # # Prepare image for prediction
69
+ # img = np.expand_dims(img, axis=0)
70
+
71
+ # # Predict
72
+ # prediction = model.predict(img)
73
+ # predicted_img = np.argmax(prediction, axis=3)[0, :, :]
74
+
75
+ return rgba_img
76
 
 
 
77
 
78
  # Gradio Interface
79
  iface = gr.Interface(