This repo has moved to: https://github.com/aws-samples/aws-deeplens-worker-safety-project
In this lab your will the following:
- Create and deploy object detection project to DeepLens.
- Modify the DeepLens object detection inference lambda function to detect persons and upload frame to S3.
- Create lambda function to identify persons who are not wearing safety hats.
- Analyze results using IoT and CloudWatch and Web Dashboard.
- Go to IAM in AWS Console at https://console.aws.amazon.com/iam
- Click on Roles
- Click create role
- Under AWS service, select Lambda and click Next: Permissions
- Under Attach permission policies
- search S3 and select AmazonS3FullAccess
- search Rekognition and select checkbox next to AmazonRekognitionReadOnlyAccess
- search cloudwatch and select checkbox next to CloudWatchLogsFullAccess and CloudWatchFullAccess
- search iot and select AWSIotDataAccess
- search lambda and select checkbox next to AWSLambdaFullAccess
- Click Next: Tags and Next: Review
- Name is “RecognizeObjectLambdaRole”
- Click Create role
- Click create role
- Under AWS service, select Lambda and click Next: Permissions
- Under Attach permission policies
- search S3 and select AmazonS3FullAccess
- search lambda and select checkbox next to AWSLambdaFullAccess
- Click Next: Tags and Next: Review
- Name is “DeepLensInferenceLambdaRole”
- Click Create role
- Go to Amazon S3 in AWS Console at https://s3.console.aws.amazon.com/s3/
- Click on Create bucket.
- Under Name and region:
- Bucket name: Enter a bucket name- your name-worker-safety (example: kashif-worker-safety)
- Choose US East (N. Virginia)
- Click Next
- Leave default values for Configure Options screen and click Next
- Under Set permissions, uncheck all four checkboxes. NOTE: This step would allow us to make objects in your S3 bucket public. We are doing this to reduce few steps in the module, but you should not do that for production workloads. Instead it is recommended to use S3 Pre-Signed URLs to give time limited access to objects in S3.
- Click Next, and click Create bucket.
- Go to Lambda in AWS Console at https://console.aws.amazon.com/lambda/
- Click on Create function.
- Under Create function, Author from scratch should be selected as default.
- Under Author from scratch:
- Name: worker-safety-cloud
- Runtime: Python 3.7
- Role: Choose and existing role
- Existing role: RecognizeObjectLambdaRole
- Click Create function
- Under Environment variables, add a variable:
- Key: iot_topic
- Value: worker-safety-demo-cloud
- Download lambda.zip.
- Under Function code:
- Code entry type: Upload a zip file
- Under Function package, click Upload and select the zip file you downloaded in earlier step.
- Click Save.
- Under Add triggers, select S3.
- Under Configure triggers:
- Bucket: Select the S3 bucket you just created in earlier step.
- Event type: Leave default Object Created (All)
- Leave defaults for Prefix and Suffix and make sure Enable trigger checkbox is checked.
- Click Add.
- Click Save on the top right to save changed to Lambda function.
- Go to Lambda in AWS Console at https://console.aws.amazon.com/lambda/.
- Click on Create function.
- Under Create function, select Blueprints.
- Under Blueprints, type greengrass and hit enter to filter blueprint templates.
- Select greengrass-hello-world and click Configure.
- Under Basic information:
- Name: name-worker-safety-deeplens (example: kashif-worker-safety-deeplens)
- Role: Choose and existing role
- Existing role: DeepLensInferenceLambdaRole
- Click Create function.
- Copy the code from deeplens-lambda.py and paste under Function code for the lambda function. You can find the python file in your resources section.
- Go to line 34 and modify line below with the name of your S3 bucket created in the earlier step.
- bucket_name = "REPLACE-WITH-NAME-OF-YOUR-S3-BUCKET"
- Click Save.
- Click on Actions, and then "Publish new version".
- For Version description enter: Detect person and push frame to S3 bucket. and click Publish.
- Using your browser, open the AWS DeepLens console at https://console.aws.amazon.com/deeplens/.
- Choose Projects, then choose Create new project.
- On the Choose project type screen
- Choose Create a new blank project, and click Next.
-
On the Specify project details screen
- Under Project information section:
- Project name: your-user-name-worker-safety (example: kashif-worker-safety)
- Under Project content:
- Click on Add model, click on radio button for deeplens-object-detection and click Add model.
- Click on Add function, click on radio button for your lambda function (example: kashif-worker-safety-deeplens) lambda function and click Add function.
- Under Project information section:
- Click Create. This returns you to the Projects screen.
- From DeepLens console, On the Projects screen, choose the radio button to the left of your project name, then choose Deploy to device.
- On the Target device screen, from the list of AWS DeepLens devices, choose the radio button to the left of the device where you want to deploy this project.
- Choose Review. This will take you to the Review and deploy screen. If a project is already deployed to the device, you will see a warning message "There is an existing project on this device. Do you want to replace it? If you Deploy, AWS DeepLens will remove the current project before deploying the new project."
- On the Review and deploy screen, review your project and click Deploy to deploy the project. This will take you to to device screen, which shows the progress of your project deployment.
- Go to IoT Console at https://console.aws.amazon.com/iot/home
- Under Subscription topic enter topic name you entered as environment variable for Lambda in earlier step (example: worker-safety-demo-cloud) and click Subscribe to topic.
- You should now see JSON message with a list of people detected and whether they are wearing safety hats or not.
- Go to CloudWatch Console at https://console.aws.amazon.com/cloudwatch
- Create a dashboard called “worker-safety-dashboard-your-name”
- Choose Line in the widget
- Under Custom Namespaces, select “string”, “Metrics with no dimensions”, and then select PersonsWithSafetyHat and PersonsWithoutSafetyHat.
- Next, set “Auto-refresh” to the smallest interval possible (1h), and change the “Period” to whatever works best for you (1 second or 5 seconds)
- Go to AWS Cognito console at https://console.aws.amazon.com/cognito
- Click on Manage Identity Pools
- Click on Create New Identity Pool
- Enter “awsworkersafety” for Identity pool name
- Select Enable access to unauthenticated identities
- We are using using Unauthenticated identity option to keep things simple in the demo. For real world application where you only want authorized users to access the app you should configure Authentication providers.
- Click Create Pool
- Expand View Details
- Under: Your unauthenticated identities would like access to Cognito, expand View Policy Document and click Edit.
- Click Ok for Edit Policy prompt.
- Copy JSON from cognitopolicy.json and paste in the text box.
- Click Allow
- Make note of the Identity Pool as you will need it in following steps.
- Got to IoT in AWS Console at: https://console.aws.amazon.com/iot
- Click on settings and make note of Endpoint, you will need this the following step.
- Download webdashboard.zip and unzip on your local drive.
- Edit aws-configuration.js and update poolId with Cognito Identity Pool Id and host with IoT EndPoint you got in earlier steps.
- From terminal go to the root of the unzipped folder and run “npm install”
- Next, run “./node_modules/.bin/webpack —config webpack.config.js”
- This will create the build we can easily deploy.
- Go to S3 bucket, and create a folder web
- From web folder in S3 bucket click upload and select bundle.js, index.html and style.css.
- From Set permission, Choose Grant public read access to the objects. and click Next
- Leave default settings for following screens and click upload.
- Click on index.html and click on the link to open the web page in browser.
- In the address URL append ?iottopic=NAME-OF-YOUR-IOT-TOPIC. This is the same value you added to Lambda environment variable and hit Enter.
- You should now see images coming from DeepLens with a green or red box around the person.
Delete Lambda functions, S3 bucket and IAM roles.