获取 ConflictException:调用 DetectAnomalies 操作时发生错误(ConflictException):

Getting ConflictException: An error occurred (ConflictException) when calling the DetectAnomalies operation:

Lambda 功能与 Amazon Lookout for Vision 集成时,无法检测图像是否有缺陷。在 运行 代码中使用 lookoutvision.detect_anomalies 方法时出现此错误。

ConflictException: An error occurred (ConflictException) when calling the DetectAnomalies operation: Detect cannot be performed when the resource is in TRAINED. The resource must be in HOSTED to perform this action

import boto3

lookoutvision = boto3.client('lookoutvision')
s3 = boto3.client('s3')

def lambda_handler(event, context):
    #print(json.dumps(event))
    bucket = event['Records'][0]['s3']['bucket']['name']
    key = event['Records'][0]['s3']['object']['key']    
    response = s3.get_object(Bucket=bucket, Key=key)
    print(response)
    content_type = response['ResponseMetadata']['HTTPHeaders']['content-type']
    image = response['Body']
    image_body = image.read()
    
    #res = lookoutvision.list_projects()
    #print(res)
    
    project_name = "new"
    model_version = "3"

    lookout_response = lookoutvision.detect_anomalies(
        ProjectName=project_name,
        ModelVersion=model_version,
        Body=image_body,
        ContentType=content_type
    )
    print(lookout_response)
    return {
        'statusCode': 200,
        'body': json.dumps('Hello from Lambda!')
    }```

使用前 lookoutvision.detect_anomalies api 需要启动模型使用 start_model() api.

检测到异常后也使用stop_model()停止模型,因为运行不使用的模型会增加成本。

参考 https://github.com/aws-samples/amazon-lookout-for-vision/blob/main/Amazon%20Lookout%20for%20Vision%20Lab.ipynb 代码示例