AWS Face Recognition

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 17

ABSTRACT

Amazon Web
Services offers
reliable, scalable, and
inexpensive cloud
computing services. In
this project I will be
building a Face
Detection Application
on AWS.

K G PRAJWAL
Student, DSU, Bengaluru 76
AWS face recognition project
By Ethnus
DAY 1 – EC2
Keys
Public and Private
Public - Server --> instance
Private - Upload while connecting to the server

Download PuTTY
PuTTY - . ppk => .pem

Getting connected
1. Convert .pem to .ppk using puttygen command
puttygen KEYFILE.pem -o KEYFILE.ppk
Save the .ppk file

2. Open up PuTTY Software and select SSH and Port No: 22 and paste the IP address
from AWS Console
Go to SHH -> Auth
Browse the .ppk file and hit open
login as: ec2-user

3. Download a server(Apache) by executing following commands:


sudo yum install httpd
sudo service httpd start
sudo service httpd status
sudo vim /var/www/html/index
Press "i"
Write anything
Press "ESC" -> Type ":wq" -> "Enter"
4. Our machine allows only SSH and denies all other traffics by default.
So make AWS allow HTTP request.
Default Port Numbers:
HTTP: 80
SSH: 22
HTTPS: 443
Therefore, Go to security groups(launch wizard) -> edit inbound rules -> add rule ->
HTTP and Source-Anywhere
Search for the IP now on your web browser.
You will see whatever you typed in the file.
DAY 2 – S3
S3

Telegram Bot EC2

Rekognition

Important keywords (with respect to instance):

1. Terminate
2. Reboot
3. Stop

On termination of the instance, by default the storage(EBS – Elastic Block Storage) will also be deleted.

S3 – Simple Storage Service

S3 stores only objects.

Objects – Actual data and metadata.

- Fundamental entities stored in Amazon S3


- Data and metadata
- Data is opaque
- Name-value
- Metadata

Bucket – Container for objects.

Keys – Unique identifier for an object.

Region – Geographical place where all servers are located physically.

Availability Zones – An actual place where data centre exists. Every region has minimum two availability
zones.

Edge Locations – Caching the files.

S3 Tiering:

1. Amazon S3 Standard
2. Amazon S3 Standard-Infrequent Access
3. Amazon S3 One Zone-Infrequent Access
4. Amazon S3 Glacier
DAY 3 – VPC
VPC = Virtual Public Cloud

VPC lets us provision a logically isolated section of the AWS Cloud where we can launch AWS resources in a
virtual network that we define.

CIDR – Classless inter-domain routing: is a set of IP standards that is used to create unique identifiers for
networks and individual devices.

Why do we need subnets?

Because there are groups inside our workplace where a single network has to divided into multiple networks
using the process called subnetting to create subnets.

Subnet = A subnetwork or subnet is a logical subdivision of an IP network.

Can EC2 upload a file to S3?

Yes. Concepts involved:

IAM( Identity and Access Management ):

1. Users : Credentials to login to AWS.


2. Roles : Temporary position.
3. Groups : (Developer, Manager, Tester, Operations)
4. Policies

Ex:

DevA – IAM User – Creating EC2 Instances

DevB – IAM User – Create EC2 Instances


TestA – IAM User – Check the EC2 Instances. -> Testers Group

TestB – IAM User – Check the EC2 Instances. -> Testers Group

Testers Group – Check the EC2 Instances. -> Policy

Give EC2 a Role to access S3.

Steps:

1. Create an AWS Instance and generate the .ppk file from the downloaded .pem file.
2. Login to the instance using PuTTY.
3. Install php using ‘sudo yum install php’

4. Install composer ‘curl -sS https://getcomposer.org/installer | php’

5. cd /var/www/html
6. sudo mkdir face
7. cd face

8. Install AWS SDK using ‘sudo php -d memory_limit=-1 ~/composer.phar require aws/aws-sdk-php’
9. sudo wget https://i.pinimg.com/originals/b9/7e/a3/b97ea33b5842c7894b804923c6c05580.jpg
10. sudo mv b97ea33b5842c7894b804923c6c05580.jpg sample.jpg
11. sudo vim index.php
12. index.php:
<?php

/*

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

*/
error_reporting(0);

require_once(__DIR__ . '/vendor/autoload.php');

use Aws\S3\S3Client;
use Aws\Rekognition\RekognitionClient;

$bucket = 'Your Bucket Name';


$keyname = 'Your File Name';

$s3 = S3Client::factory([
'profile' => 'default',
'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'
]);

// Print the URL to the object.


$imageUrl = $result['ObjectURL'];
if($imageUrl) {
echo "Image upload done... Here is the URL: " . $imageUrl;
}
} catch (Exception $e) {
echo $e->getMessage() . PHP_EOL;
}
13. sudo php index.php

Now the image has been downloaded to the mentioned bucket.


DAY 4 – App Completion
Screenshots

Login

EC2
S3

Make Public
PuTTY
Amazon Rekognition
Day 4:
Change content of index.php to:

<?php

/*

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 steps

sudo 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

*/
// error_reporting(0);

require_once(__DIR__ . '/vendor/autoload.php');

use Aws\S3\S3Client;
use Aws\Rekognition\RekognitionClient;

$bucket = 'aws-face-recognition';
$keyname = 'sample.jpg';

$s3 = new S3Client([


'region' => 'us-east-1',
'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-1',
'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;
}

1. sudo vim index.php


2. use d999d to delete its contents.
3. Run the file.
4. Start the httpd server

And type the following in address bar: [IP]/face/index.php

Integration with Telegram Bot


https://api.telegram.org/bot{my_bot_token}/setWebhook?
url={url_to_send_updates_to}

url_to_send_updates_to = IP/face/index.php

You might also like