我想使用 Microsoft 的 Face API 识别一群人中的面孔。我已经阅读了他们的文档并编写了这段代码,但它没有按预期工作。它显示一些错误。
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)
现在它会这样:
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" }
我不知道为什么会出现这种情况。关于我做错了什么的任何解决方案或建议。
x = client.face.detect({'url': sam})
正在尝试访问文字 url“url”,因为您将其作为字符串提供。我假设你打算用这个来代替:
x = client.face.detect({main: sam})
因为您调用了 get_url(token=None)
并将结果存储到 main
变量中。