900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > 调用百度人脸检测API实现简单的颜值检测

调用百度人脸检测API实现简单的颜值检测

时间:2018-08-19 04:58:02

相关推荐

调用百度人脸检测API实现简单的颜值检测

通过百度人工智能平台中的人脸检测模块,实现简单的人脸检测,百度人工智能平台免费注册,人脸检测模块免费使用。相关API文档在:/docs#/Face-Detect-V3/top,依照API文档就可以写出一个简单的检测工具。代码如下:

import base64import jsonimport requestsclass BaiduPicIndentify:def __init__(self,img):self.AK = "换成你自己的AK"self.SK = "换成你自己的SK"self.img_src = imgself.headers = {"Content-Type": "application/json; charset=UTF-8"}def get_accessToken(self):host = '/oauth/2.0/token?grant_type=client_credentials&client_id=' + self.AK + '&client_secret=' + self.SKresponse = requests.get(host, headers=self.headers)json_result = json.loads(response.text)return json_result['access_token']def img_to_BASE64(slef,path):with open(path,'rb') as f:base64_data = base64.b64encode(f.read())return base64_datadef detect_face(self):# 人脸检测与属性分析img_BASE64 = self.img_to_BASE64(self.img_src)request_url = "/rest/2.0/face/v3/detect"post_data = {"image": img_BASE64,"image_type": "BASE64","face_field": "gender,age,beauty,gender,race,expression","face_type": "LIVE"}access_token = self.get_accessToken()request_url = request_url + "?access_token=" + access_tokenresponse = requests.post(url=request_url, data=post_data, headers=self.headers)json_result = json.loads(response.text)if json_result['error_msg']!='pic not has face':print("图片中包含人脸数:", json_result['result']['face_num'])print("图片中包含人物年龄:", json_result['result']['face_list'][0]['age'])print("图片中包含人物颜值评分:", json_result['result']['face_list'][0]['beauty'])print("图片中包含人物性别:", json_result['result']['face_list'][0]['gender']['type'])print("图片中包含人物种族:", json_result['result']['face_list'][0]['race']['type'])print("图片中包含人物表情:", json_result['result']['face_list'][0]['expression']['type'])if __name__=='__main__':img_src=input('请输入需要检测的本地图片路径:')baiduDetect = BaiduPicIndentify(img_src)baiduDetect.detect_face()

输入的图片1.JPG及其检测结果如下:

检测结果: SUCCESS

图片中包含人脸数: 1

图片中包含人物年龄: 23

图片中包含人物颜值评分: 60.61511993

图片中包含人物性别: female

图片中包含人物种族: yellow

图片中包含人物表情: none

输入图片2及其检测结果如下:

检测结果: SUCCESS

图片中包含人脸数: 1

图片中包含人物年龄: 22

图片中包含人物颜值评分: 77.99679565

图片中包含人物性别: female

图片中包含人物种族: yellow

图片中包含人物表情: smile

输入图片3及其检测结果如下:

检测结果: pic not has face

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。