码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2017-02-13 23:52:27      阅读:2840      评论:0      收藏:0      [点我收藏+]

标签:file   .text   pos   建议   自己   名称   开发者   cat   open   

一.腾讯优图

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代码:

 1 import TencentYoutuyun # 这是下载的python模块
 2 
 3 appid = xxxxx # 你的appid
 4 secret_id = xxxxx #你的secret_id
 5 secret_key = xxxxx #你的secret_key
 6 userid = test # 任意字符串
 7 end_point = TencentYoutuyun.conf.API_YOUTU_END_POINT
 8 youtu = TencentYoutuyun.YouTu(appid, secret_id, secret_key, userid, end_point)
 9 
10 
11 def compare_test():
12     lfw_file = open("pairs.txt") # lfw的pair.txt 删除了第一行
13     res_file = open("youtu.txt", "a+") # txt保存比对分数
14     error_code = open("error_code.txt", "a+") # 测试用于保存调用出错编码
15     count = 0
16     while 1:
17         image_path = "xxxxxx" # 你本地lfw数据的路径
18         line = lfw_file.readline()
19         if not line:
20             break
21         line = line.strip(\n)
22         images = line.split(\t)
23         if len(images) > 3:
24             register_image = image_path + images[0] + "/" + images[0] + "_"
25             if len(images[1]) < 2:
26                 register_image = register_image + "000" + images[1] + ".jpg"
27             elif len(images[1]) < 3:
28                 register_image = register_image + "00" + images[1] + ".jpg"
29             elif len(images[1]) < 4:
30                 register_image = register_image + "0" + images[1] + ".jpg"
31             else:
32                 register_image = register_image + images[1] + ".jpg"
33 
34             verify_image = image_path + images[2] + "/" + images[2] + "_"
35             if len(images[3]) < 2:
36                 verify_image = verify_image + "000" + images[3] + ".jpg"
37             elif len(images[3]) < 3:
38                 verify_image = verify_image + "00" + images[3] + ".jpg"
39             elif len(images[3]) < 4:
40                 verify_image = verify_image + "0" + images[3] + ".jpg"
41             else:
42                 verify_image = verify_image + images[3] + ".jpg"
43         else:
44             register_image = image_path + images[0] + "/" + images[0] + "_"
45             if len(images[1]) < 2:
46                 register_image = register_image + "000" + images[1] + ".jpg"
47             elif len(images[1]) < 3:
48                 register_image = register_image + "00" + images[1] + ".jpg"
49             elif len(images[1]) < 4:
50                 register_image = register_image + "0" + images[1] + ".jpg"
51             else:
52                 register_image = register_image + images[1] + ".jpg"
53 
54             verify_image = image_path + images[0] + "/" + images[0] + "_"
55             if len(images[2]) < 2:
56                 verify_image = verify_image + "000" + images[2] + ".jpg"
57             elif len(images[2]) < 3:
58                 verify_image = verify_image + "00" + images[2] + ".jpg"
59             elif len(images[2]) < 4:
60                 verify_image = verify_image + "0" + images[2] + ".jpg"
61             else:
62                 verify_image = verify_image + images[2] + ".jpg"
63 
64         content = youtu.FaceCompare(register_image, verify_image)
65         error_code.write(str(content[errormsg]) + \n)
66         if content[errorcode] != 0:
67             score = -1
68         else:
69             score = content[similarity]
70 
71         print(str(count))
72         print(register_image)
73         print(verify_image)
74         print(str(score) + \n)
75 
76         if (int(count / 300)) % 2 == 0:
77             flag = 1
78         else:
79             flag = 0
80 
81         res_file.write(line + \t + str(score) + \t + str(flag) + \n)
82         count += 1
83 
84     lfw_file.close()
85     res_file.close()
86     error_code.close()
87 
88 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.具体测试代码,如下:

  1 # coding=utf-8
  2 import requests #该模块是第三方库,可以去网上下载
  3 import json
  4 
  5 
  6 def get_face_id(image): #上传一张图片做人脸检测,返回faceid
  7     url = http://zkfaceonline.com:8003/face/v0/faceApimatch?detect&db&matchDB=你的数据库名称
  8     headers = {
  9         Content-Type: application/octet-stream,
 10         Ocp-Apim-Subscription-Key: 你自己的key,
 11     }
 12     data = open(image, "rb").read()
 13     try:
 14         res = requests.post(url, headers=headers, data=data)
 15     except Exception:
 16         res = requests.post(url, headers=headers, data=data)
 17 
 18     result = res.text.strip(this key has no database[])
 19     try:
 20         result = json.loads(result)
 21         if len(result) == 1:
 22             res_dict = result[0]
 23             if "faceid" in res_dict.keys():
 24                 print(res_dict[faceid])
 25                 return res_dict[faceid]
 26             else:
 27                 return -1
 28         else:
 29             return -1
 30     except Exception:
 31         return -1
 32 
 33 
 34 def face_compare(image1, image2): #两张图片做人脸比对,返回比对分数
 35     face1 = get_face_id(image1)
 36     face2 = get_face_id(image2)
 37     url = http://zkfaceonline.com:8003/face/v0/faceApimatch?match&db&matchDB=你的数据库名称&faceset1= + face1 + &faceset2= + face2
 38 
 39     headers = {
 40         Content-Type: application/octet-stream,
 41         Ocp-Apim-Subscription-Key: 你自己的key,
 42     }
 43     try:
 44         res = requests.post(url, headers=headers)
 45     except Exception:
 46         res = requests.post(url, headers=headers)
 47 
 48     result = res.text.strip(this key has no database[])
 49     print(res.text)
 50     try:
 51         result = json.loads(result)
 52         if len(result) == 1:
 53             res_dict = result[0]
 54             if similarity in res_dict.keys():
 55                 print(res_dict[similarity])
 56                 return res_dict[similarity]
 57             else:
 58                 return -1
 59         else:
 60             return -1
 61     except Exception:
 62         return -1
 63 
 64 
 65 def face_compare_test():
 66     lfw_file = open("pairs.txt")
 67     res_file = open("res_zk.txt", "a+")
 68     count = 0
 69     while 1:
 70         image_path = "你lfw数据集在本地的路径"
 71         line = lfw_file.readline()
 72         if not line:
 73             break
 74         line = line.strip(\n)
 75         images = line.split(\t)
 76         if len(images) > 3:
 77             register_image = image_path + images[0] + "/" + images[0] + "_"
 78             if len(images[1]) < 2:
 79                 register_image = register_image + "000" + images[1] + ".jpg"
 80             elif len(images[1]) < 3:
 81                 register_image = register_image + "00" + images[1] + ".jpg"
 82             elif len(images[1]) < 4:
 83                 register_image = register_image + "0" + images[1] + ".jpg"
 84             else:
 85                 register_image = register_image + images[1] + ".jpg"
 86 
 87             verify_image = image_path + images[2] + "/" + images[2] + "_"
 88             if len(images[3]) < 2:
 89                 verify_image = verify_image + "000" + images[3] + ".jpg"
 90             elif len(images[3]) < 3:
 91                 verify_image = verify_image + "00" + images[3] + ".jpg"
 92             elif len(images[3]) < 4:
 93                 verify_image = verify_image + "0" + images[3] + ".jpg"
 94             else:
 95                 verify_image = verify_image + images[3] + ".jpg"
 96         else:
 97             register_image = image_path + images[0] + "/" + images[0] + "_"
 98             if len(images[1]) < 2:
 99                 register_image = register_image + "000" + images[1] + ".jpg"
100             elif len(images[1]) < 3:
101                 register_image = register_image + "00" + images[1] + ".jpg"
102             elif len(images[1]) < 4:
103                 register_image = register_image + "0" + images[1] + ".jpg"
104             else:
105                 register_image = register_image + images[1] + ".jpg"
106 
107             verify_image = image_path + images[0] + "/" + images[0] + "_"
108             if len(images[2]) < 2:
109                 verify_image = verify_image + "000" + images[2] + ".jpg"
110             elif len(images[2]) < 3:
111                 verify_image = verify_image + "00" + images[2] + ".jpg"
112             elif len(images[2]) < 4:
113                 verify_image = verify_image + "0" + images[2] + ".jpg"
114             else:
115                 verify_image = verify_image + images[2] + ".jpg"
116 
117         print(\n + str(count))
118         print(register_image)
119         print(verify_image)
120         score = face_compare(verify_image, register_image)
121 
122         if (int(count / 300)) % 2 == 0:
123             flag = 1
124         else:
125             flag = 0
126 
127         res_file.write(line + \t + str(score) + \t + str(flag) + \n)
128         count += 1
129 
130     lfw_file.close()
131     res_file.close()
132 
133 face_compare_test()

三.性能评测

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

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

标签:file   .text   pos   建议   自己   名称   开发者   cat   open   

原文地址:http://www.cnblogs.com/battlescars/p/python_youtu_zkface_test.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!