Skip to content

Commit 33a4256

Browse files
authored
Updated structure and libraries
1 parent 43f33d3 commit 33a4256

File tree

1 file changed

+24
-28
lines changed

1 file changed

+24
-28
lines changed

python/Vision/Face1.0.py

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,20 @@
11
# Copyright (c) Microsoft Corporation. All rights reserved.
22
# Licensed under the MIT License.
33

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'
2318

2419
# Request headers.
2520
headers = {
@@ -34,19 +29,20 @@
3429
'returnFaceAttributes': 'age,gender,headPose,smile,facialHair,glasses,emotion,hair,makeup,occlusion,accessories,blur,exposure,noise',
3530
}
3631

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'}
3934

4035
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()
4339

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())
4745

4846
except Exception as e:
4947
print('Error:')
5048
print(e)
51-
52-
####################################

0 commit comments

Comments
 (0)