forked from microsoft/c9-python-getting-started
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcall_api.py
47 lines (36 loc) · 1.95 KB
/
call_api.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
# 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
# You need to update the SUBSCRIPTION_KEY to
# they key for your Computer Vision Service
SUBSCRIPTION_KEY = "xxxxxxxxxxxxxxxxxxxxxxxxxxx"
# You need to update the vision_service_address to the address of
# your Computer Vision Service
vision_service_address = "https://canadacentral.api.cognitive.microsoft.com/vision/v2.0/"
# Add the name of the function you 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'}
# 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
results = response.json()
print(json.dumps(results))