14

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.

1
  • 4
    opencv3.0 will have the proper cv2.CAP_PROP_FRAME_COUNT (and also will discard the deprecated cv submodule)
    – berak
    Oct 26, 2014 at 8:59

5 Answers 5

20

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.

3
  • It seems to work, but... I'm quite bewildered. Why aren't those constants defined in the cv2 module?
    – ProGM
    Oct 26, 2014 at 5:26
  • 11
    In OpenCV 2.x, it appears that the constants are of the form cv2.cv.CV_CAP_X, while in OpenCV 3.x, they are cv2.CAP_X
    – cod3monk3y
    Nov 4, 2015 at 15:02
  • 3
    Bad API, bad OpenCV.
    – acgtyrant
    Jul 8, 2016 at 8:54
17

While running macports on OSX (opencv @3.0.0_1+python27+tbb)

You can get CAP_PROP_FRAME_HEIGHT and CAP_PROP_FRAME_WIDTH with the following:

#!/opt/local/bin/python
import cv2 
vcap = cv2.VideoCapture()
# set frame width and height
vcap.set(cv2.CAP_PROP_FRAME_WIDTH, 480)
vcap.set(cv2.CAP_PROP_FRAME_HEIGHT, 640)
vcap.open(0)
5

In OpenCV 2.x, these attributes are named starting with CV_... like CV_CAP_PROP_FRAME_COUNT.

In OpenCV 3.x and OpenCV 4.x, these attributes are named without CV_... like CAP_PROP_FRAME_COUNT.

2

http://docs.opencv.org/2.4/modules/highgui/doc/reading_and_writing_images_and_video.html#videocapture-get

#3. CV_CAP_PROP_FRAME_WIDTH
print "\t CAP_PROP_FRAME_WIDTH:     ",cap.get(3)
#4. CV_CAP_PROP_FRAME_HEIGHT
print "\t CAP_PROP_FRAME_HEIGHT:    ",cap.get(4)

http://docs.opencv.org/2.4/modules/highgui/doc/reading_and_writing_images_and_video.html#videocapture-set

#3. CV_CAP_PROP_FRAME_WIDTH
cap.set(3,320)
#4. CV_CAP_PROP_FRAME_HEIGHT
cap.set(4,240)
-1
import cv2

import cv2.cv as cv

Using cv2:

stream = cv2.VideoCapture(filename)

print stream.get(cv.CV_CAP_PROP_FRAME_COUNT)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.