How to Display a Video File in Python using OpenCV



Python


In this article, we show how to display a video file in Python using the OpenCV module.

OpenCV allow us to perform a number of image and video functions.

Using OpenCV, we can display a video file and watch it just like any other video.

But it's not as straightforward as simply a play() function.

This is because OpenCV isn't really meant for humans. It's a library that's chiefly meant for computational analysis on images (videos are streams of images). Therefore, when using the cv2.imshow() function to show a video in OpenCV, it will go through a video very quickly, because computers can analyze videos much quicker than we can watch them. This is a good thing for computers to analyze videos, because it can do so very fast. However, to be watchable by humans requires us to slow these speed down considerably.

Therefore, even though we can play a video with the cv2.imshow() function, we have to add additional functionality to allow the video to be watchable for a human.

So this is what we do in this code.

We slow down the video using a time delay so that we can make it watchable to a human.

This is shown in the following code below.



Let's now go over this code.

First, we import the cv2 module and the time module. The time module is to create a delay to slow down the video to make it viewable by a human.

Next, we create a variable, cap. We assign this variable to, cv2.VideoCapture('yoga.mp4). This allows us to open up the video in the current working directory named yoga.mp4. We can now use OpenCV functions to analyze this video.

One of the most important pieces of information to get from this video in order to make it human viewable is its frames per second. The frames per second tells us how many frames (or images) there are per every second of this video. The frames per second determines the speed at which we show the video.

When playing a video using OpenCV, we want to make sure that the video actually exists and can be found. If not, we have an error message. We check this with the isOpened() function. If it's false, then the file doesn't exist.

In the next line, we check if the video can be opened. If it can, we read in the video file.

The ret variable checks to see if frames are still being returned. If htey are, we now add a time delay to the video file. We do this using the time.sleep() function. Using the function, time (period)= 1/frequency, we can calculate the time that makes this human viewable. We create this delay in time.

Next, we show frames using the cv2.imshow() function.

Next, we create code simply so that we can break out of the video file any time we want using the 'q' button.

We have another else statement there that closes the video when there are no more frames to show, meaning the video ended.

We then stop the reading of the video and destroy all windows.

So the key to displaying a video that can be watched by a human in normal speed in Python using the OpenCV function is to determine the frames per second of the video and then set a time delay so that the video is slowed down based on this frames per second value.

We then can view a video with normal speed when there arises a need to review a video with human eyes. Again, computers don't need this slow speed, as they can do calculations much faster than we can watch.

How to Display A Video in Grayscale with OpenCV

Just to add in an additional modification to show the versatility of OpenCV, we make the video in grayscale. It just shows you some of the functionality that OpenCV has that creates modifications to your videos, if needed.

The code to make a human viewable video in grayscale is shown below.



We create a variable, gray, which we set equal to, cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

This converts the color to grayscale.

We then show the video using the cv2.imshow() function.

This code specifically makes the video human viewable by adding a time delay. With the time delay removed, the video is in grayscale but will be at a much faster speed. If not needed to be watched but simply analyzed by a computer, no time delay is necessary but would be disadvantageous due to analysis at a slower speed.

So something like adding grayscale to a video shows the versality of OpenCV to not simply allow you to watch a video but to do so in alternative colors.

And this is how we can display a video in Python using the OpenCV module.


Related Resources

How to Draw a Rectangle in Python using OpenCV

How to Draw a Circle in Python using OpenCV

How to Draw a Line in Python using OpenCV

How to Add Text to an Image in Python using OpenCV

How to Display an OpenCV image in Python with Matplotlib

How to Use Callback functions to Connect Images to Events in Python using OpenCV

How to Check for Multiple Events in Python using OpenCV



HTML Comment Box is loading comments...