Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import numpy as np
|
| 3 |
+
import gradio as gr
|
| 4 |
+
|
| 5 |
+
# Global variables to track the state of manual segmentation
|
| 6 |
+
drawing = False
|
| 7 |
+
roi_points = []
|
| 8 |
+
|
| 9 |
+
# Function to handle mouse events
|
| 10 |
+
def mouse_event(event, x, y, flags, param):
|
| 11 |
+
global roi_points, drawing
|
| 12 |
+
if event == cv2.EVENT_LBUTTONDOWN:
|
| 13 |
+
drawing = True
|
| 14 |
+
roi_points = [(x, y)]
|
| 15 |
+
elif event == cv2.EVENT_MOUSEMOVE:
|
| 16 |
+
if drawing:
|
| 17 |
+
roi_points.append((x, y))
|
| 18 |
+
elif event == cv2.EVENT_LBUTTONUP:
|
| 19 |
+
drawing = False
|
| 20 |
+
roi_points.append((x, y))
|
| 21 |
+
|
| 22 |
+
# Function to perform image segmentation using the watershed algorithm
|
| 23 |
+
def segment_image(input_image):
|
| 24 |
+
# Convert the input image (NumPy array) to a format that OpenCV can use
|
| 25 |
+
image = input_image.astype(np.uint8)
|
| 26 |
+
|
| 27 |
+
# Create a copy of the image for manual segmentation
|
| 28 |
+
manual_segmentation_image = image.copy()
|
| 29 |
+
|
| 30 |
+
# Create a mask for manual segmentation
|
| 31 |
+
mask = np.zeros(image.shape[:2], dtype=np.uint8)
|
| 32 |
+
|
| 33 |
+
# Create a window for manual segmentation
|
| 34 |
+
cv2.namedWindow("Manual Segmentation")
|
| 35 |
+
cv2.setMouseCallback("Manual Segmentation", mouse_event)
|
| 36 |
+
|
| 37 |
+
while True:
|
| 38 |
+
for point in roi_points:
|
| 39 |
+
cv2.circle(manual_segmentation_image, point, 5, (0, 0, 255), -1)
|
| 40 |
+
|
| 41 |
+
cv2.imshow("Manual Segmentation", manual_segmentation_image)
|
| 42 |
+
key = cv2.waitKey(1) & 0xFF
|
| 43 |
+
|
| 44 |
+
# Press 's' to perform segmentation
|
| 45 |
+
if key == ord("s"):
|
| 46 |
+
break
|
| 47 |
+
# Press 'r' to reset manual segmentation
|
| 48 |
+
elif key == ord("r"):
|
| 49 |
+
manual_segmentation_image = image.copy()
|
| 50 |
+
roi_points = []
|
| 51 |
+
|
| 52 |
+
# Close the manual segmentation window
|
| 53 |
+
cv2.destroyWindow("Manual Segmentation")
|
| 54 |
+
|
| 55 |
+
# Convert the image to grayscale
|
| 56 |
+
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 57 |
+
|
| 58 |
+
# Create a mask based on the manually segmented ROIs
|
| 59 |
+
if roi_points:
|
| 60 |
+
roi_points = np.array(roi_points, np.int32)
|
| 61 |
+
cv2.fillPoly(mask, [roi_points], 255)
|
| 62 |
+
|
| 63 |
+
# Apply the watershed algorithm to segment the image
|
| 64 |
+
cv2.watershed(image, mask)
|
| 65 |
+
|
| 66 |
+
# Create a segmented output image
|
| 67 |
+
output_image = image.copy()
|
| 68 |
+
output_image[mask == -1] = [0, 0, 255] # Mark watershed boundaries in red
|
| 69 |
+
|
| 70 |
+
return output_image
|
| 71 |
+
|
| 72 |
+
# Create a Gradio interface
|
| 73 |
+
iface = gr.Interface(
|
| 74 |
+
fn=segment_image,
|
| 75 |
+
inputs="image",
|
| 76 |
+
outputs="image",
|
| 77 |
+
title="Manual Image Segmentation using Watershed Algorithm",
|
| 78 |
+
description="Upload an image and perform manual image segmentation by drawing regions of interest (ROIs) before segmenting. Press 's' to segment and 'r' to reset ROIs.",
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
# Launch the Gradio app
|
| 82 |
+
iface.launch()
|