How to Recognize Celebrity in an Image using PHP and Amazon Rekognition API?

Artificial Intelligence is one of the major hottest topic among every techies. Billion dollar companies  like Amazon are already invested into machine learning for the last 10 years. Today we are going to talk about Amazon Rekognition Service – A Deep Learning-based Image Recognition. In my previous post I had explained How to convert your text to speech using Amazon Polly API with PHP

Amazon Rekognition serivce provides include Object and Scene detection, Facial Analysis, Face Comparison and Facial Recognition. We will start with facial analysis like celebrity identification.

Amazon Rekognition service automatically recognizes celebrities in images and provides confidence scores.

Amazon Rekognition - Celebrity Identification with PHP
Amazon Rekognition – Celebrity Identification with PHP

 

Contents

Why Amazon Rekognition?

Use Case 1 – Movie Database

In case if you have huge collection of movies in your database and if you want to find the celebrities in the movie poster, In this situation it requires more manual effort to find the celebrity name in the movie poster. Amazon Rekognition’s Celebrity Rekognition API Service will help you to automate the whole process in a minute without any manual intervention.

Use Case 2 – Web Application

In any web application like facebook, If you upload profile picture/cover picture, Amazon Rekognition’s Celebrity Rekognition API Service helps you to identify the celebrity in that picture and its saves your time to mention the celebrity name manually in your post.

We are now going to see how it works using PHP as illustrated in the above use cases

Download and Install Amazon PHP SDK Client

To get started, you must download the zip file, unzip it into your project to a location of your choosing, and include the autoloader:

require '/path/to/aws-autoloader.php';

Configuring the AWS Access Keys

In order to use amazon rekognition service, you should register an account with AWS and get the  AWS Access key/secret within your AWS account. Please go through this link to get the credentails. I prefer to use an IAM to generate the key.

Amazon Rekognition Client

After getting credentails from amazon account, we need to create an amazon rekognition client as below

use Aws\Rekognition\RekognitionClient;
$config = [
'version' => 'latest',
'region' => 'us-east-1', //region
'credentials' => [
'key' => 'your aws access key',
'secret' => 'your aws secret key',
]];
$client = new RekognitionClient($config);

Next we should pass the image to amazon rekognition client to analyze/detect the image and it provides the celebrity details matches in the image.

How to Pass an Image URL into Amazon Rekognition Client

In the below code, we are passing the image url into rekognition api to get the image details by analzing the image in PHP.

$data = file_get_contents($url);

$args = [
'Image' => [
'Bytes' => $data
] ];

$result = $client->recognizeCelebrities($args);

Output

After successful completion of the API, RecognizeCelebrities returns an array of recognized celebrities and an array of unrecognized faces.

JSON Array

{
"CelebrityFaces": [{
"Face": {
"BoundingBox": {
"Height": 0.617123007774353,
"Left": 0.15641026198863983,
"Top": 0.10864841192960739,
"Width": 0.3641025722026825
},
"Confidence": 99.99589538574219,
"Landmarks": [{
"Type": "eyeLeft",
"X": 0.2837241291999817,
"Y": 0.3637104034423828
}, {
"Type": "eyeRight",
"X": 0.4091649055480957,
"Y": 0.37378931045532227
}, {
"Type": "nose",
"X": 0.35267341136932373,
"Y": 0.49657556414604187
}, {
"Type": "mouthLeft",
"X": 0.2786353826522827,
"Y": 0.5455248355865479
}, {
"Type": "mouthRight",
"X": 0.39566439390182495,
"Y": 0.5597742199897766
}],
"Pose": {
"Pitch": -7.749263763427734,
"Roll": 2.004552125930786,
"Yaw": 9.012002944946289
},
"Quality": {
"Brightness": 32.69192123413086,
"Sharpness": 99.9305191040039
}
},
"Id": "3Ir0du6",
"MatchConfidence": 98.0,
"Name": "Jeff Bezos",
"Urls": ["www.imdb.com/name/nm1757263"] }],
"OrientationCorrection": "ROTATE_0",
"UnrecognizedFaces": [{
"BoundingBox": {
"Height": 0.5345501899719238,
"Left": 0.48461538553237915,
"Top": 0.16949152946472168,
"Width": 0.3153846263885498
},
"Confidence": 99.92860412597656,
"Landmarks": [{
"Type": "eyeLeft",
"X": 0.5863404870033264,
"Y": 0.36940744519233704
}, {
"Type": "eyeRight",
"X": 0.6999204754829407,
"Y": 0.3769848346710205
}, {
"Type": "nose",
"X": 0.6349524259567261,
"Y": 0.4804527163505554
}, {
"Type": "mouthLeft",
"X": 0.5872702598571777,
"Y": 0.5535582304000854
}, {
"Type": "mouthRight",
"X": 0.6952020525932312,
"Y": 0.5600858926773071
}],
"Pose": {
"Pitch": -7.386096477508545,
"Roll": 2.304218292236328,
"Yaw": -6.175624370574951
},
"Quality": {
"Brightness": 37.16635513305664,
"Sharpness": 99.9305191040039
}
}] }

You can parse the results by using the “CelebrityFaces” key. You can get all the datas such as Celebrity name, Matching score, Confidence etc.

UnrecognizedFaces is an array of faces that didn’t match any known celebrities. Each ComparedFace object in the array contains a bounding box (as well as other information) that you can use to locate the face in the image.

if(!empty($result['CelebrityFaces'])) {
foreach($result['CelebrityFaces'] as $key => $val)
{
$name = $val['Name'];
$MatchScore = $val['Face']['Confidence'];
$matchConfidence = $val['MatchConfidence'];
}
}

 

 

I believe this tutorial helps you in providing the information, you have searched for in this website. I am open to accept any projects for other requirement or any customization on this Amazon Rekognition’s Celebrity identification. I am good at all amazon web services, feel free to contact me at karthi@w3lessons.info for any help.

If you find this tutorial useful, please share it with your friends and in social networks.

Your suggestions/comments are most welcome.

 

You May Also Like

Never Miss Any Web Tutorials, Guides, Tips and Free eBooks

Join Our Community Of 50,000+ Web Lovers and get a weekly newsletter in your inbox

 

I hate spam too. Unsubscribe at any time.

Leave a Comment