百度智能云的Python-SDK调用实现人脸认证身份、人脸库管理(两步实现)

第一步(创建API)

        登录或者注册百度智能云,百度智能云官网地址,也可以直接搜“百度智能云”就OK了。登录成功后,在产品服务搜索人脸识别。在人脸识别产品页面创建自己的应用。

个人注册后的有免费的资源,如人脸搜索、人脸检测、人脸库管理等,其实这些功能就可以实现人脸认证了,各位根据自己需求选择。

创建完自己的应用,查看自己API信息

创建人脸库用户组

到这里就OK了


第二步(Python-SDK调用)

大家可以参考官方的技术文档,各类参数功能写得很详细——技术文档地址

先在终端下载baidu-aip

pip install baidu-aip

话不多说,放代码,哈哈

from aip import AipFace
import base64


# 百度智能云
class baiduyun:
    def __init__(self):
        # 百度智能云 APPID AK SK
        self.APP_ID = '01' # 根据自己的API信息填
        self.API_KEY = '01' # 根据自己的API信息填
        self.SECRET_KEY = '01' # 根据自己的API信息填
        self.client = AipFace(self.APP_ID, self.API_KEY, self.SECRET_KEY)

    # 人脸检测
    def face_detection(self, image):
        image = image_to_base64(image)
        imageType = "BASE64"
        options = {}
        options["face_field"] = "age,blur"
        options["max_face_num"] = 10
        options["face_type"] = "LIVE"
        options["liveness_control"] = "NONE"
        result = self.client.detect(image, imageType, options)
        # print(result)
        return result

    # 人脸注册
    def face_login(self, image, userId, groupId="test"):
        result = self.face_detection(image)
        image = image_to_base64(image)
        if result["error_code"] == 0:
            imageType = "BASE64"
            options = {}
            options["user_info"] = ""
            options["quality_control"] = "NORMAL"
            options["liveness_control"] = "NONE"
            options["action_type"] = "REPLACE"

            self.client.addUser(image, imageType, groupId, userId, options)

    # 人脸搜索
    def face_search(self, image, groupIdList="test"):
        image = image_to_base64(image)
        imageType = "BASE64"
        options = {}
        options["match_threshold"] = 70
        options["quality_control"] = "NONE"
        options["liveness_control"] = "NONE"
        result = self.client.search(image, imageType, groupIdList, options)
        return result

    # 删除用户
    def face_delete(self, userId, groupId='test'):
        self.client.deleteUser(groupId, userId)

    # 检测是否报错
    def extract_params(self, result):
        if result["error_code"] == 0:
            return [result["result"]["user_list"]]
        else:
            print(result["error_code"], result["error_msg"])
            return None


# 读取 JPEG 图像文件转base64格式
def image_to_base64(file_path):
    with open(file_path, "rb") as image_file:
        encoded_string = base64.b64encode(image_file.read())
        return encoded_string.decode('utf-8')

代码中需要自己的信息填入API_ID、API_KEY、SECRET_KEY、groupId。就可以当做一个人脸认证库使用了。只需要实例类,就可以调用其中的人脸注册、人脸搜索等方法,我已经自动将JPEG转base64,所以image只需要JPEG格式就好了。

  • 13
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值