百度人脸识别api及face++人脸识别api测试(python)
一.百度人脸识别服务
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例子中传入的参数是有问题的)
1 import base64 2 import urllib 3 import urllib.request 4 import urllib.parse 5 import time 6 import json 7 8 #所需的图片要以base64编码的形式上传二进制字符串 9 def image_to_base64(image_path): 10 image_data = open(image_path, "rb") 11 temp = image_data.read() 12 base64_data = base64.b64encode(temp) 13 image_data.close() 14 data_string = str(base64_data) 15 data_string = data_string.strip('\'b') 16 return data_string 17 18 def compare(image1, image2): 19 url = 'http://apis.baidu.com/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": "%s"}, "cates": {"name1":"7", "name2":"7"}}], "jsonrpc": "2.0","method": "Compare"}' % (image1, image2) 21 byte_data = reg.encode(encoding="utf-8") 22 req = urllib.request.Request(url, data=byte_data) 23 req.add_header("apikey", "你的apikey") 24 #请求第一次可能会出错,重新请求一次就好了 25 try: 26 resp = urllib.request.urlopen(req) 27 content = resp.read() 28 if content: 29 print(content) 30 return content 31 except Exception: 32 resp = urllib.request.urlopen(req) 33 content = resp.read() 34 if content: 35 print(content) 36 return content 37 38 def compare_test(): 39 lfw_file = open("pairs.txt") 40 res_file = open("res_baidu.txt", "a+") 41 count = 0 42 while 1: 43 image_path = "你本地的lfw图片地址" 44 line = lfw_file.readline() 45 if not line: 46 break 47 line = line.strip('\n') 48 images = line.split('\t') 49 if len(images) > 3: 50 register_image = image_path + images[0] + "\\" + images[0] + "_" 51 if len(images[1]) < 2: 52 register_image = register_image + "000" + images[1] + ".jpg" 53 elif len(images[1]) < 3: 54 register_image = register_image + "00" + images[1] + ".jpg" 55 elif len(images[1]) < 4: 56 register_image = register_image + "0" + images[1] + ".jpg" 57 else: 58 register_image = register_image + images[1] + ".jpg" 59 60 verify_image = image_path + images[2] + "\\" + images[2] + "_" 61 if len(images[3]) < 2: 62 verify_image = verify_image + "000" + images[3] + ".jpg" 63 elif len(images[3]) < 3: 64 verify_image = verify_image + "00" + images[3] + ".jpg" 65 elif len(images[3]) < 4: 66 verify_image = verify_image + "0" + images[3] + ".jpg" 67 else: 68 verify_image = verify_image + images[3] + ".jpg" 69 else: 70 register_image = image_path + images[0] + "\\" + images[0] + "_" 71 if len(images[1]) < 2: 72 register_image = register_image + "000" + images[1] + ".jpg" 73 elif len(images[1]) < 3: 74 register_image = register_image + "00" + images[1] + ".jpg" 75 elif len(images[1]) < 4: 76 register_image = register_image + "0" + images[1] + ".jpg" 77 else: 78 register_image = register_image + images[1] + ".jpg" 79 80 verify_image = image_path + images[0] + "\\" + images[0] + "_" 81 if len(images[2]) < 2: 82 verify_image = verify_image + "000" + images[2] + ".jpg" 83 elif len(images[2]) < 3: 84 verify_image = verify_image + "00" + images[2] + ".jpg" 85 elif len(images[2]) < 4: 86 verify_image = verify_image + "0" + images[2] + ".jpg" 87 else: 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 95 if (int(count/300)) % 2 == 0: 96 flag = 1 97 else: 98 flag = 0 99 100 if content['result']['_ret']['reslist']: 101 score = content['result']['_ret']['reslist']['name2|name1'] 102 else: 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++人脸识别服务
1.官方网址:https://console.faceplusplus.com.cn/documents/4887586
2.提供的接口包括Detect,Compare,Search等,只研究了一下Compare接口即1对1比对的接口,测试用的数据还是lfw,该请求直接传入图片的远程url地址即可,不需要直接上传图片文件,用之前还是要注册生成apikey和apisecret(注意用试用的apikey),直接上代码:
3.Python测试代码:
1 import urllib 2 import urllib.request 3 import urllib.parse 4 import json 5 6 7 def compare(image1, image2): 8 url = "https://api-cn.faceplusplus.com/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 = data.encode('utf-8') 17 req = urllib.request.Request(url, data=data) 18 19 try: 20 resp = urllib.request.urlopen(req) 21 content = resp.read() 22 if content: 23 print(content) 24 return content 25 except Exception: 26 resp = urllib.request.urlopen(req) 27 content = resp.read() 28 if content: 29 print(content) 30 return content 31 32 33 def compare_test(): 34 lfw_file = open("pairs.txt") 35 res_file = open("res_face.txt", "a+") 36 count = 0 37 while 1: 38 image_path = "http://vis-www.cs.umass.edu/lfw/images/" 39 line = lfw_file.readline() 40 if not line: 41 break 42 line = line.strip('\n') 43 images = line.split('\t') 44 if len(images) > 3: 45 register_image = image_path + images[0] + "/" + images[0] + "_" 46 if len(images[1]) < 2: 47 register_image = register_image + "000" + images[1] + ".jpg" 48 elif len(images[1]) < 3: 49 register_image = register_image + "00" + images[1] + ".jpg" 50 elif len(images[1]) < 4: 51 register_image = register_image + "0" + images[1] + ".jpg" 52 else: 53 register_image = register_image + images[1] + ".jpg" 54 55 verify_image = image_path + images[2] + "/" + images[2] + "_" 56 if len(images[3]) < 2: 57 verify_image = verify_image + "000" + images[3] + ".jpg" 58 elif len(images[3]) < 3: 59 verify_image = verify_image + "00" + images[3] + ".jpg" 60 elif len(images[3]) < 4: 61 verify_image = verify_image + "0" + images[3] + ".jpg" 62 else: 63 verify_image = verify_image + images[3] + ".jpg" 64 else: 65 register_image = image_path + images[0] + "/" + images[0] + "_" 66 if len(images[1]) < 2: 67 register_image = register_image + "000" + images[1] + ".jpg" 68 elif len(images[1]) < 3: 69 register_image = register_image + "00" + images[1] + ".jpg" 70 elif len(images[1]) < 4: 71 register_image = register_image + "0" + images[1] + ".jpg" 72 else: 73 register_image = register_image + images[1] + ".jpg" 74 75 verify_image = image_path + images[0] + "/" + images[0] + "_" 76 if len(images[2]) < 2: 77 verify_image = verify_image + "000" + images[2] + ".jpg" 78 elif len(images[2]) < 3: 79 verify_image = verify_image + "00" + images[2] + ".jpg" 80 elif len(images[2]) < 4: 81 verify_image = verify_image + "0" + images[2] + ".jpg" 82 else: 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 90 if (int(count / 300)) % 2 == 0: 91 flag = 1 92 else: 93 flag = 0 94 95 if 'confidence' in content.keys(): 96 score = content['confidence'] 97 else: 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.测试结果有待分析。。