opencv `cv2` python 模块中缺少 CAP_PROP_FRAME_COUNT 常量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26559179/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:39:46  来源:igfitidea点击:

CAP_PROP_FRAME_COUNT constant is missing in opencv `cv2` python module

pythonopencvvideo-captureattributeerror

提问by ProGM

How to access to CAP_PROP_FRAME_COUNTfrom opencv in python? I tried this:

如何CAP_PROP_FRAME_COUNT在python中从opencv访问?我试过这个:

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.5and OpenCV 2.4.9.

我正在使用python 2.7.5OpenCV 2.4.9

采纳答案by David Zwicker

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

第一版 OpenCV python 模块中的常量有一个CV_前缀。因此,您可以使用cv.CV_CAP_PROP_FRAME_COUNTcv2.cv.CV_CAP_PROP_FRAME_COUNT

回答by 1kkwrds

import cv2

import cv2.cv as cv

Using cv2:

使用 cv2:

stream = cv2.VideoCapture(filename)

print stream.get(cv.CV_CAP_PROP_FRAME_COUNT)

回答by extensa5620

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

在 OSX 上运行 macports (opencv @3.0.0_1+python27+tbb)

You can get CAP_PROP_FRAME_HEIGHT and CAP_PROP_FRAME_WIDTH with the following:

您可以通过以下方式获得 CAP_PROP_FRAME_HEIGHT 和 CAP_PROP_FRAME_WIDTH:

#!/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)

回答by Qin Heyang

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

在 OpenCV 2.x 中,这些属性以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.

在 OpenCV 3.x和 OpenCV 4.x 中,这些属性的名称不带CV_...like CAP_PROP_FRAME_COUNT