AWS Code Sample
Catalog

connector-image-classification-usage.py

Invokes the local inference service.

# Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"). You # may not use this file except in compliance with the License. A copy of # the License is located at # # http://aws.amazon.com/apache2.0/ # # or in the "license" file accompanying this file. This file is # distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF # ANY KIND, either express or implied. See the License for the specific # language governing permissions and limitations under the License. # # This sample is used in the AWS IoT Greengrass Developer Guide: # https://docs.aws.amazon.com/greengrass/latest/developerguide/image-classification-connector.html # import logging from threading import Timer import numpy as np import greengrass_machine_learning_sdk as ml # We assume the inference input image is provided as a local file # to this inference client Lambda function. with open('/test_img/test.jpg', 'rb') as f: content = f.read() client = ml.client('inference') def infer(): logging.info('invoking Greengrass ML Inference service') try: resp = client.invoke_inference_service( AlgoType='image-classification', ServiceName='imageClassification', ContentType='image/jpeg', Body=content ) except ml.GreengrassInferenceException as e: logging.info('inference exception {}("{}")'.format(e.__class__.__name__, e)) return except ml.GreengrassDependencyException as e: logging.info('dependency exception {}("{}")'.format(e.__class__.__name__, e)) return logging.info('resp: {}'.format(resp)) predictions = resp['Body'].read() logging.info('predictions: {}'.format(predictions)) # The connector output is in the format: [0.3,0.1,0.04,...] # Remove the '[' and ']' at the beginning and end. predictions = predictions[1:-1] count = len(predictions.split(',')) predictions_arr = np.fromstring(predictions, count=count, sep=',') # Perform business logic that relies on the predictions_arr, which is an array # of probabilities. # Schedule the infer() function to run again in one second. Timer(1, infer).start() return infer() def function_handler(event, context): return

Sample Details

Service: greengrass

Last tested: 2019-01-03

Author: AWS

Type: full-example

On this page: