Microsoft Docs

Image Analysis Quickstart

This quickstart guide will walk you through the essential steps to perform image analysis using Azure Cognitive Services. Image analysis allows you to extract a wealth of information from images, including descriptions, tags, categories, faces, and more.

Prerequisites

Step 1: Create a Computer Vision Resource

Navigate to the Azure portal and create a new Computer Vision resource. Choose a region and a pricing tier that suits your needs. Once created, you'll find your subscription key and endpoint in the resource's 'Keys and Endpoint' section.

Step 2: Install the SDK

As mentioned in the prerequisites, ensure you have the azure-cognitiveservices-vision-computervision library installed.

Step 3: Write the Code

Create a new Python file (e.g., analyze_image.py) and paste the following code into it. Remember to replace YOUR_SUBSCRIPTION_KEY and YOUR_ENDPOINT with your actual credentials.


import os
from azure.cognitiveservices.vision.computervision import ComputerVisionClient
from msrest.authentication import CognitiveServicesCredentials

# --- Configuration ---
# Replace with your subscription key and endpoint
subscription_key = "YOUR_SUBSCRIPTION_KEY"
endpoint = "YOUR_ENDPOINT"

# URL of the image to analyze
image_url = "https://learn.microsoft.com/en-us/azure/cognitive-services/computer-vision/media/quickstarts/analyze_image.jpg"

# --- Authentication ---
credentials = CognitiveServicesCredentials(subscription_key)
client = ComputerVisionClient(endpoint, credentials)

# --- Image Analysis ---
print(f"Analyzing image: {image_url}\n")

# Get a description of the image
description_result = client.describe_image(image_url)

print("Description:")
if len(description_result.captions) == 0:
    print("  No description detected.")
else:
    for caption in description_result.captions:
        print(f"  {caption.text} (Confidence: {caption.confidence:.2f})")

# Get tags for the image
tags_result = client.tag_image(image_url)

print("\nTags:")
if len(tags_result.tags) == 0:
    print("  No tags detected.")
else:
    for tag in tags_result.tags:
        print(f"  {tag.name} (Confidence: {tag.confidence:.2f})")

# Get categories for the image
categories_result = client.categorize_image(image_url)

print("\nCategories:")
if len(categories_result.categories) == 0:
    print("  No categories detected.")
else:
    for category in categories_result.categories:
        print(f"  {category.name} (Score: {category.score:.2f})")

# Detect faces in the image (if any)
print("\nDetecting faces...")
try:
    faces_result = client.analyze_image(
        image_url,
        visual_features=["Faces"]
    )
    if len(faces_result.faces) == 0:
        print("  No faces detected.")
    else:
        print(f"  Detected {len(faces_result.faces)} face(s).")
        for i, face in enumerate(faces_result.faces):
            print(f"  Face {i+1}:")
            print(f"    Age: {face.age}")
            print(f"    Gender: {face.gender}")
except Exception as e:
    print(f"  Could not detect faces: {e}")


print("\nImage analysis complete.")
            

Tip: You can also analyze local images by replacing the image_url with a file path and opening the image in binary read mode.


with open("path/to/your/local/image.jpg", "rb") as image_stream:
    analysis = client.analyze_image_in_stream(image_stream, visual_features=["Categories", "Tags", "Description"])
                

Step 4: Run the Code

Open your terminal or command prompt, navigate to the directory where you saved your Python file, and run it:

python analyze_image.py

Example Output

The output will vary depending on the image, but it will look something like this:


Analyzing image: https://learn.microsoft.com/en-us/azure/cognitive-services/computer-vision/media/quickstarts/analyze_image.jpg

Description:
  A group of people standing in front of a building. (Confidence: 0.85)
  People are standing in front of a building. (Confidence: 0.79)

Tags:
  people (Confidence: 0.98)
  crowd (Confidence: 0.95)
  outdoor (Confidence: 0.92)
  building (Confidence: 0.88)
  street (Confidence: 0.85)
  group (Confidence: 0.83)
  standing (Confidence: 0.80)
  city (Confidence: 0.75)

Categories:
  people_outdoors (Score: 0.89)
  person (Score: 0.82)
  building (Score: 0.78)

Detecting faces...
  No faces detected.

Image analysis complete.
            

Note: The 'Detect Faces' feature requires specific enabling and might have different confidence thresholds. For detailed face analysis, consider using the Face API.

Next Steps

Congratulations! You've successfully performed basic image analysis. Explore other features of the Computer Vision API: