Open In App

How To Use Azure Cognitive Services For Image Recognition ?

Image recognition is one of the techniques which is widely used in today’s modern world. The rise of various technologies made this process much simpler. In this article, let us understand and demonstrate the image recognition process using Azure Cognitive Services and Azure Machine Learning. Before that, let us understand the following terms.

What Are Azure Cognitive Services?

What Is Azure Machine Learning Workspace?

Environment Setup

Creating Azure Machine Learning Workspace

Step 1: Log in to your Azure portal with an active subscription.



Step 2: Create Navigate to the services and click Azure Machine Learning.



Now, click Create to create your Machine learning instance. Follow the below instructions to create your instance.

Resource details:

Workspace details:

Now click Review + Create to deploy your resource.

Once your resource is deployed. Navigate to the Go to resource button.

Click Launch Studio. We will be redirected to our workspace after some Authentication process.

Navigate to the compute option under the category of Manage.

Now create a new compute instance by clicking New.

Configure the properties of your computing instance. Mention the name and Virtual machine type.

Also, choose the size of your Virtual machine and click Review + Create.

Once your instance is created and starts running, open the JupyterLab.

Creating Azure Cognitive Service

Step 1: Navigate to the Home page and click resources. Under the category of AI + Machine Learning, click Computer Vision.

Click Create and follow the below instructions to create your resource.under the project details, choose your subscription type and resource group.

Specify the instance details, Name, and pricing tier. There are three types of tiers and one is the Free tier. Choose according to your project requirements.

Also, make sure you acknowledge the terms of AI.

click Review + Create. Now to resource will start deploying.

Image Recognition:

Navigate to your JupyterLab workspace. Choose the Python version and start coding.

code for image recognition in Python:




# import the packages
import cv2
import numpy as np
from io import BytesIO
from PIL import Image
import json
import matplotlib.pyplot as plt
import requests
%matplotlib inline
 
# Replace with your actual subscription key
subscription_key = "c27d24c0f2fc487a82f1f8c0b5ee8d20"
 
# copy the end point from the computer vision resource and append '/vision/v3.1/analyze' at the end
# take the image link as the input from the user.
image_url = input("Enter your image URL: ")
 
# prepare the rquired parameters to create a HTTP POST request in Azure.
# add the subscription_key as a header, features which you want to recognize
# in the image as params and image_url as the data
 
 
headers = {'Ocp-Apim-Subscription-Key': subscription_key}
params = {'visualFeatures': 'Categories,Description,Faces,Objects'}
data = {'url': image_url}
 
# Now create the HTTP post request using the above mentioned parameters,
# headers and data using requests library and add the json to retrive the details
# from the image in the json format and store in the analysis
try:
    response = requests.post(
        analyze_url, headers=headers, params=params, json=data)
    response.raise_for_status()
    analysis = response.json()
except Exception as e:
    print("Error:", str(e))
 
# Display the image using Image library from the mentioned image_url
# and organize it using matplotlib
image = Image.open(BytesIO(requests.get(image_url).content))
plt.imshow(image)
plt.axis("off")
plt.show()
 
# Print the analysis result
print("Details of the image:")
print(json.dumps(analysis, indent=2))
 
# from the json data retrive the faces object
analysis['faces']
 
# from the faces object, extract the boundaries details (left, top, width, height)
# of each faceRectangle object.
faces = []
for rec in analysis['faces']:
    k = []
    k.append(rec['faceRectangle']['left'])
    k.append(rec['faceRectangle']['top'])
    k.append(rec['faceRectangle']['width'])
    k.append(rec['faceRectangle']['height'])
    faces.append(k)
print("left, top, width and height details of the people: ")
print(faces)
 
# We have extracted the boudaries, import the necessary libraries
 
# define a function generate_bounds() which takes the image_url and the faces as parameters
 
 
def generate_bounds(imageURL, boudRect):
        # open the image using Image library and tranform into numpy array
    image = Image.open(BytesIO(requests.get(imageURL).content))
    np_img = np.array(image)
    drawing = np_img.copy()
    # add the bourdaries with the mentioned color using cv2 library with thickness 4
    for i in range(len(boudRect)):
        color = (255, 0, 0)
        cv2.rectangle(drawing,
                      (int(boudRect[i][0]), int(boudRect[i][1])),
                      (int(boudRect[i][0] + boudRect[i][2]),
                       int(boudRect[i][1] + boudRect[i][3])),
                      color, 4)
 
    # Display the result using matplotlib
    plt.figure(figsize=(14, 8))
    plt.imshow(drawing)
    plt.axis("off")
    plt.show()
 
 
# make a function call by passing the actual parameters
print("Final result: ")
generate_bounds(image_url, faces)

Output: Let’s see the output of each print statement.

Azure Cognitive Services For Image Recognition – FAQ’S

What type of image recognition tasks can be performed using Azure Cognitive services?

Using Azure Cognitive Services, we can perform tasks including OCR, facial detection, image tagging, Object detection.

In which format we can expect the result from the computer vision API?

Computer vision API typically outputs the result in the form of JSON and the JSON object contains information about the image including categories, description, detected objects and faces.

Is there a limit to the number of API calls I can make?

It depends on the tier you choose while creating your services. For Computer vision API, Free tier provides 20 calls per minute and 5,000 calls per month and there are other tiers which can choose based on your application requirement.

How error-handling can be done with Computer Vision API?

The Computer vision API includes HTTP status codes. refer this link to know more about status codes: HTTP status codes

Machine


Article Tags :