ตรวจจับและวิเคราะห์ใบหน้า

เสร็จสมบูรณ์เมื่อ

เมื่อต้องการใช้ Azure Vision Face API คุณต้องเตรียมใช้งานทรัพยากรสําหรับบริการในการสมัครใช้งาน Azure คุณสามารถจัดเตรียม Face เป็นทรัพยากรบริการเดียว หรือคุณสามารถใช้ Face API ในทรัพยากร Foundry Tools แบบหลายบริการก็ได้ ซึ่งสามารถจัดเตรียมเป็นทรัพยากรแบบสแตนด์อโลนหรือเป็นส่วนหนึ่งของโครงการ Microsoft Foundry

ในการใช้ทรัพยากรของคุณจากแอปพลิเคชันไคลเอ็นต์ คุณต้องเชื่อมต่อกับจุดสิ้นสุดโดยใช้การรับรองความถูกต้องโดยใช้คีย์หรือการรับรองความถูกต้องMicrosoft Entra AI เมื่อใช้อินเตอร์เฟส REST คุณสามารถใส่คีย์การรับรองความถูกต้องหรือโทเค็นในส่วนหัวของคําขอได้ เมื่อใช้ SDK เฉพาะภาษา (ตัวอย่างเช่น แพคเกจ Azure-ai-vision-face ของ Python หรือแพคเกจ Microsoft .NET Azure.AI.Vision.Face ) คุณใช้วัตถุ FaceClient เพื่อเชื่อมต่อกับบริการ

from azure.ai.vision.face import FaceClient
from azure.ai.vision.face.models import *
from azure.core.credentials import AzureKeyCredential

face_client = FaceClient(
    endpoint="<YOUR_RESOURCE_ENDPOINT>",
    credential=AzureKeyCredential("<YOUR_RESOURCE_KEY>"))
using Azure;
using Azure.AI.Vision.Face;

FaceClient faceClient = new FaceClient(
    new Uri("<YOUR_RESOURCE_ENDPOINT>"),
    new AzureKeyCredential("<YOUR_RESOURCE_KEY>"));

เมื่อต้องการตรวจหาและวิเคราะห์ใบหน้าในรูปภาพ คุณต้องระบุคุณลักษณะเฉพาะของแบบจําลองที่คุณต้องการให้บริการส่งกลับ จากนั้นใช้ไคลเอ็นต์เพื่อเรียกใช้วิธีการ ตรวจหา

# Specify facial features to be retrieved
features = [FaceAttributeTypeDetection01.HEAD_POSE,
            FaceAttributeTypeDetection01.OCCLUSION,
            FaceAttributeTypeDetection01.ACCESSORIES]

# Use client to detect faces in an image
with open("<IMAGE_FILE_PATH>", mode="rb") as image_data:
    detected_faces = face_client.detect(
        image_content=image_data.read(),
        detection_model=FaceDetectionModel.DETECTION01,
        recognition_model=FaceRecognitionModel.RECOGNITION01,
        return_face_id=True,
        return_face_attributes=features,
    )
// Specify facial features to be retrieved
FaceAttributeType[] features = new FaceAttributeType[]
{
    FaceAttributeType.Detection01.HeadPose,
    FaceAttributeType.Detection01.Occlusion,
    FaceAttributeType.Detection01.Accessories
};

// Use client to detect faces in an image
using (var imageData = File.OpenRead(imageFile))
{    
    var response = await faceClient.DetectAsync(
        BinaryData.FromStream(imageData),
        FaceDetectionModel.Detection01,
        FaceRecognitionModel.Recognition01,
        returnFaceId: false,
        returnFaceAttributes: features);
    IReadOnlyList<FaceDetectionResult> detected_faces = response.Value;
}

การตอบสนองจากบริการขึ้นอยู่กับ:

  • คุณลักษณะเฉพาะแบบจําลองที่ร้องขอ
  • จํานวนใบหน้าที่ตรวจพบในรูปภาพ

คําตอบสําหรับรูปภาพที่มีใบหน้าเดียวอาจมีลักษณะคล้ายกับตัวอย่างต่อไปนี้:

[
    {
        'faceRectangle': {'top': 174, 'left': 247, 'width': 246, 'height': 246}
        'faceAttributes':
        {
            'headPose':{'pitch': 3.7, 'roll': -7.7, 'yaw': -20.9},
            'accessories':
                [
                    {'type': 'glasses', 'confidence': 1.0}
                ],
            'occlusion':{'foreheadOccluded': False, 'eyeOccluded': False, 'mouthOccluded': False}
        }
    }
]