CAP_PROP_FRAME_COUNT constant is missing in opencv `cv2` python module

ProGM picture ProGM · Oct 25, 2014 · Viewed 43.1k times · Source

How to access to CAP_PROP_FRAME_COUNT from opencv in python? I tried this:

import cv2
cap = cv2.VideoCapture('myvideo.avi')
frames_count, fps, width, height = cap.get(cv2.CAP_PROP_FRAME_COUNT), cap.get(cv2.CAP_PROP_FPS), cap.get(cv2.CAP_PROP_FRAME_WIDTH), cap.get(cv2.CAP_PROP_FRAME_HEIGHT)

And this:

import cv2
import cv
cap = cv2.VideoCapture('myvideo.avi')
frames_count, fps, width, height = cap.get(cv.CAP_PROP_FRAME_COUNT), cap.get(cv.CAP_PROP_FPS), cap.get(cv.CAP_PROP_FRAME_WIDTH), cap.get(cv.CAP_PROP_FRAME_HEIGHT)

and also this:

import cv2
cap = cv2.VideoCapture('myvideo.avi')
frames_count, fps, width, height = cap.get(cv2.cv.CAP_PROP_FRAME_COUNT), cap.get(cv2.cv.CAP_PROP_FPS), cap.get(cv2.cv.CAP_PROP_FRAME_WIDTH), cap.get(cv2.cv.CAP_PROP_FRAME_HEIGHT)

But I'm getting this error:

AttributeError: 'module' object has no attribute 'CAP_PROP_FRAME_COUNT'

I'm using python 2.7.5 and OpenCV 2.4.9.

Answer

David Zwicker picture David Zwicker · Oct 26, 2014

The constants in the first version of OpenCV python module have a CV_ prefix. You could thus either use cv.CV_CAP_PROP_FRAME_COUNT or cv2.cv.CV_CAP_PROP_FRAME_COUNT.