-
Notifications
You must be signed in to change notification settings - Fork 558
Expand file tree
/
Copy pathImageCaptioningQuickstart.py
More file actions
151 lines (133 loc) · 5.1 KB
/
ImageCaptioningQuickstart.py
File metadata and controls
151 lines (133 loc) · 5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
'''
Computer Vision Quickstart for Microsoft Azure Cognitive Services.
Uses local and remote images in each example.
Prerequisites:
- Install the Computer Vision SDK:
pip install --upgrade azure-cognitiveservices-vision-computervision
- Install PIL:
pip install --upgrade pillow
- Create folder and collect images:
Create a folder called "images" in the same folder as this script.
Go to this website to download images:
https://github.com/Azure-Samples/cognitive-services-sample-data-files/tree/master/ComputerVision/Images
Add the following 7 images (or use your own) to your "images" folder:
faces.jpg, gray-shirt-logo.jpg, handwritten_text.jpg, landmark.jpg,
objects.jpg, printed_text.jpg and type-image.jpg
Run the entire file to demonstrate the following examples:
- Describe Image
- Categorize Image
- Tag Image
References:
- SDK: https://docs.microsoft.com/en-us/python/api/azure-cognitiveservices-vision-computervision/azure.cognitiveservices.vision.computervision?view=azure-python
- Documentaion: https://docs.microsoft.com/en-us/azure/cognitive-services/computer-vision/index
- API: https://westus.dev.cognitive.microsoft.com/docs/services/computer-vision-v3-2/operations/5d986960601faab4bf452005
'''
# <snippet_imports_and_vars>
# <snippet_imports>
from azure.cognitiveservices.vision.computervision import ComputerVisionClient
from azure.cognitiveservices.vision.computervision.models import OperationStatusCodes
from azure.cognitiveservices.vision.computervision.models import VisualFeatureTypes
from msrest.authentication import CognitiveServicesCredentials
from array import array
import os
from PIL import Image
import sys
import time
# </snippet_imports>
'''
Authenticate
Authenticates your credentials and creates a client.
'''
# <snippet_vars>
subscription_key = "PASTE_YOUR_COMPUTER_VISION_SUBSCRIPTION_KEY_HERE"
endpoint = "PASTE_YOUR_COMPUTER_VISION_ENDPOINT_HERE"
# </snippet_vars>
# </snippet_imports_and_vars>
# <snippet_client>
computervision_client = ComputerVisionClient(endpoint, CognitiveServicesCredentials(subscription_key))
# </snippet_client>
'''
END - Authenticate
'''
'''
Quickstart variables
These variables are shared by several examples
'''
# Images used for the examples: Describe an image, Categorize an image, Tag an image,
# Detect faces, Detect adult or racy content, Detect the color scheme,
# Detect domain-specific content, Detect image types, Detect objects
images_folder = os.path.join (os.path.dirname(os.path.abspath(__file__)), "images")
# <snippet_remoteimage>
remote_image_url = "https://moderatorsampleimages.blob.core.windows.net/samples/sample16.png"
# </snippet_remoteimage>
'''
END - Quickstart variables
'''
'''
Describe an Image - local
This example describes the contents of an image with the confidence score.
'''
print("===== Describe an Image - local =====")
# Open local image file
local_image_path = os.path.join (images_folder, "faces.jpg")
local_image = open(local_image_path, "rb")
# Call API
description_result = computervision_client.describe_image_in_stream(local_image)
# Get the captions (descriptions) from the response, with confidence level
print("Description of local image: ")
if ( not description_result.captions):
print("No description detected.")
else:
for caption in description_result.captions:
print("'{}' with confidence {:.2f}%".format(caption.text, caption.confidence * 100))
print()
'''
END - Describe an Image - local
'''
# <snippet_features_remote>
print("===== Analyze an image - remote =====")
# Select the visual feature(s) you want.
remote_image_features = ['description','tags']
# </snippet_features_remote>
# <snippet_analyze>
# Call API with URL and features
results_remote = computervision_client.analyze_image(remote_image_url , remote_image_features)
# Describe image
# Get the captions (descriptions) from the response, with confidence level
print("Description of remote image: ")
if ( not results_remote.description):
print("No description detected.")
else:
for caption in results_remote.description.captions:
print("'{}' with confidence {:.2f}%".format(caption.text, caption.confidence * 100))
print()
# Return tags
# Print results with confidence score
print("Tags in the remote image: ")
if (len(results_remote.tags) == 0):
print("No tags detected.")
else:
for tag in results_remote.tags:
print("'{}' with confidence {:.2f}%".format(tag.name, tag.confidence * 100))
# </snippet_analyze>
'''
Tag an Image - local
This example returns a tag (key word) for each thing in the image.
'''
print("===== Tag an Image - local =====")
# Open local image file
local_image = open(local_image_path, "rb")
# Call API local image
tags_result_local = computervision_client.tag_image_in_stream(local_image)
# Print results with confidence score
print("Tags in the local image: ")
if (len(tags_result_local.tags) == 0):
print("No tags detected.")
else:
for tag in tags_result_local.tags:
print("'{}' with confidence {:.2f}%".format(tag.name, tag.confidence * 100))
print()
'''
END - Tag an Image - local
'''
print("End of Computer Vision quickstart.")