简体   繁体   English

Boto3 无效参数异常

[英]Boto3 InvalidParameterException

I have some code that calls into AWS's Rekognition service.我有一些代码可以调用 AWS 的 Rekognition 服务。 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.但是,我在文档或代码中的任何地方都找不到InvalidParameterException ,因此我无法为发生这种情况的时间编写特定的处理程序。 Does anyone know what library module that exception lives in?有谁知道异常所在的库模块?

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.如果您在调用search_faces_by_image看到此异常,则可能表示您提供的图像中没有可检测到的人脸。 You can review a list of possible exceptions at API_SearchFacesByImage .您可以在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

I found it in boto/cognito/identity/exceptions.py :我在boto/cognito/identity/exceptions.py找到了它:

from boto.exception import BotoServerError

class InvalidParameterException(BotoServerError):
    pass

This is a misleading error by AWS, this error comes when the source image did not contain any detectable faces.这是 AWS 的一个误导性错误,当源图像不包含任何可检测的人脸时,就会出现此错误。 Make sure your source image has a detectable face.确保您的源图像具有可检测的面部。

In Python3 with boto3 you can do:在带有 boto3 的 Python3 中,您可以执行以下操作:

from botocore.exceptions import ClientError

catch ClientError as e:

jarmod's answer should work perfect, if you use boto3.如果您使用 boto3,jarmod 的答案应该很完美。

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:要更明确地回答有关InvalidParameterException所在位置的问题(在boto3 ):可以通过boto3 rekognition 客户端的类实例boto3它:

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

Now, the exception can be accessed via client.exceptions.InvalidParameterException (see jarmod's answer for a specific example).现在,可以通过client.exceptions.InvalidParameterException访问异常(有关特定示例,请参阅 jarmod 的答案)。

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). Stéphane Bruckert 建议使用从boto导入的建议对我不起作用,因为使用此导入的特定处理程序似乎没有捕获异常(但我没有对其进行广泛测试)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM