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访问我的网络摄像头吗?
4 个解决方案
#1
44
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.
At the time of writing (January 2015) there is no Python 3 support yet, so you need to use Python 2.
OpenCV支持从网络摄像头获取数据,默认情况下,它附带Python包装器,您还需要为OpenCV Python扩展(称为cv2)安装numpy。在编写本文时(2015年1月),还没有对Python 3的支持,因此需要使用Python 2。
More information on using OpenCV with Python.
更多关于在Python中使用OpenCV的信息。
An example copied from Displaying webcam feed using opencv and 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
cv2.destroyWindow("preview")
#2
9
This should have been a comment to @John Montgomery, but my rep does not allow me to make comments. Your answer is great, but at least on Windows, it is missing the line
这应该是对@John Montgomery的评论,但是我的代表不允许我发表评论。你的答案很好,但至少在Windows上,它没有意义
vc.release()
before
之前
cv2.destroyWindow("preview")
Without it, the camera resource is locked, and can not be captured again before the python console is killed.
没有它,摄像机资源被锁定,在python控制台被杀死之前不能再次捕获。
#3
1
gstreamer can handle webcam input. If I remeber well, there are python bindings for it!
gstreamer可以处理网络摄像机的输入。如果我记得很好,就会有python绑定!
#4
-2
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包装(这可能比您愿意投入的工作量要大)。
#1
44
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.
At the time of writing (January 2015) there is no Python 3 support yet, so you need to use Python 2.
OpenCV支持从网络摄像头获取数据,默认情况下,它附带Python包装器,您还需要为OpenCV Python扩展(称为cv2)安装numpy。在编写本文时(2015年1月),还没有对Python 3的支持,因此需要使用Python 2。
More information on using OpenCV with Python.
更多关于在Python中使用OpenCV的信息。
An example copied from Displaying webcam feed using opencv and 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
cv2.destroyWindow("preview")
#2
9
This should have been a comment to @John Montgomery, but my rep does not allow me to make comments. Your answer is great, but at least on Windows, it is missing the line
这应该是对@John Montgomery的评论,但是我的代表不允许我发表评论。你的答案很好,但至少在Windows上,它没有意义
vc.release()
before
之前
cv2.destroyWindow("preview")
Without it, the camera resource is locked, and can not be captured again before the python console is killed.
没有它,摄像机资源被锁定,在python控制台被杀死之前不能再次捕获。
#3
1
gstreamer can handle webcam input. If I remeber well, there are python bindings for it!
gstreamer可以处理网络摄像机的输入。如果我记得很好,就会有python绑定!
#4
-2
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包装(这可能比您愿意投入的工作量要大)。