Python使用摄像头需要使用VideoCapture类,测试的时候使用pip install的时候无法成功
这时候可以使用:(https://www.lfd.uci.edu/~gohlke/pythonlibs/)
打开地址之后直接Ctrl+f 搜索VideoCapture然后找到对应系统的文件,点击下载,会下载一个whl后缀的文件。然后在使用pip install 文件地址+下载的文件名的格式安装VideoCapture,比如我下载的文件名是VideoCapture-0.9.5-cp36-cp36m-win_amd64.whl,命令是
pip install VideoCapture-0.9.5-cp36-cp36m-win_amd64.whl
就可以正确安装了。
安装完成之后开始我们的第一个小程序。
# -*- coding: utf-8 -*- from threading import Timer import time from VideoCapture import Device cam = Device() def getImage(): timestr=time.strftime("%Y%m%d%H%M%S",time.localtime()) filename='./image'+timestr+'.jpg' global t t=Timer(2,getImage) cam.saveSnapshot(filename) print('savetime:',time.strftime("%Y%m%d%H%M%S",time.localtime())) t.start() print('runtime:',time.strftime("%Y%m%d%H%M%S",time.localtime())) t=Timer(5,getImage()) t.start() print('starttime:',time.strftime("%Y%m%d%H%M%S",time.localtime())) time.sleep(10) t.cancel()
会出问题,
从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,然后在去运行程序,正确返回,生成图片,但还是有个线程上的问题,不过图片已经生成了。
问题如下:
Exception in thread Thread-2:
Traceback (most recent call last):
File "C:\ProgramData\Anaconda3\lib\threading.py", line 916, in _bootstrap_inner
self.run()
File "C:\ProgramData\Anaconda3\lib\threading.py", line 1182, in run
self.function(*self.args, **self.kwargs)
TypeError: 'NoneType' object is not callable