Cognitive Computer Vision (Azure / Python)

Using Azure and Python to ask a computer what it sees in an image

I take it for granted that when I look at a picture I can identify what the items in the picture are. However, as a baby I could not and as a young child I would probably struggle with non-every day objects in a picture. Computers are at a point in their development where they are getting better at identifying objects in pictures. With an Azure subscription in hand I’ve decided to use some Python to see how well Microsoft’s Cognitive Services can identify objects in a picture.

This project requires an active Azure subscription, and this project falls into the “Cognitive Services” part of Azure’s AI + Machine Learning selection.

Azure Cognitive Services Menu
Azure Cognitive Services Menu

From within the Cognitive Services menu select “Compute Vision”.

geektechstuff_azure_cog_vision_2
Azure Compute Vision

As with my previous Azure projects some settings need to be added:

  • Name – a unique name for this project
  • Subscription – the subscription to charge the project to (use Free Trial if you are on the free trial)
  • Location – The location nearest to you (for me it’s UK South)
  • Pricing Tier – I went with F0 as it is the cheapest option
  • Resource Group – The resource group from which the project will consume resources
Creating Computer Vision
Creating Computer Vision

 

With settings entered hit create and wait a few moments for the resource to deploy. Once deployed you should have options for the project.

geektechstuff_azure_cog_vision_4
Computer Vision – Ready to go!

From this window make sure to click “Overview” on the left and make a note of your:

  • Endpoint (should be related to the location selected earlier)
  • API / Subscription Key (it is needed later and can be regenerated if required)
geektechstuff_azure_cog_vision_5
Make sure to note the Endpoint

Microsoft keeps a list of the API details and Endpoints at:

https://westus.dev.cognitive.microsoft.com/docs/services/5adf991815e1060e6355ad44/operations/56f91f2e778daf14a499e1fa

With all that in place it’s time for some Python 🙂

Python to analyse the image
Python to analyse the image
# geektechstuff
# Python libraries to import
import requests
import matplotlib.pyplot as plt
import json
from PIL import Image
from io import BytesIO
# Requires an active Azure API / Subscription Key
subscription_key = “ENTER SUBSCRIPTION KEY HERE”
assert subscription_key
# Make sure to use the region that matches where your Azure API / Subscription Key is from
url = endpoint+”/vision/v1.0/analyze?”
def image_analysis():
image_url = input(‘Please enter URL of image: ‘)
headers = {‘Ocp-Apim-Subscription-Key’: subscription_key }
params = {‘visualFeatures’: ‘Categories,Description,Color’}
data = {‘url’: image_url}
response = requests.post(url, headers=headers, params=params, json=data)
response.raise_for_status()
analysis = response.json()
image_caption = analysis[“description”][“captions”][0][“text”].capitalize()
# Display the image and overlays it with the caption.
image = Image.open(BytesIO(requests.get(image_url).content))
plt.imshow(image)
plt.axis(“off”)
_ = plt.title(image_caption, size=”x-large”, y=-0.1)
return(plt.show())
The GeekTechStuff home page analysed
The GeekTechStuff home page analysed

The Python code for this project can be found on my GitHub:

https://github.com/geektechdude/Python_Azure_Picture_Analysis

 

 

 

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.