freddyaboulton HF staff commited on
Commit
ccc35d4
·
1 Parent(s): 385e56e
Files changed (1) hide show
  1. app.py +20 -5
app.py CHANGED
@@ -3,10 +3,23 @@ import gradio as gr
3
  import cv2
4
  import tempfile
5
  from ultralytics import YOLOv10
 
6
 
7
  image_processor = RTDetrImageProcessor.from_pretrained("PekingU/rtdetr_r50vd")
8
  model = RTDetrForObjectDetection.from_pretrained("PekingU/rtdetr_r50vd")
9
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  @spaces.GPU
11
  def yolov10_inference(image, conf_threshold):
12
 
@@ -17,6 +30,7 @@ def yolov10_inference(image, conf_threshold):
17
 
18
  results = image_processor.post_process_object_detection(outputs, target_sizes=torch.tensor([image.size[::-1]]), threshold=0.3)
19
 
 
20
 
21
 
22
  def app():
@@ -39,23 +53,24 @@ def app():
39
  time_limit=30
40
  )
41
 
 
 
42
 
43
- gradio_app = gr.Blocks()
44
- with gradio_app:
45
  gr.HTML(
46
  """
47
  <h1 style='text-align: center'>
48
- YOLOv10 Webcam Stream
49
  </h1>
50
  """)
51
  gr.HTML(
52
  """
53
  <h3 style='text-align: center'>
54
- <a href='https://arxiv.org/abs/2405.14458' target='_blank'>arXiv</a> | <a href='https://github.com/THU-MIG/yolov10' target='_blank'>github</a>
55
  </h3>
56
  """)
57
  with gr.Row():
58
  with gr.Column():
59
  app()
60
  if __name__ == '__main__':
61
- gradio_app.launch()
 
3
  import cv2
4
  import tempfile
5
  from ultralytics import YOLOv10
6
+ from PIL import Image, ImageDraw, ImageFont
7
 
8
  image_processor = RTDetrImageProcessor.from_pretrained("PekingU/rtdetr_r50vd")
9
  model = RTDetrForObjectDetection.from_pretrained("PekingU/rtdetr_r50vd")
10
 
11
+ def draw_bounding_boxes(image, results, model, threshold=0.3):
12
+ draw = ImageDraw.Draw(image)
13
+ for result in results:
14
+ for score, label_id, box in zip(result["scores"], result["labels"], result["boxes"]):
15
+ if score > threshold:
16
+ label = model.config.id2label[label_id.item()]
17
+ box = [round(i) for i in box.tolist()]
18
+ draw.rectangle(box, outline="red", width=3)
19
+ draw.text((box[0], box[1]), f"{label}: {score:.2f}", fill="red")
20
+ return image
21
+
22
+
23
  @spaces.GPU
24
  def yolov10_inference(image, conf_threshold):
25
 
 
30
 
31
  results = image_processor.post_process_object_detection(outputs, target_sizes=torch.tensor([image.size[::-1]]), threshold=0.3)
32
 
33
+ return draw_bounding_boxes(image, results, model, threshold=conf_threshold)
34
 
35
 
36
  def app():
 
53
  time_limit=30
54
  )
55
 
56
+ css=""".my-group {max-width: 600px !important; max-height: 600 !important;}
57
+ .my-column {display: flex !important; justify-content: center !important; align-items: center !important};"""
58
 
59
+ with gr.Blocks(css=css) as app:
 
60
  gr.HTML(
61
  """
62
  <h1 style='text-align: center'>
63
+ Near Real-Time Webcam Stream with RTDetr
64
  </h1>
65
  """)
66
  gr.HTML(
67
  """
68
  <h3 style='text-align: center'>
69
+ <a href='https://arxiv.org/abs/2304.08069' target='_blank'>arXiv</a> | <a href='https://github.com/THU-MIG/yolov10' target='_blank'>github</a>
70
  </h3>
71
  """)
72
  with gr.Row():
73
  with gr.Column():
74
  app()
75
  if __name__ == '__main__':
76
+ app.launch()