Using Go with Microsoft’s Face API

(Microsoft Cognitive Services made me younger by 16 years!!)


Using Microsoft Cognitive Services Vision — Face API, one can detect human faces in an image (I used my own image as shown here) and returns face locations, and optionally with face ID, landmarks, and attributes. Attributes include age, gender, smile intensity, facial hair and head-pose.

  • JPEG, PNG, GIF(the first frame), and BMP are supported. The image file size should be no larger than 4MB.
  • The detectable face size is between 36x36 to 4096x4096 pixels. The faces out of this range will not be detected.
  • A maximum of 64 faces could be returned for an image. The returned faces are ranked by face rectangle size in descending order.
  • Frontal and near-frontal faces have the best results.
  • Attributes (age, gender, headPose, smile and facialHair, and glasses) are still experimental and may not be very accurate. No wonder the API made me younger by 16 years!

Get started for free

Start building your app or running sample code by signing up for free keys. All you need is an email address and a credit card which will not be charged. After signup, log into your account and return to your subscriptions list, grab your keys, and start coding!


The program should check the image and display some attributes like gender, age and whether I wear glasses or not.

I have created a folder “mcs” which will hold the Go source code “mcs.go”.


We shall be running our program at the command prompt in the folder “mcs” as follows:

go run mcs.go

Open the documentation for the Face API and click on the button “Open API Testing Console” as can be seen in the screenshot below:

In the new screen that comes up, enter the values true for returnFaceId; false for returnFaceLandmarks; age,gender,glasses for returnFaceAttributes; “your key value” for Ocp-Apim-Subscription-Key and “” for “url”.

You can use any URL of an image with a single human face.

Now click on the Send button below and it would respond with JSON, something like what’s seen in the screen below:

I then copied the JSON to the JSON-to-Go utility and got the struct that I used in my program “mcs.go” namely:

The complete code for “mcs.go” is:


The output from the program is:

Your gender is: male
and age is: 45.1 years.
Glasses or No Glasses? NoGlasses

Sweet! I am younger by 16 years!

Incidentally, I spent a day to get the above code working due to a minor bug that I could not figure out. Thanks to my friend Romin Irani who fixed it in a jiffy. Romin mentioned:

“There was a slight problem in the way that you were sending the JSON Body in the request, which contained the “url” of your image. As per the documentation, you need to put it in JSON format.”

That’s it. Have fun.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.