python - 如何使用 Microsoft Face API 识别集体照片中的人脸

标签 python face-recognition azure-cognitive-services face

我想使用 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 中> 变量。

关于python - 如何使用 Microsoft Face API 识别集体照片中的人脸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41326929/

相关文章:

python - 在 Django 中使用 Python 解析 JSON 文件

python - NumPy 中的高效 3x3 和 2x2 行列式

opencv - 如何在OpenCV中将<int>数组插入CvArr

javascript - 将中国 MS Azure 认知服务与全局 WEB 认知服务整合

azure-cognitive-services - 尝试从 Azure 添加 key 时参数错误、订阅 key 无效

android - Microsoft 语音 SDK : android library v1. 8.0 缺少 native 语音扩展 kws.so 库

python - python-单行嵌套循环

python - 与 mysql 的连接速度极慢

Android Mobile Vision API检测嘴巴是否张开

python - 在OpenCV中自动拍照