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 :
Blockchain Training in Indore | Data Science Training in Indore | React JS Training in Indore | Data Analytics Training in Indore | Python Training in Indore |Free Courses and Resource :
Dart | OpenCV Tutorials | Projects | Interview Questions | Python Data Structures and Algorithms | Aptitude Tests | Verbal Aptitude | Matplotlib Tutorials | Examples | Interview Questions | HackerRank Python | Pandas Tutorials | Projects | Interview Questions | Rust Tutorials | Projects | Interview Questions | ExpressJS Tutorials | Projects | Interview Questions | Django | MongoDB Tutorials | Examples | Interview Questions | HackerRank C Program Solutions | Python Tutorials by CodersDaily | React.js Tutorials | Golang Tutorials | Projects | Interview Questions | MS Sql Server Tutorials | Examples | Interview Questions | Verbal Ability Tutorial | Numpy Tutorials | Projects | Interview Questions | Power BI Tutorials | Projects | Interview Questions | Example Dashboards | Tensor Flow | HackerRank C++ Solutions | Django REST Framework Tutorial | CodeChef Python Solutions | Leetcode Python Solutions | Reasoning Ability Tutorial | Quantitative Ability Tutorial | C++ Tutorials | HackerRank Java Solutions | TCS NQT Mock Test Series | Verbal Aptitude 2 | HackerRank DSA Solutions | HackerRank SQL Solutions | Javascript |Interview Questions :
Pandas Tutorials | Projects | Interview Questions | ExpressJS Tutorials | Projects | Interview Questions | Django | Python Tutorials by CodersDaily | React.js Tutorials | Golang Tutorials | Projects | Interview Questions | Numpy Tutorials | Projects | Interview Questions | Django REST Framework Tutorial |Top Colleges in India :
Indian Institute of Technology Bombay | Jaypee University of Engineering and Technology - Guna |