|
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, requests, 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 = 'https://westcentralus.api.cognitive.microsoft.com' |
| 4 | +import json |
| 5 | +import requests |
| 6 | +import os |
| 7 | +from pprint import pprint |
| 8 | + |
| 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 |
| 13 | +''' |
| 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' |
23 | 18 |
|
24 | 19 | # Request headers. |
25 | 20 | headers = { |
|
34 | 29 | 'returnFaceAttributes': 'age,gender,headPose,smile,facialHair,glasses,emotion,hair,makeup,occlusion,accessories,blur,exposure,noise', |
35 | 30 | } |
36 | 31 |
|
37 | | -# Body. The URL of a JPEG image to analyze. |
38 | | -body = {'url': 'https://upload.wikimedia.org/wikipedia/commons/c/c3/RH_Louise_Lillian_Gish.jpg'} |
| 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'} |
39 | 34 |
|
40 | 35 | try: |
41 | | - # Execute the REST API call and get the response. |
42 | | - response = requests.request('POST', uri_base + '/face/v1.0/detect', json=body, data=None, headers=headers, params=params) |
| 36 | + # Call API. |
| 37 | + response = requests.post(endpoint, headers=headers, params=params, json=body) |
| 38 | + response.raise_for_status() |
43 | 39 |
|
44 | | - print ('Response:') |
45 | | - parsed = json.loads(response.text) |
46 | | - print (json.dumps(parsed, sort_keys=True, indent=2)) |
| 40 | + print("\nHeaders:\n") |
| 41 | + print(response.headers) |
| 42 | + |
| 43 | + print("\nJSON Response:\n") |
| 44 | + pprint(response.json()) |
47 | 45 |
|
48 | 46 | except Exception as e: |
49 | 47 | print('Error:') |
50 | 48 | print(e) |
51 | | - |
52 | | -#################################### |
0 commit comments