Spaces:
Sleeping
Sleeping
MSaadTariq
commited on
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,125 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from ultralytics import YOLOv10
|
3 |
+
import supervision as sv
|
4 |
+
import spaces
|
5 |
+
from huggingface_hub import hf_hub_download
|
6 |
+
|
7 |
+
|
8 |
+
def download_models(model_id):
|
9 |
+
hf_hub_download("kadirnar/Yolov10", filename=f"{model_id}", local_dir=f"./")
|
10 |
+
return f"./{model_id}"
|
11 |
+
|
12 |
+
box_annotator = sv.BoxAnnotator(thickness=4, text_scale=2)
|
13 |
+
category_dict = {
|
14 |
+
0: 'person', 1: 'bicycle', 2: 'car', 3: 'motorcycle', 4: 'airplane', 5: 'bus',
|
15 |
+
6: 'train', 7: 'truck', 8: 'boat', 9: 'traffic light', 10: 'fire hydrant',
|
16 |
+
11: 'stop sign', 12: 'parking meter', 13: 'bench', 14: 'bird', 15: 'cat',
|
17 |
+
16: 'dog', 17: 'horse', 18: 'sheep', 19: 'cow', 20: 'elephant', 21: 'bear',
|
18 |
+
22: 'zebra', 23: 'giraffe', 24: 'backpack', 25: 'umbrella', 26: 'handbag',
|
19 |
+
27: 'tie', 28: 'suitcase', 29: 'frisbee', 30: 'skis', 31: 'snowboard',
|
20 |
+
32: 'sports ball', 33: 'kite', 34: 'baseball bat', 35: 'baseball glove',
|
21 |
+
36: 'skateboard', 37: 'surfboard', 38: 'tennis racket', 39: 'bottle',
|
22 |
+
40: 'wine glass', 41: 'cup', 42: 'fork', 43: 'knife', 44: 'spoon', 45: 'bowl',
|
23 |
+
46: 'banana', 47: 'apple', 48: 'sandwich', 49: 'orange', 50: 'broccoli',
|
24 |
+
51: 'carrot', 52: 'hot dog', 53: 'pizza', 54: 'donut', 55: 'cake',
|
25 |
+
56: 'chair', 57: 'couch', 58: 'potted plant', 59: 'bed', 60: 'dining table',
|
26 |
+
61: 'toilet', 62: 'tv', 63: 'laptop', 64: 'mouse', 65: 'remote', 66: 'keyboard',
|
27 |
+
67: 'cell phone', 68: 'microwave', 69: 'oven', 70: 'toaster', 71: 'sink',
|
28 |
+
72: 'refrigerator', 73: 'book', 74: 'clock', 75: 'vase', 76: 'scissors',
|
29 |
+
77: 'teddy bear', 78: 'hair drier', 79: 'toothbrush'
|
30 |
+
}
|
31 |
+
|
32 |
+
|
33 |
+
#@spaces.GPU(duration=200)
|
34 |
+
def yolov10_inference(image, model_id, image_size, conf_threshold, iou_threshold):
|
35 |
+
model_path = download_models(model_id)
|
36 |
+
model = YOLOv10(model_path)
|
37 |
+
results = model(source=image, imgsz=image_size, iou=iou_threshold, conf=conf_threshold, verbose=False)[0]
|
38 |
+
detections = sv.Detections.from_ultralytics(results)
|
39 |
+
|
40 |
+
labels = [
|
41 |
+
f"{category_dict[class_id]} {confidence:.2f}"
|
42 |
+
for class_id, confidence in zip(detections.class_id, detections.confidence)
|
43 |
+
]
|
44 |
+
annotated_image = box_annotator.annotate(image, detections=detections, labels=labels)
|
45 |
+
|
46 |
+
return annotated_image
|
47 |
+
|
48 |
+
def app():
|
49 |
+
with gr.Blocks():
|
50 |
+
with gr.Row():
|
51 |
+
with gr.Column():
|
52 |
+
image = gr.Image(type="numpy", label="Image")
|
53 |
+
|
54 |
+
model_id = gr.Dropdown(
|
55 |
+
label="Model",
|
56 |
+
choices=[
|
57 |
+
"yolov10n.pt",
|
58 |
+
"yolov10s.pt",
|
59 |
+
"yolov10m.pt",
|
60 |
+
"yolov10b.pt",
|
61 |
+
"yolov10l.pt",
|
62 |
+
"yolov10x.pt",
|
63 |
+
],
|
64 |
+
value="yolov10m.pt",
|
65 |
+
)
|
66 |
+
image_size = gr.Slider(
|
67 |
+
label="Image Size",
|
68 |
+
minimum=320,
|
69 |
+
maximum=1280,
|
70 |
+
step=32,
|
71 |
+
value=640,
|
72 |
+
)
|
73 |
+
conf_threshold = gr.Slider(
|
74 |
+
label="Confidence Threshold",
|
75 |
+
minimum=0.1,
|
76 |
+
maximum=1.0,
|
77 |
+
step=0.1,
|
78 |
+
value=0.25,
|
79 |
+
)
|
80 |
+
iou_threshold = gr.Slider(
|
81 |
+
label="IoU Threshold",
|
82 |
+
minimum=0.1,
|
83 |
+
maximum=1.0,
|
84 |
+
step=0.1,
|
85 |
+
value=0.45,
|
86 |
+
)
|
87 |
+
yolov10_infer = gr.Button(value="Detect Objects")
|
88 |
+
|
89 |
+
with gr.Column():
|
90 |
+
output_image = gr.Image(type="numpy", label="Annotated Image")
|
91 |
+
|
92 |
+
yolov10_infer.click(
|
93 |
+
fn=yolov10_inference,
|
94 |
+
inputs=[
|
95 |
+
image,
|
96 |
+
model_id,
|
97 |
+
image_size,
|
98 |
+
conf_threshold,
|
99 |
+
iou_threshold,
|
100 |
+
],
|
101 |
+
outputs=[output_image],
|
102 |
+
)
|
103 |
+
|
104 |
+
|
105 |
+
|
106 |
+
gradio_app = gr.Blocks()
|
107 |
+
with gradio_app:
|
108 |
+
gr.HTML(
|
109 |
+
"""
|
110 |
+
<h1 style='text-align: center'>
|
111 |
+
YOLOv10 - Real-Time object detection!
|
112 |
+
</h1>
|
113 |
+
""")
|
114 |
+
gr.HTML(
|
115 |
+
"""
|
116 |
+
<h3 style='text-align: center'>
|
117 |
+
Follow me on
|
118 |
+
<a href='https://www.linkedin.com/in/muhammad-saad-tariq-103272233/' target='_blank'>Linkedin</a>
|
119 |
+
</h3>
|
120 |
+
""")
|
121 |
+
with gr.Row():
|
122 |
+
with gr.Column():
|
123 |
+
app()
|
124 |
+
|
125 |
+
gradio_app.launch(debug=True)
|