In Callback, how to omit frames with no detections #995
Replies: 2 comments
-
I would recommend using opencv in a while loop to annotate the frames as you go. Under the hood, supervision is just getting each frame from opencv and then calling your callback function on each frame and writing the return value to an opencv videoWriter. If you look up an example of using opencv to process a video, you can just do everything you are doing in your callback, but instead only write the frames you want to the videoWriter output. Here is an example for how to use the opencv videoWriter: |
Beta Was this translation helpful? Give feedback.
-
Hi @bancroftway 👋🏻 I think that's all you need ;) import supervision as sv
video_info = sv.VideoInfo.from_video_path(<SOURCE_VIDEO_PATH>)
frames_generator = sv.get_video_frames_generator(<SOURCE_VIDEO_PATH>)
with sv.VideoSink(target_path=<TARGET_VIDEO_PATH>, video_info=video_info) as sink:
for frame in frames_generator:
results = model(frame, verbose=False)[0]
detections = sv.Detections.from_ultralytics(results)
detections = detections[detections.class_id == 14]
if len(detections) > 0:
sink.write_frame(frame=frame) |
Beta Was this translation helpful? Give feedback.
-
I am brand new to using supervision. I have some large video files, and the objective is to shorten these videos to just those parts that contain birds. Since these shortened videos will be sent to our vendor for further processing, I do not want to annotate the frames. In the code below, I do not return a frame from the callback if there is no bird found...however the resulting mp4 is not playable. Is there a better way to output only those frames that have at least 1 bird, and stitch those frames into a shorter video?
Beta Was this translation helpful? Give feedback.
All reactions