Nathaniel Ford
Nathaniel Ford

Reputation: 21230

Boto3 InvalidParameterException

I have some code that calls into AWS's Rekognition service. Sometimes it throws this exception:

An error occurred (InvalidParameterException) when calling the DetectLabels operation: Request has Invalid Parameters

I can't find the InvalidParameterException anywhere in the documentation or code, though, so I can't write a specific handler for when that occurs. Does anyone know what library module that exception lives in?

Upvotes: 11

Views: 8055

Answers (5)

Tee
Tee

Reputation: 133

jarmod's answer should work perfect, if you use boto3.

To more explicitly answer the question regarding where the InvalidParameterException lives (in boto3): It can be acessed via a class instance of the boto3 rekognition client:

import boto3
client = boto3.client('rekognition')

Now, the exception can be accessed via client.exceptions.InvalidParameterException (see jarmod's answer for a specific example).

Stéphane Bruckert's suggestion of using an import from boto did not work for me as the exception does not appear to be caught by a specific handler using this import (but I did not tested it extensively).

Upvotes: 0

jarmod
jarmod

Reputation: 78753

If you saw this exception in response to calling search_faces_by_image then it probably indicates that there were no detectable faces in the image that you provided. You can review a list of possible exceptions at API_SearchFacesByImage.

To handle this exception, you could write code like this:

import boto3
rek = boto3.client('rekognition')

def lookup_faces(image, collection_id):
    try:
        faces = rek.search_faces_by_image(
            CollectionId=collection_id,
            Image=image,
            FaceMatchThreshold=95
        )
        logger.info('faces detected: {}'.format(faces))
        return faces
    except rek.exceptions.InvalidParameterException as e:
        logger.debug('no faces detected')
        return None

Upvotes: 8

calmsoul
calmsoul

Reputation: 15

This is a misleading error by AWS, this error comes when the source image did not contain any detectable faces. Make sure your source image has a detectable face.

Upvotes: 1

Everett Berry
Everett Berry

Reputation: 11

In Python3 with boto3 you can do:

from botocore.exceptions import ClientError

catch ClientError as e:

Upvotes: 1

Stéphane Bruckert
Stéphane Bruckert

Reputation: 22903

I found it in boto/cognito/identity/exceptions.py:

from boto.exception import BotoServerError

class InvalidParameterException(BotoServerError):
    pass

Upvotes: 3

Related Questions