【发布时间】:2020-04-08 12:39:41
【问题描述】:
首先,文档here 说“支持JPEG、PNG、GIF(第一帧)和BMP 格式。允许的图像文件大小为1KB 到6MB。”
我正在发送一个大约 1.4 MB 的 .jpg 在我的搜索中,遇到此问题的其他人是自定义形成数据包并遇到了块传输图像的问题。 但是与others 不同的是,我没有形成自己的API 调用,只是将jpg 传递给python sdk。 出了什么问题/我错过了什么?
错误是:
getting image, start time
opening image: 2019_11_30_18_40_21.jpg
time elapsed for capturing image: 8.007975816726685
time elapsed for detecting image: 0.0017137527465820312
appending face found in image
identifying face
time elapsed for identifying image: 0.8008027076721191
Person for face ID e7b2c3fe-6a62-471f-8371-8c1e96608362 is identified in 2019_11_30_18_40_21.jpg with a confidence of 0.68515.
Traceback (most recent call last):
File "./GreeterCam_V0.1 - testing.py", line 116, in <module>
face_client.person_group_person.add_face_from_stream(PERSON_GROUP_ID, face.candidates[0].person_id, image)
File "/home/pi/.local/lib/python3.7/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.
我的源代码是:
if __name__ == '__main__':
FRAMES_PER_SECOND = 0.13
ENDPOINT = os.environ['COGNITIVE_SERVICE_ENDPOINT']
KEY = os.environ['COGNITIVE_SERVICE_KEY']
face_client = FaceClient(ENDPOINT, CognitiveServicesCredentials(KEY))
PERSON_GROUP_ID = 'my-unique-person-group'
#IMAGES_FOLDER = os.path.join(os.path.dirname(os.path.realpath(__file__)))
#camera = PiCamera()
#camera.start_preview()
test_images = [file for file in glob.glob('*.jpg')]
#webcam = cv2.VideoCapture(0)
while(True):
start_time = time.time()
print('getting image, start time')
for image_name in test_images:
image = open(image_name, 'r+b')
print("opening image: ", image_name)
time.sleep(5)
faces = face_client.face.detect_with_stream(image)
#image = open(os.path.join(IMAGES_FOLDER, imageName), 'r+b')
face_ids = []
time1 = time.time()
print('time elapsed for capturing image: ' + str(time1-start_time))
# detect faces in image
time2 = time.time()
print('time elapsed for detecting image: ' + str(time2-time1))
for face in faces:
print('appending face found in image')
face_ids.append(face.face_id)
if face_ids:
print('identifying face')
# if there are faces, identify person matching face
results = face_client.face.identify(face_ids, PERSON_GROUP_ID)
time3 = time.time()
print('time elapsed for identifying image: ' + str(time3-time2))
name = 'person-created-' + str(time.strftime("%Y_%m_%d_%H_%M_%S"))
if not results:
#if there are no matching persons, make a new person and add face
print('No person in the person group for faces from {}.'.format(imageName))
new_person = face_client.person_group_person.create(PERSON_GROUP_ID, name)
face_client.person_group_person.add_face_from_stream(PERSON_GROUP_ID, new_person.person_id, image)
time4 = time.time()
print('time elapsed for creating new person: ' + str(time4-time3))
print('New Person Created: {}'.format(new_person.person_id))
for face in results:
if not face.candidates:
new_person = face_client.person_group_person.create(PERSON_GROUP_ID, name)
face_client.person_group_person.add_face_from_stream(PERSON_GROUP_ID, new_person.person_id, image)
else:
#add face to person if match was found
print('Person for face ID {} is identified in {} with a confidence of {}.'.format(face.face_id, os.path.basename(image.name), face.candidates[0].confidence)) # Get topmost confidence score
face_client.person_group_person.add_face_from_stream(PERSON_GROUP_ID, face.candidates[0].person_id, image)
time4 = time.time()
print('time elapsed for creating new person: ' + str(time4-time3))
这也是在 pi 3B(+?) 上的 Raspbian 上
【问题讨论】:
-
可能在
WIDTH x HEIGHT中太小了,而不是在MB中。 -
@furas 理论上可能,但我怀疑这是这里的问题,因为我使用的是具有正常纵横比的正常大小的图像
-
有可能。在 PyTesseract 解决方案中识别的另一个问题是将图像大小调整为 120%。但我会显示文件名来检查有问题的图像。也许不小心这个文件与其他文件不同。
-
@davidt 此代码示例完整吗?我刚刚与另一个用户合作,得到“图像太小错误”,结果发现创建人员组的步骤不完整。我正在尝试您的示例,但我没有看到您创建了一个人组。或者也许那个人组已经存在?我想解决这个问题,因为以正确顺序实现的代码不需要文件 I/O 语句作为参数,因为我看到了答案所示的解决方法。我也有识别 API 调用的工作示例,如果那是你所追求的。
-
@Azurespot 是的,我已经创建了一个人员组,并且接受的解决方案解决了我的问题。您是说在使用 person_group_person.add_face_from_stream(...) 函数时我根本不需要提供文件中的图像吗?或者如果 face.identify(...) 失败,是否有更好的方法来处理新人的创建?我想看看你的工作样本。
标签: python azure microsoft-cognitive face-recognition face-api