Contact Us

Building an Image Analyzer with React and Amazon Rekognition


awspartnerbadge

Introduction

In this tutorial, we'll create an Image Analyzer app using React and Amazon Rekognition. This app allows users to upload images and receive AI-powered analysis of the content, including object detection and text recognition. We'll focus on the technical implementation.

1: Setting Up the Project:

  1. First, create a new React project and install necessary dependencies:
    npx create-react-app image-analyzer cd image-analyzer npm install aws-sdk axios react-dropzone
    These commands set up a new React project and install:
  2. aws-sdk: For interacting with Amazon Rekognition
  3. axios: For making HTTP requests (though we won't use it directly in this tutorial)
  4. react-dropzone: For easy file uploads

2. Configuring AWS:

Create an AWS account and set up an IAM user with permissions for Amazon Rekognition. Then, create a file named aws-config.js in your src folder:

import AWS from 'aws-sdk'; AWS.config.update({ region: 'us-west-2', // Replace with your preferred region credentials: new AWS.Credentials( process.env.REACT_APP_AWS_ACCESS_KEY, process.env.REACT_APP_AWS_SECRET_KEY ) }); export const rekognition = new AWS.Rekognition();

This code configures the AWS SDK with your credentials. Make sure to create a `.env` file in your project root with your actual AWS credentials:

REACT_APP_AWS_ACCESS_KEY=your_access_key_here REACT_APP_AWS_SECRET_KEY=your_secret_key_here

IMPORTANT: Never commit your .env file to version control!

3. Building the Main Component:

In App.js, create the main structure of the application:

import React, { useState } from 'react'; import { useDropzone } from 'react-dropzone'; import { rekognition } from './aws-config'; function App() { const [image, setImage] = useState(null); const [results, setResults] = useState(null); const [loading, setLoading] = useState(false); const onDrop = (acceptedFiles) => { setImage(acceptedFiles[0]); setResults(null); }; const { getRootProps, getInputProps } = useDropzone({ onDrop, accept: 'image/*', multiple: false }); // ... (analyzeImage function and JSX will be added later) }

This sets up our component with three state variables:

The onDrop function updates the image state when a file is uploaded and clears any previous results.

The useDropzone hook is configured to accept only image files and allow only single file uploads.

4. Implementing Image Analysis:

Add the analyzeImage function to perform the analysis using Amazon Rekognition:

const analyzeImage = async () => { if (!image) return; setLoading(true); const reader = new FileReader(); reader.onload = async (event) => { const imageBytes = new Uint8Array(event.target.result); try { const labelData = await rekognition.detectLabels({ Image: { Bytes: imageBytes }, MaxLabels: 10, MinConfidence: 70 }).promise(); const textData = await rekognition.detectText({ Image: { Bytes: imageBytes } }).promise(); setResults({ labels: labelData.Labels, text: textData.TextDetections }); } catch (error) { console.error('Error analyzing image:', error); alert('Error analyzing image. Please try again.'); } finally { setLoading(false); } }; reader.readAsArrayBuffer(image); };

This function:

Initial Checks:

File Reading Setup:

File Reading Process:

Rekognition API Calls:

detectLabels Configuration:

detectText Configuration:

Handling Results:

Error Handling:

Cleanup:

Example return types:

Rendering the UI:

Add the JSX to render the application:

return ( <div className="App"> <h1>Image Analyzer</h1> <div {...getRootProps()} className="dropzone"> <input {...getInputProps()} /> <p>Drag & drop an image here, or click to select one</p> </div> {image && ( <div> <img src={URL.createObjectURL(image)} alt="Uploaded" className="uploaded-image" /> <button onClick={analyzeImage} disabled={loading}> {loading ? 'Analyzing...' : 'Analyze Image'} </button> </div> )} {results && ( <div className="results"> <h2>Analysis Results:</h2> <h3>Labels:</h3> <ul> {results.labels.map((label, index) => ( <li key={index}> {label.Name} - Confidence: {label.Confidence.toFixed(2)}% </li> ))} </ul> <h3>Detected Text:</h3> <ul> {results.text .filter(item => item.Type === 'LINE') .map((textItem, index) => ( <li key={index}>{textItem.DetectedText}</li> ))} </ul> </div> )} </div> );

This JSX:

Styling the Application:

Add your CSS styles in App.css. We won't go into detail about our CSS.

Get full code here - GitHub - Abdulmumin1/aws-demos

Check out our previous tutorial - Building an Email Subscription and Broadcasting System with React and AWS SNS.

Conclusion:

You've now created a functional Image Analyzer using React and Amazon Rekognition. This app demonstrates:

Next steps could include:

Remember to handle your AWS credentials securely and consider implementing server-side code for production use to keep your AWS credentials private.

AWS Rekognition is really powerful and it offers a wide range of image and video analysis capabilities. Its key features include:

  1. Object and Scene Detection: Identifies thousands of objects, scenes, and activities in images and videos.
  2. Facial Analysis: Detects faces, analyzes facial attributes, and can perform facial comparison and recognition.
  3. Text in Image (OCR): Recognizes and extracts text from images, supporting multiple languages.
  4. Celebrity Recognition: Identifies celebrities in images and videos.
  5. Content Moderation: Detects inappropriate or offensive content in images and videos.
  6. Custom Labels: Allows you to train the service to recognize custom objects and scenes specific to your use case.
  7. Personal Protective Equipment (PPE) Detection: Identifies if individuals in images are wearing items like face covers, hand covers, and head covers.
  8. Video Analysis: Extends many of its image capabilities to video, including object detection, face recognition, and activity recognition.
  9. Streaming Video Analysis: Provides real-time analysis of streaming video.
  10. Facial Search: Allows searching and comparing faces across large collections of images.

Rekognition's capabilities make it suitable for a wide range of applications, including:

The service is designed to be easy to use, requiring no machine learning expertise to get started. It's scalable, cost-effective (you only pay for the images and videos you analyze), and continuously improving as Amazon updates its underlying models.

However, it's important to note that, like all AI services, Rekognition has limitations. Its accuracy can vary depending on image quality, lighting conditions, and other factors. Also, the use of facial recognition technology raises privacy concerns that should be carefully considered in any implementation.

In our Image Analyzer app, we've only scratched the surface of what Rekognition can do. There's vast potential for expanding your app's capabilities by leveraging more of Rekognition's features.