热门标签 | 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)

推荐阅读
  • 本文将介绍如何编写一些有趣的VBScript脚本,这些脚本可以在朋友之间进行无害的恶作剧。通过简单的代码示例,帮助您了解VBScript的基本语法和功能。 ... [详细]
  • Explore a common issue encountered when implementing an OAuth 1.0a API, specifically the inability to encode null objects and how to resolve it. ... [详细]
  • 本文深入探讨了 Java 中的 Serializable 接口,解释了其实现机制、用途及注意事项,帮助开发者更好地理解和使用序列化功能。 ... [详细]
  • 本文详细介绍了Java中org.neo4j.helpers.collection.Iterators.single()方法的功能、使用场景及代码示例,帮助开发者更好地理解和应用该方法。 ... [详细]
  • 深入解析Spring Cloud Ribbon负载均衡机制
    本文详细介绍了Spring Cloud中的Ribbon组件如何实现服务调用的负载均衡。通过分析其工作原理、源码结构及配置方式,帮助读者理解Ribbon在分布式系统中的重要作用。 ... [详细]
  • 本文详细介绍了 GWT 中 PopupPanel 类的 onKeyDownPreview 方法,提供了多个代码示例及应用场景,帮助开发者更好地理解和使用该方法。 ... [详细]
  • Explore how Matterverse is redefining the metaverse experience, creating immersive and meaningful virtual environments that foster genuine connections and economic opportunities. ... [详细]
  • 本文详细介绍如何使用Python进行配置文件的读写操作,涵盖常见的配置文件格式(如INI、JSON、TOML和YAML),并提供具体的代码示例。 ... [详细]
  • 技术分享:从动态网站提取站点密钥的解决方案
    本文探讨了如何从动态网站中提取站点密钥,特别是针对验证码(reCAPTCHA)的处理方法。通过结合Selenium和requests库,提供了详细的代码示例和优化建议。 ... [详细]
  • 本文探讨了Hive中内部表和外部表的区别及其在HDFS上的路径映射,详细解释了两者的创建、加载及删除操作,并提供了查看表详细信息的方法。通过对比这两种表类型,帮助读者理解如何更好地管理和保护数据。 ... [详细]
  • 本文详细介绍了如何使用 Yii2 的 GridView 组件在列表页面实现数据的直接编辑功能。通过具体的代码示例和步骤,帮助开发者快速掌握这一实用技巧。 ... [详细]
  • 本文深入探讨 MyBatis 中动态 SQL 的使用方法,包括 if/where、trim 自定义字符串截取规则、choose 分支选择、封装查询和修改条件的 where/set 标签、批量处理的 foreach 标签以及内置参数和 bind 的用法。 ... [详细]
  • 本文详细介绍了Java中org.eclipse.ui.forms.widgets.ExpandableComposite类的addExpansionListener()方法,并提供了多个实际代码示例,帮助开发者更好地理解和使用该方法。这些示例来源于多个知名开源项目,具有很高的参考价值。 ... [详细]
  • 前言--页数多了以后需要指定到某一页(只做了功能,样式没有细调)html ... [详细]
  • XNA 3.0 游戏编程:从 XML 文件加载数据
    本文介绍如何在 XNA 3.0 游戏项目中从 XML 文件加载数据。我们将探讨如何将 XML 数据序列化为二进制文件,并通过内容管道加载到游戏中。此外,还会涉及自定义类型读取器和写入器的实现。 ... [详细]
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社区 版权所有