forked from roboflow/supervision
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrfdetr_example.py
More file actions
55 lines (43 loc) 路 1.89 KB
/
Copy pathrfdetr_example.py
File metadata and controls
55 lines (43 loc) 路 1.89 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
from rfdetr import RFDETRMedium
from tqdm import tqdm
import supervision as sv
from supervision import _cv2 as cv2
def main(
source_video_path: str,
target_video_path: str,
device: str = "cpu",
confidence_threshold: float = 0.3,
iou_threshold: float = 0.7,
) -> None:
"""
Video Processing with RF-DETR and ByteTrack.
Args:
source_video_path: Path to the source video file
target_video_path: Path to the target video file (output)
device: Computation device ('cpu', 'mps' or 'cuda')
confidence_threshold: Confidence threshold for the model
iou_threshold: IOU threshold for the model
"""
model = RFDETRMedium(device=device)
tracker = sv.ByteTrack()
box_annotator = sv.BoxAnnotator()
label_annotator = sv.LabelAnnotator()
frame_generator = sv.get_video_frames_generator(source_path=source_video_path)
video_info = sv.VideoInfo.from_video_path(video_path=source_video_path)
with sv.VideoSink(target_path=target_video_path, video_info=video_info) as sink:
for frame in tqdm(frame_generator, total=video_info.total_frames):
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
detections = model.predict(frame_rgb, threshold=confidence_threshold)
detections = detections.with_nms(threshold=iou_threshold)
detections = tracker.update_with_detections(detections)
annotated_frame = box_annotator.annotate(
scene=frame.copy(), detections=detections
)
annotated_labeled_frame = label_annotator.annotate(
scene=annotated_frame, detections=detections
)
sink.write_frame(frame=annotated_labeled_frame)
if __name__ == "__main__":
from jsonargparse import auto_cli, set_parsing_settings
set_parsing_settings(parse_optionals_as_positionals=True)
auto_cli(main, as_positional=False)