Home Exploring Azure AI - Face API
Post
Cancel

Exploring Azure AI - Face API

While facial recognition is, let’s say, a divisive , topic, it’s safe to say that it’s already used for many applications in our everyday lives. From actual security applications such as unlocking a phone by just looking at it to scanning an image to see who (or, more often, what) is in it to just automatically tagging your friends on photos, all of these have the same underlying foundation.

In this short article, I’m taking a look at Azure AI’s Face API and running a sample face detection script to detect the location of a person’s face in an image.

Azure AI Face API

Key features of Azure AI Face API

Azure AI Face API offers a wide variety of features beyond the basic face location detection we’ll be taking a look at shortly, for example, the following:

  • Face recognition: Matching the detected face against database entries
  • Facial attribute analysis: insights into facial attributes such as age, gender, emotion, and more
  • Face similarity matching: used, for example, for identity verification
  • Face redaction: used for blurring or anonymizing images of people for privacy or regulatory purposes

While the potential problems with technology like this are numerous, such as the potential for privacy concerns, profiling, straight-up surveillance states, and simple false positives, there are also many potential beneficial use cases that override the risks. I’m not looking to turn this into an essay on the dangers or benefits of facial recognition technology, though, so let’s jump into trying the Azure AI Face API out.

More complete reference list of Face API features can be found here: https://learn.microsoft.com/en-us/azure/ai-services/computer-vision/identity-api-reference

Creating a Face API resource

To begin with, we’re going to need the actual resource that does the heavy lifting. Face API can be found under Azure AI services, and the creation is as simple as other Azure resources. Once the deployment is complete, we need to go and copy the Endpoint URL and Key 1, which we’ll be adding to our script later on.

Keys and Endpoint

Detecting a face in an image

We’re provided the following PowerShell script for this lab:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
$key="KEY1"
$endpoint="ENDPOINT_URL"

# Code to call Face service for face detection
$img_file = "store-camera-1.jpg"
if ($args.count -gt 0 -And $args[0] -in ("store-camera-1.jpg", "store-camera-2.jpg", "store-camera-3.jpg"))
{
    $img_file = $args[0]
}

$img = "https://raw.githubusercontent.com/MicrosoftLearning/AI-900-AIFundamentals/main/data/vision/$img_file"

$headers = @{}
$headers.Add( "Ocp-Apim-Subscription-Key", $key )
$headers.Add( "Content-Type","application/json" )

$body = "{'url' : '$img'}"

write-host "Analyzing image...`n"
$result = Invoke-RestMethod -Method Post `
          -Uri "$endpoint/face/v1.0/detect?detectionModel=detection_01" `
          -Headers $headers `
          -Body $body | ConvertTo-Json -Depth 5

$analysis = ($result | ConvertFrom-Json)
Write-Host ("`nFrom June 21st 2022, Face service capabilities that return personally identifiable features are restricted.`nSee https://azure.microsoft.com/blog/responsible-ai-investments-and-safeguards-for-facial-recognition/ for details.`nThis code is restricted to returning the location of any faces detected:`n")
foreach ($face in $analysis)
{
    Write-Host("Face location: $($face.faceRectangle)`n")
}

curl -v -X POST "https://westus.api.cognitive.microsoft.com/face/v1.0/detect?returnFaceId=true&returnFaceLandmarks=false&returnFaceAttributes={string}&recognitionModel=recognition_04&returnRecognitionModel=false&detectionModel=detection_03&faceIdTimeToLive=86400"
-H "Content-Type: application/json"
-H "Ocp-Apim-Subscription-Key: {subscription key}"

It’s a little bit dirty with hardcoded arguments, but I’m not a programmer, so I’m not here to judge. In any case, it’s going to take a sample image and run it through the face detection service. As you can see at the end, the script is just using curl to make a call to the API and returning the coordinates of the face, which could then be used for something later on.

Let’s run the script twice on two different images to make sure that it’s working as expected.

Running the script Running the script

Two different images, two different location detections. All seems to be good.

In conclusion

Facial recognition will likely always remain a divisive topic; however, the Azure AI Face API offers developers the opportunity to use the technology for good in order to enhance system security, improve the user experience, and yes, even ensure privacy. While simple image content detection is a flawed approach in many cases, have you ever fooled a cheap phone with “facial recognition unlock” by showing it another phone with a photo of a person? I know I have), and there are also lots of potential use cases for the technology, even outside the most stringent of security applications.

This post is licensed under CC BY 4.0 by the author.