PowerShell, REST calls, and Microsoft Cognitive Services

Last week on Monday, I attended a presentation at Chicago Coder Conference by David Giard, a Developer Evangelist at Microsoft.  It was a demo-heavy presentation of Microsoft’s Cognitive Services (formerly code-named Project Oxford), and it showed applications of machine learning and the APIs available to access them.  These include face detection and emotion detection, and they are accessible via REST end points. I wanted to try them out using my favorite language for quickly experimenting with APIs, PowerShell.  Also, I was giving an introduction to PowerShell the following day and wanted to see if I could come up with any cool Cognitive Services demos for PowerShell.

So here’s the PowerShell script I wrote:

 

$EmotionAPIURI = "https://api.projectoxford.ai/emotion/v1.0/recognize"            
$apiKey = "PUT YOUR API KEY HERE"

#From URL                       
$testPicUrl ="https://80e324.p3cdn1.secureserver.net/wp-content/uploads/2014/11/Michael-Blumenthal-PSC-Headshot.jpg"            #Use your own picture URL here
$bod = @{url = $testPicUrl };            
$jsbod = ConvertTo-Json $bod            
$result = Invoke-RestMethod -Method Post -Uri $EmotionAPIURI -Header @{ "Ocp-Apim-Subscription-Key" = $apiKey } -Body $jsbod -ContentType "application/json" -ErrorAction Stop            
$faceCount = $result.Count            
write-host "$faceCount" faces            
$result.scores | foreach {            
    write-host $(get-MaxEmotion $_)            
    }

You can see that to sort through the different scores that come back for each face, I wrote a helper function:

function global:get-MaxEmotion($ScoreSet){            
if ($scoreset -ne $null) {            
    $maxEmotionScore = $scoreSet.anger;            
    $maxEmotionName = "Anger"            
    if ($scoreSet.contempt -gt $maxEmotionScore) { $maxEmotionScore = $scoreSet.contempt; $maxEmotionName = "Contempt"}            
    if ($scoreSet.disgust -gt $maxEmotionScore) { $maxEmotionScore = $scoreSet.disgust; $maxEmotionName = "Disgust"}            
    if ($scoreSet.fear -gt $maxEmotionScore) { $maxEmotionScore = $scoreSet cialis generika rezeptfrei schweiz.fear; $maxEmotionName = "Fear"}            
    if ($scoreSet.hapiness -gt $maxEmotionScore) { $maxEmotionScore = $scoreSet.happiness; $maxEmotionName = "Happy"}            
    if ($scoreSet.neutral -gt $maxEmotionScore) { $maxEmotionScore = $scoreSet.neutral; $maxEmotionName = "Neutral"}            
    if ($scoreSet.sadness -gt $maxEmotionScore) { $maxEmotionScore = $scoreSet.sadness; $maxEmotionName = "Sad"}            
    if ($scoreSet.surprise -gt $maxEmotionScore) { $maxEmotionScore = $scoreSet.surprise; $maxEmotionName = "Surprise"}            
    }            
else             
    {$maxEmotionName="no data"}            
return $maxEmotionName;            
}

If you run it against a picture with two faces, you might get output like:

2 faces
Neutral
Neutral

Disclaimers: This script works on my machine, when I use my API key. Your mileage may vary. I have not commented or documented this code to at all, which is a bad practice, however I thought it was important to share it as is. No warranties expressed or implied, etc.

Michael