Tips and Traps¶
- Most videoes have a FPS of 24. Some videos have higher FPS (e.g., 30 or 60) so that they can capture motions better. A record video on a computer has a FPS of the refresh rate of the monitor, which is usually 60. You can get the FPS of a video using OpenCV in Python.
Installation¶
Install OpenCV for Python following instructions at http://www.legendu.net/misc/blog/tips-on-opencv/#installation.
import numpy as np
from PIL import Image
import cv2
Download the big buck bunny video.
!wget --no-check-certificate https://www.sample-videos.com/video123/mp4/720/big_buck_bunny_720p_5mb.mp4 -O bunny.mp4
import cv2
vidcap = cv2.VideoCapture("bunny.mp4")
vidcap.get(cv2.CAP_PROP_FPS)
You can get total number of frames in a video using the following Python code.
vidcap.get(cv2.CAP_PROP_FRAME_COUNT)
cv2.VideoCapture.read
returns a tuple of (bool, numpy.ndarray)
.
The returned numpy array is the BGR
representation of the frame/image.
success, arr = vidcap.read()
The first value of the returned tuple is a flag indicating whether the read success.
success
The second value of the returned tuple is a nump array with the dimension (image_height, image_width, 3)
.
arr
The numpy array is the BGR
(instead of RGB
) representation of the image,
which means that you have to flip the last dimension of the numpy array
so that it can loaded using the Pillow library correctly.
Image.fromarray(np.flip(arr, 2))