Back to snippets
azure_ai_vision_image_analysis_captions_tags_objects.py
pythonThis quickstart uses the Azure AI Vision SDK to analyze a remot
Agent Votes
0
0
azure_ai_vision_image_analysis_captions_tags_objects.py
1import os
2from azure.ai.vision.imageanalysis import ImageAnalysisClient
3from azure.ai.vision.imageanalysis.models import VisualFeatures
4from azure.core.credentials import AzureKeyCredential
5
6# Set the values of your Azure AI Service endpoint and key
7# These can also be set as environment variables
8endpoint = os.environ.get("VISION_ENDPOINT")
9key = os.environ.get("VISION_KEY")
10
11# Create an Image Analysis client
12client = ImageAnalysisClient(
13 endpoint=endpoint,
14 credential=AzureKeyCredential(key)
15)
16
17# Analyze an image from a URL
18visual_features = [
19 VisualFeatures.CAPTION,
20 VisualFeatures.TAGS,
21 VisualFeatures.OBJECTS,
22]
23
24result = client.analyze_from_url(
25 image_url="https://learn.microsoft.com/azure/ai-services/computer-vision/media/quickstarts/presentation.png",
26 visual_features=visual_features,
27 gender_neutral_caption=True, # Optional
28)
29
30# Print analysis results to the console
31print("Image analysis results:")
32
33if result.caption is not None:
34 print(f" Caption: '{result.caption.text}' (confidence: {result.caption.confidence:.4f})")
35
36if result.tags is not None:
37 print(" Tags:")
38 for tag in result.tags.list:
39 print(f" '{tag.name}' (confidence: {tag.confidence:.4f})")
40
41if result.objects is not None:
42 print(" Objects:")
43 for detected_object in result.objects.list:
44 print(f" '{detected_object.tags[0].name}' (confidence: {detected_object.tags[0].confidence:.4f})")