freddyaboulton HF staff commited on
Commit
9740995
1 Parent(s): 8fbff22

Go back to yolov10

Browse files
Files changed (2) hide show
  1. app.py +25 -63
  2. requirements.txt +1 -1
app.py CHANGED
@@ -2,81 +2,42 @@ import spaces
2
  import gradio as gr
3
  import cv2
4
  import tempfile
5
- from PIL import Image, ImageDraw, ImageFont
6
- from transformers import RTDetrForObjectDetection, RTDetrImageProcessor
7
- import torch
8
- import requests
9
 
10
- image_processor = RTDetrImageProcessor.from_pretrained("PekingU/rtdetr_r50vd")
11
- model = RTDetrForObjectDetection.from_pretrained("PekingU/rtdetr_r50vd", torch_dtype=torch.float16).to("cuda")
12
- model = torch.compile(model, mode="reduce-overhead")
13
-
14
- # Compile by running inference
15
- url = 'http://images.cocodataset.org/val2017/000000039769.jpg'
16
- image = Image.open(requests.get(url, stream=True).raw)
17
- inputs = image_processor(images=image, return_tensors="pt").to("cuda", torch.float16)
18
- with torch.no_grad():
19
- outputs = model(**inputs)
20
-
21
- def draw_bounding_boxes(image, results, model, threshold=0.3):
22
- draw = ImageDraw.Draw(image)
23
- for result in results:
24
- for score, label_id, box in zip(
25
- result["scores"], result["labels"], result["boxes"]
26
- ):
27
- if score > threshold:
28
- label = model.config.id2label[label_id.item()]
29
- box = [round(i) for i in box.tolist()]
30
- draw.rectangle(box, outline="red", width=3)
31
- draw.text((box[0], box[1]), f"{label}: {score:.2f}", fill="red")
32
- return image
33
-
34
- import time
35
 
36
  @spaces.GPU
37
- def inference(image, conf_threshold):
38
- inputs = image_processor(images=image, return_tensors="pt")
39
-
40
  start = time.time()
41
- with torch.no_grad():
42
- outputs = model(**inputs)
43
-
44
- results = image_processor.post_process_object_detection(
45
- outputs, target_sizes=torch.tensor([image.size[::-1]]), threshold=conf_threshold
46
- )
47
  end = time.time()
48
- print("time: ", end - start)
 
 
49
 
50
- bbs = draw_bounding_boxes(image, results, model, threshold=conf_threshold)
51
- print("bbs: ", time.time() - end)
52
- return bbs
53
 
54
-
55
- css = """.my-group {max-width: 600px !important; max-height: 600 !important;}
56
  .my-column {display: flex !important; justify-content: center !important; align-items: center !important};"""
57
 
 
58
  with gr.Blocks(css=css) as app:
59
  gr.HTML(
60
  """
61
  <h1 style='text-align: center'>
62
- Near Real-Time Webcam Stream with RT-DETR
63
  </h1>
64
- """
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/lyuwenyu/RT-DETR' target='_blank'>github</a>
70
  </h3>
71
- """
72
- )
73
  with gr.Column(elem_classes=["my-column"]):
74
  with gr.Group(elem_classes=["my-group"]):
75
- image = gr.Image(
76
- type="pil",
77
- label="Image",
78
- sources="webcam",
79
- )
80
  conf_threshold = gr.Slider(
81
  label="Confidence Threshold",
82
  minimum=0.0,
@@ -84,12 +45,13 @@ with gr.Blocks(css=css) as app:
84
  step=0.05,
85
  value=0.85,
86
  )
87
- image.stream(
88
- fn=inference,
89
- inputs=[image, conf_threshold],
90
- outputs=[image],
91
- stream_every=0.1,
92
- time_limit=30,
93
- )
94
- if __name__ == "__main__":
 
95
  app.launch()
 
2
  import gradio as gr
3
  import cv2
4
  import tempfile
5
+ from ultralytics import YOLOv10
 
 
 
6
 
7
+ model = YOLOv10.from_pretrained(f'jameslahm/yolov10n')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
  @spaces.GPU
10
+ def yolov10_inference(image, conf_threshold):
11
+ width, _ = image.size
12
+ import time
13
  start = time.time()
14
+ results = model.predict(source=image, imgsz=width, conf=conf_threshold)
 
 
 
 
 
15
  end = time.time()
16
+ print("time", end - start)
17
+ annotated_image = results[0].plot()
18
+ return annotated_image[:, :, ::-1]
19
 
 
 
 
20
 
21
+ css=""".my-group {max-width: 600px !important; max-height: 600 !important;}
 
22
  .my-column {display: flex !important; justify-content: center !important; align-items: center !important};"""
23
 
24
+
25
  with gr.Blocks(css=css) as app:
26
  gr.HTML(
27
  """
28
  <h1 style='text-align: center'>
29
+ YOLOv10 Webcam Stream
30
  </h1>
31
+ """)
 
32
  gr.HTML(
33
  """
34
  <h3 style='text-align: center'>
35
+ <a href='https://arxiv.org/abs/2405.14458' target='_blank'>arXiv</a> | <a href='https://github.com/THU-MIG/yolov10' target='_blank'>github</a>
36
  </h3>
37
+ """)
 
38
  with gr.Column(elem_classes=["my-column"]):
39
  with gr.Group(elem_classes=["my-group"]):
40
+ image = gr.Image(type="pil", label="Image", sources="webcam")
 
 
 
 
41
  conf_threshold = gr.Slider(
42
  label="Confidence Threshold",
43
  minimum=0.0,
 
45
  step=0.05,
46
  value=0.85,
47
  )
48
+ image.stream(
49
+ fn=yolov10_inference,
50
+ inputs=[image, conf_threshold],
51
+ outputs=[image],
52
+ stream_every=0.2,
53
+ time_limit=30
54
+ )
55
+
56
+ if __name__ == '__main__':
57
  app.launch()
requirements.txt CHANGED
@@ -1,4 +1,4 @@
1
  safetensors==0.4.3
2
- transformers @ git+https://github.com/yonigozlan/transformers@optim-rt-detr
3
  gradio-client @ git+https://github.com/gradio-app/gradio@66349fe26827e3a3c15b738a1177e95fec7f5554#subdirectory=client/python
4
  https://gradio-pypi-previews.s3.amazonaws.com/66349fe26827e3a3c15b738a1177e95fec7f5554/gradio-4.42.0-py3-none-any.whl
 
1
  safetensors==0.4.3
2
+ git+https://github.com/THU-MIG/yolov10.git
3
  gradio-client @ git+https://github.com/gradio-app/gradio@66349fe26827e3a3c15b738a1177e95fec7f5554#subdirectory=client/python
4
  https://gradio-pypi-previews.s3.amazonaws.com/66349fe26827e3a3c15b738a1177e95fec7f5554/gradio-4.42.0-py3-none-any.whl