Quantcast
Channel: CodeSection,代码区,Python开发技术文章_教程 - CodeSec
Viewing all articles
Browse latest Browse all 9596

Count the total number of frames in a video with OpenCV and Python

$
0
0

Count the total number of frames in a video with OpenCV and Python

Today’s blog post is part of a two part series on working with video files using OpenCV and python.

The first part of this series will focus on a question emailed in by PyImageSearch reader, Alex.

Alex asks:

I need to count the total number of frames in a video file with OpenCV. The only way I’ve found to do this is to loop over each frame in the video file individually and increment a counter. Is there a faster way?

Great question Alex.

And rest assured, you’re not the first person whohas asked me this question.

In the remainder of this blog post I’ll show you how to define a function that can quickly determine the total number of frames in a video file.

Next week we’ll use this function to aid us in a fun visualization task where I’ll demonstrate how to create “movie barcodes”. In order to generate these movie barcodes we’ll first need to know how many frames there are in our input movie file.

To learn more about fast, efficient frame counting with OpenCV and Python, just keep reading.

Looking for the source code to this post?

Jump right to the downloads section. Count the total number of frames in a video with OpenCV and Python

There are two methods to determine the total number of frames in a video file using OpenCV and Python:

Method #1: The fast, efficient way using the built-in properties OpenCV provides us to access the video file meta information and return the total number of frames. Method #2: The slow, inefficient technique that requires us to manually loop over each frame and increment a counter for each frame we’ve read.

Method #1 is obviously ideal.

All we need to do is open a pointer to the video file, tell OpenCV which meta property we are interested, and get the returned value.

No looping over frames manually.

No wasted CPU cycles decoding frames.

…however, I’m sure you’ve realized there is a catch.

The problem here is that Method #1 is buggy as all hell based on your OpenCV version and video codecs installed.

You’ll find there are situations where more than half of the . get and . set methods on video pointers simply don’t work . In situations like these we’ll inevitably have to revert to Method #2.

So, is there a way to encapsulate both of these methods into a single function?

You bet there is.

I’ve already implemented the count_frames function inside the imutils library , but to ensure you’re understanding what’s going on under the hood we’ll be reviewing the entire function here today.

The easy way to count frames with OpenCV and Python

The first method to count video frames in OpenCV with Python is very fast ― it simply uses the built-in properties OpenCV provides to access a video file and read the meta information of the video.

Let’s go ahead and see how this function is implemented inside imutils now:

# import the necessary packages from ..convenienceimport is_cv3 import cv2 def count_frames(path, override=False): # grab a pointer to the video file and initialize the total # number of frames read video = cv2.VideoCapture(path) total = 0 # if the override flag is passed in, revert to the manual # method of counting frames if override: total = count_frames_manual(video)

To start, we import our necessary Python packages on Lines 2 and 3 . We’ll need the is_cv3 function to check which version of OpenCV we’re using along with cv2 for our actual OpenCV bindings.

We define the count_frames function on Line 5 . This method requires a single argument followed by a second optional one:

path : This is the path to where our video file resides on disk. override : A boolean flag used to determine if we should skip Method #1 and go directly to the slower (but guaranteed accurate/error free) Method #2.

We make a call to cv2 . VideoCapture on Line 8 to obtain a pointer to the actual video file followed by initializing the total number of frames in the video.

We then make a check on Line 13 to see if we should override . If so, we call count_frames_manual (which we’ll define in the next section).

Otherwise, let’s see how Method #1 is actually implemented:

# otherwise, let's try the fast way first else: # lets try to determine the number of frames in a video # via video properties; this method can be very buggy # and might throw an error based on your OpenCV version # or may fail entirely based on your which video codecs # you have installed try: # check if we are using OpenCV 3 if is_cv3(): total = int(video.get(cv2.CAP_PROP_FRAME_COUNT)) # otherwise, we are using OpenCV 2.4 else: total = int(video.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT)) # uh-oh, we got an error -- revert to counting manually except: total = count_frames_manual(video) # release the video file pointer video.release() # return the total number of frames in the video return total

In order to determine the number of frames in a video file via the API by provided by OpenCV, we need to utilize what are called capture properties , or what OpenCV calls CAP_PROP ― anytime you see a constant starting with CAP_PROP_* , you should know it’s related to video processing.

In OpenCV 3 the name of the frame count property is cv2 . CAP_PROP_FRAME_COUNT while in OpenCV 2.4 the property is named cv2 . cv . CV_CAP_PROP_FRAME_COUNT .

Ideally, passing the respective property name into the . get method of the video pointer will allow us to obtain the total number of frames in the video ( Lines 25-30 ).

However, there are cases where this method will fail based on your particular OpenCV install and video codecs.

If this is the case, we have wrapped ourcritical code section with a try / except block. If an exception occurs we simply revert to counting the frames manually ( Lines 33 and 34 ).

Finally, we release the video file pointer ( Line 37 ) and return the total number of frames to the calling function ( Line 40 ).

The slow way to count frames with OpenCV and Python We’ve seen the fast, efficient method to counting frames in a video ― let’s now move on to the slower method called

Viewing all articles
Browse latest Browse all 9596

Trending Articles