atalaydenknalbant commited on
Commit
acb05a8
·
verified ·
1 Parent(s): 8b3771a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +107 -38
app.py CHANGED
@@ -1,48 +1,117 @@
1
  import gradio as gr
2
  import spaces
3
  import PIL.Image as Image
4
- from ultralytics import YOLO
 
5
 
6
 
 
 
 
7
 
8
 
9
- @spaces.GPU
10
- def predict_image(img, conf_threshold, iou_threshold):
11
-
12
- model = YOLO("best.pt")
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
- results = model.predict(
15
- source=img,
16
- conf=conf_threshold,
17
- iou=iou_threshold,
18
- show_labels=True,
19
- show_conf=True,
20
- imgsz=416,
21
- max_det=1
22
- )
23
-
24
- for r in results:
25
- im_array = r.plot()
26
- im = Image.fromarray(im_array[..., ::-1])
27
-
28
- return im
29
-
30
-
31
- asl = gr.Interface(
32
- fn=predict_image,
33
- inputs=[
34
- gr.Image(type="pil", label="Upload Image"),
35
- gr.Slider(minimum=0, maximum=1, value=0.25, label="Confidence threshold"),
36
- gr.Slider(minimum=0, maximum=1, value=0.45, label="IoU threshold")
37
- ],
38
- outputs=gr.Image(type="pil", label="Result"),
39
- title="ASL Detector YOLOV9e",
40
- description="Upload images for inference. The Ultralytics YOLOV9e model is used by default. Letter Z is not supported.",
41
- examples=[
42
- ["y.jpg", 0.25, 0.45],
43
- ["b.jpg", 0.25, 0.45],
44
  ]
45
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
 
47
- if __name__ == '__main__':
48
- asl.launch()
 
1
  import gradio as gr
2
  import spaces
3
  import PIL.Image as Image
4
+ from ultralytics import YOLO, YOLOv10
5
+ from huggingface_hub import hf_hub_download
6
 
7
 
8
+ def download_models(model_id):
9
+ hf_hub_download("atalaydenknalbant/", filename="asl-models", local_dir=f"./")
10
+ return f"./{model_id}"
11
 
12
 
13
+
14
+ box_annotator = sv.BoxAnnotator()
15
+ category_dict = {'A': 0, 'B': 1, 'C': 2, 'D': 3, 'E': 4, 'F': 5, 'G': 6, 'H': 7, 'I': 8, 'J': 9,
16
+ 'K': 10, 'L': 11, 'M': 12, 'N': 13, 'O': 14, 'P': 15, 'Q': 16, 'R': 17, 'S': 18,
17
+ 'T': 19, 'U': 20, 'V': 21, 'W': 22, 'X': 23, 'Y': 24, 'Z': 25}
18
+
19
+
20
+
21
+ @spaces.GPU(duration=200)
22
+ def yolo_inference(image, model_id, conf_threshold, iou_threshold):
23
+ model_path = download_models(model_id)
24
+ if model_id[:7] == 'yolov10':
25
+ model = YOLOv10(model_path)
26
+ else:
27
+ model = YOLO(model_path)
28
+ results = model(source=image, imgsz=416, iou=iou_threshold, conf=conf_threshold, verbose=False, max_det=1)[0]
29
+ detections = sv.Detections.from_ultralytics(results)
30
 
31
+ labels = [
32
+ f"{category_dict[class_id]} {confidence:.2f}"
33
+ for class_id, confidence in zip(detections.class_id, detections.confidence)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
  ]
35
+ annotated_image = box_annotator.annotate(image, detections=detections, labels=labels)
36
+
37
+ return annotated_image
38
+
39
+ def app():
40
+ with gr.Blocks():
41
+ with gr.Row():
42
+ with gr.Column():
43
+ image = gr.Image(type="pil", label="Image")
44
+
45
+ model_id = gr.Dropdown(
46
+ label="Model",
47
+ choices=[
48
+ "yolov10x.pt",
49
+ "yolov10s.pt",
50
+ "yolov9e.pt",
51
+ "yolov8x.pt",
52
+ ],
53
+ value="yolov10s.pt",
54
+ )
55
+ conf_threshold = gr.Slider(
56
+ label="Confidence Threshold",
57
+ minimum=0.1,
58
+ maximum=1.0,
59
+ step=0.1,
60
+ value=0.25,
61
+ )
62
+ iou_threshold = gr.Slider(
63
+ label="IoU Threshold",
64
+ minimum=0.1,
65
+ maximum=1.0,
66
+ step=0.1,
67
+ value=0.45,
68
+ )
69
+ yolov10_infer = gr.Button(value="Detect Objects")
70
+
71
+ with gr.Column():
72
+ output_image = gr.Image(type="pil", label="Annotated Image")
73
+
74
+ yolov10_infer.click(
75
+ fn=yolo_inference,
76
+ inputs=[
77
+ image,
78
+ model_id,
79
+ conf_threshold,
80
+ iou_threshold,
81
+ ],
82
+ outputs=[output_image],
83
+ )
84
+
85
+ gr.Examples(
86
+ examples=[
87
+ [
88
+ "b.jpg",
89
+ "yolov10x.pt",
90
+ 0.25,
91
+ 0.45,
92
+ ],
93
+ [
94
+ "y.jpg",
95
+ "yolov10x.pt",
96
+ 0.25,
97
+ 0.45,
98
+ ],
99
+ ],
100
+ fn=yolo_inference,
101
+ inputs=[
102
+ image,
103
+ model_id,
104
+ conf_threshold,
105
+ iou_threshold,
106
+ ],
107
+ outputs=[output_image],
108
+ cache_examples=True,
109
+ )
110
+
111
+ gradio_app = gr.Blocks()
112
+ with gradio_app:
113
+ with gr.Row():
114
+ with gr.Column():
115
+ app()
116
 
117
+ gradio_app.launch(debug=True)