Spaces:
Running
on
Zero
Running
on
Zero
File size: 20,179 Bytes
dff4c96 fe66000 9f2cbff fe66000 9f2cbff fe66000 7beae6a 3fd48f0 9f2cbff fe66000 dff4c96 fe66000 82925a6 fe66000 82925a6 dff4c96 eff589b 82925a6 dff4c96 fe66000 dff4c96 61d656f fe66000 dff4c96 fe66000 9d19ec6 fe66000 7beae6a dff4c96 7beae6a dff4c96 7beae6a dff4c96 82925a6 9d19ec6 fe66000 dff4c96 9d19ec6 dff4c96 3fd48f0 7beae6a fe66000 82925a6 fe66000 9d19ec6 7beae6a 9d19ec6 82925a6 3fd48f0 82925a6 3fd48f0 82925a6 3fd48f0 82925a6 3fd48f0 455454b 7beae6a fe66000 82925a6 dff4c96 fe66000 61d656f fe66000 61d656f dff4c96 fe66000 dff4c96 fe66000 3fd48f0 fe66000 dff4c96 fe66000 745226b 9f2cbff fe66000 9f2cbff fe66000 dff4c96 3fd48f0 7beae6a a7f1cbc 7beae6a a7f1cbc 7beae6a a7f1cbc 7beae6a dff4c96 7beae6a dff4c96 fe66000 dff4c96 fe66000 dff4c96 fe66000 3fd48f0 6ba49bf 3fd48f0 fe66000 3fd48f0 9d19ec6 fe66000 7beae6a dff4c96 3fd48f0 9d19ec6 3fd48f0 7beae6a 3fd48f0 fe66000 7beae6a 9f2cbff 7beae6a fe66000 9f2cbff 9d19ec6 9f2cbff fe66000 dff4c96 7beae6a dff4c96 8ddd00a dff4c96 8ddd00a dff4c96 8ddd00a dff4c96 82925a6 8ddd00a 82925a6 8ddd00a 82fcb0b 8ddd00a 82fcb0b 82925a6 dff4c96 82925a6 dff4c96 fe66000 dff4c96 8ddd00a dff4c96 82925a6 dff4c96 61d656f dff4c96 61d656f dff4c96 61d656f dff4c96 61d656f dff4c96 fe66000 dff4c96 61d656f dff4c96 fe66000 dff4c96 7beae6a 8ddd00a dff4c96 7beae6a dff4c96 7beae6a fe66000 dff4c96 3568f5b dff4c96 82925a6 dff4c96 82925a6 dff4c96 82925a6 fe66000 dff4c96 7beae6a dff4c96 7beae6a dff4c96 82925a6 dff4c96 fe66000 dff4c96 fe66000 dff4c96 61d656f fe66000 dff4c96 fe66000 dff4c96 7beae6a fe66000 82925a6 dff4c96 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 |
import os
import cv2
import tqdm
import uuid
import logging
import torch
import spaces
import trackers
import numpy as np
import gradio as gr
import imageio.v3 as iio
import supervision as sv
from pathlib import Path
from functools import lru_cache
from typing import List, Optional, Tuple
from PIL import Image
from transformers import AutoModelForObjectDetection, AutoImageProcessor
from transformers.image_utils import load_image
# Configuration constants
CHECKPOINTS = [
"ustc-community/dfine-medium-obj2coco",
"ustc-community/dfine-medium-coco",
"ustc-community/dfine-medium-obj365",
"ustc-community/dfine-nano-coco",
"ustc-community/dfine-small-coco",
"ustc-community/dfine-large-coco",
"ustc-community/dfine-xlarge-coco",
"ustc-community/dfine-small-obj365",
"ustc-community/dfine-large-obj365",
"ustc-community/dfine-xlarge-obj365",
"ustc-community/dfine-small-obj2coco",
"ustc-community/dfine-large-obj2coco-e25",
"ustc-community/dfine-xlarge-obj2coco",
]
DEFAULT_CHECKPOINT = CHECKPOINTS[0]
DEFAULT_CONFIDENCE_THRESHOLD = 0.3
TORCH_DTYPE = torch.float32
# Image
IMAGE_EXAMPLES = [
{"path": "./examples/images/tennis.jpg", "use_url": False, "url": "", "label": "Local Image"},
{"path": "./examples/images/dogs.jpg", "use_url": False, "url": "", "label": "Local Image"},
{"path": "./examples/images/nascar.jpg", "use_url": False, "url": "", "label": "Local Image"},
{"path": "./examples/images/crossroad.jpg", "use_url": False, "url": "", "label": "Local Image"},
{
"path": None,
"use_url": True,
"url": "https://live.staticflickr.com/65535/33021460783_1646d43c54_b.jpg",
"label": "Flickr Image",
},
]
# Video
MAX_NUM_FRAMES = 250
BATCH_SIZE = 4
ALLOWED_VIDEO_EXTENSIONS = {".mp4", ".avi", ".mov"}
VIDEO_OUTPUT_DIR = Path("static/videos")
VIDEO_OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
class TrackingAlgorithm:
BYTETRACK = "ByteTrack (2021)"
DEEPSORT = "DeepSORT (2017)"
SORT = "SORT (2016)"
TRACKERS = [None, TrackingAlgorithm.BYTETRACK, TrackingAlgorithm.DEEPSORT, TrackingAlgorithm.SORT]
VIDEO_EXAMPLES = [
{"path": "./examples/videos/dogs_running.mp4", "label": "Local Video", "tracker": None, "classes": "all"},
{"path": "./examples/videos/traffic.mp4", "label": "Local Video", "tracker": TrackingAlgorithm.BYTETRACK, "classes": "car, truck, bus"},
{"path": "./examples/videos/fast_and_furious.mp4", "label": "Local Video", "tracker": None, "classes": "all"},
{"path": "./examples/videos/break_dance.mp4", "label": "Local Video", "tracker": None, "classes": "all"},
]
# Create a color palette for visualization
# These hex color codes define different colors for tracking different objects
color = sv.ColorPalette.from_hex([
"#ffff00", "#ff9b00", "#ff8080", "#ff66b2", "#ff66ff", "#b266ff",
"#9999ff", "#3399ff", "#66ffff", "#33ff99", "#66ff66", "#99ff00"
])
logging.basicConfig(
level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s"
)
logger = logging.getLogger(__name__)
@lru_cache(maxsize=3)
def get_model_and_processor(checkpoint: str):
model = AutoModelForObjectDetection.from_pretrained(checkpoint, torch_dtype=TORCH_DTYPE)
image_processor = AutoImageProcessor.from_pretrained(checkpoint)
return model, image_processor
@spaces.GPU(duration=20)
def detect_objects(
checkpoint: str,
images: List[np.ndarray] | np.ndarray,
confidence_threshold: float = DEFAULT_CONFIDENCE_THRESHOLD,
target_size: Optional[Tuple[int, int]] = None,
batch_size: int = BATCH_SIZE,
classes: Optional[List[str]] = None,
):
device = "cuda" if torch.cuda.is_available() else "cpu"
model, image_processor = get_model_and_processor(checkpoint)
model = model.to(device)
if classes is not None:
wrong_classes = [cls for cls in classes if cls not in model.config.label2id]
if wrong_classes:
gr.Warning(f"Classes not found in model config: {wrong_classes}")
keep_ids = [model.config.label2id[cls] for cls in classes if cls in model.config.label2id]
else:
keep_ids = None
if isinstance(images, np.ndarray) and images.ndim == 4:
images = [x for x in images] # split video array into list of images
batches = [images[i:i + batch_size] for i in range(0, len(images), batch_size)]
results = []
for batch in tqdm.tqdm(batches, desc="Processing frames"):
# preprocess images
inputs = image_processor(images=batch, return_tensors="pt")
inputs = inputs.to(device).to(TORCH_DTYPE)
# forward pass
with torch.no_grad():
outputs = model(**inputs)
# postprocess outputs
if target_size:
target_sizes = [target_size] * len(batch)
else:
target_sizes = [(image.shape[0], image.shape[1]) for image in batch]
batch_results = image_processor.post_process_object_detection(
outputs, target_sizes=target_sizes, threshold=confidence_threshold
)
results.extend(batch_results)
# move results to cpu
for i, result in enumerate(results):
results[i] = {k: v.cpu() for k, v in result.items()}
if keep_ids is not None:
keep = torch.isin(results[i]["labels"], torch.tensor(keep_ids))
results[i] = {k: v[keep] for k, v in results[i].items()}
return results, model.config.id2label
def process_image(
checkpoint: str = DEFAULT_CHECKPOINT,
image: Optional[Image.Image] = None,
url: Optional[str] = None,
use_url: bool = False,
confidence_threshold: float = DEFAULT_CONFIDENCE_THRESHOLD,
):
if not use_url:
url = None
if (image is None) ^ bool(url):
raise ValueError(f"Either image or url must be provided, but not both.")
if url:
image = load_image(url)
results, id2label = detect_objects(
checkpoint=checkpoint,
images=[np.array(image)],
confidence_threshold=confidence_threshold,
)
result = results[0] # first image in batch (we have batch size 1)
annotations = []
for label, score, box in zip(result["labels"], result["scores"], result["boxes"]):
text_label = id2label[label.item()]
formatted_label = f"{text_label} ({score:.2f})"
x_min, y_min, x_max, y_max = box.cpu().numpy().round().astype(int)
x_min = max(0, x_min)
y_min = max(0, y_min)
x_max = min(image.width - 1, x_max)
y_max = min(image.height - 1, y_max)
annotations.append(((x_min, y_min, x_max, y_max), formatted_label))
return (image, annotations)
def get_target_size(image_height, image_width, max_size: int):
if image_height < max_size and image_width < max_size:
new_height, new_width = image_height, image_width
elif image_height > image_width:
new_height = max_size
new_width = int(image_width * max_size / image_height)
else:
new_width = max_size
new_height = int(image_height * max_size / image_width)
# make even (for video codec compatibility)
new_height = new_height // 2 * 2
new_width = new_width // 2 * 2
return new_width, new_height
def read_video_k_frames(video_path: str, k: int, read_every_i_frame: int = 1):
cap = cv2.VideoCapture(video_path)
frames = []
i = 0
progress_bar = tqdm.tqdm(total=k, desc="Reading frames")
while cap.isOpened() and len(frames) < k:
ret, frame = cap.read()
if not ret:
break
if i % read_every_i_frame == 0:
frames.append(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
progress_bar.update(1)
i += 1
cap.release()
progress_bar.close()
return frames
def get_tracker(tracker: str, fps: float):
if tracker == TrackingAlgorithm.SORT:
return trackers.SORTTracker(frame_rate=fps)
elif tracker == TrackingAlgorithm.DEEPSORT:
feature_extractor = trackers.DeepSORTFeatureExtractor.from_timm("mobilenetv4_conv_small.e1200_r224_in1k", device="cpu")
return trackers.DeepSORTTracker(feature_extractor, frame_rate=fps)
elif tracker == TrackingAlgorithm.BYTETRACK:
return sv.ByteTrack(frame_rate=int(fps))
else:
raise ValueError(f"Invalid tracker: {tracker}")
def update_tracker(tracker, detections, frame):
tracker_name = tracker.__class__.__name__
if tracker_name == "SORTTracker":
return tracker.update(detections)
elif tracker_name == "DeepSORTTracker":
return tracker.update(detections, frame)
elif tracker_name == "ByteTrack":
return tracker.update_with_detections(detections)
else:
raise ValueError(f"Invalid tracker: {tracker}")
def process_video(
video_path: str,
checkpoint: str,
tracker_algorithm: Optional[str] = None,
classes: str = "all",
confidence_threshold: float = DEFAULT_CONFIDENCE_THRESHOLD,
progress: gr.Progress = gr.Progress(track_tqdm=True),
) -> str:
if not video_path or not os.path.isfile(video_path):
raise ValueError(f"Invalid video path: {video_path}")
ext = os.path.splitext(video_path)[1].lower()
if ext not in ALLOWED_VIDEO_EXTENSIONS:
raise ValueError(f"Unsupported video format: {ext}, supported formats: {ALLOWED_VIDEO_EXTENSIONS}")
video_info = sv.VideoInfo.from_video_path(video_path)
read_each_i_frame = max(1, video_info.fps // 25)
target_fps = video_info.fps / read_each_i_frame
target_width, target_height = get_target_size(video_info.height, video_info.width, 1080)
n_frames_to_read = min(MAX_NUM_FRAMES, video_info.total_frames // read_each_i_frame)
frames = read_video_k_frames(video_path, n_frames_to_read, read_each_i_frame)
frames = [cv2.resize(frame, (target_width, target_height), interpolation=cv2.INTER_CUBIC) for frame in frames]
# Set the color lookup mode to assign colors by track ID
# This mean objects with the same track ID will be annotated by the same color
color_lookup = sv.ColorLookup.TRACK if tracker_algorithm else sv.ColorLookup.CLASS
box_annotator = sv.BoxAnnotator(color, color_lookup=color_lookup, thickness=1)
label_annotator = sv.LabelAnnotator(color, color_lookup=color_lookup, text_scale=0.5)
trace_annotator = sv.TraceAnnotator(color, color_lookup=color_lookup, thickness=1, trace_length=100)
# preprocess classes
if classes != "all":
classes_list = [cls.strip().lower() for cls in classes.split(",")]
else:
classes_list = None
results, id2label = detect_objects(
images=np.array(frames),
checkpoint=checkpoint,
confidence_threshold=confidence_threshold,
target_size=(target_height, target_width),
classes=classes_list,
)
annotated_frames = []
# detections
if tracker_algorithm:
tracker = get_tracker(tracker_algorithm, target_fps)
for frame, result in progress.tqdm(zip(frames, results), desc="Tracking objects", total=len(frames)):
detections = sv.Detections.from_transformers(result, id2label=id2label)
detections = detections.with_nms(threshold=0.95, class_agnostic=True)
detections = update_tracker(tracker, detections, frame)
labels = [f"#{tracker_id} {id2label[class_id]}" for class_id, tracker_id in zip(detections.class_id, detections.tracker_id)]
annotated_frame = box_annotator.annotate(scene=frame, detections=detections)
annotated_frame = label_annotator.annotate(scene=annotated_frame, detections=detections, labels=labels)
annotated_frame = trace_annotator.annotate(scene=annotated_frame, detections=detections)
annotated_frames.append(annotated_frame)
else:
for frame, result in tqdm.tqdm(zip(frames, results), desc="Annotating frames", total=len(frames)):
detections = sv.Detections.from_transformers(result, id2label=id2label)
detections = detections.with_nms(threshold=0.95, class_agnostic=True)
annotated_frame = box_annotator.annotate(scene=frame, detections=detections)
annotated_frame = label_annotator.annotate(scene=annotated_frame, detections=detections)
annotated_frames.append(annotated_frame)
output_filename = os.path.join(VIDEO_OUTPUT_DIR, f"output_{uuid.uuid4()}.mp4")
iio.imwrite(output_filename, annotated_frames, fps=target_fps, codec="h264")
return output_filename
def create_image_inputs() -> List[gr.components.Component]:
return [
gr.Image(
label="Upload Image",
type="pil",
sources=["upload", "webcam"],
interactive=True,
elem_classes="input-component",
),
gr.Checkbox(label="Use Image URL Instead", value=False),
gr.Textbox(
label="Image URL",
placeholder="https://example.com/image.jpg",
visible=False,
elem_classes="input-component",
),
gr.Dropdown(
choices=CHECKPOINTS,
label="Select Model Checkpoint",
value=DEFAULT_CHECKPOINT,
elem_classes="input-component",
),
gr.Slider(
minimum=0.1,
maximum=1.0,
value=DEFAULT_CONFIDENCE_THRESHOLD,
step=0.1,
label="Confidence Threshold",
elem_classes="input-component",
),
]
def create_video_inputs() -> List[gr.components.Component]:
return [
gr.Video(
label="Upload Video",
sources=["upload"],
interactive=True,
format="mp4", # Ensure MP4 format
elem_classes="input-component",
),
gr.Dropdown(
choices=CHECKPOINTS,
label="Select Model Checkpoint",
value=DEFAULT_CHECKPOINT,
elem_classes="input-component",
),
gr.Dropdown(
choices=TRACKERS,
label="Select Tracker (Optional)",
value=None,
elem_classes="input-component",
),
gr.TextArea(
label="Specify Class Names to Detect (comma separated)",
value="all",
lines=1,
elem_classes="input-component",
),
gr.Slider(
minimum=0.1,
maximum=1.0,
value=DEFAULT_CONFIDENCE_THRESHOLD,
step=0.1,
label="Confidence Threshold",
elem_classes="input-component",
),
]
def create_button_row() -> List[gr.Button]:
return [
gr.Button(
f"Detect Objects", variant="primary", elem_classes="action-button"
),
gr.Button(f"Clear", variant="secondary", elem_classes="action-button"),
]
# Gradio interface
with gr.Blocks(theme=gr.themes.Ocean()) as demo:
gr.Markdown(
"""
# Object Detection Demo
Experience state-of-the-art object detection with USTC's [D-FINE](https://huggingface.co/docs/transformers/main/model_doc/d_fine) models.
- **Image** and **Video** modes are supported.
- Select a model and adjust the confidence threshold to see detections!
- On video mode, you can enable tracking powered by [Supervision](https://github.com/roboflow/supervision) and [Trackers](https://github.com/roboflow/trackers) from Roboflow.
""",
elem_classes="header-text",
)
with gr.Tabs():
with gr.Tab("Image"):
with gr.Row():
with gr.Column(scale=1, min_width=300):
with gr.Group():
(
image_input,
use_url,
url_input,
image_model_checkpoint,
image_confidence_threshold,
) = create_image_inputs()
image_detect_button, image_clear_button = create_button_row()
with gr.Column(scale=2):
image_output = gr.AnnotatedImage(
label="Detection Results",
show_label=True,
color_map=None,
elem_classes="output-component",
)
gr.Examples(
examples=[
[
DEFAULT_CHECKPOINT,
example["path"],
example["url"],
example["use_url"],
DEFAULT_CONFIDENCE_THRESHOLD,
]
for example in IMAGE_EXAMPLES
],
inputs=[
image_model_checkpoint,
image_input,
url_input,
use_url,
image_confidence_threshold,
],
outputs=[image_output],
fn=process_image,
label="Select an image example to populate inputs",
cache_examples=True,
cache_mode="lazy",
)
with gr.Tab("Video"):
gr.Markdown(
f"The input video will be processed in ~25 FPS (up to {MAX_NUM_FRAMES} frames in result)."
)
with gr.Row():
with gr.Column(scale=1, min_width=300):
with gr.Group():
video_input, video_checkpoint, video_tracker, video_classes, video_confidence_threshold = create_video_inputs()
video_detect_button, video_clear_button = create_button_row()
with gr.Column(scale=2):
video_output = gr.Video(
label="Detection Results",
format="mp4", # Explicit MP4 format
elem_classes="output-component",
)
gr.Examples(
examples=[
[example["path"], DEFAULT_CHECKPOINT, example["tracker"], example["classes"], DEFAULT_CONFIDENCE_THRESHOLD]
for example in VIDEO_EXAMPLES
],
inputs=[video_input, video_checkpoint, video_tracker, video_classes, video_confidence_threshold],
outputs=[video_output],
fn=process_video,
cache_examples=False,
label="Select a video example to populate inputs",
)
# Dynamic visibility for URL input
use_url.change(
fn=lambda x: gr.update(visible=x),
inputs=use_url,
outputs=url_input,
)
# Image clear button
image_clear_button.click(
fn=lambda: (
None,
False,
"",
DEFAULT_CHECKPOINT,
DEFAULT_CONFIDENCE_THRESHOLD,
None,
),
outputs=[
image_input,
use_url,
url_input,
image_model_checkpoint,
image_confidence_threshold,
image_output,
],
)
# Video clear button
video_clear_button.click(
fn=lambda: (
None,
DEFAULT_CHECKPOINT,
None,
"all",
DEFAULT_CONFIDENCE_THRESHOLD,
None,
),
outputs=[
video_input,
video_checkpoint,
video_tracker,
video_classes,
video_confidence_threshold,
video_output,
],
)
# Image detect button
image_detect_button.click(
fn=process_image,
inputs=[
image_model_checkpoint,
image_input,
url_input,
use_url,
image_confidence_threshold,
],
outputs=[image_output],
)
# Video detect button
video_detect_button.click(
fn=process_video,
inputs=[video_input, video_checkpoint, video_tracker, video_classes, video_confidence_threshold],
outputs=[video_output],
)
if __name__ == "__main__":
demo.queue(max_size=20).launch()
|