Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Take the frame by sec #116

Closed
1 task done
sceddd opened this issue Jun 1, 2023 · 6 comments
Closed
1 task done

Take the frame by sec #116

sceddd opened this issue Jun 1, 2023 · 6 comments
Assignees
Labels
question Further information is requested

Comments

@sceddd
Copy link

sceddd commented Jun 1, 2023

Search before asking

  • I have searched the Supervision issues and found no similar feature requests.

Question

Is there any way to collect the frame by sec, like if i have a clip length 400sec, then i'll have a list 400 frame? I have search through process_video() and figure out it took the frame base on the index of the frame.

Additional

No response

@sceddd sceddd added the question Further information is requested label Jun 1, 2023
@github-actions
Copy link
Contributor

github-actions bot commented Jun 1, 2023

Hello there, thank you for opening an Issue ! 🙏🏻 The team was notified and they will get back to you asap.

@sceddd
Copy link
Author

sceddd commented Jun 2, 2023

Hello,
After working a while with your videosink, i'm end up with using the fps to check the frame at every secs, i'll put the code here if anyone need it. Hope it'll help:

from typing import Callable
def process_video(
    source_path: str,
    target_path: str,
    callback: Callable[[np.ndarray, int], np.ndarray],
) -> None:
    """
    Process a video file by applying a callback function on each frame and saving the result to a target video file.

    Args:
        source_path (str): The path to the source video file.
        target_path (str): The path to the target video file.
        callback (Callable[[np.ndarray, int], np.ndarray]): A function that takes in a numpy ndarray representation of a video frame and an int index of the frame and returns a processed numpy ndarray representation of the frame.

    Examples:
        ```python
        >>> from supervision import process_video

        >>> def process_frame(scene: np.ndarray) -> np.ndarray:
        ...     ...

        >>> process_video(
        ...     source_path='source_video.mp4',
        ...     target_path='target_video.mp4',
        ...     callback=process_frame
        ... )
        ```
    """
    source_video_info = VideoInfo.from_video_path(video_path=source_path)
    with VideoSink(target_path=target_path, video_info=source_video_info) as sink:
        for index, frame in enumerate(
            get_video_frames_generator(source_path=source_path)
        ):
            result_frame = callback(frame, index)
            if index % sink.video_info.fps==0:
              plot_image(frame) # put the function you wanna work with the frame at here
            sink.write_frame(frame=result_frame)

@SkalskiP SkalskiP self-assigned this Jun 2, 2023
@SkalskiP
Copy link
Collaborator

SkalskiP commented Jun 2, 2023

Hi @sceddd 👋🏻! Thanks a lot for your interest in our project. Sorry for the late response. Here is how I would do that.

import supervision as sv

VIDEO_PATH = 'video.mp4'
video_info = sv.VideoInfo.from_video_path(video_path=VIDEO_PATH)

for index, frame in enumerate(sv.get_video_frames_generator(source_path=VIDEO_PATH)):
    if index % video_info.fps == 0:
        sv.plot_image(frame)

@sceddd
Copy link
Author

sceddd commented Jun 2, 2023

Hi @SkalskiP, Thanks for your reply. I already override your process_video() and do the same thing in the last comment. After all, I appreciate it.

@SkalskiP
Copy link
Collaborator

SkalskiP commented Jun 2, 2023

No worries :) In that case, I'm closing the issue. However, I'm thinking about adding it as a feature in supervision.

@SkalskiP SkalskiP closed this as completed Jun 2, 2023
@SkalskiP
Copy link
Collaborator

SkalskiP commented Jun 6, 2023

Hi, @sceddd 👋🏻! I just started working on a supervision update allowing you to pick every N-th frame from the video - #120. It will be added to supervision with the 0.9.0 release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants