调用Cognitive Face API时出错

时间:2018-04-04 17:23:46

标签: python-3.x face-api

当我运行python create_person.py user52

时,我遇到了用于面部识别的微软视觉认知api的问题
Traceback (most recent call last):
  File "create_person.py", line 9, in <module>
    res = CF.person.create(personGroupId, str(sys.argv[1]))
  File ".../cognitive_face/person.py", line 74, in create
    return util.request('POST', url, json=json)
  File ".../cognitive_face/util.py", line 106, in request
    response.status_code, response.text)
cognitive_face.util.CognitiveFaceException: Error when calling Cognitive Face API:
    status_code: 404
    code: 404
    message: { "statusCode": 404, "message": "Resource not found" }

这是我的create_person.py文件

import sys
import cognitive_face as CF
from global_variables import personGroupId
import sqlite3

Key = 'b065632b79ac4d97a96d5781463d54b1'
CF.Key.set(Key)
if len(sys.argv) is not 1:
    res = CF.person.create(personGroupId, str(sys.argv[1]))
    print(res)
    extractId = str(sys.argv[1])[-2:]
    connect = sqlite3.connect("Face-DataBase")
    cmd = "SELECT * FROM Students WHERE ID = " + extractId
    cursor = connect.execute(cmd)
    isRecordExist = 0
    for row in cursor:                                                          # checking wheather the id exist or not
        isRecordExist = 1
    if isRecordExist == 1:                                                      # updating name and roll no
        connect.execute("UPDATE Students SET personID = ? WHERE ID = ?",(res['personId'], extractId))
    connect.commit()                                                            # commiting into the database
    connect.close()
    print ("Person ID successfully added to the database")

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

首先检查您传入的personGroupId是否确实存在。您可以通过向/persongroups/:id发出GET来执行此操作。现场演示here

如果存在,那么API密钥的区域很可能与默认区域(which is West US)不匹配。

您可以尝试将其添加到您的代码中:

BASE_URL = 'https://YOUR-REGION.api.cognitive.microsoft.com/face/v1.0/'
CF.BaseUrl.set(BASE_URL)