百度人脸识别:最简单的Python调用

  • 安装
​pip install baidu-aip
  •  代码
# -*- coding: UTF-8 -*-  

from aip import AipFace
import base64

# 参考《百度人脸识别:功能开通》
BAIDU_APP_ID     = "15"
BAIDU_API_KEY    = "4o"
BAIDU_SECRET_KEY = "PY"

BAIDU_IMAGE_TYPE = "BASE64"
BAIDU_GROUP_ID   = "quantum6"

AVATAR_PATH      = "../faceid_avatar/"

# 图片转换为base64
def baidu_face_image_to_base64(image_file):
    image_open = open(image_file, 'rb')
    image_data = base64.b64encode(image_open.read())
    image_data = str(image_data)
    return image_data

# 初始化,读取某个目录的员工头像,并加到百度上
def baidu_face_init():
    global baidu_face_client
    baidu_face_client = AipFace(BAIDU_APP_ID, BAIDU_API_KEY, BAIDU_SECRET_KEY)
    images = glob.glob(os.path.join(AVATAR_PATH, "*.jpg"))
    if len(images) == 0:
        raise RuntimeError("no person in the database, please check folder.")
    for image in images:
        image_base64 = baidu_face_image_to_base64(image)
        # 从文件名中截取
        user_id = os.path.basename(image)[:-4]
        response = baidu_face_client.addUser(image_base64, BAIDU_IMAGE_TYPE, BAIDU_GROUP_ID, user_id)
        print(response)

# 检查这个图片是否是员工
def baidu_face_check(image):
    image_base64 = baidu_face_image_to_base64(image)
    response = baidu_face_client.search(image_base64, BAIDU_IMAGE_TYPE, BAIDU_GROUP_ID)
    print(response)

    if (0 == response["error_code"]):
        response = response["result"]["user_list"][0]
        user_id  = response["user_id"]
        score    = response["score"]
        print(user_id, score)
        return user_id, score
    else:
        return "error"


# 检查是否有人脸
def baidu_face_dected(image_file):
    options = {}
    options["max_face_num"] = 3

    image_base64 = baidu_face_image_to_base64(image)
    response = baidu_face_client.detect(image_base64, BAIDU_IMAGE_TYPE, options)
    if (0 == response["error_code"]):
        print(response["result"])
    else:
        return TEXT_NOT_FOUND

# TEST
baidu_face_init()
TEST_STRANGER_FILE="../faceid_stranger_avatar/2.jpg"
# 可以先判断是否有人脸,再进行比较
#baidu_face_dected(TEST_STRANGER_FILE)

baidu_face_check(TEST_STRANGER_FILE)

 

识别结果大概是这样 {'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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

柳鲲鹏

能给阁下一点帮助,非常荣幸

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值