热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

使用python和opencv获取电影属性-Gettingmoviepropertieswithpythonandopencv

ImusingOpenCVtodosomecalculationsonmoviesImadeinexperiments.TodothisIneedsomepro

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:

我正在使用OpenCV对我在实验中制作的电影进行一些计算。要做到这一点,我需要电影中的一些属性,如果我可以从电影本身自动检测它们,它会很方便。在文档中,我找到以下代码:

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:

在下面的列表中,它指出,对于总帧数,propId应为CV_CAP_PROP_FRAME_WIDTH。但是当我尝试以下操作时出现错误:

>> 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.

CV_CAP_PROP_FRAME_WIDTH是文档列表中的第4项,实际上当我使用正确的整数计数器3时,我得到了这个属性。我想知道是否有更简洁的方法来实现这一点,利用类本身并使用所有键,整数组合为它编写字典。

3 个解决方案

#1


10  

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

可以从cv2.cv模块访问CV_CAP_PROP_ *常量:

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.

不幸的是,并非所有有用的东西都是从cv从cv移植的,所以如果你在cv2中找不到你想要的东西,通常最好查看cv2.cv。例如,某些常量,例如cv2.CV_LOAD_IMAGE_ *已被移动。

UPDATE:- For OpenCV 3.1 use:-

更新: - 对于OpenCV 3.1使用: -

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)

基本上,属性名称已被修改,并且不再需要开头的“CV_”。 (答案部分给Blane的学分)

#2


3  

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)

我正在使用OpenCV 3.1,Hannes建议的上述方法对我不起作用。似乎OpenCV 3.1稍微更新了属性的方法调用和名称格式。例如,cap.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)返回AttributeError:'module'对象没有OpenCV 3.1的属性'cv'。以下对代码的微小调整对我有用:cap.get(cv2.CAP_PROP_FRAME_WIDTH)

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

请注意,不再需要CV_作为属性名称的前缀。

#3


0  

You can do it like this:

你可以这样做:

cap.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)

推荐阅读
author-avatar
宝丁2502907973
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有