如何在 Python 中访问我的网络摄像头? [英] How do I access my webcam in Python?
问题描述
我想从 Python 访问我的网络摄像头.
I would like to access my webcam from Python.
我尝试使用 VideoCapture 扩展 (教程),但这对我来说效果不佳,我不得不解决一些问题,例如分辨率 >320x230 时有点慢,有时它会返回无
没有明显的原因.
I tried using the VideoCapture extension (tutorial), but that didn't work very well for me, I had to work around some problems such as it's a bit slow with resolutions >320x230, and sometimes it returns None
for no apparent reason.
有没有更好的方法可以从 Python 访问我的网络摄像头?
Is there a better way to access my webcam from Python?
推荐答案
OpenCV 支持从网络摄像头获取数据,默认自带 Python 封装,你还需要安装 numpy
OpenCV Python 扩展(称为 cv2
)才能工作.截至 2019 年,您可以使用 pip 安装这两个库:pip install numpy
pip install opencv-python
OpenCV has support for getting data from a webcam, and it comes with Python wrappers by default, you also need to install numpy
for the OpenCV Python extension (called cv2
) to work.
As of 2019, you can install both of these libraries with pip:
pip install numpy
pip install opencv-python
从使用opencv和python显示网络摄像头提要复制的示例:
import cv2
cv2.namedWindow("preview")
vc = cv2.VideoCapture(0)
if vc.isOpened(): # try to get the first frame
rval, frame = vc.read()
else:
rval = False
while rval:
cv2.imshow("preview", frame)
rval, frame = vc.read()
key = cv2.waitKey(20)
if key == 27: # exit on ESC
break
vc.release()
cv2.destroyWindow("preview")
这篇关于如何在 Python 中访问我的网络摄像头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!