1.功能描述:
百度人脸识别功能,包含人脸检测与属性分析、人脸对比、人脸搜索、活体检测等能力。人脸检测与属性分析能够对人的性别、年龄等属性进行准确的分析。
2.平台接入
具体接入方式比较简单,可以参考我的另一个帖子,这里就不重复了:
http://ai.baidu.com/forum/topic/show/943327
3.调用攻略(Python3)及评测
3.1首先认证授权:
在开始调用任何API之前需要先进行认证授权,具体的说明请参考:
http://ai.baidu.com/docs#/Auth/top
具体Python3代码如下:
# -*- coding: utf-8 -*-
#!/usr/bin/env python
import urllib
import base64
import json
#client_id 为官网获取的AK, client_secret 为官网获取的SK
client_id =【百度云应用的AK】
client_secret =【百度云应用的SK】
#获取token
def get_token():
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=' + client_id + '&client_secret=' + client_secret
request = urllib.request.Request(host)
request.add_header('Content-Type', 'application/json; charset=UTF-8')
response = urllib.request.urlopen(request)
token_content = response.read()
#print (token_content)
if token_content:
token_info = json.loads(token_content)
token_key = token_info['access_token']
return token_key
3.2百度人脸检测接口调用:
详细说明请参考: https://ai.baidu.com/ai-doc/FACE/yk37c1u4t
说明的比较清晰,这里就不重复了。
大家需要注意的是:人脸检测
API访问URL:https://aip.baidubce.com/rest/2.0/face/v3/detect
Python3调用代码如下:
def draw_gestures(originfilename,gestures,resultfilename,fontsize):
setFont = ImageFont.truetype('C:/windows/fonts/simfang.ttf', fontsize)
image_origin = Image.open(originfilename)
draw =ImageDraw.Draw(image_origin)
for gesture in gestures:
location=gesture['location']
x=gesture['gender']['type']
dict = {"female" : "女", "male" : "男"}
content='性别:'+dict[x]+"\n"+'年龄:'+str(gesture['age'])
draw.rectangle((location['left'],location['top'],location['left']+location['width'],location['top']+location['height']),outline = "grey",width=2)
draw.text((location['left']+location['width'],location['top']), content,font=setFont,fill="blue")
image_origin.save(resultfilename, "JPEG")
def age_detect(filename,resultfilename):
request_url = "https://aip.baidubce.com/rest/2.0/face/v3/detect"
print(filename)
# 二进制方式打开图片文件
f = open(filename, 'rb')
img = base64.b64encode(f.read())
params = dict()
params['image'] = img
params['image_type'] = 'BASE64'
params['face_field'] = 'age,beauty,expression,face_shape,gender,glasses'
params = urllib.parse.urlencode(params).encode("utf-8")
#params = json.dumps(params).encode('utf-8')
access_token = get_token()
begin = time.perf_counter()
request_url = request_url + "?access_token=" + access_token
request = urllib.request.Request(url=request_url, data=params)
request.add_header('Content-Type', 'application/x-www-form-urlencoded')
response = urllib.request.urlopen(request)
content = response.read()
end = time.perf_counter()
print('处理时长:'+'%.2f'%(end-begin)+'秒')
if content:
#print(content)
content=content.decode('utf-8')
#print(content)
data = json.loads(content)
result=data['result']['face_list']
draw_gestures(filename,result,resultfilename,25)
4.功能评测:
具体效果如下(以下例子均来自网上)::
5.测试结论和建议
经过测试发现人脸检测对于年龄,性别等内容的判断非常准确,可以用于多种应用场景。
识别的准确率很高
效果非常不错