Spaces:
Runtime error
Runtime error
Upload folder using huggingface_hub
Browse files- README.md +8 -8
- draw_boxes.py +45 -0
- requirements.txt +7 -0
- run.ipynb +1 -0
- run.py +115 -0
README.md
CHANGED
@@ -1,12 +1,12 @@
|
|
|
|
1 |
---
|
2 |
-
title:
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
sdk: gradio
|
7 |
-
sdk_version:
|
8 |
-
app_file:
|
9 |
pinned: false
|
|
|
10 |
---
|
11 |
-
|
12 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
+
|
2 |
---
|
3 |
+
title: rt-detr-object-detection_main
|
4 |
+
emoji: π₯
|
5 |
+
colorFrom: indigo
|
6 |
+
colorTo: indigo
|
7 |
sdk: gradio
|
8 |
+
sdk_version: 5.0.0
|
9 |
+
app_file: run.py
|
10 |
pinned: false
|
11 |
+
hf_oauth: true
|
12 |
---
|
|
|
|
draw_boxes.py
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from PIL import ImageDraw, ImageFont # type: ignore
|
2 |
+
import colorsys
|
3 |
+
|
4 |
+
|
5 |
+
def get_color(label):
|
6 |
+
# Simple hash function to generate consistent colors for each label
|
7 |
+
hash_value = hash(label)
|
8 |
+
hue = (hash_value % 100) / 100.0
|
9 |
+
saturation = 0.7
|
10 |
+
value = 0.9
|
11 |
+
rgb = colorsys.hsv_to_rgb(hue, saturation, value)
|
12 |
+
return tuple(int(x * 255) for x in rgb)
|
13 |
+
|
14 |
+
|
15 |
+
def draw_bounding_boxes(image, results: dict, model, threshold=0.3):
|
16 |
+
draw = ImageDraw.Draw(image)
|
17 |
+
font = ImageFont.load_default()
|
18 |
+
|
19 |
+
for score, label_id, box in zip(
|
20 |
+
results["scores"], results["labels"], results["boxes"]
|
21 |
+
):
|
22 |
+
if score > threshold:
|
23 |
+
label = model.config.id2label[label_id.item()]
|
24 |
+
box = [round(i, 2) for i in box.tolist()]
|
25 |
+
color = get_color(label)
|
26 |
+
|
27 |
+
# Draw bounding box
|
28 |
+
draw.rectangle(box, outline=color, width=3) # type: ignore
|
29 |
+
|
30 |
+
# Prepare text
|
31 |
+
text = f"{label}: {score:.2f}"
|
32 |
+
text_bbox = draw.textbbox((0, 0), text, font=font)
|
33 |
+
text_width = text_bbox[2] - text_bbox[0]
|
34 |
+
text_height = text_bbox[3] - text_bbox[1]
|
35 |
+
|
36 |
+
# Draw text background
|
37 |
+
draw.rectangle(
|
38 |
+
[box[0], box[1] - text_height - 4, box[0] + text_width, box[1]], # type: ignore
|
39 |
+
fill=color, # type: ignore
|
40 |
+
)
|
41 |
+
|
42 |
+
# Draw text
|
43 |
+
draw.text((box[0], box[1] - text_height - 4), text, fill="white", font=font)
|
44 |
+
|
45 |
+
return image
|
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio-client @ git+https://github.com/gradio-app/gradio@bbf9ba7e997022960c621f72baa891185bd03732#subdirectory=client/python
|
2 |
+
https://gradio-pypi-previews.s3.amazonaws.com/bbf9ba7e997022960c621f72baa891185bd03732/gradio-5.0.0-py3-none-any.whl
|
3 |
+
safetensors==0.4.3
|
4 |
+
opencv-python
|
5 |
+
torch
|
6 |
+
transformers>=4.43.0
|
7 |
+
Pillow
|
run.ipynb
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
{"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: rt-detr-object-detection"]}, {"cell_type": "code", "execution_count": null, "id": "272996653310673477252411125948039410165", "metadata": {}, "outputs": [], "source": ["!pip install -q gradio safetensors==0.4.3 opencv-python torch transformers>=4.43.0 Pillow "]}, {"cell_type": "code", "execution_count": null, "id": "288918539441861185822528903084949547379", "metadata": {}, "outputs": [], "source": ["# Downloading files from the demo repo\n", "import os\n", "!wget -q https://github.com/gradio-app/gradio/raw/main/demo/rt-detr-object-detection/draw_boxes.py"]}, {"cell_type": "code", "execution_count": null, "id": "44380577570523278879349135829904343037", "metadata": {}, "outputs": [], "source": ["import spaces\n", "import gradio as gr\n", "import cv2\n", "from PIL import Image\n", "import torch\n", "import time\n", "import numpy as np\n", "import uuid\n", "\n", "from transformers import RTDetrForObjectDetection, RTDetrImageProcessor # type: ignore\n", "\n", "from draw_boxes import draw_bounding_boxes\n", "\n", "image_processor = RTDetrImageProcessor.from_pretrained(\"PekingU/rtdetr_r50vd\")\n", "model = RTDetrForObjectDetection.from_pretrained(\"PekingU/rtdetr_r50vd\").to(\"cuda\")\n", "\n", "\n", "SUBSAMPLE = 2\n", "\n", "\n", "@spaces.GPU\n", "def stream_object_detection(video, conf_threshold):\n", " cap = cv2.VideoCapture(video)\n", "\n", " video_codec = cv2.VideoWriter_fourcc(*\"mp4v\") # type: ignore\n", " fps = int(cap.get(cv2.CAP_PROP_FPS))\n", "\n", " desired_fps = fps // SUBSAMPLE\n", " width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) // 2\n", " height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) // 2\n", "\n", " iterating, frame = cap.read()\n", "\n", " n_frames = 0\n", "\n", " name = f\"output_{uuid.uuid4()}.mp4\"\n", " segment_file = cv2.VideoWriter(name, video_codec, desired_fps, (width, height)) # type: ignore\n", " batch = []\n", "\n", " while iterating:\n", " frame = cv2.resize(frame, (0, 0), fx=0.5, fy=0.5)\n", " frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)\n", " if n_frames % SUBSAMPLE == 0:\n", " batch.append(frame)\n", " if len(batch) == 2 * desired_fps:\n", " inputs = image_processor(images=batch, return_tensors=\"pt\").to(\"cuda\")\n", "\n", " print(f\"starting batch of size {len(batch)}\")\n", " start = time.time()\n", " with torch.no_grad():\n", " outputs = model(**inputs)\n", " end = time.time()\n", " print(\"time taken for inference\", end - start)\n", "\n", " start = time.time()\n", " boxes = image_processor.post_process_object_detection(\n", " outputs,\n", " target_sizes=torch.tensor([(height, width)] * len(batch)),\n", " threshold=conf_threshold,\n", " )\n", "\n", " for _, (array, box) in enumerate(zip(batch, boxes)):\n", " pil_image = draw_bounding_boxes(\n", " Image.fromarray(array), box, model, conf_threshold\n", " )\n", " frame = np.array(pil_image)\n", " # Convert RGB to BGR\n", " frame = frame[:, :, ::-1].copy()\n", " segment_file.write(frame)\n", "\n", " batch = []\n", " segment_file.release()\n", " yield name\n", " end = time.time()\n", " print(\"time taken for processing boxes\", end - start)\n", " name = f\"output_{uuid.uuid4()}.mp4\"\n", " segment_file = cv2.VideoWriter(\n", " name, video_codec, desired_fps, (width, height)\n", " ) # type: ignore\n", "\n", " iterating, frame = cap.read()\n", " n_frames += 1\n", "\n", "\n", "with gr.Blocks() as demo:\n", " gr.HTML(\n", " \"\"\"\n", " <h1 style='text-align: center'>\n", " Video Object Detection with <a href='https://huggingface.co/PekingU/rtdetr_r101vd_coco_o365' target='_blank'>RT-DETR</a>\n", " </h1>\n", " \"\"\"\n", " )\n", " with gr.Row():\n", " with gr.Column():\n", " video = gr.Video(label=\"Video Source\")\n", " conf_threshold = gr.Slider(\n", " label=\"Confidence Threshold\",\n", " minimum=0.0,\n", " maximum=1.0,\n", " step=0.05,\n", " value=0.30,\n", " )\n", " with gr.Column():\n", " output_video = gr.Video(\n", " label=\"Processed Video\", streaming=True, autoplay=True\n", " )\n", "\n", " video.upload(\n", " fn=stream_object_detection,\n", " inputs=[video, conf_threshold],\n", " outputs=[output_video],\n", " )\n", "\n", "if __name__ == \"__main__\":\n", " demo.launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}
|
run.py
ADDED
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import spaces
|
2 |
+
import gradio as gr
|
3 |
+
import cv2
|
4 |
+
from PIL import Image
|
5 |
+
import torch
|
6 |
+
import time
|
7 |
+
import numpy as np
|
8 |
+
import uuid
|
9 |
+
|
10 |
+
from transformers import RTDetrForObjectDetection, RTDetrImageProcessor # type: ignore
|
11 |
+
|
12 |
+
from draw_boxes import draw_bounding_boxes
|
13 |
+
|
14 |
+
image_processor = RTDetrImageProcessor.from_pretrained("PekingU/rtdetr_r50vd")
|
15 |
+
model = RTDetrForObjectDetection.from_pretrained("PekingU/rtdetr_r50vd").to("cuda")
|
16 |
+
|
17 |
+
|
18 |
+
SUBSAMPLE = 2
|
19 |
+
|
20 |
+
|
21 |
+
@spaces.GPU
|
22 |
+
def stream_object_detection(video, conf_threshold):
|
23 |
+
cap = cv2.VideoCapture(video)
|
24 |
+
|
25 |
+
video_codec = cv2.VideoWriter_fourcc(*"mp4v") # type: ignore
|
26 |
+
fps = int(cap.get(cv2.CAP_PROP_FPS))
|
27 |
+
|
28 |
+
desired_fps = fps // SUBSAMPLE
|
29 |
+
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) // 2
|
30 |
+
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) // 2
|
31 |
+
|
32 |
+
iterating, frame = cap.read()
|
33 |
+
|
34 |
+
n_frames = 0
|
35 |
+
|
36 |
+
name = f"output_{uuid.uuid4()}.mp4"
|
37 |
+
segment_file = cv2.VideoWriter(name, video_codec, desired_fps, (width, height)) # type: ignore
|
38 |
+
batch = []
|
39 |
+
|
40 |
+
while iterating:
|
41 |
+
frame = cv2.resize(frame, (0, 0), fx=0.5, fy=0.5)
|
42 |
+
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
43 |
+
if n_frames % SUBSAMPLE == 0:
|
44 |
+
batch.append(frame)
|
45 |
+
if len(batch) == 2 * desired_fps:
|
46 |
+
inputs = image_processor(images=batch, return_tensors="pt").to("cuda")
|
47 |
+
|
48 |
+
print(f"starting batch of size {len(batch)}")
|
49 |
+
start = time.time()
|
50 |
+
with torch.no_grad():
|
51 |
+
outputs = model(**inputs)
|
52 |
+
end = time.time()
|
53 |
+
print("time taken for inference", end - start)
|
54 |
+
|
55 |
+
start = time.time()
|
56 |
+
boxes = image_processor.post_process_object_detection(
|
57 |
+
outputs,
|
58 |
+
target_sizes=torch.tensor([(height, width)] * len(batch)),
|
59 |
+
threshold=conf_threshold,
|
60 |
+
)
|
61 |
+
|
62 |
+
for _, (array, box) in enumerate(zip(batch, boxes)):
|
63 |
+
pil_image = draw_bounding_boxes(
|
64 |
+
Image.fromarray(array), box, model, conf_threshold
|
65 |
+
)
|
66 |
+
frame = np.array(pil_image)
|
67 |
+
# Convert RGB to BGR
|
68 |
+
frame = frame[:, :, ::-1].copy()
|
69 |
+
segment_file.write(frame)
|
70 |
+
|
71 |
+
batch = []
|
72 |
+
segment_file.release()
|
73 |
+
yield name
|
74 |
+
end = time.time()
|
75 |
+
print("time taken for processing boxes", end - start)
|
76 |
+
name = f"output_{uuid.uuid4()}.mp4"
|
77 |
+
segment_file = cv2.VideoWriter(
|
78 |
+
name, video_codec, desired_fps, (width, height)
|
79 |
+
) # type: ignore
|
80 |
+
|
81 |
+
iterating, frame = cap.read()
|
82 |
+
n_frames += 1
|
83 |
+
|
84 |
+
|
85 |
+
with gr.Blocks() as demo:
|
86 |
+
gr.HTML(
|
87 |
+
"""
|
88 |
+
<h1 style='text-align: center'>
|
89 |
+
Video Object Detection with <a href='https://huggingface.co/PekingU/rtdetr_r101vd_coco_o365' target='_blank'>RT-DETR</a>
|
90 |
+
</h1>
|
91 |
+
"""
|
92 |
+
)
|
93 |
+
with gr.Row():
|
94 |
+
with gr.Column():
|
95 |
+
video = gr.Video(label="Video Source")
|
96 |
+
conf_threshold = gr.Slider(
|
97 |
+
label="Confidence Threshold",
|
98 |
+
minimum=0.0,
|
99 |
+
maximum=1.0,
|
100 |
+
step=0.05,
|
101 |
+
value=0.30,
|
102 |
+
)
|
103 |
+
with gr.Column():
|
104 |
+
output_video = gr.Video(
|
105 |
+
label="Processed Video", streaming=True, autoplay=True
|
106 |
+
)
|
107 |
+
|
108 |
+
video.upload(
|
109 |
+
fn=stream_object_detection,
|
110 |
+
inputs=[video, conf_threshold],
|
111 |
+
outputs=[output_video],
|
112 |
+
)
|
113 |
+
|
114 |
+
if __name__ == "__main__":
|
115 |
+
demo.launch()
|