0% found this document useful (0 votes)
3 views

Project Overview

The project involves creating a web application for users to upload images, which are then analyzed using AWS Rekognition and displayed on a PHP web page. Key steps include setting up AWS resources like S3 buckets and Lambda functions, writing the Lambda function code, and developing the PHP web application for image upload and result display. Additional considerations include security, cost management, and error handling to ensure a robust application.

Uploaded by

245120737303
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Project Overview

The project involves creating a web application for users to upload images, which are then analyzed using AWS Rekognition and displayed on a PHP web page. Key steps include setting up AWS resources like S3 buckets and Lambda functions, writing the Lambda function code, and developing the PHP web application for image upload and result display. Additional considerations include security, cost management, and error handling to ensure a robust application.

Uploaded by

245120737303
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Project Overview

This project involves building a web application that allows users to upload images, analyze the
images using AWS Rekognition, and display the analysis results on a PHP web page. The workflow
includes:

1. Uploading an image to an S3 bucket.

2. Triggering a Lambda function upon image upload.

3. Using AWS Rekognition to analyze the image.

4. Displaying the detected labels on a PHP web page.

Step 1: Set Up AWS Resources

1.1 Create an S3 Bucket

1. Log in to the AWS Management Console.

2. Navigate to S3.

3. Click on Create bucket.

4. Name your bucket (e.g., my-image-analysis-bucket) and select a region.

5. Configure the bucket to allow public read access (you can adjust permissions later for
security).

6. Leave other settings as default and click Create bucket.

1.2 Create an IAM Role

1. Navigate to the IAM service in the AWS Console.

2. Click on Roles and then Create role.

3. Choose AWS Service as the trusted entity and select Lambda.

4. Attach the following policies:

o AmazonS3ReadOnlyAccess

o AmazonRekognitionFullAccess

o AWSLambdaBasicExecutionRole

5. Name the role (e.g., lambda-s3-rekognition-role) and create it.

1.3 Create a Lambda Function

1. Navigate to the Lambda service in the AWS Console.

2. Click Create function.

3. Choose Author from scratch.

4. Name the function (e.g., ImageLabelingFunction).

5. Select Python as the runtime.


6. Choose the IAM role you created earlier.

7. Click Create function.

Step 2: Write the Lambda Function

2.1 Lambda Function Code

Replace the default code in your Lambda function with the following Python code:

python

Copy code

import boto3

def lambda_handler(event, context):

client = boto3.client('rekognition')

bucket = event['Records'][0]['s3']['bucket']['name']

key = event['Records'][0]['s3']['object']['key']

response = client.detect_labels(Image={'S3Object': {'Bucket': bucket, 'Name': key}})

labels = [label['Name'] for label in response['Labels']]

# Return the labels as the function's output

return {

'statusCode': 200,

'body': labels

2.2 Set Up the S3 Trigger

1. In your Lambda function, scroll down to the Designer section.

2. Click Add trigger.

3. Choose S3 as the trigger.

4. Select your bucket (my-image-analysis-bucket).

5. Set the event type to ObjectCreated.

6. Click Add.
Step 3: Set Up the PHP Web Application

3.1 Install AWS SDK for PHP

Ensure your server or local environment has PHP and Composer installed. Run the following
command to install the AWS SDK for PHP:

bash

Copy code

composer require aws/aws-sdk-php

3.2 Create the PHP Web Pages

a. Create index.php

php

Copy code

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Image Upload and Analysis</title>

</head>

<body>

<h1>Upload an Image for Analysis</h1>

<form action="upload.php" method="post" enctype="multipart/form-data">

<input type="file" name="image" accept="image/*" required>

<input type="submit" value="Upload Image">

</form>

</body>

</html>

b. Create upload.php

php

Copy code

<?php

require 'vendor/autoload.php';
use Aws\S3\S3Client;

use Aws\Lambda\LambdaClient;

$bucket = 'my-image-analysis-bucket'; // Your S3 bucket name

$key = basename($_FILES['image']['name']); // Get the uploaded file's name

$tmpFilePath = $_FILES['image']['tmp_name']; // Temporary file path

// Upload the image to S3

$s3 = new S3Client([

'version' => 'latest',

'region' => 'us-west-2', // Your AWS region

]);

try {

$result = $s3->putObject([

'Bucket' => $bucket,

'Key' => $key,

'SourceFile' => $tmpFilePath,

'ACL' => 'public-read', // Make the file publicly accessible

]);

echo "<p>Image uploaded successfully: <a


href='{$result['ObjectURL']}'>{$result['ObjectURL']}</a></p>";

// Trigger the Lambda function

$lambda = new LambdaClient([

'version' => 'latest',

'region' => 'us-west-2',

]);
$lambdaResult = $lambda->invoke([

'FunctionName' => 'ImageLabelingFunction', // Your Lambda function name

'Payload' => json_encode([

'Records' => [

's3' => [

'bucket' => ['name' => $bucket],

'object' => ['key' => $key],

]),

]);

$labels = json_decode($lambdaResult->get('Payload'), true);

echo "<h2>Detected Labels:</h2>";

echo "<ul>";

foreach ($labels['body'] as $label) {

echo "<li>{$label}</li>";

echo "</ul>";

} catch (Exception $e) {

echo "<p>Error: {$e->getMessage()}</p>";

?>

Step 4: Test the Application

1. Host the PHP files (index.php and upload.php) on a web server.

2. Navigate to the index.php page in your browser.

3. Upload an image using the form.


4. Once uploaded, the page will display the image URL and the detected labels from AWS
Rekognition.

Additional Considerations

 Security: Ensure your S3 bucket and IAM roles have the least privilege necessary. Use IAM
policies to restrict access where possible.

 Cost Management: Monitor AWS usage to avoid unexpected costs. S3 storage, Lambda
invocations, and Rekognition API calls may incur charges.

 Error Handling: The PHP script includes basic error handling. Consider improving it to handle
various edge cases (e.g., large image uploads, network issues).

By following these steps, you'll have a complete web application that integrates AWS Lambda,
Rekognition, S3, and PHP. This setup allows for automated image analysis with a user-friendly
interface.

You might also like