atalaydenknalbant commited on
Commit
f075fbc
·
verified ·
1 Parent(s): f0e3b6f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +106 -132
app.py CHANGED
@@ -1,15 +1,108 @@
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",
@@ -19,129 +112,10 @@ model_filenames = [
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()
 
 
1
+ # Import libraries
2
+ import cv2
 
3
  from ultralytics import YOLO
4
+ from huggingface_hub import hf_hub_download # For Hugging Face model download
5
  import gradio as gr
6
 
7
+ # Define constants for ASL letters with color for bounding boxes
8
+ ASL_COLORS = {
9
+ 0: (191, 100, 21), # A
10
+ 1: (2, 62, 115), # B
11
+ 2: (140, 80, 58), # C
12
+ 3: (168, 181, 69), # D
13
+ 4: (2, 69, 84), # E
14
+ 5: (83, 115, 106), # F
15
+ 6: (255, 72, 88), # G
16
+ 7: (0, 204, 192), # H
17
+ 8: (116, 127, 127), # I
18
+ 9: (0, 153, 221), # J
19
+ 10: (196, 51, 2), # K
20
+ 11: (191, 100, 21), # L
21
+ 12: (2, 62, 115), # M
22
+ 13: (140, 80, 58), # N
23
+ 14: (168, 181, 69), # O
24
+ 15: (2, 69, 84), # P
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
+ # Define the function to download the models dynamically
39
+ def download_model(model_id):
40
+ # Use Hugging Face's hf_hub_download to download models
41
+ model_path = hf_hub_download(repo_id="atalaydenknalbant/asl-yolo-models", filename=model_id, local_dir="./")
42
+ return model_path
43
 
44
+ # Function for detecting objects (ASL letters) in the image
45
+ def detect(image_path, model_id):
46
+ """
47
+ Output inference image with bounding boxes and ASL letter predictions.
48
+ Args:
49
+ - image_path: Path to the image file
50
+ - model_id: Model filename to download
51
+ Return: image with bounding boxes and labels drawn
52
+ """
53
+ # Download and load the model dynamically
54
+ model_path = download_model(model_id)
55
+ detection_model = YOLO(model_path)
56
+
57
+ image = cv2.imread(image_path)
58
+ if image is None:
59
+ return image
60
+
61
+ # Predict on image
62
+ results = detection_model.predict(source=image, conf=0.2, iou=0.8) # Predict on image
63
+ boxes = results[0].boxes # Get bounding boxes
64
+
65
+ if len(boxes) == 0:
66
+ return image
67
+
68
+ # Draw bounding boxes and labels
69
+ for box in boxes:
70
+ detection_class_conf = round(box.conf.item(), 2) # Confidence score
71
+ class_id = int(box.cls.item()) # Get class ID
72
+
73
+ # Get start and end points of the bounding box
74
+ start_box = (int(box.xyxy[0][0]), int(box.xyxy[0][1]))
75
+ end_box = (int(box.xyxy[0][2]), int(box.xyxy[0][3]))
76
+
77
+ # 01. DRAW BOUNDING BOX OF OBJECT
78
+ line_thickness = round(0.001 * (image.shape[0] + image.shape[1]) / 2) + 1
79
+ image = cv2.rectangle(img=image,
80
+ pt1=start_box,
81
+ pt2=end_box,
82
+ color=ASL_COLORS[class_id],
83
+ thickness=line_thickness) # Draw the box with predefined colors
84
+
85
+ # 02. DRAW LABEL
86
+ asl_letter = chr(65 + class_id) # Convert class ID to ASL letter
87
+ text = f"{asl_letter} {detection_class_conf:.2f}" # Label text
88
+ font_thickness = max(line_thickness - 1, 1)
89
+ (font_scale_w, font_scale_h) = (line_thickness * 0.5, line_thickness * 0.5)
90
+ (text_w, text_h), _ = cv2.getTextSize(text=text, fontFace=2, fontScale=font_scale_w, thickness=font_thickness)
91
+
92
+ # Draw wrapping box for text
93
+ image = cv2.rectangle(img=image,
94
+ pt1=(start_box[0], start_box[1] - text_h - BOX_PADDING * 2),
95
+ pt2=(start_box[0] + text_w + BOX_PADDING * 2, start_box[1]),
96
+ color=ASL_COLORS[class_id],
97
+ thickness=-1)
98
+
99
+ # Put class name on image
100
+ start_text = (start_box[0] + BOX_PADDING, start_box[1] - BOX_PADDING)
101
+ image = cv2.putText(img=image, text=text, org=start_text, fontFace=0, color=(255, 255, 255), fontScale=font_scale_w, thickness=font_thickness)
102
+
103
+ return image
104
+
105
+ # Gradio interface
106
  model_filenames = [
107
  "yolov10s.pt",
108
  "yolov10x.pt",
 
112
  "yolov9s.pt"
113
  ]
114
 
115
+ iface = gr.Interface(fn=detect,
116
+ inputs=[gr.Image(label="Upload ASL letter image", type="filepath"),
117
+ gr.Dropdown(label="Model", choices=model_filenames, value=model_filenames[0])],
118
+ outputs="image")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
119
 
120
+ # Launch the interface
121
+ iface.launch()