如何在 Python 中访问我的网络摄像头? [英] How do I access my webcam in Python?

查看:44
本文介绍了如何在 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 封装,你还需要安装 numpyOpenCV Python 扩展(称为 cv2)才能工作.截至 2019 年,您可以使用 pip 安装这两个库:pip install numpypip 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

更多关于在 Python 中使用 OpenCV 的信息.

使用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆