Reading Time: 7 minutes

Adding a little AI to your application is easier than you think! For example, Microsoft’s Computer Vision API makes it incredibly easy to detect and censor inappropriate pictures. That was a requirement in one of my projects which led me to explore this API. But I discovered that we can do so much more with almost no coding!

This is how it works:

  1. You send a picture (over https) over to Microsoft using their Computer Vision API
  2. The API analyzes the picture and responds with a JSON describing what it could detect in the picture. For example, it will tell you if the picture has racy or adult content. It tells you if the picture has a detectable face. It even tries to guess the age of people detected in the picture! And a lot more. You can test it live by uploading a picture here: https://azure.microsoft.com/en-in/services/cognitive-services/computer-vision/

I used this on a website where users upload their profile pictures. I wanted to make sure they are uploading pictures “safe for work”. And since the API gives me rich info about the picture, I was able to do so much more. For eg. I use the estimated age value to make sure my users are not uploading their childhood pictures! I used celebrity-detection feature to prevent users from uploading pictures of celebrities. You can watch a demo of this in a talk I gave at DotNet Bangalore Meetup: https://www.youtube.com/watch?v=Wi8gUoJSXng

Here’s how you can do it:

  1. Sign up for a free Azure account. You might need a valid credit card for identification, but don’t worry, you won’t be charged for any of the free services. Computer Vision API has a free-tier that gives you 5000 free calls each month. Check out more details on their pricing page.
  2. Once you login, you will arrive at Azure Portal. Click on “+ Create a resource“, then type in “Computer Vision” in the search box and hit Enter.
  3. From the search results, select Computer Vision
  4. You’ll see a short introduction. Click the “Create” button at the bottom of it.
  5. Next, you have to pick a name for your API account. Type in any unique name. Choose a location close to you. Select the free F0 pricing tier (you can change this later if you wish). Next you have to type in a Resource Group name. Resource groups help you group different Azure services so that you can manage them together (like folders). For now, just type in the same resource group name as your account name. Finally hit “Create” at the bottom of the screen.
  6. You will see a little progress indication on top of the screen at the notification icon. In about 30 seconds, your API will be ready and you will be able to click on the notification icon and click “Go to your resource“.
  7. On the Quick Start page that appears next, click on Keys
  8. Copy the value from Key 1. We’ll need this key to connect to our API.
  9. Now let’s code. You can use any language of your choice as long as you are able to make an HTTP call. You can find SDK and sample code for several languages here: https://docs.microsoft.com/en-in/azure/cognitive-services/computer-vision/.
    For our example, I’ll use C#. Create any C# project and add a reference to the Nuget package Microsoft.ProjectOxford.Vision. This client library makes it easy to talk to Computer Vision API.
  10. Here’s a sample code to call the API to analyze a picture:

    Of course, you should use your own API KEY (from step 8) as the first parameter for VisionServiceClient constructor. Also you may have to change the URL in the second parameter slightly depending on location of you chose in step 5.
    In the sample code above, I send a sample file (me.jpg) to the API and wait for the result. From the result, I look at Adult.IsAdultContent and IsRacyContent flag.
  11. There is a lot more info in the Result object. Some examples below:
  12. You can find this sample code (and bonus code for celebrity detection) on my Github: https://github.com/anandgothe/vision-api-sample

Let me know in comments below if you found it useful or if you have questions.

Cheers,
-Anand Gothe

LEAVE A REPLY

Please enter your comment!
Please enter your name here

*