Getting movie properties using python and opencv

I use OpenCV to do some calculations in films that I did in my experiments. To do this, I need some properties from the films, and it would be convenient if I could automatically detect them from the film itself. In the documentation, I found the following code:

cv2.VideoCapture.get(propId) → retval 

The list below indicates that for the total number of frames, propId must be CV_CAP_PROP_FRAME_WIDTH . However, when I try to do 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 enter 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 an easier way to do this, using the class itself and writing a dictionary for it with all combinations of keys and integers.

+5
source share
3 answers

CV_CAP_PROP_* 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 cv , so it is generally recommended that you look in cv2.cv if you cannot find what you are looking for in cv2 . For example, some constants, such as cv2.CV_LOAD_IMAGE_* , have been moved.

UPDATE : - For OpenCV 3.1 use: -

 cap.get(cv2.CAP_PROP_FRAME_COUNT) 

Basically, the property name has been changed, and "CV_" at the beginning is no longer required. (Loans to Blanc in the answer section)

+10
source

I am using OpenCV 3.1 and the above methods suggested by Hannes do not work for me. It seems that the method and formatting of property 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' since OpenCV 3.1. The following minor code adjustment worked for me: cap.get(cv2.CAP_PROP_FRAME_WIDTH)

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

+3
source

You can do it as follows:

 cap.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH) 
0
source

Source: https://habr.com/ru/post/1210654/


All Articles