Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -1,104 +1,147 @@
|
|
1 |
-
|
2 |
-
import
|
|
|
3 |
from ultralytics import YOLO
|
|
|
4 |
import gradio as gr
|
5 |
-
import spaces
|
6 |
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
16: (83, 115, 106), # Q
|
26 |
-
17: (255, 72, 88), # R
|
27 |
-
18: (0, 204, 192), # S
|
28 |
-
19: (116, 127, 127), # T
|
29 |
-
20: (0, 153, 221), # U
|
30 |
-
21: (196, 51, 2), # V
|
31 |
-
22: (191, 100, 21), # W
|
32 |
-
23: (2, 62, 115), # X
|
33 |
-
24: (140, 80, 58), # Y
|
34 |
-
25: (168, 181, 69) # Z
|
35 |
-
}
|
36 |
-
BOX_PADDING = 2
|
37 |
|
38 |
-
|
39 |
-
|
|
|
|
|
40 |
|
41 |
@spaces.GPU
|
42 |
-
def
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
|
54 |
-
|
55 |
-
|
56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
|
58 |
-
|
59 |
-
|
60 |
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
image = cv2.rectangle(img=image,
|
73 |
-
pt1=start_box,
|
74 |
-
pt2=end_box,
|
75 |
-
color=ASL_COLORS[class_id],
|
76 |
-
thickness=line_thickness)
|
77 |
-
|
78 |
-
# Draw label
|
79 |
-
asl_letter = chr(65 + class_id) # Convert class ID to ASL letter
|
80 |
-
text = f"{asl_letter} {detection_class_conf:.2f}" # Label text
|
81 |
-
font_thickness = max(line_thickness - 1, 1)
|
82 |
-
(font_scale_w, font_scale_h) = (line_thickness * 0.5, line_thickness * 0.5)
|
83 |
-
(text_w, text_h), _ = cv2.getTextSize(text=text, fontFace=2, fontScale=font_scale_w, thickness=font_thickness)
|
84 |
-
|
85 |
-
# Draw wrapping box for text
|
86 |
-
image = cv2.rectangle(img=image,
|
87 |
-
pt1=(start_box[0], start_box[1] - text_h - BOX_PADDING * 2),
|
88 |
-
pt2=(start_box[0] + text_w + BOX_PADDING * 2, start_box[1]),
|
89 |
-
color=ASL_COLORS[class_id],
|
90 |
-
thickness=-1)
|
91 |
-
|
92 |
-
# Put class name on image
|
93 |
-
start_text = (start_box[0] + BOX_PADDING, start_box[1] - BOX_PADDING)
|
94 |
-
image = cv2.putText(img=image, text=text, org=start_text, fontFace=0, color=(255, 255, 255), fontScale=font_scale_w, thickness=font_thickness)
|
95 |
|
96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
102 |
|
103 |
-
|
104 |
-
iface.launch()
|
|
|
1 |
+
import spaces
|
2 |
+
import supervision as sv
|
3 |
+
import PIL.Image as Image
|
4 |
from ultralytics import YOLO
|
5 |
+
from huggingface_hub import hf_hub_download
|
6 |
import gradio as gr
|
|
|
7 |
|
8 |
+
global repo_id
|
9 |
+
|
10 |
+
repo_id = "atalaydenknalbant/asl-yolo-models"
|
11 |
+
|
12 |
+
# Model filenames directly provided, since they are known
|
13 |
+
model_filenames = [
|
14 |
+
"yolov10s.pt",
|
15 |
+
"yolov10x.pt",
|
16 |
+
"yolov8s.pt",
|
17 |
+
"yolov8x.pt",
|
18 |
+
"yolov9e.pt",
|
19 |
+
"yolov9s.pt"
|
20 |
+
]
|
21 |
+
|
22 |
+
def download_models(repo_id, model_id):
|
23 |
+
# Download the selected model
|
24 |
+
hf_hub_download(repo_id, filename=model_id, local_dir=f"./")
|
25 |
+
return f"./{model_id}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
+
box_annotator = sv.BoxAnnotator()
|
28 |
+
category_dict = {0: 'A', 1: 'B', 2: 'C', 3: 'D', 4: 'E', 5: 'F', 6: 'G', 7: 'H', 8: 'I',
|
29 |
+
9: 'J', 10: 'K', 11: 'L', 12: 'M', 13: 'N', 14: 'O', 15: 'P', 16: 'Q',
|
30 |
+
17: 'R', 18: 'S', 19: 'T', 20: 'U', 21: 'V', 22: 'W', 23: 'X', 24: 'Y', 25: 'Z'}
|
31 |
|
32 |
@spaces.GPU
|
33 |
+
def yolo_inference(image, model_id, conf_threshold, iou_threshold, max_detection):
|
34 |
+
# Download models
|
35 |
+
model_path = download_models(repo_id, model_id)
|
36 |
+
|
37 |
+
model = YOLO(model_path)
|
38 |
+
results = model(source=image, imgsz=640, iou=iou_threshold, conf=conf_threshold, verbose=False, max_det=max_detection)[0]
|
39 |
+
detections = sv.Detections.from_ultralytics(results)
|
40 |
+
|
41 |
+
labels = [
|
42 |
+
f"{category_dict[class_id]} {confidence:.2f}"
|
43 |
+
for class_id, confidence in zip(detections.class_id, detections.confidence)
|
44 |
+
]
|
45 |
+
annotated_image = box_annotator.annotate(image, detections=detections, labels=labels)
|
46 |
+
|
47 |
+
return annotated_image
|
48 |
+
|
49 |
+
def app():
|
50 |
+
with gr.Blocks():
|
51 |
+
with gr.Row():
|
52 |
+
with gr.Column():
|
53 |
+
image = gr.Image(type="pil", label="Image", interactive=True)
|
54 |
|
55 |
+
model_id = gr.Dropdown(
|
56 |
+
label="Model",
|
57 |
+
choices=model_filenames,
|
58 |
+
value=model_filenames[0] if model_filenames else "",
|
59 |
+
)
|
60 |
+
conf_threshold = gr.Slider(
|
61 |
+
label="Confidence Threshold",
|
62 |
+
minimum=0.1,
|
63 |
+
maximum=1.0,
|
64 |
+
step=0.1,
|
65 |
+
value=0.25,
|
66 |
+
)
|
67 |
+
iou_threshold = gr.Slider(
|
68 |
+
label="IoU Threshold",
|
69 |
+
minimum=0.1,
|
70 |
+
maximum=1.0,
|
71 |
+
step=0.1,
|
72 |
+
value=0.45,
|
73 |
+
)
|
74 |
+
|
75 |
+
max_detection = gr.Slider(
|
76 |
+
label="Max Detection",
|
77 |
+
minimum=1,
|
78 |
+
step=1,
|
79 |
+
value=1,
|
80 |
+
)
|
81 |
+
yolov_infer = gr.Button(value="Detect Objects")
|
82 |
|
83 |
+
with gr.Column():
|
84 |
+
output_image = gr.Image(type="pil", label="Annotated Image", interactive=False)
|
85 |
|
86 |
+
yolov_infer.click(
|
87 |
+
fn=yolo_inference,
|
88 |
+
inputs=[
|
89 |
+
image,
|
90 |
+
model_id,
|
91 |
+
conf_threshold,
|
92 |
+
iou_threshold,
|
93 |
+
max_detection,
|
94 |
+
],
|
95 |
+
outputs=[output_image],
|
96 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
|
98 |
+
gr.Examples(
|
99 |
+
examples=[
|
100 |
+
[
|
101 |
+
"b.jpg",
|
102 |
+
"yolov10x.pt",
|
103 |
+
0.25,
|
104 |
+
0.45,
|
105 |
+
1,
|
106 |
+
],
|
107 |
+
|
108 |
+
[
|
109 |
+
"a.jpg",
|
110 |
+
"yolov10s.pt",
|
111 |
+
0.25,
|
112 |
+
0.45,
|
113 |
+
1,
|
114 |
+
],
|
115 |
+
[
|
116 |
+
"y.jpg",
|
117 |
+
"yolov10x.pt",
|
118 |
+
0.25,
|
119 |
+
0.45,
|
120 |
+
1,
|
121 |
+
],
|
122 |
+
],
|
123 |
+
fn=yolo_inference,
|
124 |
+
inputs=[
|
125 |
+
image,
|
126 |
+
model_id,
|
127 |
+
conf_threshold,
|
128 |
+
iou_threshold,
|
129 |
+
max_detection,
|
130 |
+
],
|
131 |
+
outputs=[output_image],
|
132 |
+
cache_examples="lazy",
|
133 |
+
)
|
134 |
|
135 |
+
gradio_app = gr.Blocks()
|
136 |
+
with gradio_app:
|
137 |
+
gr.HTML(
|
138 |
+
"""
|
139 |
+
<h1 style='text-align: center'>
|
140 |
+
YOLO Powered ASL(American Sign Language) Letter Detector PSA: It can't detect J or Z
|
141 |
+
</h1>
|
142 |
+
""")
|
143 |
+
with gr.Row():
|
144 |
+
with gr.Column():
|
145 |
+
app()
|
146 |
|
147 |
+
gradio_app.launch(debug=True)
|
|