Zeeshan01 commited on
Commit
ad274ba
·
1 Parent(s): c34126a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -26
app.py CHANGED
@@ -1,35 +1,37 @@
1
- import cv2
2
  import gradio as gr
 
3
  import numpy as np
4
 
5
- # Define a function for marker-based segmentation using the OpenCV watershed algorithm
6
- def watershed_segmentation(input_image, markers_image):
7
- # Convert input images to numpy arrays
8
- input_image = np.array(input_image)
9
- markers_image = np.array(markers_image)
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
- # Load the marker image (binary image with markers)
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
- # Define Gradio interfaces for the input and output
24
- input_image = gr.inputs.Image(type="numpy")
25
- markers_image = gr.inputs.Image(type="numpy")
26
- output_image = gr.outputs.Image()
27
 
28
- # Create a Gradio app
29
  gr.Interface(
30
  fn=watershed_segmentation,
31
- inputs=[input_image, markers_image],
32
  outputs=output_image,
33
- title="Marker-Based Segmentation with Watershed Algorithm",
34
- description="Upload an image and a marker image to perform marker-based segmentation.",
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)