网络摄像头在 opencv-python 和 opencv-contrib-python 的 pip 版本中不起作用
问题描述
我一直在使用 anaconda 在 python 中的 opencv 中遇到网络摄像头问题.
I've been facing a problem with the webcam in opencv in python using anaconda.
问题如下:如果通过以下任何一种方式安装了 opencv,我将无法打开网络摄像头:pip install opencv-python
(可用 3.1、3.2、3.3),或pip install opencv-contrib-python
(可用 3.2、3.3)
The problem is the following:
I cannot open the webcam if opencv is installed via any of the two:
pip install opencv-python
(available 3.1,3.2,3.3), or
pip install opencv-contrib-python
(available 3.2,3.3)
但是当且仅当我从
安装它时它才会起作用conda install opencv
(可用opencv 3.1)
However it will work if and only if I install it from
conda install opencv
(available opencv 3.1)
但是,opencv 的某些功能仅在较新的版本中提供,或者在当前无法通过 conda 提供的 contrib 版本中提供.有谁知道为什么 pip 版本不起作用?
However, some functionality of opencv is only given in newer versions or in the contrib version currently not available via conda. Does anyone have an idea why the pip versions won't work?
更新 => pypi 上的 Pip 包未链接到 FFMPEG for linux: conda install -c conda-forge opencv
提供 3.3 但没有 contrib
=> 如果需要其他版本,则需要从源代码构建
Update => The Pip Packgage on pypi is not linked against FFMPEG for linux:
conda install -c conda-forge opencv
provides 3.3 but without contrib
=> if other version is needed building from source is required
这里是使用网络摄像头的代码:(是的,我尝试了各种建议
Here the code used to use the webcam: (Yes I tried various variations suggested
import cv2
print (cv2.__version__)
camera = cv2.VideoCapture(0) #tried -1,0,1,...
if camera.isOpened(): # isOpened is always False for pip version
print ("successfully opened Webcam")
else:
print ("Webcam error")
success,img = camera.read() # success is always False for pip version
print (success,img.shape)
camera.release()
<小时>
我尝试了各种网络摄像头,并按照网络上的建议对代码进行了各种更改.
I tried various webcams, and various alternations to the code as suggested on the web.
解决方案
pip
包中没有链接底层库.这是使软件包更普遍可安装的设计选择.
The underlying libraries are not linked in the pip
package. This was a design choice for the package to be more universally installable.
来自 Python 包索引 (PyPI) opencv-python 包的文档:
From the Python Package Index (PyPI) documentation for the opencv-python package:
问:为什么我无法在 GNU/Linux 发行版 X 或 macOS 上打开视频文件?
答:OpenCV 视频 I/O 严重依赖 FFmpeg.许多 linux 和 macOS OpenCV 二进制文件不是针对它编译的.这些包的目的是为 OpenCV Python 绑定提供尽可能简单的安装体验,它们应该直接开箱即用.在没有通用"FFmpeg 构建(例如,像 Windows 轮子中的 LGPL 许可构建)的情况下,将 FFmpeg 作为附加依赖项添加,目标很难实现.这在未来可能会改变.
A: OpenCV video I/O depends heavily on FFmpeg. Manylinux and macOS OpenCV binaries are not compiled against it. The purpose of these packages is to provide as easy as possible installation experience for OpenCV Python bindings and they should work directly out-of-the-box. Adding FFmpeg as an additional dependency without a "universal" FFmpeg build (e.g. LGPL licensed build like in the Windows wheels) the goal is considerably harder to achieve. This might change in the future.
如果您想使用较新的版本并且没有这些问题,您可以从源代码编译 OpenCV.有很多这样的教程.OpenCV 有一个 Python 特定教程,用于 在 Windows 上编译 和 在 Fedora 上.此外,PyImageSearch 有大量关于在 macOS 和 Linux 上从源代码编译 OpenCV 的博文:Ubuntu 16.04, Ubuntu 14.04, macOS, macOS via Homebrew(附带 疑难解答文章),以及许多其他 Linux 风格(例如 Raspbian)和旧 Python 版本的帖子;如果这不包括您,请在网上搜索.
You could compile OpenCV from source if you want to use the newer versions and not have these problems. There's a number of such tutorials. OpenCV has a Python specific tutorial for compiling on Windows and on Fedora. Additionally, PyImageSearch has a very popular number of blog posts on compiling OpenCV from source on macOS and Linux: Ubuntu 16.04, Ubuntu 14.04, macOS, macOS via Homebrew (with an accompanying troubleshooting article), along with a number of other Linux flavors (e.g. Raspbian) and posts for older Python versions; just search the web if that doesn't cover you.
相关文章