|
1 | 1 | # Copyright (c) Microsoft Corporation. All rights reserved. |
2 | 2 | # Licensed under the MIT License. |
3 | 3 |
|
4 | | -########### Python 3.6 ############# |
5 | | -import http.client, urllib.request, urllib.parse, urllib.error, base64, json |
6 | | - |
7 | | -############################################### |
8 | | -#### Update or verify the following values. ### |
9 | | -############################################### |
10 | | - |
11 | | -# Replace the subscription_key string value with your valid subscription key. |
12 | | -subscription_key = 'Enter key here' |
13 | | - |
14 | | -# Replace or verify the region. |
15 | | -# |
16 | | -# You must use the same region in your REST API call as you used to obtain your subscription keys. |
17 | | -# For example, if you obtained your subscription keys from the westus region, replace |
18 | | -# "westcentralus" in the URI below with "westus". |
19 | | -# |
20 | | -# NOTE: Free trial subscription keys are generated in the westcentralus region, so if you are using |
21 | | -# a free trial subscription key, you should not need to change this region. |
22 | | -uri_base = 'westcentralus.api.cognitive.microsoft.com' |
23 | | - |
| 4 | +import json |
| 5 | +import os |
| 6 | +from pprint import pprint |
| 7 | +import requests |
| 8 | + |
| 9 | +''' |
| 10 | +This sample makes a call to the Computer Vision API with a URL image query to analyze an image, |
| 11 | +and then returns user input parameter data like category, description, and color. |
| 12 | +API: https://westus.dev.cognitive.microsoft.com/docs/services/5adf991815e1060e6355ad44/operations/587f2c6a154055056008f200 |
| 13 | +''' |
| 14 | + |
| 15 | +# Add your Bing Search V7 subscription key and endpoint to your environment variables. |
| 16 | +subscription_key = os.environ['COMPUTER_VISION_SUBSCRIPTION_KEY'] |
| 17 | +endpoint = os.environ['COMPUTER_VISION_ENDPOINT'] + "/vision/v2.1/analyze" |
| 18 | + |
| 19 | +# Request headers. |
24 | 20 | headers = { |
25 | | - # Request headers. |
26 | 21 | 'Content-Type': 'application/json', |
27 | 22 | 'Ocp-Apim-Subscription-Key': subscription_key, |
28 | 23 | } |
29 | 24 |
|
30 | | -params = urllib.parse.urlencode({ |
31 | | - # Request parameters. All of them are optional. |
| 25 | +# Request parameters. All of them are optional |
| 26 | +params = { |
32 | 27 | 'visualFeatures': 'Categories,Description,Color', |
33 | 28 | 'language': 'en', |
34 | | -}) |
| 29 | +} |
35 | 30 |
|
36 | 31 | # Replace the three dots below with the URL of a JPEG image of a celebrity. |
37 | | -body = "{'url':'https://upload.wikimedia.org/wikipedia/commons/1/12/Broadway_and_Times_Square_by_night.jpg'}" |
| 32 | +body = {'url': 'https://raw.githubusercontent.com/Azure-Samples/cognitive-services-sample-data-files/master/ComputerVision/Images/objects.jpg'} |
38 | 33 |
|
| 34 | +# Call the API |
39 | 35 | try: |
40 | | - # Execute the REST API call and get the response. |
41 | | - conn = http.client.HTTPSConnection('westcentralus.api.cognitive.microsoft.com') |
42 | | - conn.request("POST", "/vision/v1.0/analyze?%s" % params, body, headers) |
43 | | - response = conn.getresponse() |
44 | | - data = response.read() |
45 | | - |
46 | | - # 'data' contains the JSON data. The following formats the JSON data for display. |
47 | | - parsed = json.loads(data) |
48 | | - print ("Response:") |
49 | | - print (json.dumps(parsed, sort_keys=True, indent=2)) |
50 | | - conn.close() |
51 | | - |
52 | | -except Exception as e: |
53 | | - print('Error:') |
54 | | - print(e) |
| 36 | + response = requests.post(endpoint, headers=headers, params=params, json=body) |
| 37 | + response.raise_for_status() |
| 38 | + |
| 39 | + print("\nHeaders:\n") |
| 40 | + print(response.headers) |
| 41 | + |
| 42 | + print("\nJSON Response:\n") |
| 43 | + pprint(response.json()) |
| 44 | +except Exception as ex: |
| 45 | + raise ex |
0 commit comments