概述
百度API的各种接口非常方便易用,可以直接调用。
其实小白遇到最多的就是百度人脸识别和对比这块!!!
今天简单把最新的跑坑流程说明一下,希望咱们小白们少走坑。。。。
-
需要建立一个百度账号........不再多言了
- 依次点击 个人头像---控制台总览--左上角蓝色方块下拉的人脸识别---创建应用-进入创建应用
点击2处 进入应用列表 可以看到key 可通过后台的API Key和Secret Key 获得参数access_token
def get_Token(self):
self.client_id="API key"
self.client_secret="secret key"
host = f'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id={self.client_id}&client_secret={self.client_secret}'
response = requests.get(host)
access_token=response.json()['access_token']
return access_token
- 生成获得post的url
def get_API(self): self.access_token=self.get_Token() self.request_url='https://aip.baidubce.com/rest/2.0/face/v3/match' API_url = self.request_url + "?access_token=" + self.access_token return API_url
生成post的params 是json格式 注意str(pic1,'utf8')
def get_Param(self):
with open(r'%s' % self.img1,'rb') as f:
pic1=base64.b64encode(f.read())
with open(r'%s' % self.img2,'rb') as f:
pic2=base64.b64encode(f.read())
params=[
{
"image": str(pic1,'utf8'),
"image_type": "BASE64",
"face_type": "LIVE",
"quality_control": "LOW",
"liveness_control": "HIGH"
},
{
"image": str(pic2,'utf8'),
"image_type": "BASE64",
"face_type": "IDCARD",
"quality_control": "LOW",
"liveness_control": "HIGH"
}
]
return params
- 向API-url post并获得数据对比
def face_recogn(self): API_url=self.get_API() #pprint(API_url) params=self.get_Param() # pprint(params) headers = {'content-type': 'application/json'} response = requests.post(API_url, json=params, headers=headers) # pprint(response.json()) score=eval(response.text)['result']['score'] if score>=60: print('二人相似度得分为 %s, 是同一人的可能性极大'%str(score)) else: print('二人相似度得分为 %s, 不是同一人的可能性极大'%str(score))
- 获得的json格式
完整代码
import requests
import base64
from pprint import pprint
import json
# client_id 为官网获取的AK, client_secret 为官网获取的SK
class FaceContrast():
def __init__(self, img1,img2):
self.img1 = img1
self.img2 = img2
def get_Token(self):
self.client_id="fbGlSTRw9RsqnzpCChrwYDba"
self.client_secret="rv9ZithXkFPIpXVVAr07ldlmLQUaW6jt"
host = f'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id={self.client_id}&client_secret={self.client_secret}'
response = requests.get(host)
access_token=response.json()['access_token']
return access_token
def get_API(self):
self.access_token=self.get_Token()
self.request_url='https://aip.baidubce.com/rest/2.0/face/v3/match'
API_url = self.request_url + "?access_token=" + self.access_token
return API_url
def get_Param(self):
with open(r'%s' % self.img1,'rb') as f:
pic1=base64.b64encode(f.read())
with open(r'%s' % self.img2,'rb') as f:
pic2=base64.b64encode(f.read())
params=[
{
"image": str(pic1,'utf8'),
"image_type": "BASE64",
"face_type": "LIVE",
"quality_control": "LOW",
"liveness_control": "HIGH"
},
{
"image": str(pic2,'utf8'),
"image_type": "BASE64",
"face_type": "IDCARD",
"quality_control": "LOW",
"liveness_control": "HIGH"
}
]
return params
def face_Recogn(self):
API_url=self.get_API()
pprint(API_url)
params=self.get_Param()
# pprint(params)
headers = {'content-type': 'application/json'}
response = requests.post(API_url, json=params, headers=headers)
# pprint(response.json())
score=eval(response.text)['result']['score']
if score>=60:
print('二人相似度得分为 %s, 是同一人的可能性极大'%str(score))
else:
print('二人相似度得分为 %s, 不是同一人的可能性极大'%str(score))
if __name__ == '__main__':
img1="333.jpg"
img2="444.jpg"
face_contrast=FaceContrast(img1,img2)
face_contrast.face_Recogn()
有问题一起分享,愿共同进步!!!!!
最后
以上就是迷人刺猬为你收集整理的2021百度人脸对比(附代码)的全部内容,希望文章能够帮你解决2021百度人脸对比(附代码)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复