当前位置:   article > 正文

使用讯飞人脸对比API_signature_origin = "host: {}\ndate: {}\n{} {} http

signature_origin = "host: {}\ndate: {}\n{} {} http/1.1".format(host, date, m

参考官方文档: 讯飞人脸对比Web API文档

附加文档:错误码查询

运行前:请先填写appid、apisecret、apikey以及图片路径

appid、apisecret、apikey在控制台创建新应用后获取

  1. from datetime import datetime
  2. from wsgiref.handlers import format_date_time
  3. from time import mktime
  4. import hashlib
  5. import base64
  6. import hmac
  7. from urllib.parse import urlencode
  8. import os
  9. import traceback
  10. import json
  11. import requests
  12. class AssembleHeaderException(Exception):
  13. def __init__(self, msg):
  14. self.message = msg
  15. class Url:
  16. def __init__(this, host, path, schema):
  17. this.host = host
  18. this.path = path
  19. this.schema = schema
  20. pass
  21. # 进行sha256加密和base64编码
  22. def sha256base64(data):
  23. sha256 = hashlib.sha256()
  24. sha256.update(data)
  25. digest = base64.b64encode(sha256.digest()).decode(encoding='utf-8')
  26. return digest
  27. def parse_url(requset_url):
  28. stidx = requset_url.index("://")
  29. host = requset_url[stidx + 3:]
  30. schema = requset_url[:stidx + 3]
  31. edidx = host.index("/")
  32. if edidx <= 0:
  33. raise AssembleHeaderException("invalid request url:" + requset_url)
  34. path = host[edidx:]
  35. host = host[:edidx]
  36. u = Url(host, path, schema)
  37. return u
  38. def assemble_ws_auth_url(requset_url, method="GET", api_key="", api_secret=""):
  39. u = parse_url(requset_url)
  40. host = u.host
  41. path = u.path
  42. now = datetime.now()
  43. date = format_date_time(mktime(now.timetuple()))
  44. print(date)
  45. # date = "Thu, 12 Dec 2019 01:57:27 GMT"
  46. signature_origin = "host: {}\ndate: {}\n{} {} HTTP/1.1".format(host, date, method, path)
  47. print(signature_origin)
  48. signature_sha = hmac.new(api_secret.encode('utf-8'), signature_origin.encode('utf-8'),
  49. digestmod=hashlib.sha256).digest()
  50. signature_sha = base64.b64encode(signature_sha).decode(encoding='utf-8')
  51. authorization_origin = "api_key=\"%s\", algorithm=\"%s\", headers=\"%s\", signature=\"%s\"" % (
  52. api_key, "hmac-sha256", "host date request-line", signature_sha)
  53. authorization = base64.b64encode(authorization_origin.encode('utf-8')).decode(encoding='utf-8')
  54. print(authorization_origin)
  55. values = {
  56. "host": host,
  57. "date": date,
  58. "authorization": authorization
  59. }
  60. return requset_url + "?" + urlencode(values)
  61. def gen_body(appid, img1_path, img2_path, server_id):
  62. with open(img1_path, 'rb') as f:
  63. img1_data = f.read()
  64. with open(img2_path, 'rb') as f:
  65. img2_data = f.read()
  66. body = {
  67. "header": {
  68. "app_id": appid,
  69. "status": 3
  70. },
  71. "parameter": {
  72. server_id: {
  73. "service_kind": "face_compare",
  74. "face_compare_result": {
  75. "encoding": "utf8",
  76. "compress": "raw",
  77. "format": "json"
  78. }
  79. }
  80. },
  81. "payload": {
  82. "input1": {
  83. "encoding": "jpg",
  84. "status": 3,
  85. "image": str(base64.b64encode(img1_data), 'utf-8')
  86. },
  87. "input2": {
  88. "encoding": "jpg",
  89. "status": 3,
  90. "image": str(base64.b64encode(img2_data), 'utf-8')
  91. }
  92. }
  93. }
  94. return json.dumps(body)
  95. def run(appid, apikey, apisecret, img1_path, img2_path, server_id='s67c9c78c'):
  96. url = 'http://api.xf-yun.com/v1/private/{}'.format(server_id)
  97. request_url = assemble_ws_auth_url(url, "POST", apikey, apisecret)
  98. headers = {'content-type': "application/json", 'host': 'api.xf-yun.com', 'app_id': appid}
  99. print(request_url)
  100. response = requests.post(request_url, data=gen_body(appid, img1_path, img2_path, server_id), headers=headers)
  101. resp_data = json.loads(response.content.decode('utf-8'))
  102. print(resp_data)
  103. print(base64.b64decode(resp_data['payload']['face_compare_result']['text']).decode())
  104. #请填写控制台获取的APPID、APISecret、APIKey以及要比对的图片路径
  105. if __name__ == '__main__':
  106. run(
  107. appid='',
  108. apisecret='',
  109. apikey='',
  110. img1_path=r'C:\Users\Administrator.SC-201909161358\Desktop\微信图片_20230306093711.png',
  111. img2_path=r'C:\Users\Administrator.SC-201909161358\Desktop\微信图片_20230306093725.png',
  112. )

返回结果:

ret为0代表成功 (其他返回码参考错误码查询

score代码相似度 (0~1)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/225359
推荐阅读
相关标签
  

闽ICP备14008679号