supervision
supervision copied to clipboard
the result.mp4 cannot be played on the browser
Search before asking
- [X] I have searched the Supervision issues and found no similar feature requests.
Question
i dont know why the result.mp4 is much large than the original, and it cannot be played on the browser, the code is as following
import numpy as np
import supervision as sv
from ultralytics import YOLO
model = YOLO("yolov8n.pt")
tracker = sv.ByteTrack()
box_annotator = sv.BoundingBoxAnnotator()
label_annotator = sv.LabelAnnotator()
trace_annotator = sv.TraceAnnotator()
def callback(frame: np.ndarray, _: int) -> np.ndarray:
results = model(frame)[0]
detections = sv.Detections.from_ultralytics(results)
detections = tracker.update_with_detections(detections)
labels = [
f"#{tracker_id} {results.names[class_id]}"
for class_id, tracker_id
in zip(detections.class_id, detections.tracker_id)
]
annotated_frame = box_annotator.annotate(
frame.copy(), detections=detections)
annotated_frame = label_annotator.annotate(
annotated_frame, detections=detections, labels=labels)
return trace_annotator.annotate(
annotated_frame, detections=detections)
sv.process_video(
source_path="people-walking.mp4",
target_path="result.mp4",
callback=callback
)
Additional
No response
Hi @wilsonlv :wave:
Apologies for the late response and thank you for the report! I've tested it, and yes, I can reproduce the issue.
The reason is that by default, we encode the output with the mp4v
codec, whereas the input file you likely used was coded with h264
. While the latter takes up less space, the former works more reliably with OpenCV.
I'll make a PR shortly, adding codec selection to sv.process_video
. Although my tests show that the setup isn't trivial - OpenCV really doesn't like telling when it doesn't support the codec.
In the meantime, here's a command to convert to h264
.
ffmpeg -i input-any.mp4 -c:v libx264 output-h264.mp4
Hope this helps :wink:
Related PR
- #1126
@LinasKo Appreciate for your response, thank you !
I'm converting this issue into a discussion and moving it to the Q&A section.