How to use Microsoft Face API to identify faces in a group pictures

766 Views Asked by At

I want to identify faces in a group of people using Microsoft's Face API. I have gone through their documentation and I wrote this code but it's not working as expected. It shows some error.

from projectoxford import Client,Face
print('----- Setting Environment to use Face API -----')
client = Client('MY API KEY')
personGroup = 'friends'
print('----- Uploading Sam -----')
storage.child('images/sam.jpg').put('/home/parth/Downloads/sam.jpg')
print('----- Done Uploading -----')
sam = storage.child('images/sam.jpg').get_url(token=None)
print(sam)
print('----- Uploading Dev -----')
storage.child('images/dev.jpg').put('/home/parth/Downloads/devansh.jpg')
print('----- Done Uploading -----')
dev = storage.child('images/dev.jpg').get_url(token=None)
print(dev)
print('----- Uploading Main -----')
storage.child('images/main.jpg').put('/home/parth/Downloads/main.jpg')
print('----- Done Uploading -----')
main = storage.child('images/main.jpg').get_url(token=None) 
print(main)
print('----- Detecting Face Sam -----')
x = client.face.detect({'url': sam})
print(x)
sam_faceId = x[0]['faceId']
client.face.personGroup.createOrUpdate(personGroup, 'friends')
client.face.person.createOrUpdate(personGroup, [sam_faceId], 'Saumil')
client.face.personGroup.trainAndPollForCompletion(personGroup)
detectResults = client.face.detect({'url': main})
faceIds = []
for result in detectResults:
    faceIds.append(result['faceId'])

'''
Identification Method
'''
identifyResults = client.face.identify(personGroup, faceIds)
for result in identifyResults:
    for candidate in result['candidates']:
        confidence = candidate['confidence']
        personData = client.face.person.get(personGroup, candidate['personId'])
        name = personData['name']
        print('identified {0} with {1}% confidence'.format(name, str(float(confidence) * 100)))
client.face.personGroup.delete(personGroup)

Now it thorws this:

Traceback (most recent call last):
  File "att3.py", line 55, in <module>
    x = client.face.detect({'url': sam})
  File "/usr/local/lib/python2.7/dist-packages/projectoxford/Face.py", line 56, in detect
    return Base._postWithOptions(self, _detectUrl, options, params)
  File "/usr/local/lib/python2.7/dist-packages/projectoxford/Base.py", line 87, in _postWithOptions
    return Base._invoke(self, call)
  File "/usr/local/lib/python2.7/dist-packages/projectoxford/Base.py", line 45, in _invoke
    raise Exception('status {0}: {1}'.format(str(response.status_code), response.text))
Exception: status 404: { "statusCode": 404, "message": "Resource not found" }

I don't know why it occurs. Any solution or suggestion on what I am doing wrong.

1

There are 1 best solutions below

1
On

x = client.face.detect({'url': sam}) is trying to access the literal url "url" because you gave it that as a string. I assume you meant to use this instead: x = client.face.detect({main: sam}) because you called get_url(token=None) and stored the result into the main variable.