Over the past few years, we always talk about machine learning, AI and big data. But is it really easy to implement it? The answer is yes and no. For example, if we want to have an image recognition application, it will be very difficult to make if we start from scratch and study tens of thousands of algorithm. However, there are many built-in API on cloud to let us do machine learning or AI. In this article, we will implement a simple image recognition application on AWS within 15 mins. Of course, if you want to go through the code deeply, it will take much more time. I assume that you all had AWS account already. If you don’t have, please visit here to register a 1 year free-tier account. Our final system will look like below:
Amazon Rekognition makes it easy to add image and video analysis to your applications using proven, highly scalable, deep learning technology that requires no machine learning expertise to use. Generally speaking, it is nothing but just a list of APIs to help you do any image analysis related jobs. In this article, we will just make use of detect_labels, please visit here for more details of the APIs.
This is a very simple architecture. Only Amazon EC2, Amazon S3 and Amazon Rekognition are involved. The workflow is:
- User visits our website hosted on EC2.
- When user upload a photo, it will be stored on S3.
- S3 will return the object url, then pass this url into Rekognition.
- Rekognition will also return the API response to us.
- The result is reflected on the front end page, so users could see the label of the image.
Actually, the original resource is on edX. But it seems the original tutorial is missing some steps and a little bit of outdated, so I slightly modify it. Now, let’s start.
Start Cloud 9
Cloud 9 is a cloud IDE and we could use it for development. For your reference, my region is N. Virginia(us-east-1). You could choose any region you want, if no preference please just follow me. Search “Cloud 9” and then click “Create environment”.
There there are 3 steps for creation. The name is up to you. In the second step, if you do not want your application close suddenly, please choose “Never” on Cost-saving setting. Then click “Create environment”.
When you are the first time to use Cloud 9, it will have Welcome page and please simply close it. In the console, please git clone my repo and update the OS by using:
git clone https://github.com/manbobo2002/rekognition-demo.git
sudo yum -y update
After updating, we move to our folder and install all required packages stated in requirements.txt.
sudo pip-3.6 install -r requirements.txt
Create S3 Bucket
Search “S3” and enter it.
Click “Create bucket”, and input a unique name as your bucket name, then simply click “Create”. For your reference, my region is same as Cloud 9 i.e. N. Virginia(us-east-1).
Go back to Cloud 9, and see the config.py file. The original resource requires us to export environment variables. Here I just modify it to directly input. Just replace your own bucket name into “PHOTOS_BUCKET”. And we could keep “FLASK_SECRET” as default or edit it as any string you like. Click “Ctrl + S” to save the file.
Let me briefly explain how the code work. The main back end is application.py. When it receives GET or POST request, then you will enter the home() function. It will firstly get the existing photos from S3. If you upload the photo, it will have a validation check and upload it. Then it passes the image information to the Rekcognition. When we see “return render_template_string (xxxx)”, it means to return the html page in static folder.
Finally, we just run the application by typing:
Click the highlighted icon to enlarge the preview screen.
Then we choose the photo, upload it, the result will be displayed below.
Open to Public
In fact, we do not really open the application to public, but just test in Cloud 9. In order to let everyone access, we should add some rules in security group.
We search for EC2 and enter to the dashboard.
Then we click “Instances” and see our Cloud 9 is running. Actually, Cloud 9 is nothing but just an EC2 instance.
We firstly select the Cloud 9 instance, and then click the “Security groups”.
It redirects us to the security group, just simply click into the group.
Scroll down and click “Edit inbound rules”.
Click “Add rules”, then open the port 8080 to all (0.0.0.0/0). Of course, you are advised to open for the whitelist IPs only. Then Click “Save rules”.
Back to our Instance page, just copy either Public DNS (IPv4) or IPv4 Public IP.
Add the port 8080 behind the link, both of them should work properly, now everyone could access the application.