In this python tutorial, we will discuss how to read video frames in Python, Python read video frames, and also we will cover these below topics:
- Python read video frames
- Python extract a specific frame from video
Python read video frames
Here, we can see how to read video frames in python.
- In this example, I have imported modules called cv2 and os.
- Next, we should read the path of the video.
- Here, we are creating a folder called pet by using os.makedirs to copy the frames of the video into the folder. The pet is the name of the folder.
- I have used exceptions if the folder is not created it should raise an error.
- As we are working with the frame we should mention the current frame to work in it.
- The ret is used to return the frames. The while condition is used, if the condition is true the video is read into the folder.
- To read the images .jpg extension is used. To assign the number to each frame in the folder currentframe += 1.
- If the condition is false then else is executed, the video.release() is used to free the objects.
- The cv2.destroyAllWindows() simply destroy all the windows created.
Example:
import cv2
import os
video = cv2.VideoCapture(r'C:\Users\Administrator.SHAREPOINTSKY\Downloads\catty.mp4')
try:
if not os.path.exists('pet'):
os.makedirs('pet')
except OSError:
print ('Error')
currentframe = 0
while(True):
ret,frame = video.read()
if ret:
name = './pet/frame' + str(currentframe) + '.jpg'
print ('Captured...' + name)
cv2.imwrite(name, frame)
currentframe += 1
else:
break
video.release()
cv2.destroyAllWindows()
The below screenshot show the creating of frames as the output.
We can see the frames of the video in the folder as the output. The below screenshot shows the output.
This is how to read video frames in Python.
Python extract a specific frame from video
Here, we can see how to extract a specific frame from video in python.
- In this example, I have imported a module called numpy as np and cv2, and then read the file by specifying the path.
- To give the value of the frame cap.set(1, 16) is used. Here, 16 is the number of the frame.
- To read the frame cap.read is used. I have used cv2.imshow() to show the image of the specified frame.
Example:
import numpy as np
import cv2
cap = cv2.VideoCapture(r'C:\Users\Administrator.SHAREPOINTSKY\Downloads\catty.mp4')
cap.set(1,16);
ret, frame = cap.read()
cv2.imshow('window_name', frame)
The below screenshot shows the specified frame from the vedio.
This is how to extract a specific frame from video in Python.
You may like the following Python tutorials:
- Groupby in Python Pandas
- Python program to print element in an array
- Crosstab in Python Pandas
- Python binary tree implementation
- Python string formatting with examples
- Python program to print prime numbers
- What is NumPy in Python
In this tutorial, we have learned about Python read video frames, and also we have covered these topics:
- Python read video frames
- Python extract a specific frame from video
Python is one of the most popular languages in the United States of America. I have been working with Python for a long time and I have expertise in working with various libraries on Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… I have experience in working with various clients in countries like United States, Canada, United Kingdom, Australia, New Zealand, etc. Check out my profile.