akhil5423 commited on
Commit
5d3268a
·
verified ·
1 Parent(s): d11c47b

Update app.py with complete YOLOv10 code from GitHub

Browse files
Files changed (1) hide show
  1. app.py +162 -18
app.py CHANGED
@@ -1,23 +1,167 @@
1
  import gradio as gr
 
 
2
  from ultralytics import YOLO
3
- import PIL
4
-
5
- model = YOLO("yolov8n.pt")
6
-
7
- def predict_image(img):
8
- results = model.predict(source=img)
9
- annotated_img = results[0].plot()
10
- annotated_img = PIL.Image.fromarray(annotated_img[...,::-1])
11
-
12
- return annotated_img
13
-
14
- gradio_app = gr.Interface(
15
- fn=predict_image,
16
- inputs=gr.Image(type="pil"),
17
- outputs=gr.Image(type="pil"),
18
- title="Object Detection",
19
- examples=["dog.webp", "zidane.jpg", "huggingface.png"]
20
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
  if __name__ == "__main__":
23
  gradio_app.launch()
 
1
  import gradio as gr
2
+ import spaces
3
+ from PIL import Image
4
  from ultralytics import YOLO
5
+
6
+ # Load Models
7
+ models = {
8
+ "yolov10n": YOLO("jameslahm/yolov10n"),
9
+ "yolov10s": YOLO("jameslahm/yolov10s"),
10
+ "yolov10m": YOLO("jameslahm/yolov10m"),
11
+ "yolov10b": YOLO("jameslahm/yolov10b"),
12
+ "yolov10l": YOLO("jameslahm/yolov10l"),
13
+ "yolov10x": YOLO("jameslahm/yolov10x"),
14
+ }
15
+
16
+
17
+ @spaces.GPU(duration=30)
18
+ def yolov10_inference(image, model_id, image_size, conf_threshold, iou_threshold):
19
+ model = models[model_id]
20
+ results = model.predict(
21
+ source=image,
22
+ imgsz=image_size,
23
+ conf=conf_threshold,
24
+ iou=iou_threshold,
25
+ )
26
+ annotated_image = results[0].plot()
27
+ return Image.fromarray(annotated_image[..., ::-1])
28
+
29
+
30
+ def app():
31
+ with gr.Blocks():
32
+ with gr.Row():
33
+ with gr.Column():
34
+ image = gr.Image(type="pil", label="Image")
35
+ model_id = gr.Dropdown(
36
+ label="Model",
37
+ choices=[
38
+ "yolov10n",
39
+ "yolov10s",
40
+ "yolov10m",
41
+ "yolov10b",
42
+ "yolov10l",
43
+ "yolov10x",
44
+ ],
45
+ value="yolov10m",
46
+ )
47
+ image_size = gr.Slider(
48
+ label="Image Size",
49
+ minimum=320,
50
+ maximum=1280,
51
+ step=32,
52
+ value=640,
53
+ )
54
+ conf_threshold = gr.Slider(
55
+ label="Confidence Threshold",
56
+ minimum=0.0,
57
+ maximum=1.0,
58
+ step=0.05,
59
+ value=0.25,
60
+ )
61
+ iou_threshold = gr.Slider(
62
+ label="IoU Threshold",
63
+ minimum=0.0,
64
+ maximum=1.0,
65
+ step=0.05,
66
+ value=0.45,
67
+ )
68
+ yolov10_infer = gr.Button(value="Detect Objects")
69
+
70
+ with gr.Column():
71
+ output_image = gr.Image(type="pil", label="Annotated Image")
72
+
73
+ gr.Examples(
74
+ examples=[
75
+ ["dog.jpeg", "yolov10m", 640, 0.25, 0.45],
76
+ ["huggingface.jpg", "yolov10m", 640, 0.25, 0.45],
77
+ ["zidane.jpg", "yolov10m", 640, 0.25, 0.45],
78
+ ],
79
+ fn=yolov10_inference,
80
+ inputs=[image, model_id, image_size, conf_threshold, iou_threshold],
81
+ outputs=[output_image],
82
+ cache_examples='lazy',
83
+ )
84
+
85
+ yolov10_infer.click(
86
+ fn=yolov10_inference,
87
+ inputs=[image, model_id, image_size, conf_threshold, iou_threshold],
88
+ outputs=[output_image],
89
+ )
90
+
91
+
92
+ gradio_app = gr.Blocks()
93
+ with gradio_app:
94
+ gr.HTML(
95
+ """
96
+ <h1 style='text-align: center'>
97
+ YOLOv10: Real-Time End-to-End Object Detection
98
+ </h1>
99
+ """)
100
+ gr.HTML(
101
+ """
102
+ <h3 style='text-align: center'>
103
+ Follow me for more!
104
+ <a href='https://twitter.com/kadirnar_ai' target='_blank'>Twitter</a> | <a href='https://github.com/kadirnar' target='_blank'>Github</a> | <a href='https://www.linkedin.com/in/kadir-nar/' target='_blank'>Linkedin</a> | <a href='https://www.huggingface.co/kadirnar/' target='_blank'>HuggingFace</a>
105
+ </h3>
106
+ """)
107
+ with gr.Row():
108
+ with gr.Column():
109
+ image = gr.Image(type="pil", label="Image")
110
+ model_id = gr.Dropdown(
111
+ label="Model",
112
+ choices=[
113
+ "yolov10n",
114
+ "yolov10s",
115
+ "yolov10m",
116
+ "yolov10b",
117
+ "yolov10l",
118
+ "yolov10x",
119
+ ],
120
+ value="yolov10m",
121
+ )
122
+ image_size = gr.Slider(
123
+ label="Image Size",
124
+ minimum=320,
125
+ maximum=1280,
126
+ step=32,
127
+ value=640,
128
+ )
129
+ conf_threshold = gr.Slider(
130
+ label="Confidence Threshold",
131
+ minimum=0.0,
132
+ maximum=1.0,
133
+ step=0.05,
134
+ value=0.25,
135
+ )
136
+ iou_threshold = gr.Slider(
137
+ label="IoU Threshold",
138
+ minimum=0.0,
139
+ maximum=1.0,
140
+ step=0.05,
141
+ value=0.45,
142
+ )
143
+ yolov10_infer = gr.Button(value="Detect Objects")
144
+
145
+ with gr.Column():
146
+ output_image = gr.Image(type="pil", label="Annotated Image")
147
+
148
+ gr.Examples(
149
+ examples=[
150
+ ["dog.jpeg", "yolov10m", 640, 0.25, 0.45],
151
+ ["huggingface.jpg", "yolov10m", 640, 0.25, 0.45],
152
+ ["zidane.jpg", "yolov10m", 640, 0.25, 0.45],
153
+ ],
154
+ fn=yolov10_inference,
155
+ inputs=[image, model_id, image_size, conf_threshold, iou_threshold],
156
+ outputs=[output_image],
157
+ cache_examples='lazy',
158
+ )
159
+
160
+ yolov10_infer.click(
161
+ fn=yolov10_inference,
162
+ inputs=[image, model_id, image_size, conf_threshold, iou_threshold],
163
+ outputs=[output_image],
164
+ )
165
 
166
  if __name__ == "__main__":
167
  gradio_app.launch()