简体   繁体   English

如何访问 Python 中的网络摄像头?

[英]How do I access my webcam in Python?

I would like to access my webcam from Python.我想从 Python 访问我的网络摄像头。

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.我尝试使用VideoCapture扩展( tutorial ),但这对我来说效果不是很好,我不得不解决一些问题,例如分辨率 >320x230 时它有点慢,有时它会无缘无故地返回None

Is there a better way to access my webcam from Python?有没有更好的方法从 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. OpenCV 支持从网络摄像头获取数据,默认情况下带有 Python 包装器,您还需要安装numpy才能使 OpenCV Python 扩展名(称为cv2 )工作。 As of 2019, you can install both of these libraries with pip: pip install numpy pip install opencv-python截至 2019 年,您可以使用 pip 安装这两个库: pip install numpy pip install opencv-python

More information on using OpenCV with Python . 有关将 OpenCV 与 Python 结合使用的更多信息

An example copied from Displaying webcam feed using opencv and python :使用 opencv 和 python 从 Displaying webcam feed复制的示例:

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")

I would like to access my webcam from Python.我想从 Python 访问我的网络摄像头。

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.我尝试使用VideoCapture扩展(教程),但这对我来说效果不佳,我不得不解决一些问题,例如分辨率 >320x230 时有点慢,有时它会无缘无故地返回None

Is there a better way to access my webcam from Python?有没有更好的方法可以从 Python 访问我的网络摄像头?

gstreamer can handle webcam input. gstreamer 可以处理网络摄像头输入。 If I remeber well, there are python bindings for it!如果我没记错的话,有python个绑定!

import cv2 as cv

capture = cv.VideoCapture(0)

while True:
    isTrue,frame = capture.read()
    cv.imshow('Video',frame)
    if cv.waitKey(20) & 0xFF==ord('d'):
        break

capture.release()
cv.destroyAllWindows()

0 <-- refers to the camera , replace it with file path to read a video file 0 <-- 指摄像头,替换为文件路径读取视频文件

cv.waitKey(20) & 0xFF==ord('d') <-- to destroy window when key is pressed cv.waitKey(20) & 0xFF==ord('d') <-- 按下键时销毁窗口

The only one I've used is VideoCapture, which you've already mentioned you don't like (although I had no problems with it; what bugs did you encounter?) 我唯一使用的是VideoCapture,你已经提到过你不喜欢它(尽管我没有遇到任何问题;你遇到了什么错误?)

I was unable to find any alternatives in the past or now, so you might be stuck either using VideoCapture, or finding a nice C library and writing a Python wrapper for it (which might be more work than you're willing to put into it). 我在过去或现在都找不到任何替代方案,所以你可能会被困在使用VideoCapture,或者找到一个不错的C库并为它编写一个Python包装器(这可能比你愿意放入它的工作更多) )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 HTML 和 Javascript 的帮助下从浏览器使用 python (Django) 访问我的网络摄像头? - How do i access my webcam with python (Django) from the browser with the help of HTML and Javascript? 我如何知道我的网络摄像头当前是否正常工作? python、opencv - How do I know if my webcam is currently working? python, opencv 当我的 python 脚本在服务器上运行时,如何访问我的本地网络摄像头? (实时人脸识别) - How do I access my local webcam while my python script is running on a server? (Face-Recognition in real time) 如何使用我的网页访问客户端网络摄像头以进行实时 ML 预测? - How do i access client webcam using my webpage for realtime ML predictions? 如何使用 python 访问笔记本电脑的内置红外网络摄像头? - How can I access my laptop's built-in infrared webcam using python? 如何在 Open CV Python 中缩放我的网络摄像头? - How can I zoom my webcam in Open CV Python? 如何在Python中使用网络摄像头捕获图像? - How do I capture images using webcam in Python? 如何访问函数python中的字符串? - How do I access the string inside my function python? 如何使用 python 提供我的 access_token - How do I provide my access_token with python 如何访问数组中的“隐藏”索引? (Python) - How do I access the 'hidden' index in my array? (Python)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM