- AWS X-Ray with Amazon Rekognition
- Before you begin
- Overview
- Setup instructions
- Testing
- Analyzing app using AWS X-Ray
- Analyzing AWS Lambda and Amazon Rekognition performance using AWS X-Ray
- Delete resources and uninstall app
- You will need an active AWS account to proceed. Create one at https://aws.amazon.com/ .
- Permission to run Cloudformation template that will create S3 buckets, AWS Lambda function, IAM Roles and Cognito Identity Pools. Read more about Cloudformation from https://aws.amazon.com/cloudformation/ .
- You will need Python to deploy the resources and run the sample app from these resources. Download and install Python from https://www.python.org/ .
- You will need Node.js runtime to run the client from your local machine. Visit https://nodejs.org/en/ to install Node.js.
- User uses photoform.html to upload/delete photos in S3 bucket.
- User on clicking "Recognize faces" in photoform.html button invokes a Lambda function that calls Amazon Rekognition.
- AWS X-Ray tracks the performance of the Lambda function and Rekognition.
- Photoform.html gets response on recognized celebrity faces from Rekognition.
The Cloudformation template will create the required resources such as S3 buckets, IAM roles, AWS Lambda function and Cognito Identity Pools.
Go to the Cloudformation/ directory where you git-cloned the repository. Run the following command to deploy the resources:
python install.py
Note: Your appname will be used for naming your CloudFormation stack, public s3 bucket and as a prefix to identify all the Lambda functions, Cognito Pool and IAM Roles associated with your app.
-
After the resources are deployed go to your Amazon Cognito console. Go to Federated Identity and go to "appnamecognitounauthxrayrekognition" (where appname is your app's name) Idenity Pool to get the Identity Pool Id.
-
Update Client/xrayoperations.js file with the app name, region and Identity Pool Id.
var appName = '<appNameHere>';//update this
var resourceRegion = '<regionHere>'; //update this
var identityPoolId = '<identityPoolIdHere>'; //update this
Inorder to run the sample app you would have to copy the contents of the Client to the public S3 bucket that was created for you.
- Run the following command to copy your photoform.html to your public bucket:
python uploadclient.py
- Go to your public S3 bucket in your S3 console: https://s3.console.aws.amazon.com
- Get your public S3 bucket's static endpoint from Properties->Static web hosting
- Paste the endpoint in a browser to view and test the app.
Once you have completed the setup instructions do the following:
- Load the photoform.html either in your local browser or from the S3 bucket if you opted to statically host from S3.
- Upload an image. This image will be stored in the S3 bucket under the album that you created for storing images.
- Click "Recognize Faces" under the image. In a few seconds, you should be getting a comma-seperated response with the recognized celebrity faces in the image.
AWS X-Ray will be useful in understanding the performance of your AWS Lambda function and Amazon Rekognition. You would notice that your AWS Lambda function and Amazon Rekognition take a longer time to process when the images you uploaded do not have a celebrity in them.
- Go to your AWS X-Ray console (https://console.aws.amazon.com/xray/) and view the service map for your operation.
- You can select each operation in the AWS X-Ray console and view the corresponding traces as well.
You can use AWS X-Ray to determine the performance of your app and make it better. In the below image you can see that the first time your app calls AWS Lambda there is a 385ms time spent on initializing the AWS Lambda code. In the subsequent traces the AWS Lambda function is already initialized and you complete faster.
You can also notice the time taken by Amazon Rekognition differs with the ease of ability to find celebrity images in the photo. For example, a photo without any celebrity who could be recognized takes a shorter time to process compared to a photo with a celebrity who was able to be recognized. In image below, see the time taken to recognize Steve Jobs (1.6 sec) vs a random landscape image (714ms).
Amazon Rekognition takes more time to analyze images with more number of faces in them. For example, Rekognition took 9.2 sec to analyze an image with 15 faces Vs 6.2 sec for an image with 10 faces.
There was no correlation in the amount of time the sample app took to invoke Rekognition on all the images and return back with number of times a celebrity appeared in the images. A point to note is that every time the celebrity name is searched the sample app calls Rekognition on all the images one by one and the sample app analyzes the response from Rekognition and returns back with the number of times a celebrity's face was found in the images.
Celebrity Name | Number of times found | S3+Rekognition+Storing in array | S3 | Rekognition+Storing in array |
---|---|---|---|---|
Steve Jobs | 3 | 11.5 sec | 1.2 sec | 10.3 sec |
Jeff Bezos | 2 | 11.1 | 0.6 | 10.5 sec |
Bill Gates | 4 | 11.5 | 1.0 | 10.5 sec |
Tim Cook | 4 | 10.5 | 0.541 | 9.959 sec |
There are times when your image recognition will fail as well. AWS X-Ray helps you trace these errors and provides the errors clearly from Amazon Rekognition.
Run the following command to delete resources and uninstall the sample app:
python uninstall.py
You can go to your Cloudformation console: https://console.aws.amazon.com/cloudformation and click "xrayrekognitionstack" stack to see if deleting resources failed. In case the stack failed to delete resources you can manually delete the resources listed under that stack.