百度⼈脸识别api及face++⼈脸识别api测试(python)
⼀.百度⼈脸识别服务
2.提供的接⼝包括:
2.1 多⼈脸⽐对:请求多个⼈脸图⽚做⽐对,使⽤前⽆需⼈脸注册过程。即同时上传多张图⽚,返回结果为每对图⽚的⽐对分数
2.2 ⼈脸识别:返回指定group中所有username的注册⼈脸和query⼈脸的相似度,返回结果按照相似度排序;⼈脸识别需要先在group⾥注册username和⼈脸图⽚,您可以使⽤
这个功能⾃⼰实现⼀个⼩规模的⼈脸识别系统,⽐如明星脸识别、家庭成员识别等等。其实就是1对n的⽐对,按照⽐对的分数返回
2.3⼈脸验证:验证query⼈脸图⽚是否为指定username注册的⼈脸,需要先在username下注册⼈脸图⽚,其实就是1对1的⽐对,返回⽐对的分数
百度api接口
3.测试的接⼝:多⼈脸⽐对接⼝,不需要注册的过程,所以选择这个接⼝,
4.测试的数据:lfw数据集上的数据,可以去官⽹下载数据及,可以先研究⼀下该数据集
5.测试的代码(python):(官⽹上python例⼦中传⼊的参数是有问题的)
1import base64
2import urllib
quest
4import urllib.parse
5import time
6import json
7
8#所需的图⽚要以base64编码的形式上传⼆进制字符串
9def image_to_base64(image_path):
10    image_data = open(image_path, "rb")
11    temp = ad()
12    base64_data = base64.b64encode(temp)
13    image_data.close()
14    data_string = str(base64_data)
15    data_string = data_string.strip('\'b')
16return data_string
17
18def compare(image1, image2):
19    url = 'apis.baidu/idl_baidu/faceverifyservice/face_compare'
20    reg = r'{"params": [{"cmdid": "1000", "appid": "你的apikey", "clientip": "你的ip","type": "st_groupverify", "groupid": "12345", "versionnum": "1.0.0.1", "usernames": {"name1": "name1", "name2": "name2"}, "images": {"name1": "%s", "name2"
21    byte_data = de(encoding="utf-8")
22    req = quest.Request(url, data=byte_data)
23    req.add_header("apikey", "你的apikey")
24#请求第⼀次可能会出错,重新请求⼀次就好了
25try:
26        resp = quest.urlopen(req)
27        content = ad()
28if content:
29print(content)
30return content
31except Exception:
32        resp = quest.urlopen(req)
33        content = ad()
34if content:
35print(content)
36return content
37
38def compare_test():
39    lfw_file = open("")
40    res_file = open("", "a+")
41    count = 0
42while 1:
43        image_path = "你本地的lfw图⽚地址"
44        line = adline()
45if not line:
46break
47        line = line.strip('\n')
48        images = line.split('\t')
49if len(images) > 3:
50            register_image = image_path + images[0] + "\\" + images[0] + "_"
51if len(images[1]) < 2:
52                register_image = register_image + "000" + images[1] + ".jpg"
53elif len(images[1]) < 3:
54                register_image = register_image + "00" + images[1] + ".jpg"
55elif len(images[1]) < 4:
56                register_image = register_image + "0" + images[1] + ".jpg"
57else:
58                register_image = register_image + images[1] + ".jpg"
59
60            verify_image = image_path + images[2] + "\\" + images[2] + "_"
61if len(images[3]) < 2:
62                verify_image = verify_image + "000" + images[3] + ".jpg"
63elif len(images[3]) < 3:
64                verify_image = verify_image + "00" + images[3] + ".jpg"
65elif len(images[3]) < 4:
66                verify_image = verify_image + "0" + images[3] + ".jpg"
67else:
68                verify_image = verify_image + images[3] + ".jpg"
69else:
70            register_image = image_path + images[0] + "\\" + images[0] + "_"
71if len(images[1]) < 2:
72                register_image = register_image + "000" + images[1] + ".jpg"
73elif len(images[1]) < 3:
74                register_image = register_image + "00" + images[1] + ".jpg"
75elif len(images[1]) < 4:
76                register_image = register_image + "0" + images[1] + ".jpg"
77else:
78                register_image = register_image + images[1] + ".jpg"
79
80            verify_image = image_path + images[0] + "\\" + images[0] + "_"
81if len(images[2]) < 2:
82                verify_image = verify_image + "000" + images[2] + ".jpg"
83elif len(images[2]) < 3:
84                verify_image = verify_image + "00" + images[2] + ".jpg"
85elif len(images[2]) < 4:
86                verify_image = verify_image + "0" + images[2] + ".jpg"
87else:
88                verify_image = verify_image + images[2] + ".jpg"
89
90        content = compare(image_to_base64(register_image), image_to_base64(verify_image))
91        content = str(content)
92        content = content.strip('b\'')
93        content = json.loads(content)
94
95if (int(count/300)) % 2 == 0:
96            flag = 1
97else:
98            flag = 0
99
100if content['result']['_ret']['reslist']:
101            score = content['result']['_ret']['reslist']['name2|name1']
102else:
103            score = "-1"
104
105        res_file.write(line + '\t' + score + '\t' + str(flag) + '\n')
106        count += 1
107
108    lfw_file.close()
109    res_file.close()
110
111 compare_test()
6.⽐对结果中会出现很多个0.00000的情况,不了解具体的原因。。。知道的兄弟求告知
⼆.Face++⼈脸识别服务
2.提供的接⼝包括Detect,Compare,Search等,只研究了⼀下Compare接⼝即1对1⽐对的接⼝,测试⽤的数据还是lfw,该请求直接传⼊图⽚的远程url地址即可,不需要直接上传图⽚⽂件,⽤之前还是要注册⽣成apikey和apisecret(注意⽤试⽤的apikey),直接上代码:
3.Python测试代码:
1import urllib
quest
3import urllib.parse
4import json
5
6
7def compare(image1, image2):
8    url = "api-cn.faceplusplus/facepp/v3/compare"
9    data = {
10'api_key': '你⾃⼰的apikey',
11'api_secret': '你⾃⼰的apisecret',
12'image_url1': image1,
13'image_url2': image2
14    }
15    data = urllib.parse.urlencode(data)
16    data = de('utf-8')
17    req = quest.Request(url, data=data)
18
19try:
20        resp = quest.urlopen(req)
21        content = ad()
22if content:
23print(content)
24return content
25except Exception:
26        resp = quest.urlopen(req)
27        content = ad()
28if content:
29print(content)
30return content
31
32
33def compare_test():
34    lfw_file = open("")
35    res_file = open("", "a+")
36    count = 0
37while 1:
38        image_path = "vis-www.cs.umass.edu/lfw/images/"
39        line = adline()
40if not line:
41break
42        line = line.strip('\n')
43        images = line.split('\t')
44if len(images) > 3:
45            register_image = image_path + images[0] + "/" + images[0] + "_"
46if len(images[1]) < 2:
47                register_image = register_image + "000" + images[1] + ".jpg"
48elif len(images[1]) < 3:
49                register_image = register_image + "00" + images[1] + ".jpg"
50elif len(images[1]) < 4:
51                register_image = register_image + "0" + images[1] + ".jpg"
52else:
53                register_image = register_image + images[1] + ".jpg"
54
55            verify_image = image_path + images[2] + "/" + images[2] + "_"
56if len(images[3]) < 2:
57                verify_image = verify_image + "000" + images[3] + ".jpg"
58elif len(images[3]) < 3:
59                verify_image = verify_image + "00" + images[3] + ".jpg"
60elif len(images[3]) < 4:
61                verify_image = verify_image + "0" + images[3] + ".jpg"
62else:
63                verify_image = verify_image + images[3] + ".jpg"
64else:
65            register_image = image_path + images[0] + "/" + images[0] + "_"
66if len(images[1]) < 2:
67                register_image = register_image + "000" + images[1] + ".jpg"
68elif len(images[1]) < 3:
69                register_image = register_image + "00" + images[1] + ".jpg"
70elif len(images[1]) < 4:
71                register_image = register_image + "0" + images[1] + ".jpg"
72else:
73                register_image = register_image + images[1] + ".jpg"
74
75            verify_image = image_path + images[0] + "/" + images[0] + "_"
76if len(images[2]) < 2:
77                verify_image = verify_image + "000" + images[2] + ".jpg"
78elif len(images[2]) < 3:
79                verify_image = verify_image + "00" + images[2] + ".jpg"
80elif len(images[2]) < 4:
81                verify_image = verify_image + "0" + images[2] + ".jpg"
82else:
83                verify_image = verify_image + images[2] + ".jpg"
84
85        content = compare(register_image, verify_image)
86        content = str(content)
87        content = content.strip('b\'')
88        content = json.loads(content)
89
90if (int(count / 300)) % 2 == 0:
91            flag = 1
92else:
93            flag = 0
94
95if'confidence'in content.keys():
96            score = content['confidence']
97else:
98            score = '-1'
99        res_file.write(line + '\t' + str(score) + '\t' + str(flag) + '\n') 100
101        count += 1
102
103    lfw_file.close()
104    res_file.close()
105 compare_test()
4.测试结果有待分析。。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。