Python|Python 调用摄像头

环境搭建
需要安装VideoCapture模块,参考这里:https://blog.csdn.net/zhangbinbinz/article/details/78658619
使用方法
Device() 【Python|Python 调用摄像头】调用一个设备
saveSnapshot() 拍下照片
举例-拍一张照片

from VideoCapture import Device cam = Device() cam.saveSnapshot('image.jpg')

举例-持续拍照并进行人脸识别 (人脸识别参考百度AI接口调用)
from VideoCapture import Device import time, os import requests import base64def get_access_token(): api_key = ''#在这里填入自己的key secret_key = '' url = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=' + str(api_key) + '&client_secret=' + str(secret_key)try: res = eval(requests.get(url).text) return res['access_token'] except: print("access_token获取失败") os.system("pause")def capture_photo(): try: cam = Device() cam.saveSnapshot('image.jpg') except: print("拍照功能出错")def search_face(access_token):face_search = 'https://aip.baidubce.com/rest/2.0/face/v3/search' + '?access_token=' + str(access_token) header = { 'Content-Type':'application/json' } data = https://www.it610.com/article/{"image_type":"BASE64", "group_id_list":"group_1",#这个要在人脸识别的应用里创建人脸库,并添加相应图片 #这里弄了几个明星的照片 } with open('image.jpg', 'rb') as f: image = base64.b64encode(f.read()) data["image"] = str(image, 'utf-8')try: res2 = eval(requests.post(url=face_search, data=https://www.it610.com/article/data, headers=header).text) #转成dict print(res2) if res2['error_code'] == 110: access_token_new = get_access_token() return access_token_newprint("识别结果:", res2["result"]["user_list"][0]["user_id"]) print("准确率:", res2["result"]["user_list"][0]["score"]) except: passif __name__ == '__main__': access_token = get_access_token() access_token_new = ''while True: capture_photo() if access_token_new:#access_token过期 access_token = access_token_new access_token_new = search_face(access_token)

    推荐阅读