人臉檢測
人臉檢測指的是計算機視覺技術(shù)中用于識別和定位圖片或視頻中人臉的功能。這種技術(shù)可以用于多種應(yīng)用,比如身份驗證、監(jiān)控系統(tǒng)、智能相冊以及客戶行為分析等。本文介紹如何使用人臉檢測功能,包括如何使用該功能來識別圖像中的人臉及其相關(guān)的特征。
功能簡介
人臉檢測功能基于圖片AI技術(shù),可以檢測圖片中的人臉以及人臉信息,如果圖片中有多張人臉,系統(tǒng)會檢測多張人臉以及人臉信息。人臉信息包括人臉ID、年齡、性別、心情、吸引力、人臉質(zhì)量、人臉屬性等,其中人臉屬性包括人臉位置、頭部朝向、眼鏡、胡子、面罩等。
使用場景
身份驗證:可以通過人臉檢測與人臉相似度對比功能,實現(xiàn)用戶信息認證,多用于手機面容解鎖
人臉表情分析:通過人臉檢測和表情識別技術(shù),分析人臉表情,用于情感分析、增強現(xiàn)實(AR)、虛擬角色等應(yīng)用。
背景雜亂:復(fù)雜的背景可能會與面部特征混淆,影響檢測結(jié)果
在圖像中存在多個人臉時,相互之間可能會發(fā)生遮擋,檢測算法的性能可能會下降。
前提條件
已創(chuàng)建并獲取AccessKey。具體操作,請參見創(chuàng)建AccessKey。
已開通OSS服務(wù)、創(chuàng)建存儲空間并上傳文件到存儲空間。具體操作,請參見控制臺上傳文件。
已開通智能媒體管理服務(wù)。具體操作,請參見開通產(chǎn)品。
已通過智能媒體管理控制臺創(chuàng)建項目。具體操作,請參見創(chuàng)建項目。
說明您也可以調(diào)用API接口創(chuàng)建項目。具體操作,請參見CreateProject - 創(chuàng)建項目。
您可以調(diào)用ListProjects - 列出所有項目信息的列表接口列出指定地域下已創(chuàng)建的所有項目信息。
使用方法
調(diào)用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的訪問權(quán)限,建議您使用RAM用戶進行API訪問或日常運維。
# 強烈建議不要把AccessKey ID和AccessKey Secret保存到工程代碼里,否則可能導(dǎo)致AccessKey泄露,威脅您賬號下所有資源的安全。
# 本示例通過從環(huán)境變量中讀取AccessKey,來實現(xiàn)API訪問的身份驗證。如何配置環(huán)境變量,請參見http://bestwisewords.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:
# 復(fù)制代碼運行請自行打印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的訪問權(quán)限,建議您使用RAM用戶進行API訪問或日常運維。
# 強烈建議不要把AccessKey ID和AccessKey Secret保存到工程代碼里,否則可能導(dǎo)致AccessKey泄露,威脅您賬號下所有資源的安全。
# 本示例通過從環(huán)境變量中讀取AccessKey,來實現(xiàn)API訪問的身份驗證。如何配置環(huán)境變量,請參見http://bestwisewords.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:
# 復(fù)制代碼運行請自行打印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:])