-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsort_rtdetr_demo.py
More file actions
302 lines (262 loc) · 7.9 KB
/
sort_rtdetr_demo.py
File metadata and controls
302 lines (262 loc) · 7.9 KB
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
#!/usr/bin/env python3
"""
SORT Tracking Example with RT-DETR from Transformers
This example demonstrates using the SORT tracker with RT-DETR (Real-Time DEtection TRansformer)
from Hugging Face Transformers for multi-object tracking on video.
RT-DETR is a real-time end-to-end object detector that achieves state-of-the-art
performance while maintaining high inference speed.
Requirements:
pip install transformers torch opencv-python trackforge pillow
Usage:
python sort_rtdetr_demo.py
"""
import cv2
import torch
from PIL import Image
from transformers import RTDetrForObjectDetection, RTDetrImageProcessor
import trackforge
import time
from pathlib import Path
# COCO class names for RT-DETR
COCO_CLASSES = [
"person",
"bicycle",
"car",
"motorcycle",
"airplane",
"bus",
"train",
"truck",
"boat",
"traffic light",
"fire hydrant",
"stop sign",
"parking meter",
"bench",
"bird",
"cat",
"dog",
"horse",
"sheep",
"cow",
"elephant",
"bear",
"zebra",
"giraffe",
"backpack",
"umbrella",
"handbag",
"tie",
"suitcase",
"frisbee",
"skis",
"snowboard",
"sports ball",
"kite",
"baseball bat",
"baseball glove",
"skateboard",
"surfboard",
"tennis racket",
"bottle",
"wine glass",
"cup",
"fork",
"knife",
"spoon",
"bowl",
"banana",
"apple",
"sandwich",
"orange",
"broccoli",
"carrot",
"hot dog",
"pizza",
"donut",
"cake",
"chair",
"couch",
"potted plant",
"bed",
"dining table",
"toilet",
"tv",
"laptop",
"mouse",
"remote",
"keyboard",
"cell phone",
"microwave",
"oven",
"toaster",
"sink",
"refrigerator",
"book",
"clock",
"vase",
"scissors",
"teddy bear",
"hair drier",
"toothbrush",
]
def run_tracking(
video_path: str = "people.mp4",
output_path: str = "output_sort_rtdetr.mp4",
model_name: str = "PekingU/rtdetr_r50vd",
confidence_threshold: float = 0.5,
target_classes: list = None, # None means all classes, [0] means only person
):
"""
Run SORT tracking with RT-DETR detection on a video.
Args:
video_path: Path to input video file.
output_path: Path for output video with tracking annotations.
model_name: Hugging Face model name for RT-DETR.
confidence_threshold: Minimum confidence for detections.
target_classes: List of class indices to track (None for all).
"""
# Set device
device = (
"cuda"
if torch.cuda.is_available()
else "mps"
if torch.backends.mps.is_available()
else "cpu"
)
print(f"🖥️ Using device: {device}")
# Load RT-DETR model from Transformers
print(f"🚀 Loading RT-DETR model: {model_name}")
image_processor = RTDetrImageProcessor.from_pretrained(model_name)
model = RTDetrForObjectDetection.from_pretrained(model_name).to(device)
model.eval()
# Initialize SORT Tracker
print("📦 Initializing SORT tracker...")
tracker = trackforge.Sort(max_age=30, min_hits=3, iou_threshold=0.3)
# Open Video
cap = cv2.VideoCapture(video_path)
if not cap.isOpened():
print(f"❌ Error opening video file: {video_path}")
return
# Get video properties
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = int(cap.get(cv2.CAP_PROP_FPS))
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
print(f"📹 Video: {width}x{height} @ {fps}fps, {total_frames} frames")
# Video Writer
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
frame_count = 0
t0 = time.time()
# Color palette for different track IDs
colors = [
(255, 0, 0), # Blue
(0, 255, 0), # Green
(0, 0, 255), # Red
(255, 255, 0), # Cyan
(255, 0, 255), # Magenta
(0, 255, 255), # Yellow
(128, 0, 255), # Purple
(255, 128, 0), # Orange
]
print("🎬 Processing video...")
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
frame_count += 1
# Convert BGR to RGB for PIL
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
pil_image = Image.fromarray(frame_rgb)
# Run RT-DETR Detection
with torch.no_grad():
inputs = image_processor(images=pil_image, return_tensors="pt").to(device)
outputs = model(**inputs)
# Post-process detections
results = image_processor.post_process_object_detection(
outputs,
target_sizes=torch.tensor([[height, width]]).to(device),
threshold=confidence_threshold,
)[0]
# Prepare detections for SORT tracker: (tlwh, score, class_id)
detections = []
for score, label, box in zip(
results["scores"], results["labels"], results["boxes"]
):
# Filter by target classes if specified
if target_classes is not None and label.item() not in target_classes:
continue
# Convert xyxy to tlwh
x1, y1, x2, y2 = box.cpu().numpy()
w = x2 - x1
h = y2 - y1
tlwh = [float(x1), float(y1), float(w), float(h)]
conf = float(score.cpu().numpy())
cls = int(label.cpu().numpy())
detections.append((tlwh, conf, cls))
# Update SORT Tracker
tracks = tracker.update(detections)
# Draw tracks
for track in tracks:
track_id, tlwh, score, class_id = track
x1, y1, w, h = tlwh
x2, y2 = x1 + w, y1 + h
# Get color based on track ID
color = colors[track_id % len(colors)]
# Get class name
class_name = (
COCO_CLASSES[class_id]
if class_id < len(COCO_CLASSES)
else f"cls{class_id}"
)
# Draw bounding box
cv2.rectangle(frame, (int(x1), int(y1)), (int(x2), int(y2)), color, 2)
# Draw label with track ID
label = f"ID:{track_id} {class_name} {score:.2f}"
label_size, _ = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 0.5, 2)
cv2.rectangle(
frame,
(int(x1), int(y1) - label_size[1] - 10),
(int(x1) + label_size[0], int(y1)),
color,
-1,
)
cv2.putText(
frame,
label,
(int(x1), int(y1) - 5),
cv2.FONT_HERSHEY_SIMPLEX,
0.5,
(255, 255, 255),
2,
)
# Draw frame info
info_text = f"SORT + RT-DETR | Frame: {frame_count}/{total_frames} | Tracks: {len(tracks)}"
cv2.putText(
frame, info_text, (20, 40), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 0), 2
)
out.write(frame)
if frame_count % 50 == 0:
elapsed = time.time() - t0
fps_actual = frame_count / elapsed
print(
f" Processed {frame_count}/{total_frames} frames ({fps_actual:.1f} fps)"
)
t1 = time.time()
total_time = t1 - t0
avg_fps = frame_count / total_time
print("\n✅ Done!")
print(f" Processed {frame_count} frames in {total_time:.2f}s ({avg_fps:.1f} fps)")
print(f" Output saved to: {output_path}")
cap.release()
out.release()
if __name__ == "__main__":
# Check if video exists
video_file = Path("people.mp4")
if not video_file.exists():
print("⚠️ Video file 'people.mp4' not found in current directory.")
print(" Please provide a video file or update the path.")
else:
# Track only persons (class 0) for people.mp4
run_tracking(target_classes=[0])