In this article, we will describe how you can capture each frame from the video and also resize them according to your needs. What we will exactly do is take the video as input from the user and capture it frame by frame. Moreover, we run the video in the loop and save the image frames with certain names after resizing those frames.
Stepwise Implementation:
Step 1: Importing the libraries
Here, we are importing the cv2 library, the cv2 is the OpenCV package that helps us to call the imread(), startWindowThread(), namedWindow(), and imshow() functions respectively.
import cv2
Step 2: Define a video capture object
In this step, we will use the function VideoCapture() to get a video capture object for the camera. Here, you can use either your camera or directly upload the video from your system.
vidcap = cv2.VideoCapture(“#Define the video path”)
Step 3: Capture video frame by frame
Now, we will capture the length of the video for running the loop. Also, the video gets automatically closed once all the frames have been captured.
success,image = vidcap.read()
Step 4: Declare the variable
Then, declare the variable, and count with the value 0. This variable will further be incremented while running the loop.
count = 0
Step 5: Creating a loop for running the video
Moreover, we create a while loop for running the video and saving all the frames from your video into your local system.
while success:
Step 5.1: Capture video frame by frame
Next, we will read the video frame by frame for resizing the frames and saving them to your local computer.
success,image = vidcap.read()
Step 5.2: Resizing the image frames
In this step, we have used resize() function with particular dimensions for which the image frames need to be set.
resize = cv2.resize(image, (#x-axis dimensions, #y-axis dimensions))
Step 5.3: Saving the frames with certain names
Later on, we save the image frames we resize in the last step. Also, we give the frames certain names with the extensions.
cv2.imwrite(“%04d.jpg” % count, resize)
Step 5.4: Closing the video automatically once it gets over
Further, the waitKey() function is used for closing the video automatically once the video gets over.
if cv2.waitKey(10) == 27: break
Step 5.5 Incrementing the variable by 1
Finally, we increment the value of the variable declared, count by 1.
count += 1
Example:
In this example, we have used the video (link) from which we will extract certain frames, resize those frames and save them with the particular names on the local computer.
Python3
# Python program to resize frame's # from video with aspect ratio # Import the libraries import cv2 # Define a video capture object vidcap = cv2.VideoCapture( "gfg_video.mp4" ) # Capture video frame by frame success, image = vidcap.read() # Declare the variable with value 0 count = 0 # Creating a loop for running the video # and saving all the frames while success: # Capture video frame by frame success, image = vidcap.read() # Resize the image frames resize = cv2.resize(image, ( 700 , 500 )) # Saving the frames with certain names cv2.imwrite( "%04d.jpg" % count, resize) # Closing the video by Escape button if cv2.waitKey( 10 ) = = 27 : break # Incrementing the variable value by 1 count + = 1 |
Output: