Spaces:
Running
Running
Upload 3 files
Browse files- README.md +6 -7
- app.py +101 -0
- requirements.txt +4 -0
README.md
CHANGED
@@ -1,12 +1,11 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
sdk: gradio
|
7 |
sdk_version: 3.16.2
|
8 |
app_file: app.py
|
9 |
-
pinned:
|
|
|
10 |
---
|
11 |
-
|
12 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
---
|
2 |
+
title: YOLOv8 Segmentation
|
3 |
+
emoji: 🔥
|
4 |
+
colorFrom: black
|
5 |
+
colorTo: yellow
|
6 |
sdk: gradio
|
7 |
sdk_version: 3.16.2
|
8 |
app_file: app.py
|
9 |
+
pinned: true
|
10 |
+
license: gpl-3.0
|
11 |
---
|
|
|
|
app.py
ADDED
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import sahi
|
3 |
+
import torch
|
4 |
+
from ultralyticsplus import YOLO, render_model_output
|
5 |
+
|
6 |
+
# Images
|
7 |
+
sahi.utils.file.download_from_url(
|
8 |
+
"https://raw.githubusercontent.com/kadirnar/dethub/main/data/images/highway.jpg",
|
9 |
+
"highway.jpg",
|
10 |
+
)
|
11 |
+
sahi.utils.file.download_from_url(
|
12 |
+
"https://raw.githubusercontent.com/obss/sahi/main/tests/data/small-vehicles1.jpeg",
|
13 |
+
"small-vehicles1.jpeg",
|
14 |
+
)
|
15 |
+
sahi.utils.file.download_from_url(
|
16 |
+
"https://raw.githubusercontent.com/ultralytics/yolov5/master/data/images/zidane.jpg",
|
17 |
+
"zidane.jpg",
|
18 |
+
)
|
19 |
+
|
20 |
+
|
21 |
+
model_names = [
|
22 |
+
"yolov8n-seg.pt",
|
23 |
+
"yolov8s-seg.pt",
|
24 |
+
"yolov8m-seg.pt",
|
25 |
+
"yolov8l-seg.pt",
|
26 |
+
"yolov8x-seg.pt",
|
27 |
+
]
|
28 |
+
|
29 |
+
current_model_name = "yolov8m-seg.pt"
|
30 |
+
model = YOLO(current_model_name)
|
31 |
+
|
32 |
+
|
33 |
+
def yolov8_inference(
|
34 |
+
image: gr.inputs.Image = None,
|
35 |
+
model_name: gr.inputs.Dropdown = None,
|
36 |
+
image_size: gr.inputs.Slider = 640,
|
37 |
+
conf_threshold: gr.inputs.Slider = 0.25,
|
38 |
+
iou_threshold: gr.inputs.Slider = 0.45,
|
39 |
+
):
|
40 |
+
"""
|
41 |
+
YOLOv8 inference function
|
42 |
+
Args:
|
43 |
+
image: Input image
|
44 |
+
model_name: Name of the model
|
45 |
+
image_size: Image size
|
46 |
+
conf_threshold: Confidence threshold
|
47 |
+
iou_threshold: IOU threshold
|
48 |
+
Returns:
|
49 |
+
Rendered image
|
50 |
+
"""
|
51 |
+
global model
|
52 |
+
global current_model_name
|
53 |
+
if model_name != current_model_name:
|
54 |
+
model = YOLO(model_name)
|
55 |
+
current_model_name = model_name
|
56 |
+
model.overrides["conf"] = conf_threshold
|
57 |
+
model.overrides["iou"] = iou_threshold
|
58 |
+
results = model.predict(image, imgsz=image_size, return_outputs=True)
|
59 |
+
renders = []
|
60 |
+
for image_results in model.predict(image, imgsz=image_size, return_outputs=True):
|
61 |
+
render = render_model_output(
|
62 |
+
model=model, image=image, model_output=image_results
|
63 |
+
)
|
64 |
+
renders.append(render)
|
65 |
+
|
66 |
+
return renders[0]
|
67 |
+
|
68 |
+
|
69 |
+
inputs = [
|
70 |
+
gr.Image(type="filepath", label="Input Image"),
|
71 |
+
gr.Dropdown(
|
72 |
+
model_names,
|
73 |
+
value=current_model_name,
|
74 |
+
label="Model type",
|
75 |
+
),
|
76 |
+
gr.Slider(minimum=320, maximum=1280, value=640, step=32, label="Image Size"),
|
77 |
+
gr.Slider(
|
78 |
+
minimum=0.0, maximum=1.0, value=0.25, step=0.05, label="Confidence Threshold"
|
79 |
+
),
|
80 |
+
gr.Slider(minimum=0.0, maximum=1.0, value=0.45, step=0.05, label="IOU Threshold"),
|
81 |
+
]
|
82 |
+
|
83 |
+
outputs = gr.Image(type="filepath", label="Output Image")
|
84 |
+
title = "Ultralytics YOLOv8 Segmentation Demo"
|
85 |
+
|
86 |
+
examples = [
|
87 |
+
["zidane.jpg", "yolov8m-seg.pt", 640, 0.25, 0.45],
|
88 |
+
["highway.jpg", "yolov8m-seg.pt", 640, 0.25, 0.45],
|
89 |
+
["highway1.jpg", "yolov8m-seg.pt", 640, 0.25, 0.45],
|
90 |
+
["small-vehicles1.jpeg", "yolov8m-seg.pt", 640, 0.25, 0.45],
|
91 |
+
]
|
92 |
+
demo_app = gr.Interface(
|
93 |
+
fn=yolov8_inference,
|
94 |
+
inputs=inputs,
|
95 |
+
outputs=outputs,
|
96 |
+
title=title,
|
97 |
+
examples=examples,
|
98 |
+
cache_examples=True,
|
99 |
+
theme="default",
|
100 |
+
)
|
101 |
+
demo_app.launch(debug=True, enable_queue=True)
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
sahi
|
2 |
+
torch
|
3 |
+
ultralytics==8.0.6
|
4 |
+
ultralyticsplus==0.0.9
|