Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,35 +1,37 @@
|
|
| 1 |
-
import cv2
|
| 2 |
import gradio as gr
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
def watershed_segmentation(input_image,
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
|
| 12 |
-
markers = cv2.cvtColor(markers_image, cv2.COLOR_BGR2GRAY)
|
| 13 |
-
|
| 14 |
-
# Apply the watershed algorithm
|
| 15 |
-
image_copy = input_image.copy()
|
| 16 |
-
cv2.watershed(image_copy, markers)
|
| 17 |
-
|
| 18 |
-
# Apply color mapping to the segmented regions
|
| 19 |
-
segmented_image = cv2.applyColorMap(markers, cv2.COLORMAP_JET)
|
| 20 |
-
|
| 21 |
-
return segmented_image
|
| 22 |
|
| 23 |
-
#
|
| 24 |
-
input_image = gr.inputs.Image(type="
|
| 25 |
-
|
| 26 |
-
output_image = gr.outputs.Image()
|
| 27 |
|
| 28 |
-
# Create a Gradio app
|
| 29 |
gr.Interface(
|
| 30 |
fn=watershed_segmentation,
|
| 31 |
-
inputs=[input_image,
|
| 32 |
outputs=output_image,
|
| 33 |
-
title="
|
| 34 |
-
description="Upload an image and
|
| 35 |
-
).launch()
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import cv2
|
| 3 |
import numpy as np
|
| 4 |
|
| 5 |
+
# Function to perform image segmentation using OpenCV Watershed Algorithm
|
| 6 |
+
def watershed_segmentation(input_image, scribble_image):
|
| 7 |
+
# Load the input image and scribble image
|
| 8 |
+
image = cv2.cvtColor(input_image.astype('uint8'), cv2.COLOR_RGBA2BGR)
|
| 9 |
+
scribble = cv2.cvtColor(scribble_image.astype('uint8'), cv2.COLOR_RGBA2GRAY)
|
| 10 |
+
|
| 11 |
+
# Convert scribble to markers (0 for background, 1 for unknown, 2 for foreground)
|
| 12 |
+
markers = np.zeros_like(scribble)
|
| 13 |
+
markers[scribble == 0] = 0
|
| 14 |
+
markers[scribble == 255] = 1
|
| 15 |
+
markers[scribble == 128] = 2
|
| 16 |
+
|
| 17 |
+
# Apply watershed algorithm
|
| 18 |
+
cv2.watershed(image, markers)
|
| 19 |
+
|
| 20 |
+
# Create a segmented mask
|
| 21 |
+
segmented_mask = np.zeros_like(image, dtype=np.uint8)
|
| 22 |
+
segmented_mask[markers == 2] = [0, 0, 255] # Red color for segmented regions
|
| 23 |
|
| 24 |
+
return segmented_mask
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
# Gradio interface
|
| 27 |
+
input_image = gr.inputs.Image(type='pil', label="Upload an image")
|
| 28 |
+
scribble_image = gr.inputs.Image(type='pil', label="Scribble on the image")
|
| 29 |
+
output_image = gr.outputs.Image(type='pil', label="Segmented Image")
|
| 30 |
|
|
|
|
| 31 |
gr.Interface(
|
| 32 |
fn=watershed_segmentation,
|
| 33 |
+
inputs=[input_image, scribble_image],
|
| 34 |
outputs=output_image,
|
| 35 |
+
title="Image Segmentation using Watershed Algorithm",
|
| 36 |
+
description="Upload an image and scribble on it to perform segmentation using the Watershed Algorithm.",
|
| 37 |
+
).launch(share=True)
|