-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathposture_ms_cognitive_services.py
46 lines (42 loc) · 1.53 KB
/
posture_ms_cognitive_services.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
#!/bin/python3.6
import requests, json
import keys
from analyse_posture import Posture
# Request headers.
headers = {
'Content-Type': 'application/octet-stream',
'Ocp-Apim-Subscription-Key': keys.key_face,
}
# Request parameters.
params = {
'returnFaceId': 'false',
'returnFaceLandmarks': 'true',
'returnFaceAttributes': 'headPose,emotion',
#'returnFaceAttributes': 'age,gender,headPose,smile,facialHair,glasses,emotion,hair,makeup,occlusion,accessories,blur,exposure,noise',
}
def get_posture(image_path):
with open(image_path, 'rb') as f:
image_data = f.read()
try:
# Execute the REST API call and get the response.
response = requests.request('POST', keys.url_face, files={}, data=image_data, headers=headers, params=params)
result = json.loads(response.text)
if result:
try:
posture = Posture(
result[0]["faceAttributes"]["headPose"]["yaw"],
result[0]["faceAttributes"]["headPose"]["pitch"],
result[0]["faceAttributes"]["headPose"]["roll"],
result[0]["faceLandmarks"]["noseTip"]["y"],
result[0]["faceLandmarks"]["noseTip"]["x"],
)
return posture
except KeyError as e:
print('Error:')
print(e)
print ('Response:')
print (json.dumps(result, sort_keys=True, indent=2))
except Exception as e:
print('Error:')
print(e)
return None