File size: 1,444 Bytes
3f6148a
bea7846
 
71e2ac1
f4e3666
 
bea7846
3998f3b
3f6148a
bea7846
ee59432
 
bea7846
 
71e2ac1
bea7846
1e87812
94d967d
 
 
ee59432
bea7846
94d967d
 
 
 
 
 
 
 
 
 
71e2ac1
94d967d
 
 
 
 
bea7846
 
ee59432
e919ebf
 
4ac6b0b
 
ee59432
71e2ac1
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
38
39
40
41
42
43
44
45
46
47
import gradio as gr
import numpy as np
import cv2
from PIL import Image
from ultralytics import YOLO

# Load YOLO model
model = YOLO("best.pt")

def segment_image(input_image):
    # Resize the input image to 255x255
    img = np.array(input_image)
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

    # Perform object detection and segmentation
    # Perform object detection and segmentation
    results = model(img)
    mask =  results[0].masks.data.numpy()
    target_height = img.shape[0]
    target_width = img.shape[1]

    

    # Resize the mask using OpenCV
    resized_mask = cv2.resize(mask[0], (target_width, target_height))
    resized_mask = (resized_mask * 255).astype(np.uint8)

    # Create a copy of the original image
    overlay_image = img.copy()
    print(overlay_image.shape,  mask.shape )

    # Apply the resized mask to the overlay image
    overlay_image[resized_mask > 0] = [100, 0, 0]  # Overlay in green

    # Convert the overlay image to PIL format
    overlay_pil = Image.fromarray(overlay_image)

    return overlay_pil

iface = gr.Interface(
    fn=segment_image,
    inputs=gr.components.Image(type="pil", label="Upload an image"),
    outputs=gr.components.Image(type="numpy", label="Segmented Image"),
    title="Aorta segmentation and Detection using YOLOv8 😃",
    description= 'This software generates the segementation mask for Aorta for the Point of Care Ultrasound (POCUS) images')

iface.launch()