Analyzing videos with Amazon Rekognition and Laravel (Part 1) cover image

Analyzing videos with Amazon Rekognition and Laravel (Part 1)

Tom Oehlrich

AWS Flutter Laravel

Amazon AWS offers a great way to analyze not only images but also videos. We will use Amazon Rekognition to get a couple of labels that are describing our video as well as the timestamp at which these labels are occurring.

The initial setup and the asynchronous workflow of the recognition workflow can be a little bit tricky at first though.

As always there is more than one way to achieve what we want within the vast amount of AWS services and with PHP.

So this is what we are going to do:

In part 1 (this post) we are setting up Amazon Rekognition, IAM, S3, Simple Notification Service (SNS) and deal with the necessary rights.

In part 2 we are starting a new Laravel project that provides a video file upload to an S3 bucket and starts a label detection analysis with Amazon Rekognition.
We will write a command that checks for the results of the video analysis and that can be executed by a cronjob.
The results will be displayed in a list of uploaded videos and the corresponding detailed info pages.

Note: For the sake of this tutorial, we are using full access rights for the various Amazon services. In a productive environment we would want to restrict rights to what is really necessary.

Note: We are using the same AWS region for everything we are doing on AWS. Otherwise Amazon Rekognition won't be able to communicate with our S3 bucket.


Setting up AWS Services

First let's add a new user in IAM.

Let's switch to Amazon S3 and create a bucket. Other than with image analysis videos processed by Amazon Rekognition have to be stored in S3.

Next we will configure the Simple Notification Service (SNS).
SNS or Simple Notification Service is a service that receives the analysis results from the Amazon Rekognition process. The PHP SDK makes it easy to check for the analysis progress and get the results when finished. We will do that using a cronjob.
In a real live application there are a couple of advanced ways of handling this. We could have SNS push the rekognition results to our own HTTP endpoint and process them there.
We could couple a Simple Queue Service (SQS) with our SNS and then benefit from being able to pull data from SQS.
We could connect AWS Lambda with the SNS and write a Lambda function that deals with the results.

To use SNS we have to create a SNS topic first.

To grant Amazon Rekognition publishing permissions to the Amazon SNS topic we have to apply an IAM role to it and adjust some permissions.

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "iam:PassRole"
            ],
            "Resource": "arn:aws:iam::166755042369:role/RekognitionRole"
        }
    ]
}

That's it.
We should now be ready to proceed with the fun part in Laravel in part 2 of this tutorial.