With the help of OpenCV we can extract frames from a video and save them as image files.
cv2.videocapture() can be used to extract frames from a video by calling the read() method.
We can save or process the extracted frames.
In this tutorial, we’ll learn about extracting images from video using OpenCV.
input:
Code:
import cv2
vidcap = cv2.VideoCapture('video1.mp4')
success, image = vidcap.read()
count = 0
increment = 5
while success:
cv2.imwrite("frame%d.jpg" % count, image)
success, image = vidcap.read()
count += increment
print("Extracted %d frames from the video" % count)
First we imported required OpenCV.
Then, we used the cv2.VideoCapture() function to read the video file.
Then, we used vidcap.read() function to read every frame from the video and stored it into success.
After this we initialized two variables count and increment.
The count variable is used to keep track of the number of frames extracted and the increment variable is used to specify the number of frames to be skipped.
Then, we created a while loop to run till the last frame of the video.
Inside while loop. We used cv2.imwrite function to save each extracted frame as a jpg file.
At last, we incremented the value of count. And printed the total number of count.
Result:
Extracted 2330 frames from the video
Trainings :
Data Science Training in Indore | Data Analytics Training in Indore | Python Training in Indore | Blockchain Training in Indore | React JS Training in Indore | Web Development Training in Indore | Full Stack Development Training in Indore |Free Courses and Resource :
Verbal Aptitude | Matplotlib Tutorials | Examples | Interview Questions | Pandas Tutorials | Projects | Interview Questions | OpenCV Tutorials | Projects | Interview Questions | ExpressJS Tutorials | Projects | Interview Questions | Dart | Python Data Structures and Algorithms | Rust Tutorials | Projects | Interview Questions | Aptitude Tests | HackerRank Python | Django | MongoDB Tutorials | Examples | Interview Questions | NumPy Practice Questions | Python Tutorials by CodersDaily | Golang Tutorials | Projects | Interview Questions | React.js Tutorials | Verbal Ability Tutorial | MS Sql Server Tutorials | Examples | Interview Questions | Power BI Tutorials | Projects | Interview Questions | Example Dashboards | Numpy Tutorials | Projects | Interview Questions | Django REST Framework Tutorial | HackerRank C++ Solutions | Pandas Practice Questions | Tensor Flow | Python Practice Questions | C++ Tutorials | Quantitative Ability Tutorial | Javascript | Node.js Tutorials | HackerRank SQL Solutions | HackerRank DSA Solutions | Verbal Aptitude 2 | HackerRank Java Solutions | HTML Tutorial | TCS NQT Mock Test Series | Reasoning Ability Tutorial | CodeChef Python Solutions | HackerRank C Program Solutions | Leetcode Python Solutions | SQL Practice Question | Matplotlib Practice Questions |Interview Questions :
Pandas Tutorials | Projects | Interview Questions | ExpressJS Tutorials | Projects | Interview Questions | Django | Python Tutorials by CodersDaily | Golang Tutorials | Projects | Interview Questions | Numpy Tutorials | Projects | Interview Questions | Django REST Framework Tutorial | Matplotlib Practice Questions | Matplotlib Practice Questions | Matplotlib Practice Questions | Matplotlib Practice Questions | Matplotlib Practice Questions | Matplotlib Practice Questions | Matplotlib Practice Questions | Matplotlib Practice Questions | Matplotlib Practice Questions | Matplotlib Practice Questions |Top Colleges in India :
Indian Institute of Technology Bombay | Jaypee University of Engineering and Technology - Guna |