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