Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting movie properties with python and opencv

Tags:

python

opencv

I'm using OpenCV to do some calculations on movies I made in experiments. To do this I need some properties from the movies and it would be handy if I could automaticly detect them from the movie itself. In the documentation I find the following code:

cv2.VideoCapture.get(propId) → retval

In the list below it states that for the total number of frames propId should be CV_CAP_PROP_FRAME_WIDTH. However when I try the following I get an error:

>> cap = cv2.VideoCapture('runoff.MOV')
>> print cap.get('CV_CAP_PROP_FRAME_WIDTH')
TypeError: an integer is required

If I input an integer in the code:

>> cap = cv2.VideoCapture('runoff.MOV')
>> print cap.get(3)
1920.0

CV_CAP_PROP_FRAME_WIDTH is the 4th item in the list in the documentation and indeed when I use the correct integer counter 3 I get this property. I wonder if there is a neater way to do this, making use of the class itself and writing a dictionary for it with all key, integer combinations.

like image 939
tvandenbrande Avatar asked Jan 08 '15 10:01

tvandenbrande


People also ask

Can OpenCV capture video?

OpenCV also allows us to save that operated video for further usage. For saving images, we use cv2. imwrite() which saves the image to a specified file location. But, for saving a recorded video, we create a Video Writer object.

What is cv2 destroyAllWindows ()?

Python Opencv destroyAllWindows() function allows users to destroy or close all windows at any time after exiting the script. If you have multiple windows open at the same time and you want to close then you would use this function.

Is OpenCV same as OpenCV Python?

OpenCV-Python is a Python wrapper for the original OpenCV C++ implementation. OpenCV-Python makes use of Numpy, which is a highly optimized library for numerical operations with a MATLAB-style syntax. All the OpenCV array structures are converted to and from Numpy arrays.


2 Answers

The CV_CAP_PROP_* constants can be accessed from the cv2.cv module:

cap.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT)

Unfortunately, not all useful things have been ported from cv2 from cv so it is generally a good idea to look in cv2.cv if you can't find what you are looking for in cv2. Some constants, like cv2.CV_LOAD_IMAGE_* have been moved, for example.

UPDATE:- For OpenCV 3.1 use:-

cap.get(cv2.CAP_PROP_FRAME_COUNT)

Basically, the property name has been modified and the "CV_" in the beginning is no longer required. (Credits to Blane in the answers section)

like image 195
Hannes Ovrén Avatar answered Sep 19 '22 08:09

Hannes Ovrén


I am using OpenCV 3.1 and the above methods suggested by Hannes do not work for me. It seems that the method call and name formatting of properties have been slightly updated for OpenCV 3.1. For example, cap.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH) returns AttributeError: 'module' object has no attribute 'cv' with OpenCV 3.1. The following minor adjustment to the code worked for me: cap.get(cv2.CAP_PROP_FRAME_WIDTH)

Note that CV_ is no longer necessary as a prefix for the attribute name.

like image 25
Blane Avatar answered Sep 18 '22 08:09

Blane