Azure-Samples/cognitive-services-sample-data-files

Help in face api

Ani1211999 opened this issue · 2 comments

I am using face api for comparing two images in python by verify.I cannot do it on Python on my google colab stored files.Anyt help?

@Ani1211999 - can you provide a working sample (without your key/endpoint) and an example file?


Yes surely here is the code. Face detect works but face verify doesn't. Two functions are there for detect and bverify.IDs from detect will be passed to Face verify.

subscription_key = '43164280359044c89203821eec7ec6ae'
uri_base = 'https://centralindia.api.cognitive.microsoft.com'
headers = {
'Content-Type': 'application/octet-stream',
'Ocp-Apim-Subscription-Key': subscription_key,
}
params = {
'returnFaceId': 'true',
'returnFaceLandmarks': 'false',
'returnFaceAttributes': 'age,gender',
'recognitionModel': 'recognition_02',
}
def request_features(image_name):
data=open(r'{}'.format(image_name),'rb')
try:
#response = requests.request('POST', uri_base + '/face/v1.0/detect', json=body, data=None, headers=headers, params=params)
response = requests.request('POST', uri_base + '/face/v1.0/detect',data=data, headers=headers, params=params)
response_f=response.json()
print(response_f)
faceId=response_f[0]['faceId']
gender=response_f[0]['faceAttributes']['gender']
age=response_f[0]['faceAttributes']['age']

return faceId,gender,age

except Exception as e:
print('Error:')
print(e)
params_v={}

def verify_img(id1,id2):
data_v={
'faceId1':id1,
'faceId2':id2
}
data_json_string=json.dumps(data_v)
data_json_object=json.loads(data_json_string)
try:
#response = requests.request('POST', uri_base + '/face/v1.0/detect', json=body, data=None, headers=headers, params=params)
response = requests.request('POST', uri_base + '/face/v1.0/verify',data=data_json_object, headers=headers, params=params_v)
response_f=response.json()
print(response_f)

return response_f

except Exception as e:
print('Error:')
print(e)