腾讯优图及知脸(ZKface)人脸比对接口测试(python)

时间:2023-07-05 08:49:44

一.腾讯优图

1.开发者地址:http://open.youtu.qq.com/welcome/developer

2.接入流程:按照开发者页面的接入流程接入之后,创建应用即可获得所需的AppID、SecretID和SecretKey这是进行接口调用必须的凭证

3.测试流程:

3.1.测试可以直接调用网络接口,或者下载相应语言的sdk(http://open.youtu.qq.com/welcome/developer#/tool-sdk),我采用的是下载python版本的sdk(该sdk对应的python2.x的版本)

3.2.测试的数据还是lfw的数据集,测试结果如果调用成功则获取到比对的分数,调用失败则将改组比对的分数设置为-1,以便后续的分析,直接上python代码:

 import TencentYoutuyun # 这是下载的python模块

 appid = 'xxxxx' # 你的appid
 secret_id = 'xxxxx' #你的secret_id
 secret_key = 'xxxxx' #你的secret_key
 userid = 'test' # 任意字符串
 end_point = TencentYoutuyun.conf.API_YOUTU_END_POINT
 youtu = TencentYoutuyun.YouTu(appid, secret_id, secret_key, userid, end_point)

 def compare_test():
     lfw_file = open("pairs.txt") # lfw的pair.txt 删除了第一行
     res_file = open("youtu.txt", "a+") # txt保存比对分数
     error_code = open("error_code.txt", "a+") # 测试用于保存调用出错编码
     count = 0
     while 1:
         image_path = "xxxxxx" # 你本地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 = youtu.FaceCompare(register_image, verify_image)
         error_code.write(str(content['errormsg']) + '\n')
         if content['errorcode'] != 0:
             score = -1
         else:
             score = content['similarity']

         print(str(count))
         print(register_image)
         print(verify_image)
         print(str(score) + '\n')

         if (int(count / 300)) % 2 == 0:
             flag = 1
         else:
             flag = 0

         res_file.write(line + '\t' + str(score) + '\t' + str(flag) + '\n')
         count += 1

     lfw_file.close()
     res_file.close()
     error_code.close()

 compare_test()

二.知脸(ZKfaceOnline)

1.知脸开发者中心地址:http://zkfaceonline.com:8003/developers_doc.html

2.接入流程:注册后可获取到Ocp-Apim-Subscription-Key,然后再开发者管理界面注册人脸比对数据库,记住该数据库的名称,该名称以及Ocp-Apim-Subscription-Key在后面的测试过程中都是必需的。

3.测试流程:

3.1.人脸比对分为两个步骤:先将需要比对的两张图片上传到指定接口做人脸特征的提取,如果成功则返回这两张图片在数据库中faceid,然后将返回的两个faceid提交到指定的人脸比对接口做比对,如果比对成功则会返回两张图片之间的相似值

3.2.具体测试代码,如下:

 # coding=utf-8
 import requests #该模块是第三方库,可以去网上下载
 import json

 def get_face_id(image): #上传一张图片做人脸检测,返回faceid
     url = 'http://zkfaceonline.com:8003/face/v0/faceApimatch?detect&db&matchDB=你的数据库名称'
     headers = {
         'Content-Type': 'application/octet-stream',
         'Ocp-Apim-Subscription-Key': '你自己的key',
     }
     data = open(image, "rb").read()
     try:
         res = requests.post(url, headers=headers, data=data)
     except Exception:
         res = requests.post(url, headers=headers, data=data)

     result = res.text.strip('this key has no database[]')
     try:
         result = json.loads(result)
         if len(result) == 1:
             res_dict = result[0]
             if "faceid" in res_dict.keys():
                 print(res_dict['faceid'])
                 return res_dict['faceid']
             else:
                 return '-1'
         else:
             return '-1'
     except Exception:
         return '-1'

 def face_compare(image1, image2): #两张图片做人脸比对,返回比对分数
     face1 = get_face_id(image1)
     face2 = get_face_id(image2)
     url = 'http://zkfaceonline.com:8003/face/v0/faceApimatch?match&db&matchDB=你的数据库名称&faceset1=' + face1 + '&faceset2=' + face2

     headers = {
         'Content-Type': 'application/octet-stream',
         'Ocp-Apim-Subscription-Key': '你自己的key',
     }
     try:
         res = requests.post(url, headers=headers)
     except Exception:
         res = requests.post(url, headers=headers)

     result = res.text.strip('this key has no database[]')
     print(res.text)
     try:
         result = json.loads(result)
         if len(result) == 1:
             res_dict = result[0]
             if 'similarity' in res_dict.keys():
                 print(res_dict['similarity'])
                 return res_dict['similarity']
             else:
                 return -1
         else:
             return -1
     except Exception:
         return -1

 def face_compare_test():
     lfw_file = open("pairs.txt")
     res_file = open("res_zk.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"

         print('\n' + str(count))
         print(register_image)
         print(verify_image)
         score = face_compare(verify_image, register_image)

         if (int(count / 300)) % 2 == 0:
             flag = 1
         else:
             flag = 0

         res_file.write(line + '\t' + str(score) + '\t' + str(flag) + '\n')
         count += 1

     lfw_file.close()
     res_file.close()

 face_compare_test()

三.性能评测

建议采用画rock曲线的方式进行比较,可以将百度和face++的人脸比对结果画在一张图上进行比对