{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Extending the functionality of a project\n", "\n", "As part of this session, we will teach you how to take an existing project and add some rule based functionality to it. For this session we will demonstrate how to send a SMS notification to your phone number once your AWS DeepLens detects a hotdog. \n", "\n", "This session demonstrates the building blocks capabilities of your AWS DeepLens to extend the functionality of the projects to interact with other AWS services. For example, you can build a dashboard and search interface for all objects and faces detected by AWS DeepLens by integrating your AWS DeepLens with Amazon ElasticSearch or build anomaly detection models on the number of people walking in front of your store by integrating AWS DeepLens with Amazon Kinesis Analytics or use celebrity recognition and face search API to identify the VIP around you by integrating AWS DeepLens with Amazon Rekognition.\n", "\n", "In this session you will learn the following:\n", "\n", "1. Create a lambda function\n", "2. Create a IoT rule\n", "3. Integrate the IoT rule with AWS DeepLens\n", "4. Trigger the lambda function when a hotdog is detected" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## DeepLens SMS notification flow overview\n", "\n", "" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Step 1- Create your Lambda function\n", "\n", "In the first step you will create an AWS Lambda function that will run in the cloud and filter the messages that are coming from your DeepLens device for ones with high enough (>0.5) probability for a Hotdog. You can play with the probability as part of this session. During this process you will also create a rule in the AWS IoT rule engine to get messages from the Lambda function that you deployed to the device using AWS Greengrass. \n", "\n", "1. Login to Lambda console (https://console.aws.amazon.com/lambda/home?region=us-east-1#/functions). Make sure you are on US East (N. Virginia) \n", "\n", "2. Click on Create function\n", "\n", "3. In the blueprints search bar, search for \"iot-button-email\"\n", "\n", "4. Choose the template\n", "\n", "\n", "\n" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "### Step 2- Name your lambda function and attach policies\n", "\n", "1. Give the lambda function a name: your-name_hotdog_notifier\n", "\n", "2. Keep “Create a new Role from template(s)†value in the Role field\n", "\n", "3. Give your new role the same name as your lambda function name. For example: “your-name_hotdog_notifierâ€\n", "\n", "4. In the policy templates, search for “SNS Publish policyâ€. Add the policy\n", "\n", "" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "### Step 3- Create IoT rule\n", "\n", "1. In the “aws-iot†section switch to use “Custom IoT Ruleâ€\n", "\n", "2. Choose “Create a new ruleâ€\n", "\n", "3. Give it a name: yourname_search_hotdogs and a description\n", "\n", "4. In the Rule query statement put the SELECT query:\n", "Select Hotdog from '$aws/things/deeplens_xxxxx-xxxxx/infer'\n", "\n", "Replace $aws/things/deeplens_xxxxx-xxxxx/infer with the IoT topic for your AWS DeepLens. You can find it by navigating to Devices in your AWS DeepLens and choosing your device name. Scroll down to the bottom of the device detail page to find your IoT topic. The below query can catch messages from the AWS DeepLens device in the following JSON format: { “Hotdog†: 0.5438 }\n", "\n", "\n", "\n", "\n", " " ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "### Step 4- Enable Trigger and change environment variables\n", "\n", "1. Enable trigger in the checkbox\n", "\n", "2. Change the environment parameter from \"email\" to \"phone_number\", and put your phone number as the Value. Please note that the phone number format should include the international country code (for example, for US **+1**5555555555). You can read more on the international support for SMS in AWS SNS FAQ: https://aws.amazon.com/sns/faqs/#sms-related-questions\n", "\n", "If you do not wish to use phone number, you can keep the environment variable as email and provide your email address. Please include your full email (for example: jyothi@ xx.com)\n", "\n", "3. Click the Create function button\n", "\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Step 5- Change configuration\n", "\n", "1. Switch to the “Configuration†tab of the Lambda function that you just created. You can find configuration tab on the left (configuration, triggers and monitoring)\n", "\n", "2. In the lambda function code, remove all the existing code and copy paste the code below" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "'use strict';\n", "\n", "/**\n", " * This is a sample Lambda function that sends an SMS Notification When your\n", " * Deep Lens device detect a Hot Dog\n", " * \n", " * Follow these steps to complete the configuration of your function:\n", " *\n", " * Update the phone number environment variable with your phone number.\n", " */\n", "\n", "const AWS = require('aws-sdk');\n", "\n", "const phone_number = process.env.phone_number;\n", "const SNS = new AWS.SNS({ apiVersion: '2010-03-31' });\n", "\n", "exports.handler = (event, context, callback) => {\n", " console.log('Received event:', event);\n", "\n", " // publish message\n", " const params = {\n", " Message: 'Your DeepLens device just identified a Hot Dog. Congratulations!',\n", " PhoneNumber: phone_number\n", " };\n", " if (event.Hotdog > 0.90)\n", " SNS.publish(params, callback);\n", "};" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## If you choose email instead of phone number\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "'use strict';\n", "\n", "/**\n", " * This is a sample Lambda function that sends an SMS Notification When your\n", " * Deep Lens device detect a Hot Dog\n", " * \n", " * Follow these steps to complete the configuration of your function:\n", " *\n", " * Update the phone number environment variable with your phone number.\n", " */\n", "\n", "const AWS = require('aws-sdk');\n", "\n", "const EMAIL = process.env.email;\n", "const SNS = new AWS.SNS({ apiVersion: '2010-03-31' });\n", "\n", "exports.handler = (event, context, callback) => {\n", " console.log('Received event:', event);\n", "\n", " // publish message\n", " const params = {\n", " Message: `Your DeepLens device just identified a Hot Dog. Congratulations!`,\n", " Email: EMAIL\n", " };\n", " if (event.Hotdog > 0.90)\n", " SNS.publish(params, callback);\n", "};\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Click “Saveâ€. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Step 6- Test your configuration\n", "\n", "1. In the IoT Console (https://console.aws.amazon.com/iotv2/home?region=us-east-1#/test) select the “Test†option\n", "\n", "2. Choose Publish to a Topic and Publish to the IoT topic of your AWS DeepLens (eg: $aws/things/deeplens_xxxxx-xxxxx/infer )\n", "\n", "3. Paste the following message in the code block" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'Hotdog': 0.6875}" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "{\n", " \"Hotdog\": 0.6875\n", "}" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You should receive a message on your phone: \"Congratulations! Your DeepLens just detected a Hotdog\". This is the message you defined in your lambda function" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Step 7- Test with Hotdog project\n", "\n", "In this step, DeepLens will continue to send messages as long as you keep showing it the hotdog. If you wish to not be spammed, you can skip this step and stop with the manual test described above. If you wish to see the below in action, make sure you dont leave the hotdog in front of the DeepLens for a long time.\n", "\n", "1. If you have not already deployed a Hotdog project, go to AWS DeepLens console and choose Projects -> Create a project template -> Hotdog or Not hotdog -> deploy to device\n", "\n", "2. In the IoT console, subscribe to your DeepLens IoT topic: example: $aws/things/deeplens_xxxxx-xxxxx/infer. Click on subscribe.\n", "\n", "2. Show your DeepLens a real hotdog and see it detect and send messages to you when the confidence score is greater than 0.90 \n", "\n", "3. Feel free to play around with the probablity for trigerring the lambda function." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Step 8- Disable the IoT rule\n", "\n", "1. Go to IoT console (https://console.aws.amazon.com/iotv2/home?region=us-east-1#) and choose Act\n", "\n", "2. Find the rule that you just created and disable it (Unless you want DeepLens to keep pinging you when it detects a hotdog throughout the day ;) )" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.2" } }, "nbformat": 4, "nbformat_minor": 2 }