-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsort_yolo_demo.py
More file actions
91 lines (72 loc) · 2.41 KB
/
Copy pathsort_yolo_demo.py
File metadata and controls
91 lines (72 loc) · 2.41 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
#!/usr/bin/env python3
"""SORT tracking demo with an Ultralytics YOLO detector.
Requirements:
pip install ultralytics opencv-python trackforge
Example:
$ python sort_yolo_demo.py --video people.mp4 --model yolo11n.pt
"""
from __future__ import annotations
import argparse
import time
from pathlib import Path
from ultralytics import YOLO
import trackforge
from common import (
create_video_writer,
draw_hud,
draw_track,
label_for,
load_video,
log_progress,
yolo_detections,
)
def run_tracking(video: str, output: str, model_path: str) -> None:
"""Run SORT over a video and write an annotated copy.
Args:
video: Path to the input video.
output: Path for the annotated output video.
model_path: Path to the YOLO model weights.
"""
model = YOLO(model_path)
tracker = trackforge.SORT(max_age=30, min_hits=3, iou_threshold=0.3)
cap, info = load_video(video)
writer = create_video_writer(output, info)
print(
f"video: {info.width}x{info.height} @ {info.fps}fps, {info.total_frames} frames"
)
frame_count = 0
start = time.time()
while True:
ok, frame = cap.read()
if not ok:
break
frame_count += 1
detections = yolo_detections(model, frame, classes=[0])
tracks = tracker.update(detections)
for track_id, tlwh, score, class_id in tracks:
draw_track(
frame, track_id, tlwh, label_for(track_id, model.names[class_id], score)
)
draw_hud(
frame,
f"SORT | frame {frame_count}/{info.total_frames} | tracks {len(tracks)}",
)
writer.write(frame)
log_progress(frame_count, info.total_frames, start)
cap.release()
writer.release()
print(f"done: {frame_count} frames -> {output}")
def main() -> None:
parser = argparse.ArgumentParser(description="SORT tracking with YOLO detection.")
parser.add_argument("--video", default="people.mp4", help="input video path")
parser.add_argument(
"--output", default="output_sort_yolo.mp4", help="output video path"
)
parser.add_argument("--model", default="yolo11n.pt", help="YOLO model weights")
args = parser.parse_args()
if not Path(args.video).exists():
print(f"video not found: {args.video}")
return
run_tracking(args.video, args.output, args.model)
if __name__ == "__main__":
main()