简体   繁体   English

OpenCV Python无法打开视频文件

[英]OpenCV Python unable to open videofile

I am using Windows 10 and the 2.7.12 32-bit version of Python and Opencv 3.1.0. 我正在使用Windows 10和Python和Opencv 3.1.0的2.7.12 32位版本。 Below is the code where it goes wrong: 以下是出错的代码:

vcap = cv2.VideoCapture("StraightAhead.avi")
if not vcap.isOpened():
    print "File Cannot be Opened"

First off, all of this code works when using webcam (so VideoCapture(0) ). 首先,所有这些代码在使用网络摄像头时都适用(因此VideoCapture(0) )。

I have tried a couple of things. 我尝试了几件事。 First off, it was originally an mp4 so I converted it to avi. 首先,它最初是mp4,因此我将其转换为avi。 I tried a file (.webm) from a friend who is using Linux with python and opencv which worked for him but not for me. 我尝试了一个朋友的文件(.webm),该朋友正在将Linux与python和opencv一起使用,该文件对他有用,但对我不起作用。

I checked to see if I could move the DLL files from opencv ffmpeg to Python but there are no DLL files for me there. 我检查了一下是否可以将DLL文件从opencv ffmpeg移至Python,但是那里没有适合我的DLL文件。 I checked a lot of other questions but most of them give solutions for Linux. 我检查了许多其他问题,但大多数问题都为Linux提供了解决方案。

The most relevant question doesn't have any answers or comments so I finally decided to ask it here. 最相关的问题没有任何答案或评论,所以我最终决定在这里提问。 I hope someone can help. 我希望有人能帮帮忙。

Tried your code and it works on my 64-bit Win10 machine. 试过了您的代码,它可以在我的64位Win10机器上运行。 The file you need for 32-bit Windows is 'opencv_ffmpeg310.dll'. 32位Windows所需的文件是“ opencv_ffmpeg310.dll”。 Suggest you download the binary from OpenCV Download site , install it and try again. 建议您从OpenCV下载站点下载二进制文件,进行安装,然后重试。 The ffmpeg DLL is located in 'C:\\Program Files (x86)\\OpenCV 3.1.0\\x64\\vc14\\bin'. ffmpeg DLL位于'C:\\ Program Files(x86)\\ OpenCV 3.1.0 \\ x64 \\ vc14 \\ bin'中。 Also need to add the paths below if the installer doesn't add them to the windows environment. 如果安装程序未将以下路径添加到Windows环境中,则还需要添加以下路径。

OPENCV_DIR='C:\Program Files (x86)\OpenCV 3.1.0\x64\vc14' 
PATH = %PATH%;%OPENCV_DIR%\bin

Note that vc14 is compiled by Visual Studio 2015 or vc12 by VS2013. 请注意,vc14是由Visual Studio 2015编译的,而vc12是由VS2013编译的。

Hope this help. 希望能有所帮助。

You may extact the binary to C:\\ and try again. 您可以将二进制文件扩展到C:\\,然后重试。

  1. Set OPENCV_DIR = C:\\opencv\\build\\x64\\vc14 { opencv_ffmpeg310_64.dll are there } 设置OPENCV_DIR = C:\\ opencv \\ build \\ x64 \\ vc14 {opencv_ffmpeg310_64.dll在那里}
  2. Add PATH with %OPENCV_DIR%\\bin 使用%OPENCV_DIR%\\ bin添加PATH
  3. Copy C:\\opencv\\build\\python\\2.7\\x86\\cv2.pyd to %PYTHON%\\lib\\site-packages\\cv2.pyd { where %PYTHON% is the 32-bit python root directory, eg "C:\\Program File (x86)\\Python27" } 将C:\\ opencv \\ build \\ python \\ 2.7 \\ x86 \\ cv2.pyd复制到%PYTHON%\\ lib \\ site-packages \\ cv2.pyd {其中%PYTHON%是32位python根目录,例如“ C:\\程序文件(x86)\\ Python27“}

If still doesn't work, change OpenCV path to use 32-bit version of ffmpeg DLL deliberately. 如果仍然无法使用,请更改OpenCV路径以故意使用32位版本的ffmpeg DLL。

  1. Set OPENCV_DIR to C:\\opencv\\build\\ { to use 32-bit ffmpeg under bin\\ } 将OPENCV_DIR设置为C:\\ opencv \\ build \\ {以在bin下使用32位ffmpeg \\}

If problem still exists, log the Traceback message. 如果问题仍然存在,请记录回溯消息。

EDIT: Just tested the opencv-3.1.0.exe downloaded from opencv website. 编辑:刚刚测试了从opencv网站下载的opencv-3.1.0.exe。 Video file playback, used 'Wildlife.wmv' comes with 32-bit Win7, works. 使用32位Win7附带的“ Wildlife.wmv”视频文件可以正常播放。

It looks that the EXE zipped with two set of files ... The OPENCV_DIR was set to C:\\opencv\\opencv\\build\\x86\\vc12. 看起来EXE压缩了两组文件... OPENCV_DIR设置为C:\\ opencv \\ opencv \\ build \\ x86 \\ vc12。 And copied c:\\opencv\\opencv\\build\\python\\2.7\\x86\\cv2.pyd cv2.pyd to python's lib\\site-packages. 并将c:\\ opencv \\ opencv \\ build \\ python \\ 2.7 \\ x86 \\ cv2.pyd cv2.pyd复制到python的lib \\ site-packages中。 That's all. 就这样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM