4.9 KiB
Vendored
4.9 KiB
Vendored
In [ ]:
#!/bin/bash
!python -m venv venv
!source venv/bin/activate
!pip install -q inference "supervision[assets]"In [ ]:
from supervision.assets import download_assets, VideoAssets
# Download a supervision video asset
path_to_video = download_assets(VideoAssets.PEOPLE_WALKING)In [ ]:
import supervision as sv
from supervision.assets import download_assets, VideoAssets
from inference.models.utils import get_roboflow_model
if __name__ == '__main__':
# Install our video from supervision assets
PATH_TO_VIDEO = download_assets(VideoAssets.PEOPLE_WALKING)
# load the yolov8X model from roboflow inference
model = get_roboflow_model('yolov8n-640')
# get video info from the video path
video_info = sv.VideoInfo.from_video_path(PATH_TO_VIDEO)
# create a trace and label annotator, with dynamic video info
label = sv.LabelAnnotator()
# create a ByteTrack object to track detections
byte_tracker = sv.ByteTrack(frame_rate=video_info.fps)
# get frames iterable from video and loop over them
frame_generator = sv.get_video_frames_generator(PATH_TO_VIDEO)
# create a video sink context manager to write the annotated frames to
with sv.VideoSink(target_path="output.mp4", video_info=video_info) as sink:
for frame in frame_generator:
# run inference on the frame
result = model.infer(frame)[0]
# convert the detections to a supervision detections object
detections = sv.Detections.from_inference(result)
# update detections with tracker ids
tracked_detections = byte_tracker.update_with_detections(detections)
# create label text for annotator
labels = [ f"{tracker_id}" for tracker_id in tracked_detections.tracker_id ]
# apply label annotator to frame
annotated_frame = label.annotate(scene=frame.copy(), detections=tracked_detections, labels=labels)
# save the annotated frame to the video sink
sink.write_frame(frame=annotated_frame)