CDN加速

人脸对比

更新时间:2019-12-12 17:25:43
适用产品:定制化接口

接口描述

根据传上来的两张人脸图片,对比其相似度

请求参数

参数名称类型必填描述
file1string用户将待上传的人脸图片编码为base64字符串。通过此字段上传。
file2string用户将待上传的人脸图片编码为base64字符串。通过此字段上传。

返回参数

参数名称类型描述
codestring返回调用结果的状态
messagestring调用接口,返回状态码的说明
datafloat返回两张人脸的相似度

错误码

错误代码(code)描述(message)HTTP状态码语义
30052002post字段没有图片内容200

示例

示例说明
请求示例
复制
#!/usr/bin/env python
# -- coding: utf-8 --

import requests
import sys
import os
import json
import base64
from hashlib import sha256

url = 'http://open.chinanetcenter.com/wsface/v1/face/verify'
username = 'xxx'
apikey = 'xxx'
__curPath__ = os.path.split(os.path.realpath(__file__))[0]


def getDate():
    import datetime
    GMT_FORMAT = '%a, %d %b %Y %H:%M:%S GMT'
    date_gmt = datetime.datetime.utcnow().strftime(GMT_FORMAT)
    print("getDate: " + date_gmt)
    return date_gmt


def sha(date):
    import hmac
    print("sha: " + date)
    signed_apikey = hmac.new(apikey.encode(
        'utf-8'), date.encode('utf-8'), sha256).digest()
    signed_apikey = base64.b64encode(signed_apikey)
    print("sha: " + signed_apikey.decode())
    return signed_apikey


def encode(auth):
    print("encode: " + auth.decode())
    msg = username + ":" + auth.decode()
    result = base64.b64encode(msg.encode('utf-8'))
    print("encode: " + result.decode())
    return result


def getAuth():
    return encode(sha(getDate()))


def getABase64StrFromFile(fname):
    with open(fname, 'rb') as f:
        c = f.read()
        c = base64.b64encode(c)
        return str(c, encoding='utf-8')


def connect(fname1='./img/Adam_Sandler_0001.jpg', fname2='./img/Adam_Sandler_0001.jpg'):
    auth = getAuth()
    auth = 'Basic ' + auth.decode()
    d = {'file1': getABase64StrFromFile(
        fname1), 'file2': getABase64StrFromFile(fname2)}
    headers = {'Content-Type': 'application/json', 'Accept': "application/json",
               "Date": getDate(), 'Authorization': auth}
    r = requests.post(url, headers=headers, data=json.dumps(d))
    ret = r.text
    print(ret)

if __name__ == '__main__':
    connect()
返回示例
复制
{"code": "0", "message": "success", "data": 0.99}