In this post we extract and save the video frames using OpenCV in Python. OpenCV provides us many different types of the methods to perform on the images. We have VideoCapture()
, read()
, isOpened()
, imwrite()
and many more to play with the video and video frames. We use these them to capture video, extract and save the frames.
Complete Example
Bellow is Python 3 code snippet to extract and save the video frames. We use VideoCapture()
method to read the video from local folder or start the webcam. To read the video from local machine we set video path as parameter to this method. isOpened()
is used to check whether video capture is successful or not. If successful it returns True
else returns False
. We use read()
to extract the frames. Finally imwrite()
is used to save the frames.
# Python3 program to extract and save video frame# using OpenCV Python#import computer vision moduleimport cv2# define the video pathfile = 'test_video.mp4'# capture the videocap = cv2.VideoCapture(file)i = 0 # frame index to save frames# extract and save the video frameswhile cap.isOpened():ret, frame = cap.read()if ret:cv2.imwrite('test_frame_'+str(i)+'.png', frame)i+=1
The images are saved as of type .png
. You can save the images in any other format providing the suitable extension.
Read the Video
The first step is to read the video. cv2.VideoCapture()
is used to capture/ read the video. If you want to capture the video from webcam use cv2.VideoCapture(0)
.
#import conputer vision moduleimport cv2# define the video pathfile = 'test_video.mp4'# capture the videocap = cv2.VideoCapture(file)cap = cv2.VideoCapture(0) # To take video from webcam
file = r'D:\MyCVProject\LaneDetection-DriverlessCar\test_video.mp4'
True
else False
. print(cap.isOpened())
Extract Video Frames
The next step is to extract the video frames, if our video is captured successfully. Using cap.read()
we read the frames one by one. It returns two values, first value is True
or False
and second value is the frame. If frame is read successfully then the first value is True
else False
.
ret, frame = cap.read()
To check whether frame reading if successful you can print ret value
print(ret)
Save the Extracted Frames
When frame is successfully read, now we save the frame as .jpg using cv2.mwrite()
. When image is successfully saved it returns True
else False
. See the below snippet.
cv2.imwrite('test_frame_'+str(i)+'.jpg', frame)
cv2.imwrite(r'D:\MyCVProject\LaneDetection-DriverlessCar\test_frame_'+str(i)+'.jpg', frame)
Note: Don't forget to give the type of image (.jpg, .png, etc.)
FAQ:
Q. How to check if our frames are saved?
A.while cap.isOpened():ret, frame = cap.read()saved = cv2.imwrite(r'D:\MyCVProject\LaneDetection-DriverlessCar\test_frame_'+str(i)+'.jpg', frame)if saved:print('image saved')else:print('image not saved')
If our frames are saved successfully cv2.imwrite() will return True
and if not saved successfully it will return False
.
Further Readings:
- How to Load and Visualize Multiple Images in Python
- Mathematical Operations On Images Using OpenCV and NumPy in Python
- Basic Operations on Images using openCV in Python
- Different Ways to Load Images in Python from Scratch
you forgot i +=1 in the last line for the while loop
ReplyDeleteThanks for pointing error. Updated
Delete