Spaces:
Running
on
Zero
Running
on
Zero
Commit
•
54e5741
1
Parent(s):
09aafbd
add app.py
Browse files- .gitattributes +3 -0
- README.md +3 -1
- app.py +180 -0
- cat.mp4 +3 -0
- football.mp4 +3 -0
- requirement.txt +5 -0
- safari2.mp4 +3 -0
.gitattributes
CHANGED
@@ -33,3 +33,6 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
+
football.mp4 filter=lfs diff=lfs merge=lfs -text
|
37 |
+
cat.mp4 filter=lfs diff=lfs merge=lfs -text
|
38 |
+
safari2.mp4 filter=lfs diff=lfs merge=lfs -text
|
README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
---
|
2 |
title: Omdet Turbo Open Vocabulary
|
3 |
-
emoji:
|
4 |
colorFrom: red
|
5 |
colorTo: blue
|
6 |
sdk: gradio
|
@@ -8,6 +8,8 @@ sdk_version: 4.42.0
|
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
license: apache-2.0
|
|
|
|
|
11 |
---
|
12 |
|
13 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
---
|
2 |
title: Omdet Turbo Open Vocabulary
|
3 |
+
emoji: 📹
|
4 |
colorFrom: red
|
5 |
colorTo: blue
|
6 |
sdk: gradio
|
|
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
license: apache-2.0
|
11 |
+
short_description: Video captioning/open-vocabulary/zero-shot
|
12 |
+
|
13 |
---
|
14 |
|
15 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
@@ -0,0 +1,180 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import time
|
3 |
+
|
4 |
+
import cv2
|
5 |
+
import gradio as gr
|
6 |
+
import numpy as np
|
7 |
+
import spaces
|
8 |
+
import supervision as sv
|
9 |
+
import torch
|
10 |
+
from PIL import Image
|
11 |
+
from tqdm import tqdm
|
12 |
+
|
13 |
+
from transformers import AutoModelForZeroShotObjectDetection, AutoProcessor
|
14 |
+
|
15 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
16 |
+
|
17 |
+
processor = AutoProcessor.from_pretrained("omdet-turbo-tiny-timm")
|
18 |
+
model = AutoModelForZeroShotObjectDetection.from_pretrained("omdet-turbo-tiny-timm").to(
|
19 |
+
device
|
20 |
+
)
|
21 |
+
|
22 |
+
css = """
|
23 |
+
#warning {background-color: #FFCCCB}
|
24 |
+
.feedback textarea {font-size: 24px !important}
|
25 |
+
"""
|
26 |
+
|
27 |
+
BOUNDING_BOX_ANNOTATOR = sv.BoundingBoxAnnotator()
|
28 |
+
MASK_ANNOTATOR = sv.MaskAnnotator()
|
29 |
+
LABEL_ANNOTATOR = sv.LabelAnnotator()
|
30 |
+
|
31 |
+
|
32 |
+
def calculate_end_frame_index(source_video_path):
|
33 |
+
video_info = sv.VideoInfo.from_video_path(source_video_path)
|
34 |
+
return min(video_info.total_frames, video_info.fps * 2)
|
35 |
+
|
36 |
+
|
37 |
+
def annotate_image(input_image, detections, labels) -> np.ndarray:
|
38 |
+
output_image = MASK_ANNOTATOR.annotate(input_image, detections)
|
39 |
+
output_image = BOUNDING_BOX_ANNOTATOR.annotate(output_image, detections)
|
40 |
+
output_image = LABEL_ANNOTATOR.annotate(output_image, detections, labels=labels)
|
41 |
+
return output_image
|
42 |
+
|
43 |
+
|
44 |
+
def resize_to_max_side(frame: np.ndarray, max_side: int = 640):
|
45 |
+
h, w = frame.shape[:2]
|
46 |
+
if h > w:
|
47 |
+
new_h, new_w = max_side, int(w * max_side / h)
|
48 |
+
else:
|
49 |
+
new_h, new_w = int(h * max_side / w), max_side
|
50 |
+
|
51 |
+
return cv2.resize(frame, (new_w, new_h))
|
52 |
+
|
53 |
+
|
54 |
+
@spaces.GPU
|
55 |
+
def process_video(
|
56 |
+
input_video,
|
57 |
+
confidence_threshold,
|
58 |
+
classes,
|
59 |
+
max_side,
|
60 |
+
progress=gr.Progress(track_tqdm=True),
|
61 |
+
):
|
62 |
+
classes = classes.strip(" ").split(",")
|
63 |
+
video_info = sv.VideoInfo.from_video_path(input_video)
|
64 |
+
total = calculate_end_frame_index(input_video)
|
65 |
+
frame_generator = sv.get_video_frames_generator(source_path=input_video, end=total)
|
66 |
+
|
67 |
+
result_file_name = "output.mp4"
|
68 |
+
result_file_path = os.path.join(os.getcwd(), result_file_name)
|
69 |
+
all_fps = []
|
70 |
+
with sv.VideoSink(result_file_path, video_info=video_info) as sink:
|
71 |
+
for _ in tqdm(range(total), desc="Processing video.."):
|
72 |
+
frame = next(frame_generator)
|
73 |
+
results, fps = query(
|
74 |
+
frame, classes, confidence_threshold, max_side=max_side
|
75 |
+
)
|
76 |
+
all_fps.append(fps)
|
77 |
+
detections = []
|
78 |
+
|
79 |
+
detections = sv.Detections(
|
80 |
+
xyxy=results[0]["boxes"].cpu().detach().numpy(),
|
81 |
+
confidence=results[0]["scores"].cpu().detach().numpy(),
|
82 |
+
class_id=np.array(
|
83 |
+
[
|
84 |
+
classes.index(results_class)
|
85 |
+
for results_class in results[0]["classes"]
|
86 |
+
]
|
87 |
+
),
|
88 |
+
data={"class_name": results[0]["classes"]},
|
89 |
+
)
|
90 |
+
frame = annotate_image(
|
91 |
+
input_image=frame,
|
92 |
+
detections=detections,
|
93 |
+
labels=results[0]["classes"],
|
94 |
+
)
|
95 |
+
sink.write_frame(frame)
|
96 |
+
|
97 |
+
avg_fps = np.mean(all_fps)
|
98 |
+
return result_file_path, gr.Markdown(
|
99 |
+
f'<h3 style="text-align: center;">Model inference FPS: {avg_fps:.2f}</h3>',
|
100 |
+
visible=True,
|
101 |
+
)
|
102 |
+
|
103 |
+
|
104 |
+
def query(frame, classes, confidence_threshold, max_side=360):
|
105 |
+
frame_resized = resize_to_max_side(frame, max_side=max_side)
|
106 |
+
image = Image.fromarray(cv2.cvtColor(frame_resized, cv2.COLOR_BGR2RGB))
|
107 |
+
inputs = processor(images=image, text=classes, return_tensors="pt").to(device)
|
108 |
+
with torch.no_grad():
|
109 |
+
start = time.time()
|
110 |
+
outputs = model(**inputs)
|
111 |
+
fps = 1 / (time.time() - start)
|
112 |
+
target_sizes = [frame.shape[:2]]
|
113 |
+
|
114 |
+
results = processor.post_process_grounded_object_detection(
|
115 |
+
outputs=outputs,
|
116 |
+
classes=classes,
|
117 |
+
score_threshold=confidence_threshold,
|
118 |
+
target_sizes=target_sizes,
|
119 |
+
)
|
120 |
+
return results, fps
|
121 |
+
|
122 |
+
|
123 |
+
with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
124 |
+
gr.Markdown("## Real Time Open Vocabulary Object Detection with Omdet-Turbo")
|
125 |
+
gr.Markdown(
|
126 |
+
"This is a demo for open vocabulary object detection using OmDet-Turbo. \\"
|
127 |
+
"It runs on ZeroGPU which captures GPU every first time you infer. This combined with video processing time means that the demo inference time is slower than the model's actual inference time. \\"
|
128 |
+
"The actual model inference FPS is displayed under the processed video after inference."
|
129 |
+
)
|
130 |
+
gr.Markdown(
|
131 |
+
"Simply upload a video, and write the objects you want to detect! You can also play with confidence threshold, image size, or try the examples below. 👇"
|
132 |
+
)
|
133 |
+
|
134 |
+
with gr.Row():
|
135 |
+
with gr.Column():
|
136 |
+
input_video = gr.Video(label="Input Video")
|
137 |
+
submit = gr.Button()
|
138 |
+
with gr.Column():
|
139 |
+
output_video = gr.Video(label="Output Video")
|
140 |
+
actual_fps = gr.Markdown("", visible=False)
|
141 |
+
with gr.Row():
|
142 |
+
classes = gr.Textbox(
|
143 |
+
"person, cat, dog",
|
144 |
+
label="Objects to detect. Change this as you like!",
|
145 |
+
elem_classes="feedback",
|
146 |
+
scale=3,
|
147 |
+
)
|
148 |
+
conf = gr.Slider(
|
149 |
+
label="Confidence Threshold",
|
150 |
+
minimum=0.1,
|
151 |
+
maximum=1.0,
|
152 |
+
value=0.2,
|
153 |
+
step=0.05,
|
154 |
+
)
|
155 |
+
max_side = gr.Slider(
|
156 |
+
label="Image Size",
|
157 |
+
minimum=240,
|
158 |
+
maximum=1080,
|
159 |
+
value=640,
|
160 |
+
step=10,
|
161 |
+
)
|
162 |
+
example = gr.Examples(
|
163 |
+
fn=process_video,
|
164 |
+
examples=[
|
165 |
+
["./football.mp4", 0.3, "person, ball, shoe", 640],
|
166 |
+
["./cat.mp4", 0.2, "cat", 640],
|
167 |
+
["./safari2.mp4", 0.3, "elephant, giraffe, springbok, zebra", 640],
|
168 |
+
],
|
169 |
+
inputs=[input_video, conf, classes, max_side],
|
170 |
+
outputs=output_video,
|
171 |
+
)
|
172 |
+
|
173 |
+
submit.click(
|
174 |
+
fn=process_video,
|
175 |
+
inputs=[input_video, conf, classes, max_side],
|
176 |
+
outputs=[output_video, actual_fps],
|
177 |
+
)
|
178 |
+
|
179 |
+
if __name__ == "__main__":
|
180 |
+
demo.launch(show_error=True)
|
cat.mp4
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:07539c031a516acecf58b8751f74ba90182efe4c4ad25513038f10564739eadd
|
3 |
+
size 810095
|
football.mp4
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:56a85c5c7d5d6e0825f76a71e5e3ee2ce35c8ffbe841ef4bfa544af1089259aa
|
3 |
+
size 2855852
|
requirement.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
torch
|
2 |
+
timm
|
3 |
+
git+https://github.com/yonigozlan/transformers.git@add-om-det-turbo
|
4 |
+
supervision
|
5 |
+
spaces
|
safari2.mp4
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:1c7f26f775768d06219b19acb4c071e40928f1042b7b4fa2d876095c72139e19
|
3 |
+
size 3011687
|