参考官方文档: 讯飞人脸对比Web API文档
附加文档:错误码查询
运行前:请先填写appid、apisecret、apikey以及图片路径
appid、apisecret、apikey在控制台创建新应用后获取
from datetime import datetime
from wsgiref.handlers import format_date_time
from time import mktime
import hashlib
import base64
import hmac
from urllib.parse import urlencode
import os
import traceback
import json
import requestsclass AssembleHeaderException(Exception):def __init__(self, msg):self.message = msgclass Url:def __init__(this, host, path, schema):this.host = hostthis.path = paththis.schema = schemapass# 进行sha256加密和base64编码
def sha256base64(data):sha256 = hashlib.sha256()sha256.update(data)digest = base64.b64encode(sha256.digest()).decode(encoding='utf-8')return digestdef parse_url(requset_url):stidx = requset_url.index("://")host = requset_url[stidx + 3:]schema = requset_url[:stidx + 3]edidx = host.index("/")if edidx <= 0:raise AssembleHeaderException("invalid request url:" + requset_url)path = host[edidx:]host = host[:edidx]u = Url(host, path, schema)return udef assemble_ws_auth_url(requset_url, method="GET", api_key="", api_secret=""):u = parse_url(requset_url)host = u.hostpath = u.pathnow = datetime.now()date = format_date_time(mktime(now.timetuple()))print(date)# date = "Thu, 12 Dec 2019 01:57:27 GMT"signature_origin = "host: {}\ndate: {}\n{} {} HTTP/1.1".format(host, date, method, path)print(signature_origin)signature_sha = hmac.new(api_secret.encode('utf-8'), signature_origin.encode('utf-8'),digestmod=hashlib.sha256).digest()signature_sha = base64.b64encode(signature_sha).decode(encoding='utf-8')authorization_origin = "api_key=\"%s\", algorithm=\"%s\", headers=\"%s\", signature=\"%s\"" % (api_key, "hmac-sha256", "host date request-line", signature_sha)authorization = base64.b64encode(authorization_origin.encode('utf-8')).decode(encoding='utf-8')print(authorization_origin)values = {"host": host,"date": date,"authorization": authorization}return requset_url + "?" + urlencode(values)def gen_body(appid, img1_path, img2_path, server_id):with open(img1_path, 'rb') as f:img1_data = f.read()with open(img2_path, 'rb') as f:img2_data = f.read()body = {"header": {"app_id": appid,"status": 3},"parameter": {server_id: {"service_kind": "face_compare","face_compare_result": {"encoding": "utf8","compress": "raw","format": "json"}}},"payload": {"input1": {"encoding": "jpg","status": 3,"image": str(base64.b64encode(img1_data), 'utf-8')},"input2": {"encoding": "jpg","status": 3,"image": str(base64.b64encode(img2_data), 'utf-8')}}}return json.dumps(body)def run(appid, apikey, apisecret, img1_path, img2_path, server_id='s67c9c78c'):url = 'http://api.xf-yun.com/v1/private/{}'.format(server_id)request_url = assemble_ws_auth_url(url, "POST", apikey, apisecret)headers = {'content-type': "application/json", 'host': 'api.xf-yun.com', 'app_id': appid}print(request_url)response = requests.post(request_url, data=gen_body(appid, img1_path, img2_path, server_id), headers=headers)resp_data = json.loads(response.content.decode('utf-8'))print(resp_data)print(base64.b64decode(resp_data['payload']['face_compare_result']['text']).decode())#请填写控制台获取的APPID、APISecret、APIKey以及要比对的图片路径
if __name__ == '__main__':run(appid='',apisecret='',apikey='',img1_path=r'C:\Users\Administrator.SC-201909161358\Desktop\微信图片_20230306093711.png',img2_path=r'C:\Users\Administrator.SC-201909161358\Desktop\微信图片_20230306093725.png',)
返回结果:
ret为0代表成功 (其他返回码参考错误码查询)
score代码相似度 (0~1)