File size: 1,210 Bytes
0a08c3a
e54b7cb
3977c7b
 
0a08c3a
3977c7b
 
 
 
0a08c3a
3977c7b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import opencv
import numpy as np
import watershed  # Your Python bindings module

def segment_image(input_image):
    # Convert the image received from Gradio into a format suitable for our C++ function
    is_success, im_buf_arr = cv2.imencode(".jpg", input_image)
    byte_im = im_buf_arr.tobytes()

    # You can save this to a file or directly pass it if your C++ function can handle byte streams
    with open("temp_input.jpg", "wb") as f:
        f.write(byte_im)

    # Call the C++ watershed_segmentation function
    watershed.watershed_segmentation("temp_input.jpg", "output.jpg")

    # Read the output image to display in Gradio
    output_img = cv2.imread("output.jpg")

    # Convert from BGR (OpenCV format) to RGB (for displaying in Gradio)
    output_img = cv2.cvtColor(output_img, cv2.COLOR_BGR2RGB)
    return output_img

# Define Gradio interface
interface = gr.Interface(
    fn=segment_image, 
    inputs=gr.inputs.Image(type="numpy", label="Input Image"), 
    outputs=gr.outputs.Image(type="numpy", label="Segmented Image"),
    live=True,
    title="Watershed Image Segmentation",
    description="Upload an image for watershed segmentation."
)

interface.launch()