def function_handler()

in iot-blog/image-classification-connector-and-feedback/part_2/beverageclassifier.py [0:0]


def function_handler(event, context):
    image_filename = create_image_filename()
    capture_and_save_image_as(image_filename)

    predicted_category, prediction_confidence = get_inference(image_filename)

    gg_client.publish(
        topic='/response/prediction/beverage_container',
        payload=json.dumps({'message':'Classified image as {} with a confidence of {}'.format(predicted_category, str(prediction_confidence))})
    )

    if prediction_confidence < INFERENCE_CONFIDENCE_THRESHOLD_LOWER or \
       prediction_confidence > INFERENCE_CONFIDENCE_THRESHOLD_UPPER:
        # We will store our raw data in a folder under our
        # configured S3 bucket called /image-classification
        s3_file_name = "image-classification" + create_s3_filename(image_filename, predicted_category, prediction_confidence)

        gg_client.publish(
            topic='/response/prediction/beverage_container',
            payload=json.dumps({'message':'Prediction fell above upper threshold ({}) or below lower threshold ({}). Uploading to S3 ({}/{}) for manual labeling.'\
                .format(str(INFERENCE_CONFIDENCE_THRESHOLD_UPPER), str(INFERENCE_CONFIDENCE_THRESHOLD_LOWER), S3_BUCKET_NAME, s3_file_name)})
        )

        upload_to_s3(image_filename, s3_file_name)
    
    os.remove(image_filename)
    return