百度人脸识别api及face++人脸识别api测试(python)

时间:2021-04-30 04:46:18

一.百度人脸识别服务

1.官方网址:http://apistore.baidu.com/apiworks/servicedetail/464.html

2.提供的接口包括:

2.1 多人脸比对:请求多个人脸图片做比对,使用前无需人脸注册过程。即同时上传多张图片,返回结果为每对图片的比对分数

2.2 人脸识别:返回指定group中所有username的注册人脸和query人脸的相似度,返回结果按照相似度排序;人脸识别需要先在group里注册username和人脸图片,您可以使用这个功能自己实现一个小规模的人脸识别系统,比如明星脸识别、家庭成员识别等等。其实就是1对n的比对,按照比对的分数返回

2.3人脸验证:验证query人脸图片是否为指定username注册的人脸,需要先在username下注册人脸图片,其实就是1对1的比对,返回比对的分数

3.测试的接口:多人脸比对接口,不需要注册的过程,所以选择这个接口,

4.测试的数据:lfw数据集上的数据,可以去官网下载数据及pairs.txt,可以先研究一下该数据集

5.测试的代码(python):(官网上python例子中传入的参数是有问题的)

 import base64
import urllib
import urllib.request
import urllib.parse
import time
import json #所需的图片要以base64编码的形式上传二进制字符串
def image_to_base64(image_path):
image_data = open(image_path, "rb")
temp = image_data.read()
base64_data = base64.b64encode(temp)
image_data.close()
data_string = str(base64_data)
data_string = data_string.strip('\'b')
return data_string def compare(image1, image2):
url = 'http://apis.baidu.com/idl_baidu/faceverifyservice/face_compare'
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": "%s"}, "cates": {"name1":"7", "name2":"7"}}], "jsonrpc": "2.0","method": "Compare"}' % (image1, image2)
byte_data = reg.encode(encoding="utf-8")
req = urllib.request.Request(url, data=byte_data)
req.add_header("apikey", "你的apikey")
#请求第一次可能会出错,重新请求一次就好了
try:
resp = urllib.request.urlopen(req)
content = resp.read()
if content:
print(content)
return content
except Exception:
resp = urllib.request.urlopen(req)
content = resp.read()
if content:
print(content)
return content def compare_test():
lfw_file = open("pairs.txt")
res_file = open("res_baidu.txt", "a+")
count = 0
while 1:
image_path = "你本地的lfw图片地址"
line = lfw_file.readline()
if not line:
break
line = line.strip('\n')
images = line.split('\t')
if len(images) > 3:
register_image = image_path + images[0] + "\\" + images[0] + "_"
if len(images[1]) < 2:
register_image = register_image + "" + images[1] + ".jpg"
elif len(images[1]) < 3:
register_image = register_image + "" + images[1] + ".jpg"
elif len(images[1]) < 4:
register_image = register_image + "" + images[1] + ".jpg"
else:
register_image = register_image + images[1] + ".jpg" verify_image = image_path + images[2] + "\\" + images[2] + "_"
if len(images[3]) < 2:
verify_image = verify_image + "" + images[3] + ".jpg"
elif len(images[3]) < 3:
verify_image = verify_image + "" + images[3] + ".jpg"
elif len(images[3]) < 4:
verify_image = verify_image + "" + images[3] + ".jpg"
else:
verify_image = verify_image + images[3] + ".jpg"
else:
register_image = image_path + images[0] + "\\" + images[0] + "_"
if len(images[1]) < 2:
register_image = register_image + "" + images[1] + ".jpg"
elif len(images[1]) < 3:
register_image = register_image + "" + images[1] + ".jpg"
elif len(images[1]) < 4:
register_image = register_image + "" + images[1] + ".jpg"
else:
register_image = register_image + images[1] + ".jpg" verify_image = image_path + images[0] + "\\" + images[0] + "_"
if len(images[2]) < 2:
verify_image = verify_image + "" + images[2] + ".jpg"
elif len(images[2]) < 3:
verify_image = verify_image + "" + images[2] + ".jpg"
elif len(images[2]) < 4:
verify_image = verify_image + "" + images[2] + ".jpg"
else:
verify_image = verify_image + images[2] + ".jpg" content = compare(image_to_base64(register_image), image_to_base64(verify_image))
content = str(content)
content = content.strip('b\'')
content = json.loads(content) if (int(count/300)) % 2 == 0:
flag = 1
else:
flag = 0 if content['result']['_ret']['reslist']:
score = content['result']['_ret']['reslist']['name2|name1']
else:
score = "-1" res_file.write(line + '\t' + score + '\t' + str(flag) + '\n')
count += 1 lfw_file.close()
res_file.close() compare_test()

6.比对结果中会出现很多个0.00000的情况,不了解具体的原因。。。知道的兄弟求告知!!!

二.Face++人脸识别服务

1.官方网址:https://console.faceplusplus.com.cn/documents/4887586

2.提供的接口包括Detect,Compare,Search等,只研究了一下Compare接口即1对1比对的接口,测试用的数据还是lfw,该请求直接传入图片的远程url地址即可,不需要直接上传图片文件,用之前还是要注册生成apikey和apisecret(注意用试用的apikey),直接上代码:

3.Python测试代码:

 import urllib
import urllib.request
import urllib.parse
import json def compare(image1, image2):
url = "https://api-cn.faceplusplus.com/facepp/v3/compare"
data = {
'api_key': '你自己的apikey',
'api_secret': '你自己的apisecret',
'image_url1': image1,
'image_url2': image2
}
data = urllib.parse.urlencode(data)
data = data.encode('utf-8')
req = urllib.request.Request(url, data=data) try:
resp = urllib.request.urlopen(req)
content = resp.read()
if content:
print(content)
return content
except Exception:
resp = urllib.request.urlopen(req)
content = resp.read()
if content:
print(content)
return content def compare_test():
lfw_file = open("pairs.txt")
res_file = open("res_face.txt", "a+")
count = 0
while 1:
image_path = "http://vis-www.cs.umass.edu/lfw/images/"
line = lfw_file.readline()
if not line:
break
line = line.strip('\n')
images = line.split('\t')
if len(images) > 3:
register_image = image_path + images[0] + "/" + images[0] + "_"
if len(images[1]) < 2:
register_image = register_image + "" + images[1] + ".jpg"
elif len(images[1]) < 3:
register_image = register_image + "" + images[1] + ".jpg"
elif len(images[1]) < 4:
register_image = register_image + "" + images[1] + ".jpg"
else:
register_image = register_image + images[1] + ".jpg" verify_image = image_path + images[2] + "/" + images[2] + "_"
if len(images[3]) < 2:
verify_image = verify_image + "" + images[3] + ".jpg"
elif len(images[3]) < 3:
verify_image = verify_image + "" + images[3] + ".jpg"
elif len(images[3]) < 4:
verify_image = verify_image + "" + images[3] + ".jpg"
else:
verify_image = verify_image + images[3] + ".jpg"
else:
register_image = image_path + images[0] + "/" + images[0] + "_"
if len(images[1]) < 2:
register_image = register_image + "" + images[1] + ".jpg"
elif len(images[1]) < 3:
register_image = register_image + "" + images[1] + ".jpg"
elif len(images[1]) < 4:
register_image = register_image + "" + images[1] + ".jpg"
else:
register_image = register_image + images[1] + ".jpg" verify_image = image_path + images[0] + "/" + images[0] + "_"
if len(images[2]) < 2:
verify_image = verify_image + "" + images[2] + ".jpg"
elif len(images[2]) < 3:
verify_image = verify_image + "" + images[2] + ".jpg"
elif len(images[2]) < 4:
verify_image = verify_image + "" + images[2] + ".jpg"
else:
verify_image = verify_image + images[2] + ".jpg" content = compare(register_image, verify_image)
content = str(content)
content = content.strip('b\'')
content = json.loads(content) if (int(count / 300)) % 2 == 0:
flag = 1
else:
flag = 0 if 'confidence' in content.keys():
score = content['confidence']
else:
score = '-1'
res_file.write(line + '\t' + str(score) + '\t' + str(flag) + '\n') count += 1 lfw_file.close()
res_file.close()
compare_test()

4.测试结果有待分析。。