-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathread_key_pair_list.py
57 lines (43 loc) · 2.13 KB
/
read_key_pair_list.py
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
# This code will show you how to call the Computer Vision API from Python
# You can find documentation on the Computer Vision Analyze Image method here
# https://westus.dev.cognitive.microsoft.com/docs/services/5adf991815e1060e6355ad44/operations/56f91f2e778daf14a499e1fa
# Use the requests library to simplify making a REST API call from Python
import requests
# We will need the json library to read the data passed back
# by the web service
import json
# We need the address of our Computer vision service
vision_service_address = "https://canadacentral.api.cognitive.microsoft.com/vision/v2.0/"
# Add the name of the function we want to call to the address
address = vision_service_address + "analyze"
# According to the documentation for the analyze image function
# There are three optional parameters: language, details & visualFeatures
parameters = {'visualFeatures':'Description,Color',
'language':'en'}
# We need the key to access our Computer Vision Service
subscription_key = "xxxxxxxxxxxxxxxxxxxxxxx"
# Open the image file to get a file object containing the image to analyze
image_path = "./TestImages/PolarBear.jpg"
image_data = open(image_path, 'rb').read()
# According to the documentation for the analyze image function
# we need to specify the subscription key and the content type
# in the HTTP header. Content-Type is application/octet-stream when you pass in a image directly
headers = {'Content-Type': 'application/octet-stream',
'Ocp-Apim-Subscription-Key': subscription_key}
# According to the documentation for the analyze image function
# we use HTTP POST to call this function
response = requests.post(address, headers=headers, params=parameters, data=image_data)
# Raise an exception if the call returns an error code
response.raise_for_status()
# Display the JSON results returned in their raw JSON format
results = response.json()
print(json.dumps(results))
# Print out all the tags in the description
print()
print('all tags')
for item in results['description']['tags']:
print(item)
# print out the first tag in the description
print()
print('first_tag')
print(results['description']['tags'][0])