|
2 | 2 | # Licensed under the MIT License. |
3 | 3 |
|
4 | 4 | import json |
5 | | -import requests |
6 | | -import os |
| 5 | +import os |
7 | 6 | from pprint import pprint |
| 7 | +import requests |
8 | 8 |
|
9 | 9 | ''' |
10 | | -This sample makes a call to the Face API with a URL image query |
11 | | -to detect faces and features in an image. |
12 | | -Face API: https://westus.dev.cognitive.microsoft.com/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f30395236 |
| 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 | 13 | ''' |
14 | 14 |
|
15 | | -# Add your Face subscription key and endpoint to your environment variables. |
16 | | -subscription_key = os.environ['FACE_SUBSCRIPTION_KEY'] |
17 | | -endpoint = os.environ['FACE_ENDPOINT'] + '/face/v1.0/detect' |
| 15 | +# Add your Computer Vision 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 | 18 |
|
19 | 19 | # Request headers. |
20 | 20 | headers = { |
21 | 21 | 'Content-Type': 'application/json', |
22 | 22 | 'Ocp-Apim-Subscription-Key': subscription_key, |
23 | 23 | } |
24 | 24 |
|
25 | | -# Request parameters. |
| 25 | +# Request parameters. All of them are optional. |
26 | 26 | params = { |
27 | | - 'returnFaceId': 'true', |
28 | | - 'returnFaceLandmarks': 'false', |
29 | | - 'returnFaceAttributes': 'age,gender,headPose,smile,facialHair,glasses,emotion,hair,makeup,occlusion,accessories,blur,exposure,noise', |
| 27 | + 'visualFeatures': 'Categories,Description,Color', |
| 28 | + 'language': 'en', |
30 | 29 | } |
31 | 30 |
|
32 | | -# The URL of a JPEG image of faces to analyze. |
33 | | -body = {'url': 'https://raw.githubusercontent.com/Azure-Samples/cognitive-services-sample-data-files/master/Face/images/test-image-person-group.jpg'} |
| 31 | +# Any image with objects will work. |
| 32 | +body = {'url': 'https://raw.githubusercontent.com/Azure-Samples/cognitive-services-sample-data-files/master/ComputerVision/Images/objects.jpg'} |
34 | 33 |
|
| 34 | +# Call the API. |
35 | 35 | try: |
36 | | - # Call API. |
37 | 36 | response = requests.post(endpoint, headers=headers, params=params, json=body) |
38 | 37 | response.raise_for_status() |
39 | 38 |
|
|
42 | 41 |
|
43 | 42 | print("\nJSON Response:\n") |
44 | 43 | pprint(response.json()) |
45 | | - |
46 | | -except Exception as e: |
47 | | - print('Error:') |
48 | | - print(e) |
| 44 | +except Exception as ex: |
| 45 | + raise ex |
0 commit comments