atalaydenknalbant commited on
Commit
cb1fbea
·
verified ·
1 Parent(s): a6ae674

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +135 -92
app.py CHANGED
@@ -1,104 +1,147 @@
1
- # Import libraries
2
- import cv2
 
3
  from ultralytics import YOLO
 
4
  import gradio as gr
5
- import spaces
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
- # Load the model from the local directory
39
- DETECTION_MODEL = YOLO("yolov10s.pt")
 
 
40
 
41
  @spaces.GPU
42
- def detect(image_path):
43
- """
44
- Output inference image with bounding boxes and ASL letter predictions.
45
- Args:
46
- - image_path: Path to the image file
47
- Return: image with bounding boxes and labels drawn
48
- """
49
- # Load the image
50
- image = cv2.imread(image_path)
51
- if image is None:
52
- return image
 
 
 
 
 
 
 
 
 
 
53
 
54
- # Predict on image
55
- results = DETECTION_MODEL.predict(source=image, conf=0.2, iou=0.8) # Predict on image
56
- boxes = results[0].boxes # Get bounding boxes
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
 
58
- if len(boxes) == 0:
59
- return image
60
 
61
- # Draw bounding boxes and labels
62
- for box in boxes:
63
- detection_class_conf = round(box.conf.item(), 2) # Confidence score
64
- class_id = int(box.cls.item()) # Get class ID
65
-
66
- # Get start and end points of the bounding box
67
- start_box = (int(box.xyxy[0][0]), int(box.xyxy[0][1]))
68
- end_box = (int(box.xyxy[0][2]), int(box.xyxy[0][3]))
69
-
70
- # Draw bounding box
71
- line_thickness = round(0.001 * (image.shape[0] + image.shape[1]) / 2) + 1
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
- return image
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97
 
98
- # Gradio interface
99
- iface = gr.Interface(fn=detect,
100
- inputs=gr.Image(label="Upload ASL letter image", type="filepath"),
101
- outputs="image")
 
 
 
 
 
 
 
102
 
103
- # Launch the interface
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)