Python安装使用VideoCapture类

Python使用摄像头需要使用VideoCapture类,本机使用的是Python2.7.14,测试的时候使用pip install的时候无法成功,会有如下返回:

Python安装使用VideoCapture类_第1张图片

这时候可以使用:(https://www.lfd.uci.edu/~gohlke/pythonlibs/)

打开地址之后直接Ctrl+f 搜索VideoCapture然后找到对应系统的文件,点击下载,会下载一个whl后缀的文件。然后在使用pip install 文件地址+下载的文件名的格式安装VideoCapture,比如我下载的文件名是VideoCapture-0.9.5-cp27-none-win_amd64.whl,所放在的文件目录是D:\Download,所以我的命令是

pip install D:\Download\VideoCapture-0.9.5-cp27-none-win_amd64.whl

Python安装使用VideoCapture类_第2张图片
就可以正确安装了。
安装完成之后开始我们的第一个小程序。


from VideoCapture import Device

cam = Device()
cam.saveSnapshot('1.jpg')

执行后竟然有报错

Python安装使用VideoCapture类_第3张图片

从Traceback上看init.py中调用了fromstring这个方法,但是这个方法直接返回

def fromstring(*args, **kw):
    raise NotImplementedError("fromstring() has been removed. " +
                              "Please call frombytes() instead.")

所以也就是调用了就会返回错误,只能修改VideoCapture中的init.py提示错误的语句:

im = Image.fromstring(
                'RGB', (width, height), buffer, 'raw', 'BGR', 0, -1)

将 fromstring修改为frombytes,然后在去运行程序,正确返回,生成图片,搞定!!

你可能感兴趣的:(Python)