Day 17 of 100DaysofML

Charan Soneji
100DaysofMLcode
Published in
8 min readJul 3, 2020

Facial Recognition app with AWS. So I thought of creating this app from absolute scratch to build an application which can identify faces in a picture that we have. As a prerequisite, it would be good if you understand the fundamentals of AWS architecture. It would make things much more simpler to understand. Keep in mind that developing this app from scratch is ABSOLUTELY FREE and I’m going to be using the Free Tier that AWS provides us with.

So the first and most important step is to login to your AWS management console from where you would be launching your instances and accessing your services. The management console looks something like this.

Management console

Once you have logged in, we need to head onto our Services → EC2 in order to launch our instances. For the ones who don’t know what an EC2 instance is, it is basically a CPU or a server that we are running on the cloud. The page to launch our EC2 instances should look something like this.

EC2 page

Click on the Launch Instance to Launch our first instance on the cloud. There are a few steps that go into setting up our instance so make sure to pay attention.
After clicking on the launch instance button:
1. Choosing an AMI: Choose the Amazon Linux AMI since it is part of the free tier and click on next.
2. Choose an Instance type: This is basically the type of CPU you want to configure for your instance. Again, we would be using a free of cost instance so make sure to choose t2.micro (You can read about the specification on the AWS page for EC2) and then click on next.
3. Configuring Instance details: Make sure to leave everything on default over here and click on next.
4. Adding Storage: Here again, let everything be left to default. We are basically initializing our EBS storage volume which we would be linking to our instance.
5. Configuring Security Groups: For now, leave this as default and click on next. But after you create the instance, you need to head onto the Security groups and click on create a new Security group and allow HTTP or SSH based access to the instance. It may all seem overwhelming, but its actually easy so don't stress.
6. Key Pair Download: Here, AWS gives us the option to download the key pair which comes in .pem format.
NOTE: For the people using Windows based machines, you need to install a software named PUTTY and PUTTYGEN to be able to access your EC2 instance from your command line. PUTTYGEN is used to convert the .pem to .ppk format so that it can be read by PUTTY while accessing your instance from the command line. The (.pem) file is basically the private key which is used to connect to your EC2 instance along with the IP which you will get from the EC2 console page after your instance is up and running.
You need to play around a bit with the tools until you get familiar but a small gist of the steps to be taken is shown in the video below:

Creating your instance and deploying

For the connection part, open PUTTY and add your (.ppk) file before connecting to your instance. I’ve put a simplified video which can help with the process:

Along with this, the installation of PUTTY is:

This is just the first part of our project. Next we need to get a picture and link it to our ML service on AWS. We’ll get there, one step at a time.

To identify the faces from our picture, we shall be using a service called AWS Rekognition. I have spoken about this in my previous blogs so I’m going to get right into the implementation.

So for the implementation segment, we are going to be using a segment of PHP code that would be getting our pictures from a S3 bucket and passing it onto our service.

Our next step would be to create our S3 bucket to store our picture and uploading our image object to it.

Click on Services → S3.
1. Create new bucket
2. Enter the name of the bucket (should be unique)
3. Leave everything on default and click on create bucket at the end of the page.
4. We need to upload a picture file.
Pick up any random picture from the internet which consists of a face and click on the upload objects/file and then when it asks you for information to be filled out, make sure to choose the STANDARD (Free Tier) option so that you won’t be charged for anything that you upload to the bucket.
5. Next step is to make the bucket and its object public. Click on the Permissions tab and unselect the Block access box and enter the required info and save. Next to make the object/file that you have uploaded public, click on the object and look for options whereby you can convert into a public object.
Check out the video/ animation below to get a gist of what is to be done.

S3 bucket creation and object uploading along with public access declaration

We are almost there, now we need to open our terminal on the EC2 instance and download PHP in order to run a simple script. We need to run a list of commands which I shall mention below:

Install php — sudo yum install php
curl -sS
https://getcomposer.org/installer | php
cd /var/www/html
sudo mkdir face
cd face
sudo php -d memory_limit=-1 ~/composer.phar require aws/aws-sdk-php
In case if you get memory error -
sudo /bin/dd if=/dev/zero of=/var/swap.1 bs=1M count=1024
sudo /sbin/mkswap /var/swap.1
sudo /sbin/swapon /var/swap.1
sudo wget https://i.pinimg.com/originals/b9/7e/a3/b97ea33b5842c7894b804923c6c05580.jpg
sudo mv b97ea33b5842c7894b804923c6c05580.jpg sample.jpg
Incase if you are getting any class NOT found error, follow these stepssudo yum remove php*
sudo yum remove httpd*
sudo yum clean all
sudo yum upgrade -y
sudo amazon-linux-extras install php7.2
sudo yum install php-json php-xml php-cli php-mbstring
sudo yum install httpd

The above code contains specific lines which will help you debug incase you reach any memory error. The process takes about 15 minutes to get your PHP installed and proceed with the script below:

Things to keep in mind and Replace in the script below:
1. Look for the region and replace it with the region in which your instance has been set up.
2. Bucket name: Look for the $bucket variable and replace the name in it with the name of the S3 bucket that you have created.
3. Object name: Look for the $keyname variable and replace the name in it with the name of the S3 bucket that you have created.
NOTE: You might have to set up a role in the IAM section in order to allow your EC2 instance to access your S3 instance else you will keep running into a permission error.

require_once(__DIR__ . ‘/vendor/autoload.php’);use Aws\S3\S3Client;
use Aws\Rekognition\RekognitionClient;
$bucket = ‘aws-webinar-ethnus’;
$keyname = ‘s.jpg’;
$s3 = new S3Client([
‘region’ => ‘us-east-2’,
‘version’ => ‘2006–03–01’,
‘signature’ => ‘v4’
]);
try {
// Upload data.
$result = $s3->putObject([
‘Bucket’ => $bucket,
‘Key’ => $keyname,
‘SourceFile’ => __DIR__. “/$keyname”,
‘ACL’ => ‘public-read-write’
]);
// Print the URL to the object.
$imageUrl = $result[‘ObjectURL’];
if($imageUrl) {
echo “Image upload done… Here is the URL: “ . $imageUrl;
$rekognition = new RekognitionClient([
‘region’ => ‘us-east-2’,
‘version’ => ‘latest’,
]);
$result = $rekognition->detectFaces([
‘Attributes’ => [‘DEFAULT’],
‘Image’ => [
‘S3Object’ => [
‘Bucket’ => $bucket,
‘Name’ => $keyname,
‘Key’ => $keyname,
],
],
]);
echo “Totally there are “ . count($result[“FaceDetails”]) . “ faces”;
}
} catch (Exception $e) {
echo $e->getMessage() . PHP_EOL;
}

Once you have copied the above code and made the changes, open your EC2 terminal using PUTTY and create a file named anything you want but make sure to keep the subscript as .php.

Next, copy the above PHP code with the changes you have made into the file and save and close the file. Its very similar to using a vi editor on Linux. Its actually pretty much the same. If your script runs smoothly, it shall take the object which you have declared to be public and refer to Amazon Rekognition in order to identify the faces from your picture ad return the result. In case you run into any errors, you may google them or you could DM me on my social media, I would be glad to help.

Now, I’ll show you the picture that I used for the facial recognition process.

Image used as input to AWS Rekognition

The output was supposed to identify the number of faces in the given picture. Check the graphic below to understand what has been done.

EC2 commands to run AWS Rekognition

Final output which I obtained after a lot of debugging was:

Screenshot of output where faces were Recognised

Yess. Rekognition read the picture and the output was given on my terminal. The project might seem a little complex and overwhelming but it is quite easy and is an amazing way to get an insight into the working of AWS and its ML softwares. In my case, I used AWS Rekognition. That’s it for today. Keep Learning.

Cheers.

--

--