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

Protocol to match Decord / VideoReader #259

Open
johndpope opened this issue Mar 12, 2024 · 0 comments
Open

Protocol to match Decord / VideoReader #259

johndpope opened this issue Mar 12, 2024 · 0 comments

Comments

@johndpope
Copy link

johndpope commented Mar 12, 2024

🚀 Feature

training dataset / pytorch on videos should allow me to take an mp4

and get the frames


class VideoReader(EncodedVideo):
    def __init__(self, uri):
        super().__init__(uri)
        self._num_frame = int(self.duration * self.video_meta['fps'])
    
    def __len__(self):
        return self._num_frame

    def __getitem__(self, idx):
        if isinstance(idx, slice):
            return self.get_batch(range(*idx.indices(len(self))))
        if idx < 0:
            idx += self._num_frame
        if idx >= self._num_frame or idx < 0:
            raise IndexError("Index out of range")
        return self.get_frame_by_index(idx)

    def get_frame_by_index(self, frame_index):
        fps = self.video_meta['fps']
        time_sec = frame_index / fps
        video_frame = self.get_clip(start_sec=time_sec, end_sec=time_sec + 1 / fps)['video']
        return video_frame[0]

    def get_batch(self, indices):
        frames = [self.get_frame_by_index(idx) for idx in indices]
        return torch.stack(frames)

    def seek(self, frame_index):
        # EncodedVideo does not support non-sequential access like seek.
        # This is a placeholder if you need a similar method.
        pass

    @classmethod
    def from_path(cls, uri):
        return cls.from_path(uri)

NOTE: Please look at the existing list of Issues tagged with the label 'enhancement`. Only open a new issue if you do not see your feature request there.

Motivation

Pitch

NOTE: we only consider adding new features if they are useful for many users.

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

No branches or pull requests

1 participant