python开启笔记本摄像头拍照

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MC数据局

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值