gpt4 book ai didi

python - azure.cognitiveservices.vision.face.models._models_py3.APIErrorException : (InvalidImageSize) Image size is too small

转载 作者:行者123 更新时间:2023-12-03 04:05:15 33 4
gpt4 key购买 nike

错误消息我正在尝试上传合理大小的图像(大约 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.

代码

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")

最佳答案

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

在您的代码中,在将图像添加到人员组人员 (PGP) 之前,您必须为该 PGP 所属的人员组 (PG) 创建一个。然后,在创建人员组(开始时为空)后,您必须创建一个包含该 PG ID 的人员组人员。创建这两件事后,您就可以开始将图像添加到您的人员组人员中。

以下是上面总结的步骤:

  1. 使用 API 调用 create() 创建人员组
  2. 创建一个人员组人员,并对其 API 调用 create()
  3. 使用 API 调用 add_face_from_stream() 将您的图像添加到人员组人员中

添加属于您的人员组人员的所有图像后,您就可以根据需要使用其中的数据。

请参阅下面的代码示例,其中上传单个本地镜像并将其添加到人员组人员中。如果您想下载和测试,我将包含我正在使用的图像。

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))

关于python - azure.cognitiveservices.vision.face.models._models_py3.APIErrorException : (InvalidImageSize) Image size is too small,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60325360/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com