Cinelytics uses OpenCV to read video. The main function is `read_video`, where you can flexibly grab either all or different sequences of the frames of any given video.

Reading Video using OpenCV

read_video[source]

read_video(fname:Union[str, VideoCapture], start_idx:Optional[int]=None, end_idx:Optional[int]=None, frame_stride:Optional[int]=None, target_frames:Union[tuple, list, int, array]=None, resize_func:Optional[Callable]='resize', cvt_color_func:Optional[Callable]='bgr2rgb', apply:Optional[Callable]=None)

Flexible video reader where you can grab frames in different ways and return as different dtypes.

Args

  • fname : a cv2.VideoCapture object, or path to the video file
  • start_idx : index of the frame where you'd like to start reading the file
  • end_idx : index of the frame where you'd like to end reading the file
  • frame_stride : after reading the frame, every frame_strideth index will be read
  • target_frames: if this arg is used, start_idx, end_idx, and frame_stride are ignored. Can be an

      - `int`            : returns frame at this index
      - `list`/`np.array`: returns frame at index of each element
      - `tuple`          : a tuple like `(start_idx, end_idx, frame_stride)` where
                           `frame_stride`is optional. If this is what you want, using the
                           other args instead of `target_frames` is more flexible.
  • resize_func : a function that resizes a single np.array of shape (height, width, channels)
  • cvt_color_func : a function that applies a cv2.cvtColor transformation
  • apply : a function that transforms a single np.array of shape (height, width, channels)
                   and is called **after** individual frames of the video have been read i.e. just
                   before returning the collection (list/tensor) of frames

Helper Functions

read_frames[source]

read_frames(cap:VideoCapture, target_frames:Union[tuple, list, int, array], resize_func:Optional[Callable]=None, cvt_color_func:Optional[Callable]=None)

Read specific frames from a cv2.VideoCapture object

Helper function for read_video


capture[source]

capture(x:Union[str, VideoCapture])

Ensure cv2.VideoCapture works properly

Helper function for read_video


get_target_frames[source]

get_target_frames(file, start, end, stride)

flexibly get the indixes of the frames you want to grab

Helper function for read_video


Examples

vid = read_video('files/interstellar-waves-edit.mp4', target_frames=[0,1,2,3,4,5])

len(vid); vid[0].shape
6
(480, 720, 3)

resizer = partial(resize, keep_aspect_ratio=True, scale_factor=2)
vid = read_video('files/interstellar-waves-edit.mp4', target_frames=[0,1,2,3,4,5], 
                 resize_func=resizer)

vid[0].shape
(960, 1440, 3)

vid  = read_video('files/interstellar-waves-edit.mp4', target_frames=np.arange(100))
vid2 = read_video('files/interstellar-waves-edit.mp4', start_idx=0, end_idx=100, frame_stride=1)
vid3 = read_video('files/interstellar-waves-edit.mp4', target_frames=(0, 100, 1))
vid4 = read_video('files/interstellar-waves-edit.mp4', end_idx=100)

len(vid)
len(vid) == len(vid2) == len(vid3) == len(vid4)
100
True

vid = read_video('files/interstellar-waves-edit.mp4', target_frames=5, apply=as_tensor)
vid.shape
torch.Size([1, 480, 720, 3])