Image Recognition
Navigate to your JupyterLab workspace. Choose the Python version and start coding.
code for image recognition in Python:
- Required libraries:
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 analyze_url = "https://cv-az.cognitiveservices.azure.com/vision/v3.1/analyze" # 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.
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.