Search an S3 Bucket for Pictures with Smiles using AWS Rekognition. Send the Smile via MMS/Text Message. Trigger it all with an AWS IoT Button to turn into a Smile Button!
- Obtain list of Objects in an S3 Bucket
- Pick a random Object in the list, and pass it to AWS Rekognition DetectFaces API.
- Check to see if the Object contains a Smile at >=90% Confidence. If not, go back to Step 2.
- Generate a pre-signed URL to the S3 Object that will last 60 seconds.
- Use Twilio to send an MMS/Text Message of the Object.
Twilio is used to send the MMS/Text Messages. A free trial account can be used. Once an account is created, follow the standard setup, and then grab the Account Sid and Auth Token; these will be used during deployment so the text messages can be sent. Amazon SNS cannot send MMS messages, which is why Twilio is used.
Before running the deployment through CloudFormation, run npm install to add the twilio helper library.
Included is a template to deploy the code with AWS CloudFormation using a SAM Template. This template's parameters are configured through the config.parameters file (or Defaults manually changed in deploy.yaml), which requires:
- ImageBucketName: The name of the S3 Bucket to create that will host the images.
- TwilioAccountSid: The Twilio Account Sid
- TwilioAuthToken: The Twilio Auth Token
- TwilioFromNumber: Phone number to use from Twilio Account
- TwilioToNumber: Phone number to send the image.
Within the package.json are the commands to deploy through CloudFormation using aws cloudformation package and aws cloudformation deploy. Make sure to modify:
- [INSERT-LAMBDA-UPLOAD-BUCKET]: This is the bucket where the Lambda function zip file will be uploaded so it can be deployed.
- [INSERT-STACK-NAME]: The name of the CloudFormation Stack.
Execute the following to deploy: npm deploy. A CloudFormation stack will be created with:
- AWS::S3::Bucket: An S3 Bucket will be created to house the images.
- AWS::Lambda::Function: The Lambda function that makes up the core of the application.
- AWS::IAM::Role: An IAM Role that contains policies to allow Lambda execution, read access to the created S3 bucket, and access to Rekognition's DetectFaces functionality.
First, upload a bunch of images to the created S3 bucket. Make sure to add some with smiles, but also some without. Next, navigate to the created Lambda function, and run a Test of the function. If all is configured properly, an MMS with a smile should be sent!
To turn it into an actual button, configure an AWS IoT button to execute the Lambda function. I use the AWS IoT Button Dev iOS App personally, but this could also be configured programmatically by adding the trigger to the CloudFormation template code.