AI自动识别人脸特征-Python
引导式阅读
Python
AI自动识别人脸特征-Python
作者
Codelabs助理
上架时间
2021-09-27 11:39:16

场景介绍

人脸识别以开放API(Application Programming Interface,应用程序编程接口)的方式提供给用户,用户通过实时访问和调用API获取人脸处理结果,帮助用户自动进行人脸的识别检测等,打造智能化业务系统,提升业务效率。

前提条件

  • 申请服务:用户在FRS管理控制台页面“服务管理”栏选择人脸检测服务申请开通。具体操作请参见 申请服务章

  • 配置环境:获取SDK和样例工程,导入到开发Python环境中。

    1、获取API相关文档,人脸识别服务的API请见《人脸识别API参考》

    2、获取人脸识别服务的Endpoint,请参见地区和终端节点

    3、开通人脸识别服务:登录人脸识别管理控制台,选择对应的子服务,单击右侧的“开通服务”。服务开通一次即可,后续使用时无需再开通。

    4、获取华为云账号的AK/SK。如果之前没有生成过AK/SK,可登录我的凭证界面,选择“管理访问密钥 > 新增访问密钥”来获取。

    5、已经安装好Python环境,Python SDK适用于Python2.7、Python3.6。

示例代码

使用如下代码(Python3.6)同步查询特定 Region 下的人脸识别服务(FRS),调用前请根据实际情况替换如下变量 {your ak string}{your sk string}{your endpoint string}{your projectId string} 以及 {your file path string}

#!/usr/bin/python # -*- coding: utf-8 -*- from frsclient import * def build_client(): ak = "{your ak string}" sk = "{your sk string}" project_id = "{your projectId string}" end_point = "{your endpoint string}" auth = AuthInfo(ak=ak, sk=sk, end_point=end_point) client = FrsClient(auth_info=auth, project_id=project_id) # 当您需要代理才可以访问到服务时,在第三个参数增加代理信息即可 # proxy = {"http": "http://127.0.0.1:1234", "https": "http://127.0.0.1:1234"} # client = FrsClient(auth_info=auth, project_id=project_id, proxies=proxy) return client def translate(value_str): if value_str and value_str != "none": return "是" return "否" if __name__ == "__main__": ds = build_client().get_detect_service() res = ds.detect_face_by_file("{your file path string}", "0,1,2,3,4,5") attributes = res.get_faces()[0]["attributes"] print(f""" 年龄: {attributes["age"]} 性别: {attributes["gender"]} 姿势: 俯仰角: {attributes["headpose"]["pitch_angle"]}, 航向角: {attributes["headpose"]["yaw_angle"]}, 横滚角: {attributes["headpose"]["roll_angle"]} 穿戴: 是否戴帽子: {translate(attributes["dress"]["hat"])}, 是否戴眼镜: {translate(attributes["dress"]["glass"])} 是否笑: {attributes["smile"]}""")

运行示例

  • 根据实际情况替换如下变量 {your ak string}{your sk string}{your endpoint string}{your projectId string} 以及 {your file path string}
  • 在控制台执行 python demo.py 命令

运行结果

  • 执行命令后,在 console 查看日志打印了图片人物信息,包括年龄、性别、坐姿、穿戴、是否微笑。