azure.cognitiveservices.vision.face.models._models_py3.APIErrorException:(InvalidImageSize)图像尺寸太小

问题描述 投票:0回答:1

错误消息我正在尝试上传合理大小的图像(大约20KB)。但根据文档图像大小可以上传1KB至6MB。我希望程序的某些部分需要修改以纠正错误。

  File "add_person_faces.py", line 46, in <module>
    res = face_client.person_group_person.add_face_from_stream(global_var.personGroupId, person_id, img_data)
  File "C:\Python\Python36\lib\site-packages\azure\cognitiveservices\vision\face\operations\_person_group_person_operations.py", line 785, in add_face_from_stream
    raise models.APIErrorException(self._deserialize, response)
azure.cognitiveservices.vision.face.models._models_py3.APIErrorException: (InvalidImageSize) Image size is too small.

CODE

import os, time
import global_variables as global_var
from azure.cognitiveservices.vision.face import FaceClient
from msrest.authentication import CognitiveServicesCredentials
from azure.cognitiveservices.vision.face.models import TrainingStatusType, Person, SnapshotObjectType, OperationStatusType
import urllib
import sqlite3
import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning

requests.packages.urllib3.disable_warnings(InsecureRequestWarning)


KEY = global_var.key

ENDPOINT = 'https://centralindia.api.cognitive.microsoft.com'

face_client = FaceClient(ENDPOINT,CognitiveServicesCredentials(KEY))

def get_person_id():
    person_id = ''
    extractId = str(sys.argv[1])[-2:]
    connect = sqlite3.connect("Face-DataBase")
    c = connect.cursor()
    cmd = "SELECT * FROM Students WHERE ID = " + extractId
    c.execute(cmd)
    row = c.fetchone()
    person_id = row[3]
    connect.close()
    return person_id

if len(sys.argv) is not 1:
    currentDir = os.path.dirname(os.path.abspath(__file__))
    imageFolder = os.path.join(currentDir, "dataset/" + str(sys.argv[1]))
    person_id = get_person_id()
    for filename in os.listdir(imageFolder):
        if filename.endswith(".jpg"):
            print(filename)
            img_data = open(os.path.join(imageFolder,filename), "rb")
            res = face_client.face.detect_with_stream(img_data)
            if not res:
                print('No face detected from image {}'.format(filename))
                continue

            res = face_client.person_group_person.add_face_from_stream(global_var.personGroupId, person_id, img_data)
            print(res)  
            time.sleep(6)
else:
    print("supply attributes please from dataset folder")
python azure image-processing face-recognition face-api
1个回答
0
投票

查看完您正在进行的API调用后,我意识到缺少一些元素。您可能没有发布完整的代码,但是我将在下面添加一个示例来说明步骤。使用这些步骤可避免任何图像错误,因此“错误尺寸”图像错误很可能是由于缺少步骤而引起的。

在您的代码中,必须先为该PGP所属的人员组创建一个人员组(PG),然后才能将图像添加到人员组人员(PGP)。然后,在创建人员组(开始时为空)之后,必须创建其中具有该PG ID的人员组人。一旦创建了这两项,就可以开始将图像添加到“人员组”中。

所以这是上面总结的步骤:

  1. 使用API​​调用create()创建人员组
  2. 使用create()的API调用创建人员组人员>
  3. 通过API调用add_face_from_stream()将您的图像添加到人员组人员中>
  4. 一旦添加了属于“人物组人物”的所有图像,便可以根据需要使用其中的数据。

    请参见下面的代码示例,在该示例中,将单个本地图像上传并添加到人员组人员中。如果您要下载和测试,我将提供我正在使用的图像。

enter image description here

import os
from azure.cognitiveservices.vision.face import FaceClient
from msrest.authentication import CognitiveServicesCredentials

KEY = os.environ['FACE_SUBSCRIPTION_KEY']
ENDPOINT = os.environ['FACE_ENDPOINT']

face_client = FaceClient(ENDPOINT, CognitiveServicesCredentials(KEY))

person_group_id = 'women_person_group'
person_id = 'women_hats'

image_name = 'woman_with_sunhat.jpg'

# Create empty Person Group. Person Group ID must be lower case, alphanumeric, and/or with '-', '_'.
print('Creating a Person Group:', person_group_id)
face_client.person_group.create(person_group_id=person_group_id, name=person_group_id)

# Create a Person Group Person.
print('Creating the Person Group Person:', person_id)
women_hat_group = face_client.person_group_person.create(person_group_id, person_id)

# Add image to our Person Group Person.
print('Adding face to the Person Group Person:', person_id)
face_image = open(image_name, 'r+b')
face_client.person_group_person.add_face_from_stream(person_group_id, women_hat_group.person_id, face_image)
# Print ID from face.
print('Person ID:', women_hat_group.person_id)

# Since testing, delete the Person Group, so no duplication conflicts if script is run again.
face_client.person_group.delete(person_group_id)
print()
print("Deleted the person group {} from the Azure Face account.".format(person_group_id))
© www.soinside.com 2019 - 2024. All rights reserved.