Extracting and Saving Video Frames using OpenCV Python

 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. 

Extracting and saving video 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 module
import cv2

# define the video path
file = 'test_video.mp4'

# capture the video
cap = cv2.VideoCapture(file)
i = 0  # frame index to save frames

# extract and save the video frames

while 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 module
import cv2

# define the video path
file = 'test_video.mp4'

# capture the video
cap = cv2.VideoCapture(file)

cap = cv2.VideoCapture(0) # To take video from webcam


You can also provide the full video path as below.

file = r'D:\MyCVProject\LaneDetection-DriverlessCar\test_video.mp4'

If the video is successfully read/ captured, the below code will print  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)


Or you can use the full path to save in a particular location.

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:

Useful Resources:


Previous Post: Object Detection and Recognition using PyTorch YOLOv5





Comments

  1. you forgot i +=1 in the last line for the while loop

    ReplyDelete

Post a Comment