网络摄像头在 opencv-python 和 opencv-contrib-python 的 pip 版本中不起作用
webcam does not work in pip version of opencv-python and opencv-contrib-python
我在 python 使用 anaconda 的 opencv 中遇到网络摄像头问题。
问题如下:
如果通过以下两者之一安装了 opencv,我将无法打开网络摄像头:
pip install opencv-python
(可用 3.1、3.2、3.3),或
pip install opencv-contrib-python
(可用 3.2、3.3)
但是当且仅当我从
安装它时它才会工作
conda install opencv
(可用 opencv 3.1)
但是,opencv 的某些功能仅在较新版本或当前无法通过 conda 提供的 contrib 版本中提供。
有谁知道为什么 pip 版本不起作用?
更新 => pypi 上的 Pip Packgage 未链接到 FFMPEG linux:
conda install -c conda-forge opencv
提供 3.3 但没有贡献
=> 如果需要其他版本,则需要从源代码构建
这里是用于使用网络摄像头的代码:
(是的,我尝试了建议的各种变体
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()
我尝试了各种网络摄像头,并按照网络上的建议对代码进行了各种更改。
pip
包中未链接基础库。这是一个设计选择,目的是让软件包更易于安装。
来自 Python 包索引 (PyPI) documentation for the opencv-python package:
Q: Why I can’t open video files on GNU/Linux distribution X or on macOS?
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 有针对 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 的 Python 特定教程),以及许多其他 Linux 风格(例如 Raspbian)和旧 Python 版本的帖子;如果没有涵盖您,请在网上搜索。
只需卸载 opencv-contrib-python 库,然后重新安装 opencv-python 和 opencv-python-headless
以下代码为我解决了这个问题:
video = cv2.VideoCapture(0,cv2.CAP_DSHOW)
我在 python 使用 anaconda 的 opencv 中遇到网络摄像头问题。
问题如下:
如果通过以下两者之一安装了 opencv,我将无法打开网络摄像头:
pip install opencv-python
(可用 3.1、3.2、3.3),或
pip install opencv-contrib-python
(可用 3.2、3.3)
但是当且仅当我从
安装它时它才会工作
conda install opencv
(可用 opencv 3.1)
但是,opencv 的某些功能仅在较新版本或当前无法通过 conda 提供的 contrib 版本中提供。 有谁知道为什么 pip 版本不起作用?
更新 => pypi 上的 Pip Packgage 未链接到 FFMPEG linux:
conda install -c conda-forge opencv
提供 3.3 但没有贡献
=> 如果需要其他版本,则需要从源代码构建
这里是用于使用网络摄像头的代码: (是的,我尝试了建议的各种变体
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()
我尝试了各种网络摄像头,并按照网络上的建议对代码进行了各种更改。
pip
包中未链接基础库。这是一个设计选择,目的是让软件包更易于安装。
来自 Python 包索引 (PyPI) documentation for the opencv-python package:
Q: Why I can’t open video files on GNU/Linux distribution X or on macOS?
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 有针对 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 的 Python 特定教程),以及许多其他 Linux 风格(例如 Raspbian)和旧 Python 版本的帖子;如果没有涵盖您,请在网上搜索。
只需卸载 opencv-contrib-python 库,然后重新安装 opencv-python 和 opencv-python-headless
以下代码为我解决了这个问题:
video = cv2.VideoCapture(0,cv2.CAP_DSHOW)