Table of Contents Show
Hi guys! Happy New Year. It’s been a long since the last topic. Hope you enjoyed the vacation. Here is the “Video to image conversion” topic.
In this tutorial, we will learn how to convert a video frame to an image. We will also look at some of the limitations of this method as well as some possible solutions.
The tutorial is divided into three parts:
1) Converting a video frame to an image using Python and OpenCV
2) Limitations of the video-to-image conversion process
3) Possible solutions for some of these limitations.
So today we will see how we can convert images from video frame by frame using Python and OpenCV Library in a very simple way for the beginner level.
Python:
Python is an interpreted high-level general-purpose programming language. Its design philosophy emphasizes code readability with its use of significant indentation. Its language constructs as well as its object-oriented approach aim to help programmers write clear, logical code for small and large-scale projects.
It is a general-purpose language, which means it’s designed to be used in a range of applications, including artificial intelligence, IoT, data science, software and web development, automation, and generally getting stuff done. Python uses many libraries to run a project as per the nature of the application or project.
OpenCV:
OpenCV is a library of programming functions mainly aimed at real-time computer vision. OpenCV library is developed by Intel, it was later supported by Willow Garage and then Itseez.
OpenCV is an image processing and computer vision application library. It is open source and is available in C, C++, Java, etc. OpenCV is commonly used in research as well as day-to-day applications.
Now let’s get started with the project:
Step 1:
Download Python here
Install Python
Step 2:
Create a directory “videoimage”
Step 3:
Install OpenCV
pip install opencv-python
Step 4:
Create a python file inside the “videoimage” directory
convert.py
Create a new directory “images” inside the “videoimage” directory, where our images will store.
We have an mp4 video in the “videoimage” directory
Copy and paste the below code:
import cv2
vidcap = cv2.VideoCapture('video.mp4')
def getFrame(sec):
vidcap.set(cv2.CAP_PROP_POS_MSEC,sec*1000)
hasFrames,image = vidcap.read()
if hasFrames:
cv2.imwrite("./images/image"+str(count)+".jpg", image)
return hasFrames
sec = 0
frameRate = 0.5
count=1
success = getFrame(sec)
while success:
count = count + 1
sec = sec + frameRate
sec = round(sec, 2)
success = getFrame(sec)
That’s it. Our code is ready now run the code in the terminal:
python .\convert.py
Check the images folder:
Let’s see the comparison: