StevenChen16 commited on
Commit
ffa2e5d
·
verified ·
1 Parent(s): eaf77a6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +93 -39
app.py CHANGED
@@ -1,5 +1,7 @@
1
  import gradio as gr
2
- from ultralytics import YOLOv10
 
 
3
  import supervision as sv
4
  import spaces
5
  from huggingface_hub import hf_hub_download
@@ -30,28 +32,71 @@ category_dict = {
30
  }
31
 
32
 
33
- @spaces.GPU(duration=200)
34
- def yolov10_inference(image, model_id, image_size, conf_threshold, iou_threshold):
35
  model_path = download_models(model_id)
36
  model = YOLOv10(model_path)
37
- results = model(source=image, imgsz=image_size, iou=iou_threshold, conf=conf_threshold, verbose=False)[0]
38
- detections = sv.Detections.from_ultralytics(results)
39
 
40
- labels = [
41
- f"{category_dict[class_id]} {confidence:.2f}"
42
- for class_id, confidence in zip(detections.class_id, detections.confidence)
43
- ]
44
- annotated_image = box_annotator.annotate(image, detections=detections, labels=labels)
 
 
 
 
 
 
 
 
 
 
45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
  return annotated_image
47
 
 
48
  def app():
49
  with gr.Blocks():
50
  with gr.Row():
51
  with gr.Column():
52
- # image = gr.Image(type="pil", label="Image")
53
- video = gr.Video(type="Video", visible=False)
54
-
 
 
 
 
55
  model_id = gr.Dropdown(
56
  label="Model",
57
  choices=[
@@ -88,46 +133,54 @@ def app():
88
  yolov10_infer = gr.Button(value="Detect Objects")
89
 
90
  with gr.Column():
91
- output_image = gr.Image(type="pil", label="Annotated Image")
92
  output_video = gr.Video(label="Annotated Video", visible=False)
93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94
  yolov10_infer.click(
95
- fn=yolov10_inference,
96
- inputs=[
97
- image,
98
- model_id,
99
- image_size,
100
- conf_threshold,
101
- iou_threshold,
102
- ],
103
- outputs=[output_image],
104
  )
105
 
106
  gr.Examples(
107
  examples=[
108
  [
109
- "dog.jpeg",
110
- "yolov10x.pt",
111
  640,
112
  0.25,
113
  0.45,
114
  ],
115
  [
116
- "huggingface.jpg",
117
- "yolov10m.pt",
118
- 640,
119
- 0.25,
120
- 0.45,
121
- ],
122
- [
123
- "zidane.jpg",
124
- "yolov10b.pt",
125
  640,
126
  0.25,
127
  0.45,
128
  ],
129
  ],
130
- fn=yolov10_inference,
131
  inputs=[
132
  image,
133
  model_id,
@@ -136,9 +189,10 @@ def app():
136
  iou_threshold,
137
  ],
138
  outputs=[output_image],
139
- cache_examples=True,
140
  )
141
 
 
142
  gradio_app = gr.Blocks()
143
  with gradio_app:
144
  gr.HTML(
@@ -150,12 +204,12 @@ with gradio_app:
150
  gr.HTML(
151
  """
152
  <h3 style='text-align: center'>
153
- Follow me for more!
154
- <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>
155
  </h3>
156
  """)
157
  with gr.Row():
158
  with gr.Column():
159
  app()
160
 
161
- gradio_app.launch(debug=True)
 
 
1
  import gradio as gr
2
+ import cv2
3
+ import tempfile
4
+ from ultralytics import YOLOv10
5
  import supervision as sv
6
  import spaces
7
  from huggingface_hub import hf_hub_download
 
32
  }
33
 
34
 
35
+ def yolov10_inference(image, video, model_id, image_size, conf_threshold, iou_threshold):
 
36
  model_path = download_models(model_id)
37
  model = YOLOv10(model_path)
 
 
38
 
39
+ if image:
40
+ results = model(source=image, imgsz=image_size, iou=iou_threshold, conf=conf_threshold, verbose=False)[0]
41
+ detections = sv.Detections.from_ultralytics(results)
42
+
43
+ labels = [
44
+ f"{category_dict[class_id]} {confidence:.2f}"
45
+ for class_id, confidence in zip(detections.class_id, detections.confidence)
46
+ ]
47
+ annotated_image = box_annotator.annotate(image, detections=detections, labels=labels)
48
+ return annotated_image[:, :, ::-1], None
49
+ else:
50
+ video_path = tempfile.mktemp(suffix=".webm")
51
+ with open(video_path, "wb") as f:
52
+ with open(video, "rb") as g:
53
+ f.write(g.read())
54
 
55
+ cap = cv2.VideoCapture(video_path)
56
+ fps = cap.get(cv2.CAP_PROP_FPS)
57
+ frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
58
+ frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
59
+
60
+ output_video_path = tempfile.mktemp(suffix=".webm")
61
+ out = cv2.VideoWriter(output_video_path, cv2.VideoWriter_fourcc(*'vp80'), fps, (frame_width, frame_height))
62
+
63
+ while cap.isOpened():
64
+ ret, frame = cap.read()
65
+ if not ret:
66
+ break
67
+
68
+ results = model(source=frame, imgsz=image_size, iou=iou_threshold, conf=conf_threshold, verbose=False)[0]
69
+ detections = sv.Detections.from_ultralytics(results)
70
+
71
+ labels = [
72
+ f"{category_dict[class_id]} {confidence:.2f}"
73
+ for class_id, confidence in zip(detections.class_id, detections.confidence)
74
+ ]
75
+ annotated_frame = box_annotator.annotate(frame, detections=detections, labels=labels)
76
+ out.write(annotated_frame)
77
+
78
+ cap.release()
79
+ out.release()
80
+
81
+ return None, output_video_path
82
+
83
+
84
+ def yolov10_inference_for_examples(image, model_id, image_size, conf_threshold, iou_threshold):
85
+ annotated_image, _ = yolov10_inference(image, None, model_id, image_size, conf_threshold, iou_threshold)
86
  return annotated_image
87
 
88
+
89
  def app():
90
  with gr.Blocks():
91
  with gr.Row():
92
  with gr.Column():
93
+ image = gr.Image(type="pil", label="Image", visible=True)
94
+ video = gr.Video(label="Video", visible=False)
95
+ input_type = gr.Radio(
96
+ choices=["Image", "Video"],
97
+ value="Image",
98
+ label="Input Type",
99
+ )
100
  model_id = gr.Dropdown(
101
  label="Model",
102
  choices=[
 
133
  yolov10_infer = gr.Button(value="Detect Objects")
134
 
135
  with gr.Column():
136
+ output_image = gr.Image(type="numpy", label="Annotated Image", visible=True)
137
  output_video = gr.Video(label="Annotated Video", visible=False)
138
 
139
+ def update_visibility(input_type):
140
+ image_visibility = input_type == "Image"
141
+ return (
142
+ gr.update(visible=image_visibility),
143
+ gr.update(visible=not image_visibility),
144
+ gr.update(visible=image_visibility),
145
+ gr.update(visible=not image_visibility),
146
+ )
147
+
148
+ input_type.change(
149
+ fn=update_visibility,
150
+ inputs=[input_type],
151
+ outputs=[image, video, output_image, output_video],
152
+ )
153
+
154
+ def run_inference(image, video, model_id, image_size, conf_threshold, iou_threshold, input_type):
155
+ if input_type == "Image":
156
+ return yolov10_inference(image, None, model_id, image_size, conf_threshold, iou_threshold)
157
+ else:
158
+ return yolov10_inference(None, video, model_id, image_size, conf_threshold, iou_threshold)
159
+
160
  yolov10_infer.click(
161
+ fn=run_inference,
162
+ inputs=[image, video, model_id, image_size, conf_threshold, iou_threshold, input_type],
163
+ outputs=[output_image, output_video],
 
 
 
 
 
 
164
  )
165
 
166
  gr.Examples(
167
  examples=[
168
  [
169
+ "ultralytics/assets/bus.jpg",
170
+ "yolov10s.pt",
171
  640,
172
  0.25,
173
  0.45,
174
  ],
175
  [
176
+ "ultralytics/assets/zidane.jpg",
177
+ "yolov10s.pt",
 
 
 
 
 
 
 
178
  640,
179
  0.25,
180
  0.45,
181
  ],
182
  ],
183
+ fn=yolov10_inference_for_examples,
184
  inputs=[
185
  image,
186
  model_id,
 
189
  iou_threshold,
190
  ],
191
  outputs=[output_image],
192
+ cache_examples='lazy',
193
  )
194
 
195
+
196
  gradio_app = gr.Blocks()
197
  with gradio_app:
198
  gr.HTML(
 
204
  gr.HTML(
205
  """
206
  <h3 style='text-align: center'>
207
+ <a href='https://arxiv.org/abs/2405.14458' target='_blank'>arXiv</a> | <a href='https://github.com/THU-MIG/yolov10' target='_blank'>github</a>
 
208
  </h3>
209
  """)
210
  with gr.Row():
211
  with gr.Column():
212
  app()
213
 
214
+ if __name__ == '__main__':
215
+ gradio_app.launch()