python+tkinter百度云人脸识别可视化小工具

首先看效果图python+tkinter百度云人脸识别可视化小工具

小工具基于百度云人脸识别api制作,可视化操作。

首先申请百度云的账号,不用本菜鸟多说,

python+tkinter百度云人脸识别可视化小工具

记住里面的appid,apikey secretkey

python+tkinter百度云人脸识别可视化小工具

百度技术文档里找V3的说明

人脸检测的文档

https://cloud.baidu.com/doc/FACE/Face-Detect.html

人脸对比的文档

https://cloud.baidu.com/doc/FACE/Face-Match.html#Face-Match

需要获取 “Access Token

方法如下

通过requests的post方法可获取access_token值,30天有效期,API_Key,Secret_Key值上面已获得,直接使用

url = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id={}&client_secret={}'.format(API_Key,Secret_Key)
header={'Content-Type':'application/json; charset=UTF-8'}

def get_access_token():#获取 access_token
    res = requests.post(url).content.decode('utf-8')
    res=json.loads(res)
    if 'error' in res:
        print('解析失败')
    else:
        access_token=res['access_token']
        # print(access_token)
        return access_token

接下来测试图片信息

如果是本地图片,必须对图片进行base64编码

这是图片检测的请求参数

参数 必选 类型 说明
image string base64编码后的图片数据,需urlencode,编码后的图片大小不超过2M
max_face_num uint32 最多处理人脸的数目,默认值为1,仅检测图片中面积最大的那个人脸
face_fields string 包括age,beauty,expression,faceshape,gender,glasses,landmark,race,qualities信息,逗号分隔,默认只返回人脸框、概率和旋转角度
base64编码方法如下
def trans_base64(path):# 对图片进行base64编码
    with open(path,'rb') as f:
        data = f.read()
        encodestr = base64.b64encode(data)  # 得到 byte 编码的数据
        s=str(encodestr, 'utf-8')
        # print(s)
        return s

def check_img(flag,path):#面部识别
    cli=AipFace(AppId,API_Key,Secret_Key)
    image, image_type='',''
    if flag=='1':
        image='https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1538151319150&di=196eda34652fd03b3335ec68aca74122&imgtype=0&src=http%3A%2F%2F01.minipic.eastday.com%2F20170306%2F20170306130710_4f981e8e34f472af11274a7cc68fa3bb_4.jpeg'
        image_type = 'URL'
    elif flag=='2':
        image=trans_base64(path)
        image_type = 'BASE64'
    options={}
    options['face_field']='age,beauty'
    options['max_face_num']=1
    options['face_type']='LIVE'
    res=cli.detect(image,image_type,options)
    print(res)
    if res['error_code']==0:
        print('读取成功')
        face_num=res['result']['face_num']
        face_age=res['result']['face_list'][0]['age']
        face_beaty=res['result']['face_list'][0]['beauty']
        print('人脸数量',face_num)
        print('人脸年龄',face_age)
        print('颜值',face_beaty)
        return face_num,face_age,face_beaty
    else:
        print('读取失败')

输出结果

测试用图片

python+tkinter百度云人脸识别可视化小工具

输出json数据

{'error_code': 0, 'error_msg': 'SUCCESS', 'log_id': 10545051510, 'timestamp': 1538189699, 'cached': 0, 'result': {'face_num': 1, 'face_list': [{'face_token': '1474a50eec11226d009e0f35b031c6f4', 'location': {'left': 454.1082764, 'top': 247.6040649, 'width': 68, 'height': 73, 'rotation': 7}, 'face_probability': 1, 'angle': {'yaw': -4.729311466, 'pitch': -6.492731571, 'roll': 6.83347559}, 'age': 23, 'beauty': 66.00269318}]}}
读取成功
人脸数量 1
人脸年龄 23
颜值 66.00269318

检测还是挺准的

接下来测试2张图片对比

这是图片对比的请求参数

参数 必选 类型 说明
images string 分别base64编码后的2张图片数据,需urlencode,半角逗号分隔,单次请求最大不超过20M
ext_fields string 返回质量信息,取值固定,目前支持qualities(质量检测)(对所有图片都会做改处理)
image_liveness string 返回的活体信息,“faceliveness,faceliveness” 表示对比对的两张图片都做活体检测;“,faceliveness” 表示对第一张图片不做活体检测、第二张图做活体检测;“faceliveness,” 表示对第一张图片做活体检测、第二张图不做活体检测;

代码如下

def check_face(flag1,flag2,path_1,path_2):#对比图片
    url='https://aip.baidubce.com/rest/2.0/face/v3/match'
    options=[{'image':'','image_type':'','face_type':'LIVE','quality_control':'LOW'},
             {'image':'','image_type':'','face_type':'LIVE','quality_control':'LOW'}]
    access_token=get_access_token()#得到access_token
    full_url=url+"?access_token=" + access_token
    if flag1=='1':#通道1 url
        if flag2=='1':#通道2 url
            options[0]['image']=''
            options[0]['image_type'] = 'URL'
            options[1]['image'] = ''
            options[1]['image_type'] = 'URL'
        elif flag2=='2':#通道2 base64
            options[0]['image']=''
            options[0]['image_type'] = 'URL'
            options[1]['image'] = trans_base64(path_2)
            options[1]['image_type'] = 'BASE64'
    elif flag1=='2':#通道1 base64
        if flag2=='1':#通道2 url
            options[0]['image']=trans_base64(path_1)
            options[0]['image_type'] = 'BASE64'
            options[1]['image'] = ''
            options[1]['image_type'] = 'URL'
        elif flag2=='2':#通道2 base64
            options[0]['image']=trans_base64(path_1)
            options[0]['image_type'] = 'BASE64'
            options[1]['image'] = trans_base64(path_2)
            options[1]['image_type'] = 'BASE64'
    res=requests.post(full_url,data=json.dumps(options),headers=header).content.decode('utf-8')
    print(res)
    res=json.loads(res)
    if res['error_code']==0:
        print('读取成功')
        face_res=res['result']['score']
        print('人脸相似度',face_res)
        return face_res
    else:
        print('读取失败')

 

测试用图片2张

python+tkinter百度云人脸识别可视化小工具                                                                 python+tkinter百度云人脸识别可视化小工具

输出结果

{"error_code":0,"error_msg":"SUCCESS","log_id":1019445001452,"timestamp":1538189910,"cached":0,"result":{"score":73.89654541,"face_list":[{"face_token":"1474a50eec11226d009e0f35b031c6f4"},{"face_token":"7120d4f0520d094d3bcd4a0c6da023d8"}]}}
读取成功
人脸相似度 73.89654541