Use AWS DeepLens and Amazon Rekognition to build an application that helps identify if a person at a construction site is wearing the right safety gear, in this case, a hard hat.
In this lab you will learn the following:
- Create and deploy an object detection project to AWS DeepLens.
- Modify the AWS DeepLens object detection inference Lambda function to detect persons and upload the frame to Amazon S3.
- Create a Lambda function to identify persons who are not wearing safety hats.
- Analyze the results using AWS IoT , Amazon CloudWatch and a web dashboard.
Follow the modules below or refer to the online course to learn how to build the application in 30 minutes.
https://www.aws.training/Details/eLearning?id=32077
- Go to AWS 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 for S3 and select AmazonS3FullAccess
- search for Rekognition and select checkbox next to AmazonRekognitionReadOnlyAccess
- search for cloudwatch and select checkbox next to CloudWatchLogsFullAccess and CloudWatchFullAccess
- search for iot and select AWSIotDataAccess
- search for 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 it “DeepLensInferenceLambdaRole”
- Click Create role
- Go to AWS IAM in AWS Console at https://console.aws.amazon.com/iam
- Click on Roles
- Serach for AWSDeepLensGreengrassGroupRole and click on the Role name
- Under permissions, click on Attach Policies
- Search S3, select AmazonS3FullAccess and click Attach policy
- 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 the default values for Configure Options screen and click Next
- 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, by default Author from scratch should be selected.
- Under Author from scratch, provide the following details:
- Name: worker-safety-cloud
- Runtime: Python 3.7
- Role: Choose an existing role
- Existing role: RecognizeObjectLambdaRole
- Click Create function
- Under Environment variables, add a variable:
- Key: iot_topic
- Value: worker-safety-demo-cloud
-
Copy the code from cloud-lambda.py and paste it under the Function code for the Lambda function.
-
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 the changes to the Lambda function.
- Go to AWS Lambda in AWS Console at https://console.aws.amazon.com/lambda/.
- Click on Create function.
- Under Create function, select Author from scratch.
- Under Basic information, provide the following details:
- Name: name-worker-safety-deeplens (example: kashif-worker-safety-deeplens)
- Runtime: Python 3.7
- Role: Choose and existing role
- Existing role: DeepLensInferenceLambdaRole
- Click Create function.
- Copy the code from deeplens-lambda.py and paste it under the Function code for the Lambda function.
- Go to line 34 and modify the 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 a 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 the AWS 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." Go ahead and choose Deploy.
- On the Review and deploy screen, review your project and click Deploy to deploy the project to your AWS DeepLens. This will take you to the device screen, which shows the progress of your project deployment. Once your deployment is successful, proceed to the next step.
- Go to AWS IoT console at https://console.aws.amazon.com/iot/home
- Under Subscription topic, enter the topic name that you entered as an environment variable for the Lambda function you created in the 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 Amazon 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 Unauthenticated identity option to keep things simple in the demo. For real world applications 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 a note of the Identity Pool as you will need it in the following steps.
- Go to AWS IoT in AWS Console at: https://console.aws.amazon.com/iot
- Click on settings and make a note of the 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 the earlier steps.
- From the 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 a build that we can easily deploy.
- Go to Amazon S3 bucket, and create a folder titled "web"
- From the 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 the Lambda environment variable and hit Enter.
- You should now see images coming in from AWS DeepLens with a green or red box around the person. Green indicates the person is wearing a safety hat and red indicates a person who is not.
This completes the application- you have now learnt to build an application that detects if a person at a construction site is wearing a safety hat or not. You can quickly extend this application to detect lab coats or helmets etc.
Before you exit, make sure you clean up any resources you created as part of this lab.
Delete Lambda functions, S3 bucket and IAM roles.
This sample code is made available under the MIT-0 license. See the LICENSE file.