知识点:
- 创建百度AI 人脸识别应用,获取自己的[APP_ID]、[API_KEY]、[SECRET_KEY]
- 查看SDK文档,调用人脸识别接口,实现颜值打分系统
实现步骤:
搜索【百度大脑】,打开【百度AI开放平台】网站,其中有人脸识别技术,根据人脸识别SDK文档安装Python SDK。安装命令(cmd命令):
pip install baidu-aip # 安装了pip,可通过该命令安装
python setup.py install # 安装了setup.tool,通过该命令安装
在【百度AI开放平台】中控制台中创建人脸识别应用,获取系统为你分配的APPID AK SK(需先注册百度账号)
代码实现:
导包
1 import base64 2 from aip import AipFace
创建百度对象
1 """ 你的 APPID AK SK """ 2 APP_ID = '你的 App ID' # 百度AI平台提供 3 API_KEY = '你的 Api Key' # 百度AI平台提供 4 SECRET_KEY = '你的 Secret Key' # 百度AI平台提供 5 client = AipFace(APP_ID, API_KEY, SECRET_KEY)
设置人脸检测接口的参数
1 # 该参数为可选参数,默认只返回face_token、人脸框、概率和旋转角度,其他的属性值需自行获取并用 , 隔开 2 option = {'face_field': "age,gender,beauty"} 3 # 图片类型 BASE64:图片的base64值,base64编码后的图片数据,编码后的图片大小不超过2M 4 imageType = "BASE64" 5 # 文件路径 6 file = 'image路径'
处理图片格式
1 # 该函数用于处理图片格式 2 def set_img(file): 3 with open(file, 'rb') as f: 4 res = base64.b64encode(f.read()) 5 # print(type(res)) 6 return res.decode('utf-8')
调用人脸检测
1 """ 调用人脸检测 """ 2 results = client.detect(set_img(file), imageType, option) 3 print(results)
返回结果示例
{'error_code': 0, 'error_msg': 'SUCCESS', 'log_id': 2012579895594,'timestamp': 1572346654, 'cached': 0, 'result': {'face_num': 1,
'face_list': [{'face_token': '3099def6a6b4611ee4f54e0c40dcbe71','location': {'left': 92.53, 'top': 88.92, 'width': 90, 'height': 94, 'rotation': 12},
'face_probability': 1, 'angle': {'yaw': -0.02, 'pitch': 6.33, 'roll': 8.31}, 'age': 23, 'gender': {'type': 'male', 'probability': 1}, 'beauty': 79.28}]}}
对人脸检测返回的结果进行处理
1 def get_score(file): 2 sex = '' 3 results = client.detect(set_img(file), imageType, option) 4 age = results['result']['face_list'][0]['age'] 5 gender = results['result']['face_list'][0]['gender']['type'] 6 sex = ('男' if gender == 'male' else '女') 7 beauty = results['result']['face_list'][0]['beauty'] 8 return print(f"姓名:wyb,性别:{sex},年龄:{age},颜值:{beauty}") 9 10 get_score(file)
处理后的结果
姓名:wyb,性别:男,年龄:23,颜值:79.28
补充:这是项目老师做的优化窗口版打分系统,可自行下载给自己测评一下
分享链接:https://pan.baidu.com/s/1uWKX6R9C1OQAX2kqXC_Qaw 提取码:uke9