1

statement:
This article is transferred from the DEV Community website, and the article translation is provided by the developer community;
Click the link below to view the original English text:

introduce

My company held a hackathon a while ago and I and a colleague created a smart doorbell using AWS Serverless services and a raspberry pi.

Every time someone clicks the "doorbell" button, it takes a picture and checks whether the face on the picture has been indexed through Amazon Rekognition's collection of faces. It sends a message to Slack containing the scaled image with a watermark and timestamp, and the number of people on the image and their names (if they already exist in the faces collection).

This blog post describes how we built the project and some of our experiences.

Architecture

image.png
Image: Architecture

image.png
Image: State Machine

how it works

It consists of two main components - face indexing and face recognition.

face index

  1. We created a simple frontend with VueJS, hosted on an S3 bucket. Here, we need to upload an image with a face and name. .
  2. After uploading the image, we proxy a lambda function through the API Gateway to create a pre-signed url, and using this generated pre-signed url, we upload the image to the s3 bucket with the name as the metadata value.
  3. Once the image is uploaded to the s3 bucket, a lambda function is triggered which will detect the face in the image and create an entry in the pre-defined AWS Rekognition collection (the face collection), named after the external id.

face recognition

  1. Using a Raspberry pi with its camera module, a solderless breadboard, and a button, we created the part that captures the image when the button is pressed - the doorbell.
  2. This captured image is uploaded to AWS S3, which triggers a lambda function to initiate the execution of the Step function.
  3. In the Step function, there are two parallel processes.
  4. A process detects faces in an image and searches through a collection of faces. This function will output the total number of detected faces. If there are recognized faces, it outputs the names of those faces.
  5. Another process resizes the image and creates a watermark with a timestamp. All of these functions use lambda functions.
  6. After these two processes are completed, another lambda function is triggered to write and send a message to the Slack channel.

output

In a Slack channel, the output is as follows:

image.png
Image: Example of output

At this point, (my son) Wanuja and Thenuja have been indexed into the face collection, and I have not.

code

Check out the full source code: https://github.com/pubudusj/serverless-bell

How to set

You can easily deploy this stack using the AWS SAM framework.

prerequisites:

  • AWS SAM cli + AWS profile settings
  • npm (for building the frontend)
  • Slack Webhook URL

At https://api.slack.com/apps/ , create a Slack app. Enable 'Incoming web hook' and add the created webhook to the workspace, select a channel. This will generate a webhook URL in the format: https://hooks.slack.com/services/XXXX/XXXX/XXXXXX

deploy

1. Start by creating an AWS Rekognition collection in the region where you want to deploy the stack.
2. aws rekognition create-collection \
3. --collection-id serverless-bell-faces-collection
4. Copy the github repo. Here are several directories for different purposes:

  • backend - s the source code that needs to be deployed using SAM
  • face_index_frontend - source code for the face index frontend
  • testing - For local testing without a Raspberry pi, this code can be used to test the face recognition functionality. This uploads the provided image, similar to how the Pi uploads the image to s3.
  • scripts_in_pi - Simple python script used internally by the Pi to capture images from the camera module and upload to s3.

5. In the cli, go to the /backend directory
6. Run the command: Sam build --use-container to build the python function with the necessary dependencies.
7. Then, to deploy the resources, run: sam deploy -g You need to enter the details of the stack to be created in AWS, including stack name, region, Rekognition face collection, and slack url. Make sure that the stack you create is in the same region as the Rekognition face collection.
8. After the deployment is complete, copy these output values, which will be used in the next steps: FaceIndexHostingS3Bucket, FaceIndexWebsiteURL, GeneratePresignedUrl, GeneratePresignedUrlForTesting, FaceDetectUploadBucketName
9. Now go to the face_index_frontend directory, where the source code of the face index frontend is located.
10. Create a new .env file, copy .env.example. For the VUE_APP_GENERATE_URL_API variable, use GeneratePresignedUrl to output the value.
11. Run npm install to install the required modules, then npm run build to build the project. This will create the dist directory.
12. Then, upload the contents of the dist directory to s3 as a website hosted by s3. Use the value of the output FaceIndexHostingS3Bucket as the s3 bucket.
13. aws s3 cp dist s3://[BucketName] --recursive
14. The face index website can now be accessed using the output value FaceIndexWebsiteURL.
15. Upload an image of a face with a name and you will see that face is indexed into the face collection.

aws rekognition list-faces --collection-id "serverless-bell-faces-collection"

Raspberry Pi

1. Set up the Raspberry PI with the camera module and AWS configuration file.
2. Use the sample scripts in the scripts_in_pi directory to capture and upload pictures to S3. Replace bucket-name with the output value FaceDetectUploadBucketName. Depending on your setup, use the relevant gpiozero button number.
3. After capturing the picture, you can view the message in the Slack channel.

Local testing without Raspberry pi

1. Enter the testing directory.
2. Create a new .env file, copy .env.example. For the VUE_APP_GENERATE_URL_API variable, use GeneratePresignedUrlForTesting to output the value.
3. Run npm install and npm run serve
4. With the provided URL, you can access the front end, upload pictures to detect faces.
5. After uploading the picture, you can view the message in the Slack channel.

some experience

1. In the Rekognition face collection, ExternalImageId can only contain alphanumeric characters. Therefore, when storing names with multiple spaces in between, we have to replace spaces with underscores, and the same when retrieving them.
2. When a lambda function is triggered from an S3 file upload, the lambda does not receive the metadata of the uploaded file. Therefore, to retrieve the metadata of the file, the file needs to be read again.
3. In SAM, the auto-generated S3 bucket name cannot be used in the function's policy object - ref.

Because of this, we have to create a name for the S3 bucket instead of randomly generating the S3 bucket name like SAM described here.

possible improvements

1. Implement authentication for the face index front-end, API, and Lambda functions.
2. Handle failures in the execution of the Step function.
3. Process the EXIF orientation data of the uploaded image to get the correct orientation.

You can try the above methods, welcome to exchange your views with me.

Article author: Pubudu Jayawardana
Pubudu Jayawardana for AWS Community Builders


亚马逊云开发者
2.9k 声望9.6k 粉丝

亚马逊云开发者社区是面向开发者交流与互动的平台。在这里,你可以分享和获取有关云计算、人工智能、IoT、区块链等相关技术和前沿知识,也可以与同行或爱好者们交流探讨,共同成长。