Frames can be extracted from a video using opencv, or more conveniently, mmcv.
Opencv
Given a video file, video.mp4, to extract frames from it using opencv, first load into a video capture:
vcap = cv2.VideoCapture('video.mp4')
The file path needs to be a str
; a Path
will raise an error. To find out how many frames the video has, you can get its length with:
vlen = int(vcap.get(cv2.CAP_PROP_FRAME_COUNT))
Next,
success, frame = vcap.read()
will try to read the next frame in the video. Since this is the first time it's executed, it will try to return the first frame of the video. If it's executed again, it will try to return the second frame of the video. 'Try to', because sometimes it might not successfully read a frame. In this case, success
returned will be False
, otherwise it's True
. frame
is a numpy array of shape (height of frame, width of frame, 3). In most situations, you will also want to run the following to get normal-looking colours:
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
Now you can visualise the frame, and the colours should appear the same as in the original video. When you are finished reading frames from the video capture, close it with:
vcap.release()
Do it more concisely
There is an open source library for computer vision research called mmcv, and it allows the loading of a video into its frames:
vid = mmcv.VideoReader('video.mp4')
vid
is a list-like object that already contains all the frames that can be read from the video. len(vid)
is the number of frames, and vid[:10]
will return the first 10 frames. VideoReader
is pretty fast, faster than if vcap.read()
is repeatedly executed within a normal for-loop for the length of the video, so mmcv
probably contains some sort of optimization.
As with above, to get RGB colours you will need to do:
frames = [cv2.cvtColor(o, cv2.COLOR_BGR2RGB) for o in vid[:10]]