Image Text/Face Recognition With AWS Rekognition👀
Tags: aws, machinelearning, python, tutorial
Published: 2019-10-18 22:03:47

<h1>AWS Rekognition</h1>

What is AWS Rekognition?

Rekognition is an AWS service that provides deep learning visual analysis for your images. Rekognition is very easy to integrate into your application by providing an image or video to the AWS Rekognition API. The service will identify some of the following: objects, people, text, scenes, and activities. "Amazon Rekognition also provides highly accurate facial analysis and facial recognition. You can detect, analyze, and compare faces for a wide variety of use cases, including user verification, cataloging, people counting, and public safety." - <i><a href="https://docs.aws.amazon.com/rekognition/latest/dg/what-is.html">AWS Official Docs </a></i>

<h2>Now let's start using AWS Rekognition.</h2>

Let's start with trying some of their demos to see how AWS Rekognition works.

1) Go to the following <a href="https://console.aws.amazon.com/rekognition/home?region=us-east-1#/label-detection"> link</a> and play with the demos. 


<center><h1>Time to get our hands dirty</h1></center>

Warning🚨 :
1) You need to have an AWS Management Console account.
2) It will ask you for your credit card info, but you won't be charged for what you use in this tutorial since it's part of the FREE TIER.

<center><h2>Setting up our S3 Bucket</h2></center>



<center><img src="https://thepracticaldev.s3.amazonaws.com/i/y5ysa2lftzsvcydynxds.png" width="100px"/></center>

<i>Amazon S3 or Amazon Simple Storage Service is a service offered by Amazon Web Services that provides object storage through a web service interface.</i>

1) Go to Find Services and look for S3
<img src="https://thepracticaldev.s3.amazonaws.com/i/gldmshbg3x6n8973uw8h.png" />
2) Click on <strong>CREATE A BUCKET</strong>
3) Enter the bucket name as thisdot-rk-YOUR_NAME.
4) Click on <strong>NEXT</strong> twice.
5) Uncheck all the boxes to grant public access to the bucket. Click <strong>NEXT</strong>.

<i>Note: I'm making this bucket public because, for the purpose of this tutorial, I'm not worried about security.</i>

<img src="https://thepracticaldev.s3.amazonaws.com/i/it8zjbitnzy2y600ntrx.png" />
6) Click on <strong>CREATE BUCKET</strong>
7) Time to upload some images to S3. Click on the bucket you just created.
8) <strong/>Download</strong> this image and save it as thisdot.png.
https://thisdot-rk-pato.s3.amazonaws.com/thisdot.png
9) Click on the dropdown from Mange Permissions and click on Grant public read access to this object(s).
<img src="https://thepracticaldev.s3.amazonaws.com/i/glx0ax45c2qsotz0lji7.png" />
10) Click <strong>NEXT</strong>
11) In the Storage Class, select <strong>Standard</strong>, and click <strong>NEXT</strong>
12) Click <strong>UPLOAD</strong>

<center><h2>Setting up our Lambda Function</h2></center>
<center><img src="https://thepracticaldev.s3.amazonaws.com/i/j2tp25v2s7ljr9rox03x.png" width="100px"/></center>

<i>AWS Lambda is an event-driven, serverless computing platform provided by Amazon as a part of Amazon Web Services. It is a computing service that runs code in response to events and automatically manages the computing resources required by that code.</i>

1) Go to Find Services and look for Lambda
<img src="https://thepracticaldev.s3.amazonaws.com/i/ltwnkijwe7vf00676web.png" width=""/>
2) Click on <strong>CREATE FUNCTION</strong>
3) For Lambda function name, enter thisdot-rk-YOUR_NAME
4) Under Runtime, click on the dropdown, and select Python 3.7.
5) Click on <strong>CREATE FUNCTION</strong>
<img src="https://thepracticaldev.s3.amazonaws.com/i/fynj95hjzq21s3ilfw0s.png" />
6) Scroll down to where it says Function code. You should see something like this:
<img src="https://thepracticaldev.s3.amazonaws.com/i/1ppvbxbtwbiw6cr1pui4.png" />
7) Erase everything you see in the editor and paste the following code:

<i>Note: Change the name of the bucket to the bucket name you created thisdot-rk-YOUR_NAME. </i>

The following code is going to help us finding the text inside of the images using the <i>.detect_text</i> method.

   ```python
    import json
    import boto3

    s3 = boto3.resource('s3')
    def lambda_handler(event, context):
     print(event)
     dump = json.loads(json.dumps(event))
     fileName = dump['image']
     print(fileName)

     bucket='thisdot-rk-YOUR_NAME'

     client=boto3.client('rekognition')
     text=client.detect_text(Image={'S3Object': 
     {'Bucket':bucket,'Name':str(fileName)}})
     res = {
      "textFound": text
     }
     return res
   ```

<i>Note: To Learn more about other AWS Boto Rekognition functions, visit <a href="https://boto3.amazonaws.com/v1/documentation/api/1.9.42/reference/services/rekognition.html" >this</a> website. </i>

8) Scroll down to change the <strong>BASIC SETTINGS</strong> of the lambda.
9) Change Memory to 512MB, and Timeout to 2min 30sec. This is to ensure your lambda doesn't timeout when processing images.
10) Scroll all the way to the top. In the upper right corner, you should see the <strong>SAVE</strong> button. Click on it.


<center><h2> Setting up our Security Roles Using IAM </h2></center>

<center><img src="https://thepracticaldev.s3.amazonaws.com/i/h134oqyjwo4gjobevmmp.png" width="40%"/></center>

<i>AWS Identity and Access Management (IAM) is a web service that helps you securely control access to AWS resources. You use IAM to control who is authenticated (signed in) and authorized (has permissions) to use resources.</i>

1) Search for the IAM Service (Services IAM)
2) On the left navigation bar, click on </strong>ROLES</strong>.
3) You can select any lambda you have created to give it a specific role. In this tutorial, we will select the following to give it access to AWS Rekognition.

<img src="https://thepracticaldev.s3.amazonaws.com/i/kkjd25cd0vn7d1q8tz2q.png" />

4) Then click on <strong>ATTACH POLICIES</strong>
5) Search for rekognition
6) Select AmazonRekognitionFUllAccess
<img src="https://thepracticaldev.s3.amazonaws.com/i/dencbidsezjj85p9mppj.png" />
7) Click on </strong>ATTACH POLICY</strong>
<i> Note: You can have multiple policies attached </i>


<center><h2>Time to Test</h2></center>

1) Go back to your lambda function.
2) In the top right corner, select the dropdown that says "Select a test event"
3) Then select "Configure test events"
4) Give a name to your event
5) Then enter the following JSON object
   ```json
        {
          "image": "thisdot.png"
        }    
   ```
Where "thisdot.png" is the name of your image inside of your S3 bucket.

6) Click <strong>CREATE</strong>
7) On Your top right corner, you will see the <strong>TEST</strong> button. Click on it.
8) You should see a green box. Click on expand details.
<img src="https://thepracticaldev.s3.amazonaws.com/i/okxg02435nnc5hb8fklx.png" />
 9) Take a look at the response object. As you can see, it has found our text inside of the image.
<img src="https://thepracticaldev.s3.amazonaws.com/i/0z56y7xxwttzs1yl9ik8.png" />

<h2>Awesome!!!, right?</h2>

Now, let's jump to compare faces. Imagine you want to see if one person appears in the same picture. Rekognition can do this. Imagine all the possibilities!


1) Download these 2 images.

Source Image https://thepracticaldev.s3.amazonaws.com/i/ktpt1lx1ubzt3ilupph7.jpg

Target Image
https://thepracticaldev.s3.amazonaws.com/i/p5j8z6hiey8z8rkspms6.jpg

2) Upload them to S3, following the steps previously mentioned.

3) Go back to your lambda, and create a new test, or edit the existing test. Your test will look like this:
   ```json
      {
        "sourceImage": "source.jpg",
        "targetImage": "target.jpg"
      }
   ```
4) Then, we are going to modify our lambda code to be able to compare faces
   ```python
    import json
    import boto3

    s3 = boto3.resource('s3')
    def lambda_handler(event, context):
     print(event)
     dump = json.loads(json.dumps(event))
     sourceImage = dump['sourceImage']
     targetImage = dump['targetImage']


     bucket='thisdot-rk-YOUR_NAME'

     client = boto3.client('rekognition')
     faceComparison= client.compare_faces(
     SourceImage={'S3Object': {'Bucket':bucket,'Name':str(sourceImage)}},
     TargetImage={'S3Object': {'Bucket':bucket,'Name':str(targetImage)}}
     )
     res = {
      "faceRecognition": faceComparison
     }
     return res
   ```

5) Look at the Execution results, and analyze the data.

Tell me what you think about this tutorial on twitter or comment below!

*Enjoy this article? Head on over to [This Dot Labs](https://www.thisdotlabs.com) and check us out!  We are a tech consultancy that does all things javascript and front end.  We specialize in open source software like, Angular, React and Vue.*