python安装百度aip_百度Aip人脸识别之python代码

用python来做人脸识别代码量少 思路清晰,

在使用之前我们需要在我们的配置的编译器中通过pip install baidu-aip 即可

from aip import AipFace

就可以开始使用api了 我们第一次接触这个东西不妨

help(AipFace)

你就可以看到他所支持的功能。

在使用之前我们需要在百度的后台创建应用。将我们人脸都存放入库中。

其次我们要了解一个概念,我们要将本机中的图片与后台的人脸对比的话我们需要将图片转成base64的字符串的格式

importbase64

f = open('./photo/mr.jpg', 'rb')

image = base64.b64encode(f.read())

image64 = str(image,'utf-8')

image_type = "BASE64"

当然我们也可以将base64码转换成图片。

人脸检测的原理是通过机器学习转化提前图片人脸中的七十二个关键点。并进行其他的分析。

python3的代码为

'''# 人脸检测与属性分析'''request_url= "https://aip.baidubce.com/rest/2.0/face/v3/detect"f= open('./photo/mr.jpg', 'rb')

image=base64.b64encode(f.read())

image64= str(image,'utf-8')

image_type= "BASE64"

#params = "{\"image\":\"%s\",\"image_type\":\"BASE64\",\"face_field\":\"faceshape,facetype\"}"%image64

params = {'image': image64,'image_type':"BASE64",'face_field': 'faceshape,facetype'}#此处的faceshape和facetype需要自己加上去 更具自己需要的返回值

params= urllib.parse.urlencode(params).encode("utf-8")

access_token= '[24.3941b86dfcbc8eaea432d11df4f6660d.2592000.1542368987.282335-14255146]'request_url= request_url + "?access_token=" +access_token

request= urllib.request.urlopen(url=request_url, data=params) #发送请求

content= request.read() #将返回结果读取出来

print(content) #显示返回结果

importurllib.request,sys,base64importurllib.parse#返回实例

a = {'error_code': 0,'error_msg': 'SUCCESS','log_id': 1011018945051,'timestamp': 1540301526,'cached': 0,'result': {'face_num': 1,'face_list': [{'face_token': '80ed04e5e8771730b3fe240f8ead4e97','location': {'left': 564.6082764,'top': 117.9681244,'width': 263,'height': 265,'rotation': 1},'face_probability': 1,'angle': {'yaw': -0.301689893,'pitch': -15.59528351,'roll': 0.9747127891}

}

]

}

}

具体各种属性请看百度文档 https://ai.baidu.com/docs#/Face-Detect-V3/top

介绍完人脸检测我们就可以进行人脸对比了

importbase64from aip importAipFace'''新建aipface的配置'''

'''你的 app id ak sk'''AppId= '14255146'ApiKey= 'UoyrHmKFG3nGPL5HmDiGo80G'SecretKey= 'HUo1z36aDc1UxOwuS8d7Vxldh4GsQg8l'client=AipFace(AppId, ApiKey, SecretKey)

f= open('./photo/huge.jpg', 'rb')

image=base64.b64encode(f.read())

image64= str(image,'utf-8')

image_type= "BASE64"

print(client.detect(image64, image_type)) #此处的返回值为人脸的基本检测的数值效果

#print(strs)#人脸检测#image = str(strs) # 取决于image_type参数,传入BASE64字符串或URL字符串或FACE_TOKEN字符串

imageType= "BASE64"groupIdList= "17ai_1"

"""调用人脸搜索"""

print(client.search(str(image64), image_type, groupIdList))

# 将返回对比结果'''""" 如果有可选参数 """

options = {}

options["quality_control"] = "NORMAL"

options["liveness_control"] = "LOW"

options["user_id"] = "233451"

options["max_user_num"] = 3

""" 带参数调用人脸搜索 """

# print(client.search(image, imageType, , options))

# 人脸搜索返回例子'''face={"face_token": "fid","user_list": [

{"group_id": "test1","user_id": "u333333","user_info": "Test User","score": 99.3}

]

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
识别结果大概是这样 {'result': {'face_num': 1, 'face_list': [{'quality': {'occlusion': {'right_eye': 0, 'left_cheek': 0.1459853947, 'right_cheek': 0.05144193396, 'left_eye': 0.465408802, 'mouth': 0.02919708006, 'chin_contour': 0.01420217194, 'nose': 0}, 'illumination': 116, 'blur': 7.266304692e-06, 'completeness': 1}, 'age': 22, 'face_token': 'dc6f8f9df5d977ea476e2d04acdf5063', 'race': {'type': 'white', 'probability': 0.6173604727}, 'glasses': {'type': 'common', 'probability': 0.9834988713}, 'gender': {'type': 'male', 'probability': 0.655915916}, 'face_probability': 0.9185044169, 'beauty': 51.21487427, 'angle': {'roll': -2.750922441, 'yaw': 28.97134399, 'pitch': 5.202290535}, 'location': {'height': 65, 'top': 112.0704803, 'width': 76, 'left': 76.20765686, 'rotation': -4}, 'face_type': {'type': 'human', 'probability': 0.9992217422}, 'face_shape': {'type': 'oval', 'probability': 0.4419156313}, 'expression': {'type': 'none', 'probability': 0.9999142885}}]}, 'error_msg': 'SUCCESS', 'timestamp': 1537413754, 'cached': 0, 'error_code': 0, 'log_id': 9465840013520} 年龄:22 颜值:51.21487427 表情-type(none:不笑;smile:微笑;laugh:大笑):none 表情-probability(表情置信度,范围【0~1】,0最小、1最大):0.9999142885 脸型-type(square: 正方形 triangle:三角形 oval: 椭圆 heart: 心形 round: 圆形):oval 脸型-probability(置信度,范围【0~1】,代表这是人脸形状判断正确的概率,0最小、1最大):0.4419156313 性别-type(male:男性 female:女性):male 性别-probability(性别置信度,范围【0~1】,0代表概率最小、1代表最大。):0.655915916 是否带眼镜-type(none:无眼镜,common:普通眼镜,sun:墨镜):common 是否带眼镜-probability(眼镜置信度,范围【0~1】,0代表概率最小、1代表最大。):0.9834988713 人种-type(yellow: 黄种人 white: 白种人 black:黑种人 arabs: 阿拉伯人):white 人种-probability(人种置信度,范围【0~1】,0代表概率最小、1代表最大。):0.6173604727 真实人脸/卡通人脸 -type(human: 真实人脸 cartoon: 卡通人脸):human 真实人脸/卡通人脸 -probability(人脸类型判断正确的置信度,范围【0~1】,0代表概率最小、1代表最大。):0.9992217422

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值