Computer Vision (CV) can be a very theory heavy and intense field with a lot of ML and Mathematical knowledge required to unpack and understand the algorithms that power topics such as Object Detection, Facial Recognition, and Object Tracking. If you don’t have the requisite experience theoretically or don’t have the time to build a custom ML/CV model, AWS Rekognition enables you to build powerful CV applications through API calls. AWS Rekognition is one of many Auto-AI services offered by AWS. These services are meant for developers who don’t have much of a background in ML, or Data Scientists low on time to quickly build powerful ML applications or to benchmark performance against a custom model that you may be developing. AWS Rekognition is the de-facto Auto-AI service on AWS for Computer Vision. If you’re interested in building custom models check out AWS SageMaker and my article on how to deploy custom TensorFlow models there. For this article, we will be building a Streamlit application that accepts an image and returns the major emotion detected in the image by using Rekognition.
NOTE: For those of you new to AWS (it’s good to have some experience to fully understand the article), make sure you make an account at the following link if you want to follow along. I’ll also provide a list of services we’ll be using along with more in-depth definitions. If you’re already familiar with these services, feel free to skip ahead to the code demonstration.
Table of Contents (ToC)
- AWS Services
- Setup Streamlit Application
- Integrate Rekognition
- Entire Code & Conclusion
AWS Services
AWS Rekognition: AWS’s main Auto-AI Computer Vision service. Has a vast amount of features ranging from Face Detection, Text Detection, to Object Tracking.
AWS S3: Amazon’s primary storage service, we will be using this service to store our training data and model artifacts/information.
Boto3: AWS Software Development Kit (SDK) for Python developers, can use this to work with AWS services. For this use case we will use Boto3 to work with S3 to access our images and to call Rekognition.
Identity Access and Management (IAM): Lets you manage access of AWS services through permissions and roles. When working with my code, you will have wanted to authenticate a User through the AWS CLI. Here is an article detailing the process if you need help with setup.
Setup Streamlit Application
Before we can get to work on the Rekognition portion we want to setup our web application. We want our application to be able to upload a file which we can then process on the backend using Rekognition. Streamlit has a neat feature for file uploads that we can utilize in this use case. I’ve created a small directory of sample images that we can call for analysis.
We capture the file from our directory of sample images. We then use Boto3 to work with S3 to store our local image in the S3 Bucket we created. To create an S3 bucket you can go to the AWS Console and manually create or if you have the AWS CLI setup you can simply run aws mb unique-bucket-name to create your bucket. We need our data in S3 because Rekognition is directly integrated with S3 for its API calls, thus the code for uploading our images to S3.
We now have our Streamlit application set to accept and push images to S3 to work with Rekognition.
Integrate Rekognition
We can now focus on being able to run emotion detection on our sample image. Rekognition has an API call in detect_faces that takes an input image and returns a variety of parameters such as: age range, gender, eyes open, emotion, and more. For the purpose of this application we will be focusing on the emotion parameter. We first need to create a boto3 client for Rekognition as we did with S3.
We now have the client and can now work with feeding in the appropriate data to the detect_faces API call.
Now that we have gotten our API response we want to parse the output to focus on the emotions detected in the face. Our sample image that we are uploading for the demo is displayed below.
We now parse the response to display the majority emotion and all other emotions detected as well as the confidence score that Rekognition has in the emotion.
Now if we run our streamlit application with streamlit run filename.py we should see our app running successfully.
We see the majority emotion as well as all emotions detected by Rekognition, to clean up the UI you can also add a CV library such as PIL to display the sample image or file that you are uploading.
Entire Code & Conclusion
To access the entire code for the project click the link above. Auto-AI is expanding rapidly and cloud providers such as AWS, Azure, and GCP are also growing and offering various cutting-edge services such as Rekognition that enable you to power apps with ML in a simple manner. Rekognition is of great use to Data Scientists and those of non-ML backgrounds to quickly get a Computer Vision application up and running. Check out some more cool AWS Rekognition use cases in these blogs.
I hope this article has been useful for those working on Computer Vision projects or exploring AWS. Feel free to connect with me on LinkedIn or follow me on Medium for more of my writing. Share any thoughts or feedback, thank you for reading!