Update app.py
Browse files
app.py
CHANGED
@@ -55,40 +55,40 @@ def perform_inference(image):
|
|
55 |
# Apply the mask to the original image (heatmap for visualization)
|
56 |
heatmap_img = cv2.applyColorMap(mask_binary, cv2.COLORMAP_JET)
|
57 |
segmented_image = cv2.addWeighted(image, 0.7, heatmap_img, 0.3, 0)
|
58 |
-
segmented_image_with_box = segmented_image.copy()
|
59 |
|
60 |
-
|
61 |
-
# Get bounding boxes for all contours
|
62 |
for contour in contours:
|
63 |
x, y, w, h = cv2.boundingRect(contour)
|
64 |
cv2.rectangle(segmented_image_with_box, (x, y), (x + w, y + h), (0, 0, 255), 2)
|
|
|
65 |
|
66 |
|
67 |
#Convert back to BGR
|
68 |
segmented_image_with_box = cv2.cvtColor(segmented_image_with_box, cv2.COLOR_RGB2BGR)
|
69 |
-
#Return image with bounding box
|
70 |
return (Image.fromarray(image),
|
71 |
Image.fromarray(mask_binary.astype(np.uint8)),
|
72 |
Image.fromarray(segmented_image_with_box))
|
73 |
|
74 |
|
|
|
75 |
# Gradio app
|
76 |
def gradio_app():
|
77 |
with gr.Blocks() as demo:
|
78 |
with gr.Row():
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
|
88 |
|
89 |
submit_btn.click(perform_inference, inputs=input_image, outputs=[original_image_output, mask_output, segmented_image_output])
|
90 |
-
|
91 |
demo.launch()
|
92 |
|
|
|
93 |
if __name__ == "__main__":
|
94 |
gradio_app()
|
|
|
55 |
# Apply the mask to the original image (heatmap for visualization)
|
56 |
heatmap_img = cv2.applyColorMap(mask_binary, cv2.COLORMAP_JET)
|
57 |
segmented_image = cv2.addWeighted(image, 0.7, heatmap_img, 0.3, 0)
|
58 |
+
segmented_image_with_box = segmented_image.copy()
|
59 |
|
60 |
+
# Get bounding boxes for all contours and annotate
|
|
|
61 |
for contour in contours:
|
62 |
x, y, w, h = cv2.boundingRect(contour)
|
63 |
cv2.rectangle(segmented_image_with_box, (x, y), (x + w, y + h), (0, 0, 255), 2)
|
64 |
+
cv2.putText(segmented_image_with_box, "Tumour Detected", (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 0, 255), 2)
|
65 |
|
66 |
|
67 |
#Convert back to BGR
|
68 |
segmented_image_with_box = cv2.cvtColor(segmented_image_with_box, cv2.COLOR_RGB2BGR)
|
|
|
69 |
return (Image.fromarray(image),
|
70 |
Image.fromarray(mask_binary.astype(np.uint8)),
|
71 |
Image.fromarray(segmented_image_with_box))
|
72 |
|
73 |
|
74 |
+
|
75 |
# Gradio app
|
76 |
def gradio_app():
|
77 |
with gr.Blocks() as demo:
|
78 |
with gr.Row():
|
79 |
+
with gr.Column():
|
80 |
+
input_image = gr.Image(label="Input Image", type="numpy")
|
81 |
+
submit_btn = gr.Button("Submit")
|
82 |
|
83 |
+
with gr.Column():
|
84 |
+
original_image_output = gr.Image(label="Original Image")
|
85 |
+
mask_output = gr.Image(label="Predicted Mask")
|
86 |
+
segmented_image_output= gr.Image(label="Segmented Image")
|
87 |
|
88 |
|
89 |
submit_btn.click(perform_inference, inputs=input_image, outputs=[original_image_output, mask_output, segmented_image_output])
|
|
|
90 |
demo.launch()
|
91 |
|
92 |
+
|
93 |
if __name__ == "__main__":
|
94 |
gradio_app()
|