Detect and Analyze Faces

The ability to detect and analyze human faces is a core AI capability. In this exercise, you’ll explore two Azure AI Services that you can use to work with faces in images: the Azure AI Vision service, and the Face service.

Important: This lab can be completed without requesting any additional access to restricted features.

Note: From June 21st 2022, capabilities of Azure AI services that return personally identifiable information are restricted to customers who have been granted limited access. Additionally, capabilities that infer emotional state are no longer available. For more details about the changes Microsoft has made, and why - see Responsible AI investments and safeguards for facial recognition.

Clone the repository for this course

If you have not already done so, you must clone the code repository for this course:

  1. Start Visual Studio Code.
  2. Open the palette (SHIFT+CTRL+P) and run a Git: Clone command to clone the https://github.com/MicrosoftLearning/mslearn-ai-vision repository to a local folder (it doesn’t matter which folder).
  3. When the repository has been cloned, open the folder in Visual Studio Code.
  4. Wait while additional files are installed to support the C# code projects in the repo.

    Note: If you are prompted to add required assets to build and debug, select Not Now.

Provision an Azure AI Services resource

If you don’t already have one in your subscription, you’ll need to provision an Azure AI Services resource.

  1. Open the Azure portal at https://portal.azure.com, and sign in using the Microsoft account associated with your Azure subscription.
  2. In the top search bar, search for Azure AI services, select Azure AI Services, and create an Azure AI services multi-service account resource with the following settings:
    • Subscription: Your Azure subscription
    • Resource group: Choose or create a resource group (if you are using a restricted subscription, you may not have permission to create a new resource group - use the one provided)
    • Region: Choose any available region
    • Name: Enter a unique name
    • Pricing tier: Standard S0
  3. Select the required checkboxes and create the resource.
  4. Wait for deployment to complete, and then view the deployment details.
  5. When the resource has been deployed, go to it and view its Keys and Endpoint page. You will need the endpoint and one of the keys from this page in the next procedure.

Prepare to use the Azure AI Vision SDK

In this exercise, you’ll complete a partially implemented client application that uses the Azure AI Vision SDK to analyze faces in an image.

Note: You can choose to use the SDK for either C# or Python. In the steps below, perform the actions appropriate for your preferred language.

  1. In Visual Studio Code, in the Explorer pane, browse to the 04-face folder and expand the C-Sharp or Python folder depending on your language preference.
  2. Right-click the computer-vision folder and open an integrated terminal. Then install the Azure AI Vision SDK package by running the appropriate command for your language preference:

    C#

     dotnet add package Azure.AI.Vision.ImageAnalysis -v 1.0.0-beta.3
    

    Python

     pip install azure-ai-vision-imageanalysis==1.0.0b3
    
  3. View the contents of the computer-vision folder, and note that it contains a file for configuration settings:
    • C#: appsettings.json
    • Python: .env
  4. Open the configuration file and update the configuration values it contains to reflect the endpoint and an authentication key for your Azure AI services resource. Save your changes.

  5. Note that the computer-vision folder contains a code file for the client application:

    • C#: Program.cs
    • Python: detect-people.py
  6. Open the code file and at the top, under the existing namespace references, find the comment Import namespaces. Then, under this comment, add the following language-specific code to import the namespaces you will need to use the Azure AI Vision SDK:

    C#

     // Import namespaces
     using Azure.AI.Vision.ImageAnalysis;
    

    Python

     # import namespaces
     from azure.ai.vision.imageanalysis import ImageAnalysisClient
     from azure.ai.vision.imageanalysis.models import VisualFeatures
     from azure.core.credentials import AzureKeyCredential
    

View the image you will analyze

In this exercise, you will use the Azure AI Vision service to analyze an image of people.

  1. In Visual Studio Code, expand the computer-vision folder and the images folder it contains.
  2. Select the people.jpg image to view it.

Detect faces in an image

Now you’re ready to use the SDK to call the Vision service and detect faces in an image.

  1. In the code file for your client application (Program.cs or detect-people.py), in the Main function, note that the code to load the configuration settings has been provided. Then find the comment Authenticate Azure AI Vision client. Then, under this comment, add the following language-specific code to create and authenticate a Azure AI Vision client object:

    C#

     // Authenticate Azure AI Vision client
     ImageAnalysisClient cvClient = new ImageAnalysisClient(
         new Uri(aiSvcEndpoint),
         new AzureKeyCredential(aiSvcKey));
    

    Python

     # Authenticate Azure AI Vision client
     cv_client = ImageAnalysisClient(
         endpoint=ai_endpoint,
         credential=AzureKeyCredential(ai_key)
     )
    
  2. In the Main function, under the code you just added, note that the code specifies the path to an image file and then passes the image path to a function named AnalyzeImage. This function is not yet fully implemented.

  3. In the AnalyzeImage function, under the comment Get result with specified features to be retrieved (PEOPLE), add the following code:

    C#

     // Get result with specified features to be retrieved (PEOPLE)
     ImageAnalysisResult result = client.Analyze(
         BinaryData.FromStream(stream),
         VisualFeatures.People);
    

    Python

     # Get result with specified features to be retrieved (PEOPLE)
     result = cv_client.analyze(
         image_data=image_data,
         visual_features=[
             VisualFeatures.PEOPLE],
     )
    
  4. In the AnalyzeImage function, under the comment Draw bounding box around detected people, add the following code:

    C#

     // Draw bounding box around detected people
     foreach (DetectedPerson person in result.People.Values)
     {
         if (person.Confidence > 0.5) 
         {
             // Draw object bounding box
             var r = person.BoundingBox;
             Rectangle rect = new Rectangle(r.X, r.Y, r.Width, r.Height);
             graphics.DrawRectangle(pen, rect);
         }
    
         // Return the confidence of the person detected
         //Console.WriteLine($"   Bounding box {person.BoundingBox.ToString()}, Confidence: {person.Confidence:F2}");
     }
    

    Python

     # Draw bounding box around detected people
     for detected_people in result.people.list:
         if(detected_people.confidence > 0.5):
             # Draw object bounding box
             r = detected_people.bounding_box
             bounding_box = ((r.x, r.y), (r.x + r.width, r.y + r.height))
             draw.rectangle(bounding_box, outline=color, width=3)
    
         # Return the confidence of the person detected
         #print(" {} (confidence: {:.2f}%)".format(detected_people.bounding_box, detected_people.confidence * 100))
    
  5. Save your changes and return to the integrated terminal for the computer-vision folder, and enter the following command to run the program:

    C#

     dotnet run
    

    Python

     python detect-people.py
    
  6. Observe the output, which should indicate the number of faces detected.
  7. View the people.jpg file that is generated in the same folder as your code file to see the annotated faces. In this case, your code has used the attributes of the face to label the location of the top left of the box, and the bounding box coordinates to draw a rectangle around each face.

If you’d like to see the confidence score of all people the service detected, you can uncomment the code line under the comment Return the confidence of the person detected and rerun the code.

Prepare to use the Face SDK

While the Azure AI Vision service offers basic face detection (along with many other image analysis capabilities), the Face service provides more comprehensive functionality for facial analysis and recognition.

  1. In Visual Studio Code, in the Explorer pane, browse to the 04-face folder and expand the C-Sharp or Python folder depending on your language preference.
  2. Right-click the face-api folder and open an integrated terminal. Then install the Face SDK package by running the appropriate command for your language preference:

    C#

     dotnet add package Azure.AI.Vision.Face -v 1.0.0-beta.2
    

    Python

     pip install azure-ai-vision-face==1.0.0b2
    
  3. View the contents of the face-api folder, and note that it contains a file for configuration settings:
    • C#: appsettings.json
    • Python: .env
  4. Open the configuration file and update the configuration values it contains to reflect the endpoint and an authentication key for your Azure AI services resource. Save your changes.

  5. Note that the face-api folder contains a code file for the client application:

    • C#: Program.cs
    • Python: analyze-faces.py
  6. Open the code file and at the top, under the existing namespace references, find the comment Import namespaces. Then, under this comment, add the following language-specific code to import the namespaces you will need to use the Vision SDK:

    C#

     // Import namespaces
     using Azure;
     using Azure.AI.Vision.Face;
    

    Python

     # Import namespaces
     from azure.ai.vision.face import FaceClient
     from azure.ai.vision.face.models import FaceDetectionModel, FaceRecognitionModel, FaceAttributeTypeDetection03
     from azure.core.credentials import AzureKeyCredential
    
  7. In the Main function, note that the code to load the configuration settings has been provided. Then find the comment Authenticate Face client. Then, under this comment, add the following language-specific code to create and authenticate a FaceClient object:

    C#

     // Authenticate Face client
     faceClient = new FaceClient(
         new Uri(cogSvcEndpoint),
         new AzureKeyCredential(cogSvcKey));
    

    Python

     # Authenticate Face client
     face_client = FaceClient(
         endpoint=cog_endpoint,
         credential=AzureKeyCredential(cog_key)
     )
    
  8. In the Main function, under the code you just added, note that the code displays a menu that enables you to call functions in your code to explore the capabilities of the Face service. You will implement these functions in the remainder of this exercise.

Detect and analyze faces

One of the most fundamental capabilities of the Face service is to detect faces in an image, and determine their attributes, such as head pose, blur, the presence of mask, and so on.

  1. In the code file for your application, in the Main function, examine the code that runs if the user selects menu option 1. This code calls the DetectFaces function, passing the path to an image file.
  2. Find the DetectFaces function in the code file, and under the comment Specify facial features to be retrieved, add the following code:

    C#

     // Specify facial features to be retrieved
     FaceAttributeType[] features = new FaceAttributeType[]
     {
         FaceAttributeType.Detection03.HeadPose,
         FaceAttributeType.Detection03.Blur,
         FaceAttributeType.Detection03.Mask
     };
    

    Python

     # Specify facial features to be retrieved
     features = [FaceAttributeTypeDetection03.HEAD_POSE,
                 FaceAttributeTypeDetection03.BLUR,
                 FaceAttributeTypeDetection03.MASK]
    
  3. In the DetectFaces function, under the code you just added, find the comment Get faces and add the following code:

C#

// Get faces
using (var imageData = File.OpenRead(imageFile))
{    
    var response = await faceClient.DetectAsync(
        BinaryData.FromStream(imageData),
        FaceDetectionModel.Detection03,
        FaceRecognitionModel.Recognition04,
        returnFaceId: false,
        returnFaceAttributes: features);
    IReadOnlyList<FaceDetectionResult> detected_faces = response.Value;

    if (detected_faces.Count() > 0)
    {
        Console.WriteLine($"{detected_faces.Count()} faces detected.");

        // Prepare image for drawing
        Image image = Image.FromFile(imageFile);
        Graphics graphics = Graphics.FromImage(image);
        Pen pen = new Pen(Color.LightGreen, 3);
        Font font = new Font("Arial", 4);
        SolidBrush brush = new SolidBrush(Color.White);
        int faceCount=0;

        // Draw and annotate each face
        foreach (var face in detected_faces)
        {
            faceCount++;
            Console.WriteLine($"\nFace number {faceCount}");
            
            // Get face properties
            Console.WriteLine($" - Head Pose (Yaw): {face.FaceAttributes.HeadPose.Yaw}");
            Console.WriteLine($" - Head Pose (Pitch): {face.FaceAttributes.HeadPose.Pitch}");
            Console.WriteLine($" - Head Pose (Roll): {face.FaceAttributes.HeadPose.Roll}");
            Console.WriteLine($" - Blur: {face.FaceAttributes.Blur.BlurLevel}");
            Console.WriteLine($" - Mask: {face.FaceAttributes.Mask.Type}");

            // Draw and annotate face
            var r = face.FaceRectangle;
            Rectangle rect = new Rectangle(r.Left, r.Top, r.Width, r.Height);
            graphics.DrawRectangle(pen, rect);
            string annotation = $"Face number {faceCount}";
            graphics.DrawString(annotation,font,brush,r.Left, r.Top);
        }

        // Save annotated image
        String output_file = "detected_faces.jpg";
        image.Save(output_file);
        Console.WriteLine(" Results saved in " + output_file);   
    }
}

Python

# Get faces
with open(image_file, mode="rb") as image_data:
    detected_faces = face_client.detect(
        image_content=image_data.read(),
        detection_model=FaceDetectionModel.DETECTION03,
        recognition_model=FaceRecognitionModel.RECOGNITION04,
        return_face_id=False,
        return_face_attributes=features,
    )

    if len(detected_faces) > 0:
        print(len(detected_faces), 'faces detected.')

        # Prepare image for drawing
        fig = plt.figure(figsize=(8, 6))
        plt.axis('off')
        image = Image.open(image_file)
        draw = ImageDraw.Draw(image)
        color = 'lightgreen'
        face_count = 0

        # Draw and annotate each face
        for face in detected_faces:

            # Get face properties
            face_count += 1
            print('\nFace number {}'.format(face_count))

            print(' - Head Pose (Yaw): {}'.format(face.face_attributes.head_pose.yaw))
            print(' - Head Pose (Pitch): {}'.format(face.face_attributes.head_pose.pitch))
            print(' - Head Pose (Roll): {}'.format(face.face_attributes.head_pose.roll))
            print(' - Blur: {}'.format(face.face_attributes.blur.blur_level))
            print(' - Mask: {}'.format(face.face_attributes.mask.type))

            # Draw and annotate face
            r = face.face_rectangle
            bounding_box = ((r.left, r.top), (r.left + r.width, r.top + r.height))
            draw = ImageDraw.Draw(image)
            draw.rectangle(bounding_box, outline=color, width=5)
            annotation = 'Face number {}'.format(face_count)
            plt.annotate(annotation,(r.left, r.top), backgroundcolor=color)

        # Save annotated image
        plt.imshow(image)
        outputfile = 'detected_faces.jpg'
        fig.savefig(outputfile)

        print('\nResults saved in', outputfile)
  1. Examine the code you added to the DetectFaces function. It analyzes an image file and detects any faces it contains, including attributes for head pose, blur, and the presence of mask. The details of each face are displayed, including a unique face identifier that is assigned to each face; and the location of the faces is indicated on the image using a bounding box.
  2. Save your changes and return to the integrated terminal for the face-api folder, and enter the following command to run the program:

    C#

     dotnet run
    

    The C# output may display warnings about asynchronous functions not using the await operator. You can ignore these.

    Python

     python analyze-faces.py
    
  3. When prompted, enter 1 and observe the output, which should include the ID and attributes of each face detected.
  4. View the detected_faces.jpg file that is generated in the same folder as your code file to see the annotated faces.

More information

There are several additional features available within the Face service, but following the Responsible AI Standard those are restricted behind a Limited Access policy. These features include identifying, verifying, and creating facial recognition models. To learn more and apply for access, see the Limited Access for Azure AI Services.

For more information about using the Azure AI Vision service for face detection, see the Azure AI Vision documentation.

To learn more about the Face service, see the Face documentation.