人脸检测指的是计算机视觉技术中用于识别和定位图片或视频中人脸的功能。这种技术可以用于多种应用,比如身份验证、监控系统、智能相册以及客户行为分析等。本文介绍如何使用人脸检测功能,包括如何使用该功能来识别图像中的人脸及其相关的特征。
功能简介
人脸检测功能基于图片AI技术,可以检测图片中的人脸以及人脸信息,如果图片中有多张人脸,系统会检测多张人脸以及人脸信息。人脸信息包括人脸ID、年龄、性别、心情、吸引力、人脸质量、人脸属性等,其中人脸属性包括人脸位置、头部朝向、眼镜、胡子、面罩等。
使用场景
身份验证:可以通过人脸检测与人脸相似度对比功能,实现用户信息认证,多用于手机面容解锁
人脸表情分析:通过人脸检测和表情识别技术,分析人脸表情,用于情感分析、增强现实(AR)、虚拟角色等应用。
背景杂乱:复杂的背景可能会与面部特征混淆,影响检测结果
在图像中存在多个人脸时,相互之间可能会发生遮挡,检测算法的性能可能会下降。
前提条件
已创建并获取AccessKey。具体操作,请参见创建AccessKey。
已开通OSS服务、创建存储空间并上传文件到存储空间。具体操作,请参见控制台上传文件。
已开通智能媒体管理服务。具体操作,请参见开通产品。
已通过智能媒体管理控制台创建项目。具体操作,请参见创建项目。
说明您也可以调用API接口创建项目。具体操作,请参见CreateProject - 创建项目。
您可以调用ListProjects - 列出所有项目信息的列表接口列出指定地域下已创建的所有项目信息。
使用方法
调用DetectImageFaces - 通过AI模型能力检测图片中的人脸以及人脸信息接口检测图片中的人脸以及人脸信息,包括年龄、性别等。
图片信息
IMM项目名称:test-project
待检测图片的存储地址:oss://test-bucket/test-object.jpg
图片示例:
请求示例
{
"ProjectName": "test-project",
"SourceURI": "oss://test-bucket/test-object.jpg",
}
返回示例
{
"RequestId": "47449201-245D-58A7-B56B-BDA483874B20",
"Faces": [
{
"Beard": "none",
"MaskConfidence": 0.724,
"Gender": "male",
"Boundary": {
"Left": 138,
"Top": 102,
"Height": 19,
"Width": 17
},
"BeardConfidence": 0.801,
"FigureId": "b6525b63-cb12-4fab-a9f4-9c7de08b80c3",
"Mouth": "close",
"Emotion": "none",
"Age": 36,
"MouthConfidence": 0.984,
"FigureType": "face",
"GenderConfidence": 0.999,
"HeadPose": {
"Pitch": -9.386,
"Roll": -3.478,
"Yaw": 14.624
},
"Mask": "none",
"EmotionConfidence": 0.998,
"HatConfidence": 0.794,
"GlassesConfidence": 0.999,
"Sharpness": 0.025,
"FigureClusterId": "figure-cluster-id-unavailable",
"FaceQuality": 0.3,
"Attractive": 0.002,
"AgeSD": 8,
"Glasses": "none",
"FigureConfidence": 0.998,
"Hat": "none"
},
{
"Beard": "none",
"MaskConfidence": 0.649,
"Gender": "male",
"Boundary": {
"Left": 85,
"Top": 108,
"Height": 18,
"Width": 14
},
"BeardConfidence": 0.975,
"FigureId": "798ab164-ae05-4a9f-b8c9-4b69ca183c3f",
"Mouth": "close",
"Emotion": "none",
"Age": 34,
"MouthConfidence": 0.97,
"FigureType": "face",
"GenderConfidence": 0.917,
"HeadPose": {
"Pitch": -0.946,
"Roll": -1.785,
"Yaw": -39.264
},
"Mask": "mask",
"EmotionConfidence": 0.966,
"HatConfidence": 0.983,
"GlassesConfidence": 1,
"Sharpness": 0.095,
"FigureClusterId": "figure-cluster-id-unavailable",
"FaceQuality": 0.3,
"Attractive": 0.022,
"AgeSD": 9,
"Glasses": "none",
"FigureConfidence": 0.998,
"Hat": "none"
},
{
"Beard": "none",
"MaskConfidence": 0.534,
"Gender": "female",
"Boundary": {
"Left": 245,
"Top": 128,
"Height": 16,
"Width": 13
},
"BeardConfidence": 0.998,
"FigureId": "b9fb1552-cc98-454a-ac7c-18e5c55cc5bf",
"Mouth": "close",
"Emotion": "none",
"Age": 6,
"MouthConfidence": 0.999,
"FigureType": "face",
"GenderConfidence": 0.972,
"HeadPose": {
"Pitch": 21.686,
"Roll": 16.806,
"Yaw": 50.348
},
"Mask": "mask",
"EmotionConfidence": 0.991,
"HatConfidence": 0.999,
"GlassesConfidence": 1,
"Sharpness": 0.389,
"FigureClusterId": "figure-cluster-id-unavailable",
"FaceQuality": 0.3,
"Attractive": 0.046,
"AgeSD": 6,
"Glasses": "none",
"FigureConfidence": 0.991,
"Hat": "none"
},
{
"Beard": "none",
"MaskConfidence": 0.654,
"Gender": "male",
"Boundary": {
"Left": 210,
"Top": 130,
"Height": 18,
"Width": 15
},
"BeardConfidence": 0.738,
"FigureId": "a00154ad-6e5a-48a8-b79e-4cd3699e3281",
"Mouth": "close",
"Emotion": "none",
"Age": 24,
"MouthConfidence": 0.999,
"FigureType": "face",
"GenderConfidence": 0.999,
"HeadPose": {
"Pitch": -3.356,
"Roll": 1.734,
"Yaw": 12.431
},
"Mask": "none",
"EmotionConfidence": 0.993,
"HatConfidence": 1,
"GlassesConfidence": 0.984,
"Sharpness": 0.449,
"FigureClusterId": "figure-cluster-id-unavailable",
"FaceQuality": 0.3,
"Attractive": 0.005,
"AgeSD": 15,
"Glasses": "none",
"FigureConfidence": 0.985,
"Hat": "none"
}
]
}
返回示例显示了当前图片中有四个人脸以及四个人脸的信息,包括人物性别、年龄、心情等。
示例代码
以Python SDK为例,人脸检测的完整示例代码如下。
# -*- coding: utf-8 -*-
# This file is auto-generated, don't edit it. Thanks.
import sys
import os
from typing import List
from alibabacloud_imm20200930.client import Client as imm20200930Client
from alibabacloud_tea_openapi import models as open_api_models
from alibabacloud_imm20200930 import models as imm_20200930_models
from alibabacloud_tea_util import models as util_models
from alibabacloud_tea_util.client import Client as UtilClient
class Sample:
def __init__(self):
pass
@staticmethod
def create_client(
access_key_id: str,
access_key_secret: str,
) -> imm20200930Client:
"""
使用AccessKey ID&AccessKey Secret初始化账号Client。
@param access_key_id:
@param access_key_secret:
@return: Client
@throws Exception
"""
config = open_api_models.Config(
access_key_id=access_key_id,
access_key_secret=access_key_secret
)
# 填写访问的IMM域名。
config.endpoint = f'imm.cn-beijing.aliyuncs.com'
return imm20200930Client(config)
@staticmethod
def main(
args: List[str],
) -> None:
# 阿里云账号AccessKey拥有所有API的访问权限,建议您使用RAM用户进行API访问或日常运维。
# 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。
# 本示例通过从环境变量中读取AccessKey,来实现API访问的身份验证。如何配置环境变量,请参见https://help.aliyun.com/document_detail/2361894.html。
imm_access_key_id = os.getenv("AccessKeyId")
imm_access_key_secret = os.getenv("AccessKeySecret")
client = Sample.create_client(imm_access_key_id, imm_access_key_secret)
detect_image_faces_request = imm_20200930_models.DetectImageFacesRequest(
project_name='test-project',
source_uri='oss://test-bucket/test-object.jpg'
)
runtime = util_models.RuntimeOptions()
try:
# 复制代码运行请自行打印API的返回值。
client.detect_image_faces_with_options(detect_image_faces_request, runtime)
except Exception as error:
# 如有需要,请打印错误信息。
UtilClient.assert_as_string(error.message)
@staticmethod
async def main_async(
args: List[str],
) -> None:
# 阿里云账号AccessKey拥有所有API的访问权限,建议您使用RAM用户进行API访问或日常运维。
# 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。
# 本示例通过从环境变量中读取AccessKey,来实现API访问的身份验证。如何配置环境变量,请参见https://help.aliyun.com/document_detail/2361894.html。
imm_access_key_id = os.getenv("AccessKeyId")
imm_access_key_secret = os.getenv("AccessKeySecret")
client = Sample.create_client(imm_access_key_id, imm_access_key_secret)
detect_image_faces_request = imm_20200930_models.DetectImageFacesRequest(
project_name='test-project',
source_uri='oss://test-bucket/test-object.jpg'
)
runtime = util_models.RuntimeOptions()
try:
# 复制代码运行请自行打印API的返回值。
await client.detect_image_faces_with_options_async(detect_image_faces_request, runtime)
except Exception as error:
# 如有需要,请打印错误信息。
UtilClient.assert_as_string(error.message)
if __name__ == '__main__':
Sample.main(sys.argv[1:])