百度人脸识别BFR API效果测试及测试代码

先简单说一下测试完百度人脸识别BFR的结果:

1、我的多张生活照之间(拍摄间隔时间较短),测试相似度平均在95%以上;

2、我的身份证照片和多张最近的生活照(换了发型且相隔时间超过8年),相似度平均在85%以上;

3、我的身份证照和多张监控摄像头抓拍照片,相似度基本维持在85%左右小幅波动;

4、我的身份证照和多张监控摄像头抓拍的其他人照片,相似度基本维持在22%以下;

      综合上述效果,如果不考虑为公安系统等数据安全非常重要的应用(该测试需要上传编码后的图片进行对比相似度),比如酒店的VIP迎宾系统,可以考虑使用BFR API。所以,百度除了会推荐医院还会做点技术的。

       无图无真相,自己的身份证照和生活照就不放出来了。继续感受下不老男神华仔、小鲜肉夫仔和实力派黄渤的对比效果(第一张和后四张的match得分):


       match得分分别为:



扫描二维码关注公众号,回复: 901504 查看本文章



所以可以说用来做人脸识别目前足够了。有兴趣的可以copy下边的python代码继续进一步测试。

顺便让大家感受下摄像头捕捉的渣渣画面(我的人脸就是从类似的渣渣画面检测到并识别的):



最后祭出百度云BFR的官网,https://cloud.baidu.com/product/bfr.html。

以及测试的代码:(测试代码后给出了代码使用方法)

#!/usr/bin/python
#encoding=utf8

import os
import sys
import urllib
import urllib2
import json
import hashlib
import hmac
import time
import datetime
import base64
from urllib import urlencode
import urllib2
from urllib import quote
from urlparse import urlparse

def gen_auth(access_key, secret_key, utc_time_str, url, method):
    url_parse_ret = urlparse(url)
    host = url_parse_ret.hostname
    path = url_parse_ret.path
    version = "1"
    expiration_seconds = "1800"
    signature_headers = "host"

    # 1 Generate SigningKey
    val = "bce-auth-v%s/%s/%s/%s" % (version, access_key, utc_time_str, expiration_seconds)
    signing_key = hmac.new(secret_key, val, hashlib.sha256).hexdigest().encode('utf-8')

    # 2 Generate CanonicalRequest
    # 2.1 Genrate CanonicalURI
    canonical_uri = quote(path)
    # 2.2 Generate CanonicalURI: not used here
    # 2.3 Generate CanonicalHeaders: only include host here
    canonical_headers = "host:%s" % quote(host).strip()
    # 2.4 Generate CanonicalRequest
    canonical_request = "%s\n%s\n\n%s" % (method.upper(), canonical_uri, canonical_headers)

    # 3 Generate Final Signature 
    signature = hmac.new(signing_key, canonical_request, hashlib.sha256).hexdigest()
    authorization = "bce-auth-v%s/%s/%s/%s/%s/%s" % (version, access_key, utc_time_str, expiration_seconds, signature_headers, signature)
    print authorization
    return authorization

def get_image(files):
    images = ''
    for file in files:
        print files
        handler = open(file, 'rb')
        image = handler.read()
        handler.close()
        if (images != ''):
            images += ','
        images += base64.b64encode(image)
    return images


if __name__ == "__main__":
    access_key = "AK"
    secret_key = "SK"
    url = "http://bfr.bj.baidubce.com/api/v1/faceverify/user/match"
    method = "POST"
    utc_time = datetime.datetime.utcnow()
    utc_time_str = utc_time.strftime("%Y-%m-%dT%H:%M:%SZ")
    auth = gen_auth(access_key, secret_key, utc_time_str, url, method)
    header = {
        'Host':'bfr.bj.baidubce.com',
        'x-bce-date': utc_time_str,
        'authorization': auth,
        'accept':'*/*'
    }
    files=["match1.jpg","match2.jpg"]
    data = {
        'images': get_image(files),
    }
    request = urllib2.Request(url, urlencode(data), header)
    response = None
    try :
        response = urllib2.urlopen(request)
        post_res_str = response.read()
        print post_res_str
    except Exception as e:
        print e
    

使用方法:

1、文件夹下,新建文件match.py,copy以上代码进去。

2、修改access_key = "AK"   secret_key = "SK"中的AK和SK为自己的AK,SK(AK和SK是32byte长的字符串,包含32位字母或数字)。其中AK,SK在百度BFR官网申请。如下图:


3、同一个文件夹下放入两张图片,重命名为match1.jpg,match2.jpg。

4、使用python运行以上代码即可。

有任何问题,欢迎Email:[email protected]

猜你喜欢

转载自blog.csdn.net/jwwxuxu/article/details/53883568