Powershell Cognitive Services Face API

Today I want to share with you my Powershell Cognitive Services Face API script.

Microsoft Cognitive Services are set of APIs (available from Azure) which are helping with make applications more intelligent.
They base on machine learning to detect in intelligent way emotions, speech, faces etc.

In my script I wanted to use Face API to detect person on picture.
If you are curious how I’ve checked Mona Lisa age, follow below instruction 🙂

In first way you must have Azure subscription.
Look for Cognitive Services and create new Face API.

Once it is done you should see keys to access Face API. Both are valid, so it doesn’t matter which will be used in script.

In script you should define three variables
$filePath – path to picture which should be recognized
$FaceAPIkey – face API key from Azure
$APILocation – API location choosed during Face API account creation.

You can also adjust $FaceAttributes if you want to check another attributes (please refer to Face API documentation).

Script:

# Add .Net Framework type
Add-Type -AssemblyName System.Net.Http
Add-Type -AssemblyName System.IO

# Read file
$filePath = 'D:\monalisa.jpg'
# Provide Face API key
$FaceAPIkey = "Here Provide your API key"
$APILocation = "westus"

# Create the HttpClient object
$client = New-Object -TypeName System.Net.Http.Httpclient
$client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "$FaceAPIkey")

# Provide query details
$returnFaceID = "true"
$returnFaceLandmarks= "false"
$FaceAttributes = "age,gender,smile,facialHair,glasses,emotion"

$queryString = "returnFaceId=$returnFaceID&returnFaceLandmarks=$returnFaceID&returnFaceAttributes=$FaceAttributes";
$uri = "https://$APILocation.api.cognitive.microsoft.com/face/v1.0/detect?" + $queryString;
$array = New-Object -TypeName byte

# Do the magic 🙂
$filestream = New-Object IO.FileStream $filePath ,'Append','Write','Read'
$br = New-Object System.IO.BinaryReader([System.IO.File]::Open($filepath, [System.IO.FileMode]::Open, [System.IO.FileAccess]::Read, [System.IO.FileShare]::ReadWrite))
$ReadBytes = $br.ReadBytes($filestream.Length)

[byte[]]$arr = Get-Content $filePath -Encoding Byte -ReadCount 0
$binaryContent = New-Object System.Net.Http.ByteArrayContent -ArgumentList @(,$arr)
$binaryContent.Headers.ContentType = "application/octet-stream"
$task = $client.PostAsync($uri,$binaryContent)

# Wait for the async call to finish
$task.wait();

# Check result
if($task.IsCompleted) 
{
    $Result = $task.Result.Content.ReadAsStringAsync().Result | ConvertFrom-Json
    foreach($face in $Result)
    {
        Write-Host ---- Face API result ----
        Write-Host Face ID:
        $face.faceId
        Write-Host
        Write-Host Face attributes:
        $face.faceAttributes
        Write-Host Face rectangle:
        $face.faceRectangle
        Write-Host -------------------------
    } 
}
else 
{
    Write-Host "Sorry, something went wrong: " + $task.Exception.Message -ForegroundColor Red
}

Result:

Now I know that Mona Lisa was 22 years old 🙂

I hope it will be usefull for some of you 🙂
Enjoy!

One thought on “Powershell Cognitive Services Face API

Leave a Reply

Your email address will not be published. Required fields are marked *