Spaces:
Running
on
Zero
Running
on
Zero
Commit
·
082831f
1
Parent(s):
790227b
Try compiled
Browse files- app.py +17 -36
- requirements.txt +1 -1
app.py
CHANGED
|
@@ -2,12 +2,18 @@ import spaces
|
|
| 2 |
import gradio as gr
|
| 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 |
|
| 12 |
def draw_bounding_boxes(image, results, model, threshold=0.3):
|
| 13 |
draw = ImageDraw.Draw(image)
|
|
@@ -22,47 +28,25 @@ def draw_bounding_boxes(image, results, model, threshold=0.3):
|
|
| 22 |
draw.text((box[0], box[1]), f"{label}: {score:.2f}", fill="red")
|
| 23 |
return image
|
| 24 |
|
|
|
|
| 25 |
|
| 26 |
@spaces.GPU
|
| 27 |
def inference(image, conf_threshold):
|
| 28 |
inputs = image_processor(images=image, return_tensors="pt")
|
| 29 |
|
|
|
|
| 30 |
with torch.no_grad():
|
| 31 |
outputs = model(**inputs)
|
| 32 |
|
| 33 |
results = image_processor.post_process_object_detection(
|
| 34 |
-
outputs, target_sizes=torch.tensor([image.size[::-1]]), threshold=
|
| 35 |
)
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
def app():
|
| 41 |
-
with gr.Blocks():
|
| 42 |
-
with gr.Row():
|
| 43 |
-
with gr.Column():
|
| 44 |
-
image = gr.Image(
|
| 45 |
-
type="pil",
|
| 46 |
-
label="Image",
|
| 47 |
-
visible=True,
|
| 48 |
-
sources="webcam",
|
| 49 |
-
height=500,
|
| 50 |
-
width=500,
|
| 51 |
-
)
|
| 52 |
-
conf_threshold = gr.Slider(
|
| 53 |
-
label="Confidence Threshold",
|
| 54 |
-
minimum=0.0,
|
| 55 |
-
maximum=1.0,
|
| 56 |
-
step=0.05,
|
| 57 |
-
value=0.25,
|
| 58 |
-
)
|
| 59 |
-
image.stream(
|
| 60 |
-
fn=yolov10_inference,
|
| 61 |
-
inputs=[image, conf_threshold],
|
| 62 |
-
outputs=[image],
|
| 63 |
-
stream_every=0.2,
|
| 64 |
-
time_limit=30,
|
| 65 |
-
)
|
| 66 |
|
| 67 |
|
| 68 |
css = """.my-group {max-width: 600px !important; max-height: 600 !important;}
|
|
@@ -88,10 +72,7 @@ with gr.Blocks(css=css) as app:
|
|
| 88 |
image = gr.Image(
|
| 89 |
type="pil",
|
| 90 |
label="Image",
|
| 91 |
-
visible=True,
|
| 92 |
sources="webcam",
|
| 93 |
-
height=500,
|
| 94 |
-
width=500,
|
| 95 |
)
|
| 96 |
conf_threshold = gr.Slider(
|
| 97 |
label="Confidence Threshold",
|
|
@@ -104,7 +85,7 @@ with gr.Blocks(css=css) as app:
|
|
| 104 |
fn=inference,
|
| 105 |
inputs=[image, conf_threshold],
|
| 106 |
outputs=[image],
|
| 107 |
-
stream_every=0.
|
| 108 |
time_limit=30,
|
| 109 |
)
|
| 110 |
if __name__ == "__main__":
|
|
|
|
| 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 |
|
| 9 |
image_processor = RTDetrImageProcessor.from_pretrained("PekingU/rtdetr_r50vd")
|
| 10 |
+
model = RTDetrForObjectDetection.from_pretrained("PekingU/rtdetr_r50vd", torch_dtype=torch.float16).to("cuda")
|
| 11 |
+
model = torch.compile(model, mode="reduce-overhead")
|
| 12 |
|
| 13 |
+
# Compile by running inference
|
| 14 |
+
inputs = image_processor(images="bus.png", return_tensors="pt").to("cuda", torch.float16)
|
| 15 |
+
with torch.no_grad():
|
| 16 |
+
outputs = model(**inputs)
|
| 17 |
|
| 18 |
def draw_bounding_boxes(image, results, model, threshold=0.3):
|
| 19 |
draw = ImageDraw.Draw(image)
|
|
|
|
| 28 |
draw.text((box[0], box[1]), f"{label}: {score:.2f}", fill="red")
|
| 29 |
return image
|
| 30 |
|
| 31 |
+
import time
|
| 32 |
|
| 33 |
@spaces.GPU
|
| 34 |
def inference(image, conf_threshold):
|
| 35 |
inputs = image_processor(images=image, return_tensors="pt")
|
| 36 |
|
| 37 |
+
start = time.time()
|
| 38 |
with torch.no_grad():
|
| 39 |
outputs = model(**inputs)
|
| 40 |
|
| 41 |
results = image_processor.post_process_object_detection(
|
| 42 |
+
outputs, target_sizes=torch.tensor([image.size[::-1]]), threshold=conf_threshold
|
| 43 |
)
|
| 44 |
+
end = time.time()
|
| 45 |
+
print("time: ", end - start)
|
| 46 |
|
| 47 |
+
bbs = draw_bounding_boxes(image, results, model, threshold=conf_threshold)
|
| 48 |
+
print("bbs: ", time.time() - end)
|
| 49 |
+
return bbs
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
|
| 52 |
css = """.my-group {max-width: 600px !important; max-height: 600 !important;}
|
|
|
|
| 72 |
image = gr.Image(
|
| 73 |
type="pil",
|
| 74 |
label="Image",
|
|
|
|
| 75 |
sources="webcam",
|
|
|
|
|
|
|
| 76 |
)
|
| 77 |
conf_threshold = gr.Slider(
|
| 78 |
label="Confidence Threshold",
|
|
|
|
| 85 |
fn=inference,
|
| 86 |
inputs=[image, conf_threshold],
|
| 87 |
outputs=[image],
|
| 88 |
+
stream_every=0.1,
|
| 89 |
time_limit=30,
|
| 90 |
)
|
| 91 |
if __name__ == "__main__":
|
requirements.txt
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
git+https://github.com/THU-MIG/yolov10.git
|
| 2 |
safetensors==0.4.3
|
|
|
|
| 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 |
+
transformers
|
| 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
|