Spaces:
Runtime error
Runtime error
Commit
·
0f1bb12
1
Parent(s):
5c5501b
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import supervision as sv
|
2 |
+
import gradio as gr
|
3 |
+
from ultralytics import YOLO
|
4 |
+
import sahi
|
5 |
+
|
6 |
+
|
7 |
+
|
8 |
+
# Images
|
9 |
+
sahi.utils.file.download_from_url(
|
10 |
+
"https://transform.roboflow.com/zZuu207UOVOOJKuuCpmV/3512b3839afacecec643949bef398e99/thumb.jpg",
|
11 |
+
"tu1.jpg",
|
12 |
+
)
|
13 |
+
sahi.utils.file.download_from_url(
|
14 |
+
"https://transform.roboflow.com/zZuu207UOVOOJKuuCpmV/5b8b940fae2f9e4952395bcced0688aa/thumb.jpg",
|
15 |
+
"tu2.jpg",
|
16 |
+
)
|
17 |
+
sahi.utils.file.download_from_url(
|
18 |
+
"https://transform.roboflow.com/zZuu207UOVOOJKuuCpmV/347e10ab7aa2b399ec546f2037d8c786/thumb.jpg",
|
19 |
+
"tu3.jpg",
|
20 |
+
)
|
21 |
+
|
22 |
+
|
23 |
+
|
24 |
+
|
25 |
+
annotatorbbox = sv.BoxAnnotator()
|
26 |
+
annotatormask=sv.MaskAnnotator()
|
27 |
+
|
28 |
+
|
29 |
+
def yolov8_inference(
|
30 |
+
image: gr.inputs.Image = None,
|
31 |
+
model_name: gr.inputs.Dropdown = None,
|
32 |
+
image_size: gr.inputs.Slider = 320,
|
33 |
+
conf_threshold: gr.inputs.Slider = 0.25,
|
34 |
+
iou_threshold: gr.inputs.Slider = 0.45,
|
35 |
+
):
|
36 |
+
|
37 |
+
|
38 |
+
model = YOLO("/content/segment/train/weights/best.pt")
|
39 |
+
|
40 |
+
results = model(image,conf=conf_threshold,iou=iou_threshold ,imgsz=320)[0]
|
41 |
+
detections = sv.Detections.from_yolov8(results)
|
42 |
+
annotated_image = annotatorbbox.annotate(scene=image, detections=detections)
|
43 |
+
annotated_image = annotatormask.annotate(scene=annotated_image, detections=detections)
|
44 |
+
|
45 |
+
|
46 |
+
|
47 |
+
|
48 |
+
return annotated_image
|
49 |
+
|
50 |
+
image_input = gr.inputs.Image() # Adjust the shape according to your requirements
|
51 |
+
|
52 |
+
inputs = [
|
53 |
+
gr.inputs.Image(label="Input Image"),
|
54 |
+
gr.Slider(
|
55 |
+
minimum=0.0, maximum=1.0, value=0.25, step=0.05, label="Confidence Threshold"
|
56 |
+
),
|
57 |
+
gr.Slider(minimum=0.0, maximum=1.0, value=0.45, step=0.05, label="IOU Threshold"),
|
58 |
+
]
|
59 |
+
|
60 |
+
outputs = gr.Image(type="filepath", label="Output Image")
|
61 |
+
title = "Ultralytics YOLOv8 Segmentation Demo"
|
62 |
+
import os
|
63 |
+
examples = [
|
64 |
+
["tu1.jpg", 0.6, 0.45],
|
65 |
+
["tu2.jpg", 0.25, 0.45],
|
66 |
+
["tu3.jpg", 0.25, 0.45],
|
67 |
+
]
|
68 |
+
demo_app = gr.Interface(examples=examples,
|
69 |
+
fn=yolov8_inference,
|
70 |
+
inputs=inputs,
|
71 |
+
outputs=outputs,
|
72 |
+
title=title,
|
73 |
+
cache_examples=True,
|
74 |
+
theme="default",
|
75 |
+
)
|
76 |
+
demo_app.launch(debug=False, enable_queue=True)
|