HOW TO scan images in a web page & analyze them with Microsoft Cognitive Services Face API with PowerShell

Head to this sample PowerShell script by Rob Sewell published in the PowerShell Gallery

The code for the Get-SpeakerFace function to get the face attributes using the Microsoft Cognitive Services Face API is customized to get the face attributes of  images from a webpage whose URL is hard-coded. With minor tweaks, the same sample can be used to work scan images in any web page & analyze them Face API

Some observations & findings from my experience running the script -
If you're trying PowerShell ISE on a computer for the first time, use Run as Administrator option &  type this or variations of it into your PowerShell window before you execute the function -
Set-ExecutionPolicy RemoteSigned

You can choose to direct the output to a CSV file -
Get-SpeakerFace | Export-csv -path  C:\PS\results.csv

You can choose to specify the access key required to utilize the Face API as an environment variable as in the sample -
$apiKey = $Env:MS_Faces_Key
or provide it directly through the code -
$apiKey = "key_generated_through_Azure_Portal"
$headers = @{ "Ocp-Apim-Subscription-Key" = $apiKey }

It may take up to 10 minutes for the newly (re)generated keys to take effect. If you encounter the error "Access denied due to invalid subscription key. Make sure you are subscribed to an API you are trying to call and provide the right key." after running the function, you know the key has not taken effect.

The sample uses the Face API of the West Europe region -
https://westeurope.api.cognitive.microsoft.com/face/v1.0/
If you're using other Azure services from a particular region, you can replace the region in the URL accordingly.

This line filters images containing just head-shots rather than all the images on the page -
$webpage.Images.Where{$_.class -eq 'speaker-image lazyOwl wp-post-image'}.src | ForEach-Object {
Assuming, a web page has just the profile images, it can be replaced by the following -
$webpage.Images.src | ForEach-Object {

Un-comment this line if you are using the free tier of the API which allows 20 transactions per minute and 30K calls free per month. No SLA is provided for the free tier. Usage is throttled if the transaction limit is reached on the free tier.
Start-Sleep -Seconds 4
With the Face API – Standard tier, you can make up to 10 calls per second. You may upgrade to a higher tier at any time. The billing rate and included quantities corresponding to the higher tier will begin immediately.

The face attributes were not returned for 7 of the 71 images in the output of the sample

Comments

Popular posts from this blog

Maven Crash Course - Learn Power Query, Power Pivot & DAX in 15 Minutes

"Data Prep & Exploratory Data Analysis" course by Maven Analytics

Oracle Cloud Infrastructure 2024 Generative AI Professional Course & Certification Exam (1Z0-1127-24)