python-百度云人脸识别——调用方法(1)

首次写博客,谢谢您的阅读

本教程前提是安装好环境
先附上例程

from aip import AipFace
import base64

# 定义常量
APP_ID ="xxxxxx"#xxxx换成自己的APP_ID 
API_KEY ="xxxxxx"#xxxx换成自己的API_KEY 
SECRET_KEY ="xxxxxx"#xxxx换成自己的SECRET_KEY 
imageType = "BASE64"
groupIdList = "1"   #人脸组

filePath="123.jpg"#照片路径
# 初始化AipFace对象
client = AipFace(APP_ID, API_KEY, SECRET_KEY)

f=open(filePath,"rb")
data = base64.b64encode(f.read())#编码格式,技术文档要求
f.close()
image=str(data,'UTF-8')

result = client.search(image, imageType, groupIdList);

if result["error_msg"] in "SUCCESS":
    score=result["result"]["user_list"][0]["score"]
    user_id=result["result"]["user_list"][0]["user_id"]
    if score>80:
        print(user_id,":验证成功")
    else:
        print("没有找到此人")
else:
    print("error:",result["error_msg"])

以上代码是识别人脸,通过对比你百度云人脸库的照片,返回结果,其中:
result是一个dict字典
其内容是
{
‘error_code’: 0,
‘error_msg’: ‘SUCCESS’,
‘log_id’: 744193247093702911,
‘timestamp’: 1544709370,
‘cached’: 0,
‘result’: {‘face_token’: ‘878857faccb27a83a2206082abe22a11’,
‘user_list’: [
{
‘group_id’: ‘1’,
‘user_id’: ‘123’,
‘user_info’: ‘’,
‘score’: 100
}
]
}
}
通过查找得到的score就是人脸对比的像是结果,一般score大于80就认为是同一个人
score=result[“result”][“user_list”][0][“score”]

附上search的函数接口代码
来源于API的face.py

def search(self, image, image_type, group_id_list, options=None):
        """
            人脸搜索
        """
        options = options or {}

        data = {}
        data['image'] = image
        data['image_type'] = image_type
        data['group_id_list'] = group_id_list

        data.update(options)
        return self._request(self.__searchUrl, json.dumps(data, ensure_ascii=False), {
            'Content-Type': 'application/json',
        })

本人刚学python,在这个例程所遇到的坑是 search接口参数image的格式问题
这是百度云的技术文档里面的截图附上编码解码的代码

f=open(filePath,"rb")
data = base64.b64encode(f.read())#编码格式,技术文档要求
f.close()
image=str(data,'UTF-8')

可以将这4行代码封装成函数,得到image传给search的第一参数image

到此,本教程结束。
通过调用百度的API,做了一个人脸考勤系统,后期再分享出来。
对此还有其他问题,欢迎留言,一起学习探讨。

  • 5
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
识别结果大概是这样 {'result': {'face_num': 1, 'face_list': [{'quality': {'occlusion': {'right_eye': 0, 'left_cheek': 0.1459853947, 'right_cheek': 0.05144193396, 'left_eye': 0.465408802, 'mouth': 0.02919708006, 'chin_contour': 0.01420217194, 'nose': 0}, 'illumination': 116, 'blur': 7.266304692e-06, 'completeness': 1}, 'age': 22, 'face_token': 'dc6f8f9df5d977ea476e2d04acdf5063', 'race': {'type': 'white', 'probability': 0.6173604727}, 'glasses': {'type': 'common', 'probability': 0.9834988713}, 'gender': {'type': 'male', 'probability': 0.655915916}, 'face_probability': 0.9185044169, 'beauty': 51.21487427, 'angle': {'roll': -2.750922441, 'yaw': 28.97134399, 'pitch': 5.202290535}, 'location': {'height': 65, 'top': 112.0704803, 'width': 76, 'left': 76.20765686, 'rotation': -4}, 'face_type': {'type': 'human', 'probability': 0.9992217422}, 'face_shape': {'type': 'oval', 'probability': 0.4419156313}, 'expression': {'type': 'none', 'probability': 0.9999142885}}]}, 'error_msg': 'SUCCESS', 'timestamp': 1537413754, 'cached': 0, 'error_code': 0, 'log_id': 9465840013520} 年龄:22 颜值:51.21487427 表情-type(none:不笑;smile:微笑;laugh:大笑):none 表情-probability(表情置信度,范围【0~1】,0最小、1最大):0.9999142885 脸型-type(square: 正方形 triangle:三角形 oval: 椭圆 heart: 心形 round: 圆形):oval 脸型-probability(置信度,范围【0~1】,代表这是人脸形状判断正确的概率,0最小、1最大):0.4419156313 性别-type(male:男性 female:女性):male 性别-probability(性别置信度,范围【0~1】,0代表概率最小、1代表最大。):0.655915916 是否带眼镜-type(none:无眼镜,common:普通眼镜,sun:墨镜):common 是否带眼镜-probability(眼镜置信度,范围【0~1】,0代表概率最小、1代表最大。):0.9834988713 人种-type(yellow: 黄种人 white: 白种人 black:黑种人 arabs: 阿拉伯人):white 人种-probability(人种置信度,范围【0~1】,0代表概率最小、1代表最大。):0.6173604727 真实人脸/卡通人脸 -type(human: 真实人脸 cartoon: 卡通人脸):human 真实人脸/卡通人脸 -probability(人脸类型判断正确的置信度,范围【0~1】,0代表概率最小、1代表最大。):0.9992217422
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值