我是靠谱客的博主 迷人刺猬,这篇文章主要介绍2021百度人脸对比(附代码),现在分享给大家,希望可以做个参考。

百度API的各种接口非常方便易用,可以直接调用。

其实小白遇到最多的就是百度人脸识别和对比这块!!!

今天简单把最新的跑坑流程说明一下,希望咱们小白们少走坑。。。。

  • 需要建立一个百度账号........不再多言了

  • 依次点击  个人头像---控制台总览--左上角蓝色方块下拉的人脸识别---创建应用-进入创建应用

点击2处 进入应用列表 可以看到key 可通过后台的API Key和Secret Key   获得参数access_token

复制代码
1
2
3
4
5
6
7
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
    复制代码
    1
    2
    3
    4
    5
    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')

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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并获得数据对比
    复制代码
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    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格式

完整代码

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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百度人脸对比(附代码)内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(93)

评论列表共有 0 条评论

立即
投稿
返回
顶部