Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,9 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import sahi
|
| 3 |
import torch
|
| 4 |
-
from ultralyticsplus import YOLO
|
| 5 |
|
| 6 |
-
# Download
|
| 7 |
sahi.utils.file.download_from_url(
|
| 8 |
"https://raw.githubusercontent.com/kadirnar/dethub/main/data/images/highway.jpg",
|
| 9 |
"highway.jpg",
|
|
@@ -17,7 +17,7 @@ sahi.utils.file.download_from_url(
|
|
| 17 |
"zidane.jpg",
|
| 18 |
)
|
| 19 |
|
| 20 |
-
#
|
| 21 |
model_names = [
|
| 22 |
"yolov8n-seg.pt",
|
| 23 |
"yolov8s-seg.pt",
|
|
@@ -26,7 +26,6 @@ model_names = [
|
|
| 26 |
"yolov8x-seg.pt",
|
| 27 |
]
|
| 28 |
|
| 29 |
-
# Set the initial model
|
| 30 |
current_model_name = "yolov8m-seg.pt"
|
| 31 |
model = YOLO(current_model_name)
|
| 32 |
|
|
@@ -62,18 +61,29 @@ def yolov8_inference(
|
|
| 62 |
|
| 63 |
# Perform model prediction
|
| 64 |
results = model.predict(image, imgsz=image_size, return_outputs=True)
|
| 65 |
-
|
| 66 |
-
#
|
| 67 |
output = []
|
|
|
|
|
|
|
| 68 |
for result in results:
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
return output
|
| 75 |
|
| 76 |
-
# Define Gradio
|
| 77 |
inputs = [
|
| 78 |
gr.Image(type="filepath", label="Input Image"),
|
| 79 |
gr.Dropdown(
|
|
@@ -88,25 +98,26 @@ inputs = [
|
|
| 88 |
gr.Slider(minimum=0.0, maximum=1.0, value=0.45, step=0.05, label="IOU Threshold"),
|
| 89 |
]
|
| 90 |
|
| 91 |
-
# Output
|
| 92 |
-
outputs = gr.JSON(label="
|
| 93 |
|
| 94 |
-
# Title and example inputs
|
| 95 |
title = "Ultralytics YOLOv8 Segmentation Demo"
|
|
|
|
|
|
|
| 96 |
examples = [
|
| 97 |
["zidane.jpg", "yolov8m-seg.pt", 640, 0.6, 0.45],
|
| 98 |
["highway.jpg", "yolov8m-seg.pt", 640, 0.25, 0.45],
|
| 99 |
["small-vehicles1.jpeg", "yolov8m-seg.pt", 640, 0.25, 0.45],
|
| 100 |
]
|
| 101 |
|
| 102 |
-
#
|
| 103 |
demo_app = gr.Interface(
|
| 104 |
fn=yolov8_inference,
|
| 105 |
inputs=inputs,
|
| 106 |
outputs=outputs,
|
| 107 |
title=title,
|
| 108 |
examples=examples,
|
| 109 |
-
cache_examples=
|
| 110 |
theme="default",
|
| 111 |
)
|
| 112 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import sahi
|
| 3 |
import torch
|
| 4 |
+
from ultralyticsplus import YOLO, render_model_output
|
| 5 |
|
| 6 |
+
# Download sample images
|
| 7 |
sahi.utils.file.download_from_url(
|
| 8 |
"https://raw.githubusercontent.com/kadirnar/dethub/main/data/images/highway.jpg",
|
| 9 |
"highway.jpg",
|
|
|
|
| 17 |
"zidane.jpg",
|
| 18 |
)
|
| 19 |
|
| 20 |
+
# List of YOLOv8 segmentation models
|
| 21 |
model_names = [
|
| 22 |
"yolov8n-seg.pt",
|
| 23 |
"yolov8s-seg.pt",
|
|
|
|
| 26 |
"yolov8x-seg.pt",
|
| 27 |
]
|
| 28 |
|
|
|
|
| 29 |
current_model_name = "yolov8m-seg.pt"
|
| 30 |
model = YOLO(current_model_name)
|
| 31 |
|
|
|
|
| 61 |
|
| 62 |
# Perform model prediction
|
| 63 |
results = model.predict(image, imgsz=image_size, return_outputs=True)
|
| 64 |
+
|
| 65 |
+
# Initialize an empty list to store the output
|
| 66 |
output = []
|
| 67 |
+
|
| 68 |
+
# Iterate over the results
|
| 69 |
for result in results:
|
| 70 |
+
# Check if segmentation masks are available
|
| 71 |
+
if 'masks' in result and result['masks'] is not None:
|
| 72 |
+
masks = result['masks']['data']
|
| 73 |
+
for i, (mask, box) in enumerate(zip(masks, result['boxes'])):
|
| 74 |
+
label = model.names[int(result['boxes']['cls'][i])]
|
| 75 |
+
mask_coords = mask.tolist() # Convert mask coordinates to list format
|
| 76 |
+
output.append({"label": label, "mask_coords": mask_coords})
|
| 77 |
+
else:
|
| 78 |
+
# If masks are not available, just extract bounding box information
|
| 79 |
+
for i, box in enumerate(result['boxes']):
|
| 80 |
+
label = model.names[int(result['boxes']['cls'][i])]
|
| 81 |
+
bbox = box['xyxy'].tolist() # Bounding box coordinates
|
| 82 |
+
output.append({"label": label, "bbox_coords": bbox})
|
| 83 |
|
| 84 |
return output
|
| 85 |
|
| 86 |
+
# Define Gradio interface inputs and outputs
|
| 87 |
inputs = [
|
| 88 |
gr.Image(type="filepath", label="Input Image"),
|
| 89 |
gr.Dropdown(
|
|
|
|
| 98 |
gr.Slider(minimum=0.0, maximum=1.0, value=0.45, step=0.05, label="IOU Threshold"),
|
| 99 |
]
|
| 100 |
|
| 101 |
+
# Output is a dictionary containing label names and coordinates of masks or boxes
|
| 102 |
+
outputs = gr.JSON(label="Output Masks and Labels")
|
| 103 |
|
|
|
|
| 104 |
title = "Ultralytics YOLOv8 Segmentation Demo"
|
| 105 |
+
|
| 106 |
+
# Example images for the interface
|
| 107 |
examples = [
|
| 108 |
["zidane.jpg", "yolov8m-seg.pt", 640, 0.6, 0.45],
|
| 109 |
["highway.jpg", "yolov8m-seg.pt", 640, 0.25, 0.45],
|
| 110 |
["small-vehicles1.jpeg", "yolov8m-seg.pt", 640, 0.25, 0.45],
|
| 111 |
]
|
| 112 |
|
| 113 |
+
# Build the Gradio demo app
|
| 114 |
demo_app = gr.Interface(
|
| 115 |
fn=yolov8_inference,
|
| 116 |
inputs=inputs,
|
| 117 |
outputs=outputs,
|
| 118 |
title=title,
|
| 119 |
examples=examples,
|
| 120 |
+
cache_examples=False, # Set to False to avoid caching issues
|
| 121 |
theme="default",
|
| 122 |
)
|
| 123 |
|