Microsoft Cognitive Services (Computer Vision)
Tags: azure, machinelearning, computerscience
Published: 2020-04-15 06:53:30

Hello everyone, Welcome to my blog on Azure Cognitive Services. This blog is written as part of MSP Developer Stories initiative by [Microsoft Student Partners](https://studentpartners.microsoft.com/) (India) program.

In this post, we will discuss on one of the hottest cognitive service provided by Microsoft Azure i.e. Computer Vision. Azure Computer Vision provides us the ability to extract information from images and analyze them. It makes use of the advanced algorithms that process images based on our requirement. All we need to do is just to make an REST API call and make use of those results in our application.

There are several things we can do computer vision API. Some of them are listed below.
* Tagging Visual Features.
* Object Detection.
* OCR (Optical Character Recognition).
* Detecting faces and facial features.
* Detect Brands.
* Categorizing Images.
* Generating Thumbnails.
* Content Moderation.

Let's say we are developing an application which <b>guides visually impaired people</b>. Azure Computer Vision makes it easy for us by <b>Describing the image</b> using <b> Analyze API </b>, and let them experience the real world as others see. All we need to just embed this API call in our application and then organize those results to give a beautiful description of the real world.

Before we learn those things, we must need an active azure subscription to make use of Cognitive services. If you don't have one, Azure provides a [12-month free trail](https://azure.microsoft.com/en-in/free/search/?&OCID=AID2000081_SEM_ERf0AeW4&MarinID=ERf0AeW4_79302359141420_azure%20free%20trial_be_c__1268836759311304_kwd-79302521531747:loc-90&lnkd=Bing_Azure_Brand&msclkid=e14a0bc76054108766a35551f9ac2bff&ef_id=XoTOFwAABOJSOuVW:20200415052203:s&dclid=CjgKEAjwvtX0BRCT_My3rsP03zASJADdaODq0l18IBWtsbBEvqAgYM_-tTtPvWbv9ThcEOLyCAJvfPD_BwE), Go and grab yours. If you are a student, you can get one without any credit information using [Azure For Students](https://azure.microsoft.com/en-us/free/students/).

### <u> Azure Computer Vision: Tagging Visual Features </u>

In this post, we will make use of Python API provided by Microsoft Cognitive Services to leverage the awesomeness of Azure Computer Vision in our application.

Before we start, here are the list of pre-requisites:
* Azure Subscription.
* Python.
* Python Packages:
  * matplotlib.
  * requests.
  * pillow.

If you don't have one, open the command prompt in your python environment and install the packages using <b> [pip](https://packaging.python.org/tutorials/installing-packages/) install package-name </b>.

<b> Hurray!! We are all set to go. </b>

## Step 1:
### Create a Cognitive service resource using [Azure portal](https://portal.azure.com/).

* In the Azure Portal, Click on All Services.
* You can find <b>AI + Machine Learning</b> listed in the given services. Click on it.
* In the list of services you can find cognitive services. Click on it.

![AI + Machine Learning Services](https://dev-to-uploads.s3.amazonaws.com/i/b95chk7ozapz62nkgjb7.png)

* If there is no existing Cognitive service, it asks you to create one. Go on and create one by clicking on Create Cognitive services.
* Now you can find a grid of services listed in the portal. Find Cognitive Services and click and see More.

![Services](https://dev-to-uploads.s3.amazonaws.com/i/o80rwyml632bmi1s4qer.png)

* Find Computer Vision and click on Create.
* Now it redirects you to the Create Page, where you need to fill in the name of your service, your azure subscription, region, pricing tier and a Resource group.

![Create Service](https://dev-to-uploads.s3.amazonaws.com/i/yekjj4aqpt8c7pmxnk7a.png)

Find a region where F0 pricing tier is available, it indicates a free service.

* Click on Create, now it validates your request and then the deployment will be initialized.
* After the deployment is complete you can go to your resource and find your cognitive service API key.

Hold on, now let's see how we can use them.

#### Here's the image we will be analyzing through Computer Vision Analyze API.

![Test Image](https://dev-to-uploads.s3.amazonaws.com/i/j667b6rwimbory8qfafp.jpg)

## Step 2:
### Write a Python program to make a REST API call.
Create a python file and copy the following code into your editor.

* In the first part we are importing all the required libraries into out python code.

``` python
import requests
import matplotlib.pyplot as plt
import json
from PIL import Image
from io import BytesIO
```
* If you are using Jupyter Notebook to run this program, you need to add the below line of code.

``` python
% matplotlib inline
```

* Next, in order to make an API call, we need to have our subscription key and endpoint which you can find in your deployed resource.

``` python
subscription_key = "Your-subcription-key"
endpoint = "Your-end-point"
```

* Initialize the <b>subscription-key</b> and <b>endpoint</b> variables with your respective key and endpoint.
* Analyze URL is the corresponding API call for Azure Analyze API.

```python
analyze_url = endpoint + "vision/v2.1/analyze"
```

* Next there is the url to the image we wanted to analyze.

``` python
image_url = "https://cdn.geekwire.com/wp-content/uploads/2014/03/DSC06612.jpg"
```

* Then, we will make a post request to the API with the corresponding headers and the parameters.
* We can send in the image data using URL or Bytes data. We are using the url attribute to pass the image to <b>Analyze API</b>

```python
headers = {'Ocp-Apim-Subscription-Key': subscription_key}
params = {'visualFeatures': 'Categories,Description'}
data = {'url': image_url}
response = requests.post(analyze_url, headers=headers,
                         params=params, json=data)
response.raise_for_status()
```
* The returned response from the post request contains the <b>analysis</b> we need in the form of <b>json</b>.

```python
analysis = response.json()
```
* In the analysis, we have description of the image with the <b>text</b> attribute.

``` python
image_caption = analysis["description"]["captions"][0]["text"].capitalize()
```

* To add aesthetics to our output we will display the image from the url along with the caption.

* To do that, we need to get the image from url and get the image bytes and display the image using plt.imshow() method.

``` python
image = Image.open(BytesIO(requests.get(image_url).content))
plt.imshow(image)
plt.axis("off")
plt.title(image_caption)
plt.show()
```

<b>Here we go with the description of the image we have sent to the Analyze API</b>

![Result](https://dev-to-uploads.s3.amazonaws.com/i/keecq8cf2vz8xdsmgl5d.png)

<b>That's AWESOME</b>. Azure had made it too easy for us to analyze images using Cognitive Services. The real implementation of it involves months of training, even years to build such an awesome Deep Learning Model.

Thank you so much Microsoft for this wonderful opportunity. I would also like to thank the <b>MSP Global Manager Pablo Veramendi</b>, <b>MSP India CPM's Aparna Mittal and Angela Carlson</b> and our <b>Microsoft AI Developer Advocate Arkodyuti Saha</b> for this wonderful initiative of MSP Dev Stories.