Spaces:
Runtime error
Runtime error
Commit
·
1d3f775
1
Parent(s):
cd0d99e
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
import cv2
|
4 |
+
from PIL import Image
|
5 |
+
from ultralytics import YOLO
|
6 |
+
|
7 |
+
# Define available YOLO models
|
8 |
+
available_models = {
|
9 |
+
"Model 1 (best.pt)": YOLO("best.pt"),
|
10 |
+
"Model 2 (another_model.pt)": YOLO("another_model.pt"),
|
11 |
+
# Add more models as needed
|
12 |
+
}
|
13 |
+
|
14 |
+
# Create a function to perform image segmentation using the selected model
|
15 |
+
def segment_image(input_image, selected_model):
|
16 |
+
# Resize the input image to 255x255
|
17 |
+
img = np.array(input_image)
|
18 |
+
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
19 |
+
|
20 |
+
# Perform object detection and segmentation using the selected model
|
21 |
+
model = available_models[selected_model]
|
22 |
+
results = model(img)
|
23 |
+
mask = results[0].masks.data.numpy()
|
24 |
+
target_height = img.shape[0]
|
25 |
+
target_width = img.shape[1]
|
26 |
+
|
27 |
+
# Resize the mask using OpenCV
|
28 |
+
resized_mask = cv2.resize(mask[0], (target_width, target_height))
|
29 |
+
resized_mask = (resized_mask * 255).astype(np.uint8)
|
30 |
+
|
31 |
+
# Create a copy of the original image
|
32 |
+
overlay_image = img.copy()
|
33 |
+
|
34 |
+
# Apply the resized mask to the overlay image
|
35 |
+
overlay_image[resized_mask > 0] = [100, 0, 0] # Overlay in green
|
36 |
+
|
37 |
+
# Convert the overlay image to PIL format
|
38 |
+
overlay_pil = Image.fromarray(overlay_image)
|
39 |
+
|
40 |
+
return overlay_pil
|
41 |
+
|
42 |
+
# Create the Gradio interface with a dropdown for model selection
|
43 |
+
iface = gr.Interface(
|
44 |
+
fn=segment_image,
|
45 |
+
inputs=[
|
46 |
+
gr.inputs.Image(type="pil", label="Upload an image"),
|
47 |
+
gr.inputs.Dropdown(
|
48 |
+
choices=list(available_models.keys()),
|
49 |
+
label="Select YOLO Model",
|
50 |
+
default="Model 1 (best.pt)"
|
51 |
+
)
|
52 |
+
],
|
53 |
+
outputs=gr.outputs.Image(type="numpy", label="Segmented Image"),
|
54 |
+
title="Aorta segmentation and Detection using YOLOv8 😃",
|
55 |
+
description='This software generates the segmentation mask for Aorta for Point of Care Ultrasound (POCUS) images'
|
56 |
+
)
|
57 |
+
|
58 |
+
iface.launch()
|