人脸识别
自己训练模型或者使用网上已有的模型
使用百度提供的技术支持(在线API和离线SDK)
在线API
- 登录百度智能云
- 查阅人脸识别的技术文档
- 人脸检测
- 人脸对比
- 人脸搜索
- 人脸库管理
实现过程
-
摄像头采集画面
-
实现人脸检测
-
向百度AI发送人脸检测请求,返回检测结果
-
获取API key和Secret Key
- 在应用列表创建一个新应用
- 在创建的应用获取APIKEY和SecrKEY
-
获取Access Token
# encoding:utf-8 import requests # client_id 为官网获取的API Key, client_secret 为官网获取的Secret Key host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=【官网获取的API Key】&client_secret=【官网获取的Secret Key】' response = requests.get(host) if response: print(response.json()['access_token'])
如果成功则返回一个字典,我们输出其中的access_token就可以了
-
示例
# encoding:utf-8 import requests # 图片处理,转换为BASE64编码 path = 'IU.jpg' # 我用iu小姐姐的一张图试试 fp = open(path, 'rb') base64_image = base64.b64encode(fp.read()) # 请求的地址 request_url = "https://aip.baidubce.com/rest/2.0/face/v3/detect" # 请求参数 params = { "image": base64_image, # 图片信息字符串 "image_type": "BASE64", # 图片信息格式 "face_field": "faceshape,facetype" # 请求返回的人脸属性(可选)这里注意不要有空格 } # 访问令牌 access_token = '[调用鉴权接口获取的token]' request_url = request_url + "?access_token=" + access_token headers = {'content-type': 'application/json'} response = requests.post(request_url, data=params, headers=headers) if response: print(response.json()) # 输出内容看看 data = response.json() print(data) if data['error_msg'] == 'SUCCESS': result = data['result'] print(result)
-
-
-
完成人脸搜索,比对人脸库中的人脸,完成签到