VideoReader¶
- class mmcv.video.VideoReader(filename, cache_capacity=10)[source]¶
Video class with similar usage to a list object.
This video wrapper class provides convenient apis to access frames. There exists an issue of OpenCV’s VideoCapture class that jumping to a certain frame may be inaccurate. It is fixed in this class by checking the position after jumping each time. Cache is used when decoding videos. So if the same frame is visited for the second time, there is no need to decode again if it is stored in the cache.
Examples
>>> import mmcv >>> v = mmcv.VideoReader('sample.mp4') >>> len(v) # get the total frame number with `len()` 120 >>> for img in v: # v is iterable >>> mmcv.imshow(img) >>> v[5] # get the 6th frame
- current_frame()[source]¶
Get the current frame (frame that is just visited).
- Returns:
If the video is fresh, return None, otherwise return the frame.
- Return type:
ndarray or None
- cvt2frames(frame_dir, file_start=0, filename_tmpl='{:06d}.jpg', start=0, max_num=0, show_progress=True)[source]¶
Convert a video to frame images.
- Parameters:
frame_dir (str) – Output directory to store all the frame images.
file_start (int) – Filenames will start from the specified number.
filename_tmpl (str) – Filename template with the index as the placeholder.
start (int) – The starting frame index.
max_num (int) – Maximum number of frames to be written.
show_progress (bool) – Whether to show a progress bar.
- get_frame(frame_id)[source]¶
Get frame by index.
- Parameters:
frame_id (int) – Index of the expected frame, 0-based.
- Returns:
Return the frame if successful, otherwise None.
- Return type:
ndarray or None
- read()[source]¶
Read the next frame.
If the next frame have been decoded before and in the cache, then return it directly, otherwise decode, cache and return it.
- Returns:
Return the frame if successful, otherwise None.
- Return type:
ndarray or None
- property vcap¶
The raw VideoCapture object.
- Type:
cv2.VideoCapture