Python cv2.VideoCapture.open() 总是返回 FALSE
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/21792909/
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
cv2.VideoCapture.open() always returns FALSE
提问by user26641
I am trying to access a Logitech c310 webcam on my beaglebone. It always returns false for any device ID, I am not sure why.
我正在尝试访问我的 beaglebone 上的 Logitech c310 网络摄像头。对于任何设备 ID,它总是返回 false,我不知道为什么。
I use the following code.
我使用以下代码。
>>> import cv2, numpy as np
>>> cam = cv2.VideoCapture(0)
>>> cam.open(0)
False
The camera does show up as video0 in dev/ and also in root@arm:~#lsusb, like below,
相机确实在 dev/ 和 中显示为 video0 root@arm:~#lsusb,如下所示,
root@arm:~# lsusb
Bus 001 Device 002: ID 046d:081b Logitech, Inc. Webcam C310
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
I could also access  the camera with v4l2-ctl. 
Note that I am very new to OpenCV, so this may sound silly and I apologize for that in advance.
我也可以使用v4l2-ctl. 请注意,我对 OpenCV 非常陌生,所以这可能听起来很傻,我提前为此道歉。
回答by kaushik94
HIGHGUI ERROR: V4L: index 1 is not correct!
False
OpenCV Error: Bad flag (parameter or structure field) (Unrecognized or unsupported array  type) in cvGetMat, file /home/kaushik/Desktop/OpenCV-2.4.1/modules/core/src/array.cpp, line 2482
Traceback (most recent call last):
File "x2.py", line 8, in <module>
cv2.imshow('frame', frame)
cv2.error: /home/kaushik/Desktop/OpenCV-2.4.1/modules/core/src/array.cpp:2482: error: (-206) Unrecognized or unsupported array type in function cvGetMat
if you are getting this kind of an error then probably something is wrong with the indexing.
如果您收到此类错误,则索引可能有问题。
instead of cv2.VideoCapture(0) add:
而不是 cv2.VideoCapture(0) 添加:
cv2.VideoCapture(-1)
this will get you the first working camera. And if anything goes wrong, just post the stack trace here and i'll see if i can help you :)
这将为您提供第一台工作相机。如果出现任何问题,只需在此处发布堆栈跟踪,我会看看是否可以帮助您:)
回答by user26641
It is a matter of missing packages. I had installed a bunch of packages after installing OpenCV. I ran the cmake again and it worked.
这是缺少包的问题。安装 OpenCV 后,我安装了一堆软件包。我再次运行了 cmake 并且它起作用了。
回答by user26641
video related functionality is not supported (not compiled with FFmpeg), if cv2 was installed from pypi wheel:
如果 cv2 是从 pypi wheel 安装的,则不支持视频相关功能(未使用 FFmpeg 编译):
pip install opencv-python
回答by Song
If you didn't solve it in many ways,you can try to
find and install "opencv3.2.0-dev",I use it to solve
the problem twice.
just pip install opencv-python is not enough.(hmm,
at least sometimes)
如果很多方法都没有解决,你可以尝试
找到并安装“opencv3.2.0-dev”,我用它解决
了两次问题。
仅 pip install opencv-python 是不够的。(嗯,
至少有时)
回答by Dragon
It depends on the argument being passed to the cv2.VideoCapture().
这取决于传递给 cv2.VideoCapture() 的参数。
Normally, it is 0 for making the primary webcam of your pc work. Similarly, if you want to get access to a second camera installed on you system pass the argument as 2.
通常,它是 0 使您的电脑的主网络摄像头工作。同样,如果您想访问系统上安装的第二个摄像头,请将参数传递为 2。
But if you have only 1 camera and indexing '0' is not helping, then try passing the index as -1 instead.
但是,如果您只有 1 个相机并且索引 '0' 没有帮助,那么请尝试将索引传递为 -1。
回答by Ashish Gupta
I found something in documentation which might just help.
我在文档中发现了一些可能会有所帮助的内容。
cap.read() returns a bool (True/False). If frame is read correctly, it will be True. Sometimes, cap may not have initialized the capture. In that case, the code shows error. You can check whether it is initialized or not by the method cap.isOpened(). If it is True, OK. Otherwise open it using cap.open().
cap.read() 返回一个布尔值(真/假)。如果正确读取帧,则为 True。有时, cap 可能没有初始化捕获。在这种情况下,代码显示错误。您可以通过 cap.isOpened() 方法检查它是否已初始化。如果是真的,好的。否则使用 cap.open() 打开它。
来源:http: //opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_gui/py_video_display/py_video_display.html#display-video
回答by Mingjiang Shi
I met the similar issue. It may be related to user permission. Try using the follow procedure to diagnose the issue.
我遇到了类似的问题。可能与用户权限有关。尝试使用以下过程来诊断问题。
- Run the following command to determine the camera access permission - ls -la /dev/video*- You might get similar output as below (you might get video1 if you have multiple cameras). As you can see, only - rootuser and users in- videogroup have the permission to access the camera.- crw-rw----. 1 root video 188, 0 Apr 3 21:16 /dev/video0
- So the fix is simple, add your user account to the - videogroup, using the follow command:- sudo usermod -a -G video <you login name>
- 运行以下命令确定摄像头访问权限 - ls -la /dev/video*- 您可能会得到如下类似的输出(如果您有多个摄像头,您可能会得到 video1)。如您所见,只有 - root用户和- video组中的用户才有访问摄像机的权限。- crw-rw----. 1 root video 188, 0 Apr 3 21:16 /dev/video0
- 所以修复很简单 - video,使用以下命令将您的用户帐户添加到组中:- sudo usermod -a -G video <you login name>
Hope it helps!
希望能帮助到你!
回答by Palki
For me the solution was to restart the computer, seems that there was mismanagement in releasing the camera. I don't like it though bc seems that is not a definitive solution. But it might be that it was a problem with jupyter notebook, now I'm working with spyder bc I read that jupyter is more prone to problems.
对我来说,解决办法是重新启动计算机,似乎在释放相机时管理不善。我不喜欢它,尽管 bc 似乎不是一个明确的解决方案。但这可能是 jupyter notebook 的问题,现在我正在使用 spyder bc 我读到 jupyter 更容易出现问题。

