Crear y publicar una función Lambda de inferencia en
client.publish(topic=iot_topic, payload=json.dumps(cloud_output))
12. Elija Save (Guardar) para guardar la función Lambda actualizada. La función debe ser similar a la
siguiente:
import json
import awscam
import mo
import cv2
import greengrasssdk
import os
from local_display import LocalDisplay
def lambda_handler(event, context):
"""Empty entry point to the Lambda function invoked from the edge."""
return
def infinite_infer_run():
""" Run the DeepLens inference loop frame by frame"""
# Create an IoT client for sending to messages to the cloud.
client = greengrasssdk.client('iot-data')
iot_topic = '$aws/things/{}/infer'.format(os.environ['AWS_IOT_THING_NAME'])
# Create a local display instance that will dump the image bytes to a FIFO
# file that the image can be rendered locally.
local_display = LocalDisplay('480p')
local_display.start()
# Load the model here
# Model details
input_width = 224
input_height = 224
model_name = 'image-classification'
model_type = 'classification'
output_map = {0: 'dog', 1: 'cat'}
# Optimize the model
error, model_path = mo.optimize(model_name,input_width,input_height)
# Load the model onto the GPU.
model = awscam.Model(model_path, {'GPU': 1})
while True:
# Get a frame from the video stream
ret, frame = awscam.getLastFrame()
# Do inference with the model here
# Resize frame to the same size as the training set.
frame_resize = cv2.resize(frame, (input_height, input_width))
predictions = model.doInference(frame_resize)
parsed_inference_results = model.parseResult(model_type, predictions)
k = 2
# Get top k results with highest probabilities
top_k = parsed_inference_results[model_type][0:k]
print(top_k)
# Send results back to IoT or output to video stream
# Add the label of the top result to the frame used by local display.
cv2.putText(frame, output_map[top_k[0]['label']], (10, 70),
cv2.FONT_HERSHEY_SIMPLEX, 3, (255, 165, 20), 8)
# Set the next frame in the local display stream.
local_display.set_frame_data(frame)
# Send the top k results to the IoT console via MQTT
cloud_output = {}
for obj in top_k:
cloud_output[output_map[obj['label']]] = obj['prob']
client.publish(topic=iot_topic, payload=json.dumps(cloud_output))
AWS DeepLens Guía para desarrolladores
90