牛叔叔 的笔记

好好学习

2020-11-05 09:19

使用Python通过百度智能API进行人脸识别测试,测测你得颜值

牛叔叔

Python

(2084)

(2)

收藏

blog

首先需要在百度注册账号,并申请创建APP,包含人脸识别API调用权限。


上代码

#万码学堂Python课程 人脸识别 demo
#https://login.bce.baidu.com/
#pip先安装baidu-aip模块
#导入baidu-aip模块
from aip import AipFace
import base64

APP_ID = "你在百度创建的app的id"
API_KEY = "从百度创建的APP中拷贝"
SECRET_KEY = "从百度创建的APP中拷贝"

client = AipFace(APP_ID,API_KEY,SECRET_KEY)
#print(client)
#读取照片文件
file = open("d:/wanmait.com/photos/11.jpg","rb")

#对照片二进制进行base64编码
img = base64.b64encode(file.read()).decode()
#print(img)
#检测img这个照片中的人脸信息
options = {
    "max_face_num":10,#最多的人脸数量
    "face_field":"age,beauty" #希望检测结果数据中包含年龄信息,颜值
}
data = client.detect(img,"BASE64",options)
print(data)
if data["error_code"]==0:
    #返回数据中的检测结果
    result = data["result"]
    #人脸数量
    faceNum = result["face_num"]
    faceList = result["face_list"]
    for face in faceList:
        print(f"年龄:{face['age']}岁,颜值:{face['beauty']}")
else:
    print("出错了")


测试了一下陆老师的颜值100!


201904101554884665818019416.jpg

2条评论

点击登录参与评论