其他分享
首页 > 其他分享> > 人脸识别外部接口调用

人脸识别外部接口调用

作者:互联网

本文代码及数据集来自《Python大数据分析与机器学习商业案例实战》

要在Python中调用百度人脸识别接口,得先安装baidu-aip库。可以在Windows命令行窗口中输入并执行“pip install baidu-aip”命令,或者在JupyterNotebook中输入并运行“!pip install baidu-aip”代码,即可开始安装。
在编写代码调用接口之前,得先做一些准备工作。先在浏览器中打开百度人脸识别官网,单击“立即使用”按钮,然后在弹出的界面中登录百度账号(如果没有账号就注册一个)。
在这里插入图片描述
即可看到AppID、API Key、Secret Key,如下图所示,这些参数在调用接口时会用到。

from aip import AipFace
import base64
APP_ID = '2255xxxx7'
API_KEY = 'grqDNLHdwyeNRjjrjgxxxxoc'
SECRET_KEY = 'qK7xVpLHD6Qkjkb4bHEQU5hwxxxxxxxF'
aipFace = AipFace(APP_ID, API_KEY, SECRET_KEY)
filePath = r'吴彦祖.jpg'

def get_file_content(filePath):
    with open(filePath, 'rb') as fp:
        content = base64.b64encode(fp.read())
        return content.decode('utf-8')
imageType = "BASE64"

options = {}
options["face_field"] = "age,gender,beauty"

result = aipFace.detect(get_file_content(filePath), imageType, options)
print(result)

age = result['result']['face_list'][0]['age']
print('年龄预测为:' + str(age))
gender = result['result']['face_list'][0]['gender']['type']
print('性别预测为:' + gender)
beauty = result['result']['face_list'][0]['beauty']
print('颜值评分为:' + str(beauty))

如果运行过程报错如下:{‘error_code’: 18, ‘error_msg’: ‘Open api qps request limit reached’},需要领取免费额度。运行结果:
在这里插入图片描述
前面只提取了3个参数age、gender、beauty,如果想了解更多参数,可以阅读官方说明文档

标签:人脸识别,beauty,filePath,gender,age,接口,调用,result,KEY
来源: https://blog.csdn.net/m0_46388544/article/details/122783769