1

ERROR MESSAGE I am trying to upload images of reasonable size(around 20KB). But according to documentation image of size 1KB to 6MB can be uploaded. I hope there is some part of the program that needs modification to rectify the error.

  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")
Arun Soorya
  • 447
  • 2
  • 9

1 Answers1

0

After looking through the API calls you are making, I realized there are some elements missing. You might not have posted the entire code, but I will add a sample below that illustrates the steps. Using the steps avoids any image errors, so the 'wrong size' image error could have likely been from missing steps.

In your code, before you can add an image to a Person Group Person (PGP), you have to create a Person Group(PG) for that PGP to belong to. Then after you create the Person Group (it is empty at the start), you must create a Person Group Person with that PG ID in it. Once those two things are created, then you can start adding images to your Person Group Person.

So here are the steps summarized above:

  1. Create a Person Group with the API call create()
  2. Create a Person Group Person with its API call for create()
  3. Add your image(s) to the Person Group Person with the API call add_face_from_stream()

Once you have added all your images that belong to your Person Group Person, then you can use data from it however you like.

See the code sample below, where a single local image is uploaded and added to a Person Group Person. I'll include the image I am using if you wanted to download and test.

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))
Azurespot
  • 3,066
  • 3
  • 45
  • 73
  • I changed images to high quality but still getting the same error. I don't think there is problem with the image. – Arun Soorya Feb 21 '20 at 13:38
  • @ Azurespot I found a similar error here: 'https://stackoverflow.com/questions/41806979/microsoft-cognitive-services-emotion-api-error-image-size-is-too-small-or-too' . And also look at this:'https://stackoverflow.com/questions/59350034/face-api-python-sdk-image-size-too-small-persongroupperson-add-face-from-stre?rq=1'. How can I modify the code to rectify the error. Please help – Arun Soorya Feb 21 '20 at 14:20
  • I reconfigured the code into a simplified sample, looks like some steps were missing. Test if you'd like. Let me know if this solves the problem. – Azurespot Feb 22 '20 at 01:01
  • The error was rectified after adding another variable to read the image. Thanks for the help :) If you find time please look at this error also: https://stackoverflow.com/questions/60360341/apierrorexception-badargument-recognitionmodel-is-incompatible-azure-cogni – Arun Soorya Feb 23 '20 at 08:19