Last week I ran the Lisbon Marathon, well the half marathon if I have to be honest! A few days later, I got an email with my pictures taken during the event, available to be purchased online… awesome!
A question suddenly came to my mind: how they can organize pictures of more than 15.000 participants in just a couple of days? Well, I don’t know how they did, but I know how to do it using AWS Rekognition, and it can be a very good example of artificial intelligence (AI) applied to real-life (or real-business) scenarios.
What is AWS Rekognition?
As this is the first time we use an AWS AI service on this blog, we need a bit of background about it.
AWS Rekognition is an API based on machine learning, capable to analyze images and videos, and provide the following data with a good (in my opinion level) of accuracy:
- Object and scene detection
- Image moderation
- Facial analysis
- Celebrity recognition
- Face comparison
- Text in image
Some of these features may sound familiar to you! remember when Facebook automatically tags you in pictures? Well, that is facial analysis / comparison 🙂
That said, “text in image” recognition looks really perfect to organize a huge amount of images where all subjects have a number on them, just like a marathon!
What’s the purpose of this tool?
In my case, the organizers want to sell pictures to each participant, so when we open the link in the email we only see images portraying us, which is an insane amount of work to do manually considering that most of the pictures had more people in and they are taken in a random order.
With all these good premises, is time to build a tool to automatically organize our images in hours (instead of days, maybe weeks) and with no human effort!
As the first step, we should create an S3 bucket to upload and store our images. This is not really required, but AWS services play very well together and this will make our coding work much easier later.
Our strategy to organize pictures will be the following:
- scan the S3 bucket and analyze all the images using AWS Rekognition API
- detect the text labels in the images
- validate the images as much as we can to avoid the system to recognize invalid strings
- for each valid participant number, create a folder within our S3 bucket and copy/paste the image in the folder
As an outcome, we will get all the images divided in folders, where each folder is a runner number and contains (hopefully) all his pictures!
We could use any programming language to build this tool, as AWS has SDKs for almost every language. But we will do it using bash scripting and AWS CLI as it will be a very performant way, and we can keep the script running as long as we need to process a large amount of images.
The only disadvantage, is that bash script is not really semantic/readable as language, so I’ll go trough each line and explain what it does.
Of course, to run it you will need a Linux Bash Shell (available natively on Linux and Mac) with AWS CLI installed and configured.
And here are our magic 12 lines:
First, we declare a variable with the bucket name we used to upload our images, and we sue S3 CLI to list them, then we loop through each image and make the magic happen using the AWS Rekognition API and his detect-text endpoint.
This way we get a pretty reliable list of runner IDs read from their badge, and we use them to organize images in properly named directories in our S3 bucket!
The code already validates the strings detected to process numbers only, but a very nice add-on would be to validate the numbers detected against a database of valid runner IDs.
And that’s it, now we have all our images organized by runner number and ready to be distributed or sold!