Search code examples
pythonpython-3.xiterable-unpacking

No errors: But its not indexing faces and not generating the face id's


Thank you so much :) Please find the entire code

import boto3

s3_client = boto3.client('s3', aws_access_key_id='xxxxxxxxxxxxx', aws_secret_access_key='xxxxxxxxxxxxxxxxx',)

collectionId='xxxxxxxxx' #collection name

rek_client=boto3.client('rekognition', aws_access_key_id='xxxxxxxxxx', aws_secret_access_key='xxxxxxxxxxxxxxx',)

bucket = 'xxxxxxxxxxx' #S3 bucket name

all_objects = s3_client.list_objects(Bucket =bucket )

for content in all_objects['Contents']:

collection_name, sep, collection_image =content['Key'].parition('/')

if collection_image:
    label = collection_name
    print('indexing: ',label)

    image = content['Key']
    index_response=rek_client.index_faces(CollectionId=collectionId,
                            Image={'S3Object': 'Bucket':bucket,'Name':image}},
                            ExternalImageId=label,
                            MaxFaces=1,
                            QualityFilter="AUTO", DetectionAttributes=['ALL'])

print('FaceId: ',index_response['FaceRecords'][0]['Face']['FaceId'])


Solution

  • collection_name,collection_image =content['Key'].split('/')
    

    requires that content['Key'] contain exactly one forward slash (/).

    The error indicates that it contains none, so .split('/') just returned the equivalent of doing [content['Key']] (making a list with one element, the entire contents of content['Key']). Thus, you only have one value to unpack, but you tried to unpack to two values, and get the error you see.

    You either need to filter out the values that don't meet your requirements, or use a system that guarantees you get the expected number of values, e.g. str.partition:

    collection_name, sep, collection_image = content['Key'].partition('/')
    

    With str.partition, if the partition string doesn't occur, then sep and collection_image will be assigned the empty string, "".