[英]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.