本文共 3927 字,大约阅读时间需要 13 分钟。
使用百度账号登录百度AI平台,网址:
若没有直接注册一个账号。登录后需要点击“创建应用”填写命名一下,完成后返回,点击“管理应用”,就可以看到已经申请的[应用名称、AppID、API Key、Secret Key].1.向授权服务地址: 并在此URL后带上以下参数:
· grant_type: 必须参数,固定为client_credentials; · client_id: 必须参数,应用的API Key; · client_secret: 必须参数,应用的Secret Key; 例如:你申请的API Key是Va5yQRHlA4Fq5eR30vV4, Secret Key是0rDSjzQ20XUj5itV6WRtznPQSzr5pV,进行如下拼接:https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=Va5yQRHlA4Fq5eR30vV4&client_secret=0rDSjzQ20XUj5itV6WRtznPQSzr5pV此url 为向授权服务请求的完整地址,先命名为 api1
2,获取access_token的完整内容
response=requests.get(api1) print( response) # 打印结果如下: { "refresh_token": "25.b55fe1d287227ca97aab219bb249b8ab.315360000.1798284651.282335-8574074","expires_in": 2592000,"scope": "public wise_adapt","session_key": "9mzdDZXu3dENdFZQurfg0Vz8slgSgvvOAUebNFzyzcpQ5EnbxbF+hfG9DQkpUVQdh4p6HbQcAiz5RmuBAja1JJGgIdJI","access_token": "24.6c5e1ff107f0e8bcef8c46d3424a0e78.2592000.1485516651.282335-8574074","session_secret": "dfac94a3489fe9fca7c3221cbf7525ff" } # 我们需要其中的 access_token
3, 人脸匹配相似度的地址:“”
# 2,获取token值,拼接APIimport requestsdef get_token(): response=requests.get(api1) access_token=eval(response.text)['access_token'] #eval函数将字符串转化为字典 api2="https://aip.baidubce.com/rest/2.0/face/v3/match"+"?access_token="+access_token return api2
import base64import jsondef read_img(img1,img2): # 两个图片参数 with open(img1,'rb') as f: # 读取图片数据 pic1=base64.b64encode(f.read()) # 图片数据编码为base64格式数据 with open(img2,'rb') as f: pic2=base64.b64encode(f.read()) params=json.dumps([ # 将字典数据转化为字符串 {"image":str(pic1,"utf-8"),"image_type":'BASE64',"face_type":"LIVE"}, {"image":str(pic2,"utf-8"),"image_type":'BASE64',"face_type":"IDCARD"} ]) return params`在这里插入代码片`
def analyse_img(file1,file2): params=read_img(file1,file2) # 调用第一个函数的结果api api=get_token() # 调用第二个函数的图片数据 content=requests.post(api,params).text # 获取对比详细结果 print(content) analyse_img("zly01.jpg","zly02.jpg"): # 找的两张 赵丽颖的照片 # 打印content内容如下:{"error_code":0,"error_msg":"SUCCESS","log_id":1345050733350687141,"timestamp":1553335068,"cached":0,"result":{"score":95.51683807,"face_list",[{"face_token":"938e0c197a7f53d9eced7551c6cd6c50"},{"face_token":"81ab41769b6fc5877d944415e380e326"}]}}# 我们需要的是"score":95.51683807",相似度95.5,可以确认是同一个人。
找的图片:赵丽颖2张(zly01.jpg,zly02.jpg),刘亦菲一张(lyf01.jpg)
import requestsimport base64import json# 1,准备好申请的人脸识别api,API Key, Secret Keyapi1=“https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=Va5yQRHlA4Fq5eR30vV4&client_secret=0rDSjzQ20XUj5itV6WRtznPQSzr5pV”# api2="https://aip.baidubce.com/rest/2.0/face/v3/match"# 2,获取token值,拼接APIdef get_token(): response=requests.get(api1) access_token=eval(response.text)['access_token'] api2="https://aip.baidubce.com/rest/2.0/face/v3/match"+"?access_token="+access_token return api2# 3,读取图片数据def read_img(img1,img2): with open(img1,'rb') as f: pic1=base64.b64encode(f.read()) with open(img2,'rb') as f: pic2=base64.b64encode(f.read()) params=json.dumps([ {"image":str(pic1,"utf-8"),"image_type":'BASE64',"face_type":"LIVE"}, {"image":str(pic2,"utf-8"),"image_type":'BASE64',"face_type":"IDCARD"} ]) return params# 4,发起请求拿到对比结果def analyse_img(file1,file2): params=read_img(file1,file2) api=get_token() content=requests.post(api,params).text # print(content) score=eval(content)['result']['score'] if score>80: print('图片识别相似度度为'+str(score)+',是同一人') else: print('图片识别相似度度为'+str(score)+',不是同一人')analyse_img("zly01.jpg","zly02.jpg")# 打印执行结果:图片识别相似度度为88.23068237,是同一人# 换图片zly02.jpg和lyf01.jpg:图片识别相似度度为29.28668785,不是同一人
转载地址:http://mnqxi.baihongyu.com/