Developer42

2016-02-05

PowerShell PlayTime :: Project Oxford

I recently stumbled across Microsoft’s Project Oxford; a collection of “AI” APIs for computer vision, speech and language. More info can be found here:

  1. MSDN: https://msdn.microsoft.com/en-us/library/mt422983.aspx
  2. Demo: https://www.projectoxford.ai/demo
  3. GitHub: https://github.com/Microsoft/ProjectOxford-ClientSDK

The project has links to a C# SDK, but that felt a bit heavy-weight for what are essentially just web services; so I thought I’d have a quick play with these functions using PowerShell.

Before coding, an API key is required. This can be obtained by logging into the following site using your MS Account (aka .net passport / live account) and registering for the free API keys: https://www.projectoxford.ai/Subscription.

I began by looking at the Emotion API, trawling through the client SDK code found here: https://github.com/Microsoft/ProjectOxford-ClientSDK/blob/master/Emotion/Windows/ClientLibrary/EmotionServiceClient.cs

Based on that code I was able to find all the info needed to call the rest services through PowerShell, resulting in this short script:

Clear-Host
$imageFilePath = 'c:\SomeFolder\ImageOfMeLookingAwesome.jpg'
$emotionApiKeyFree = '**my emotion aki key goes here**' #api keys: https://www.projectoxford.ai/Subscription

$RecogniseUri = ("https://api.projectoxford.ai/emotion/v1.0/recognize?subscription-key={0}" -f $emotionApiKeyFree)
$ContentType = 'application/octet-stream'
$Body = [System.IO.MemoryStream][System.Convert]::FromBase64String([convert]::ToBase64String((get-content $imageFilePath -Encoding Byte)))
$Result = Invoke-RestMethod -Method Post -Uri $RecogniseUri -ContentType $ContentType -Body $Body 

#show the results
$Result | format-list #information saying I don't look awesome; just angry, contemptable, and sad... until you spot the Es

#open the image file so we can compare the results with the picture
invoke-item $imageFilePath

To make it easier to see how the results map to the image, I then appended this to the end of my script, generating an HTML page with an image map. Hovering your mouse over the image’s faces gives you the emotions.

$htmlResultPath = 'c:\SomeFolder\EmotiomApiMap.html'

$html = @"
<html>
<head><title>really simple html page for demoing project oxford's emotion api</title></head>
<body>
<img src="$imageFilePath" usemap="#emotionMap">
<map name="emotionMap">
{0}
</map>
</body>
</html>
"@

$html = $html -f ($Result | %{
        $top = $_.faceRectangle.top
        $left = $_.faceRectangle.left
        $bottom = ($_.faceRectangle.top + $_.faceRectangle.height)
        $right = ($_.faceRectangle.left + $_.faceRectangle.width)

        "<area shape='rect' coords='{0},{1},{2},{3}' title='{4}'>" -f $left, $top, $right, $bottom, ($_.scores | out-string) 
    } | Join-String)

$html | out-file $htmlResultPath -Force
invoke-item $htmlResultPath 
My emotional state

PowerShell Project Oxford Emotion API PoC

I plan to play with these APIs more over the next few months / hope to find time to knock up a PowerShell module for them which I could share on GibHub.

2014-10-12

PowerShell Script: Demo of Calling a (StackExchange’s) Rest Web Service

Filed under: Microsoft, Technology — Tags: , , , , , , — Developer42 @ 01:25

Warning: this post is not useful unless you just like playing with scripts & APIs.

Reading a few posts on StackExchange’s Meta site, I saw that there was demand for, but no intent to, create some kind of synonym sub-type feature for tags.
e.g. Users looking for items tagged PowerShell would also see those tagged only PowerShell-1.0, PowerShell-2.0, PowerShell-ISE, etc.
Users looking for PowerShell-1.0 are being more specific though, so shouldn’t see those tagged simply PowerShell, and definitely not those tagged PowerShell-2.0.

Since SE have no intention of creating this, I figured it would be useful to have a site which could present a view over Stack Exchange sites, giving users these additional features.
Since I don’t have time to do that though, I satisfied my craving to build something by knocking up a quick script to play with the SE API.
…And since implementing synonyms would require a huge dictionary, which I’d have to create, I sufficed myself with simply playing with the API without the synonymn hierarchy feature.
So this is just me playing about with my new favourite scripting language and an API I hope one day to play more with (though then it would be with some other language).
Here’s what I came up with…

Code:

$tagList='c#','.net'
$tags = $taglist -join ';'
[Reflection.Assembly]::LoadWithPartialName("System.Web") | Out-Null
$tags = [System.Web.HttpUtility]::UrlEncode($tags)
$url = "http://api.stackexchange.com/2.2/search/advanced?page=1&amp;pagesize=10&amp;order=desc&amp;sort=creation&amp;ans
wers=0&amp;closed=False&amp;tagged={0}&amp;site=stackoverflow" -f $tags
$json = Invoke-RestMethod $url
$json.items | select title,link | %{ "{0}`n{1}`n" -f [System.Web.HttpUtility]::HtmlDecode($_.title), $_.link; }

Output:

ASP.NET Web Forms and MySql Entity Framework: "Nested transactions are not supported"
http://stackoverflow.com/questions/26320679/asp-net-web-forms-and-mysql-entity-framework-nested-transactions-are-not-su
ppo

.NET C# - Both Bitmap and Image Loads Images Smaller than Expected
http://stackoverflow.com/questions/26320637/net-c-both-bitmap-and-image-loads-images-smaller-than-expected

Mono TypeLoadException in an executable
http://stackoverflow.com/questions/26320146/mono-typeloadexception-in-an-executable

Developing a music player in C# without using com component
http://stackoverflow.com/questions/26319978/developing-a-music-player-in-c-without-using-com-component

Fastest way to get directory data
http://stackoverflow.com/questions/26319973/fastest-way-to-get-directory-data

c# datagridview multi-page print
http://stackoverflow.com/questions/26319852/c-datagridview-multi-page-print

How to access an object in c sharp from outside my method?
http://stackoverflow.com/questions/26319550/how-to-access-an-object-in-c-sharp-from-outside-my-method

.NET &amp; jQuery Jcrop Initialization Causing Double Page Load
http://stackoverflow.com/questions/26319428/net-jquery-jcrop-initialization-causing-double-page-load

Why i'm getting exception InvalidOperationException when using backgroundworker?
http://stackoverflow.com/questions/26319024/why-im-getting-exception-invalidoperationexception-when-using-backgroundwor
ker

Index was out of range error when I add a UserControl to a panel, Microsoft VS C#
http://stackoverflow.com/questions/26318864/index-was-out-of-range-error-when-i-add-a-usercontrol-to-a-panel-microsoft-
vs-c

So not that useful, but a fun play project.

2011-06-28

A Request / Random Thoughts

As with many of my posts, this is basically an unedited brain dump – apologies. Hopefully this will encourage some interesting comments / discussion though. . .

A Standard for Developer APIs
Facebook, Twitter, Worpress (and I’m sure Google+ will) offer APIs to developers allowing them to pull data out of their applications and manipulate it as they like. Most of these services offer similar functions; authentication, get the last x posts, pull back a grid of contacts, etc. All do this in their own way.
What would be great is some unification – either a library over the top of the existing APIs to pull them all in line, or for some set of social standards to be formed in the same way Netscape, IE, Mozilla and more came up with ECMAScript as a way to allow javascript to become portable. What I’m hoping for is something like this:

//this is entirely made up code - not (yet) some awesome new Google thing 
var application = GetApplication('Google+'); //creates a new object with an "application interface" for Google+
if (application.authenticate('Developer42','DemoCodePassword') //authenticate a user against the web app
{
    var identity = application.me(); //pull back an object which represents me
    var allFriends = me.ListContacts(); //by default pull back all contacts
    var colleagues = me.ListContacts('colleagues'); //or filter by group
    var posts = identity.GetPosts(20); //get my last 20 posts
    var friendsPosts = allFriends.GetPosts(100); //Get the last 100 posts by my friends/contacts
}

A Service for Services
This is probably what the guys who came up with UDDI were thinking:

If two companies offer a service to give out exchange rate information, and both use the same standard, when I want to get back exchange rate info why can’t I just post a request to the web saying “give me the exchange rate from USD to GBP” and have it chuck back .67 without all the hassle of searching for a suitable service.

There’s a whole bunch of data which we often need, but have to trawl the web for. Search engines began to make this better, WolframAlpha got a bit closer, but no one’s yet cracked it. What I’d like is a single web site containing a catalogue of services and their schemas. I pick a service, write code to its schema, then use the service url to pull back this data. From my point of view I’m just pulling data from http://www.UsefulServices.com/ExchangeRates, but in the background that could be talking to any (approved) provider. I guess the reason this doesn’t yet exist is the issue around monetisation; but surely there’s a way. . . ?
Below’s my wish list of services:

- Exchange Rates
- Share Prices
- National Holidays
- Daylight Savings Dates
- Post Code / Geo (long & lat) Conversion
- Credit Checks
- Product Prices
- Companies House Info

Blog at WordPress.com.

%d bloggers like this: