Hey everyone! In this article, we'll be creating a simple application to perform image text detection using AWS Rekognition with Node.js.
What is AWS Rekognition?
Amazon Rekognition is a service that makes it easy to add image and video analysis to your applications. It offers features like text detection, facial recognition, and even celebrity detection.
While Rekognition can analyze images or videos stored in S3, for this tutorial, we'll be working without S3 to keep things simple.
We'll be using Express for the backend and React for the frontend.
First Steps
Before we start, you'll need to create an AWS account and set up an IAM user. If you already have these, you can skip this section.
Creating IAM user
Configuring aws-sdk
Project Directory
my-directory/ │ ├── client/ │ └── src/ │ └── App.jsx │ └── public/ │ └── package.json │ └── ... (other React project files) │ └── server/ ├── index.js └── rekognition/ └── aws.rek.js
Setting up frontend
npm create vite @latest . -- --template react
it will create the react project in the client folder.
In the App.jsx
import { useState } from "react"; function App() { const [img, setImg] = useState(null); const handleImg = (e) => { setImg(e.target.files[0]); // Store the selected image in state }; const handleSubmit = (e) => { e.preventDefault(); if (!img) return; const formData = new FormData(); formData.append("img", img); console.log(formData); // Log the form data to the console }; return (); } export default App;
Let's test this out by ensuring the image is logged to the console after submitting.
Now, Let's move to backend and start making the soul, for this project.
Initializing the backend
in the server folder
npm init -y
npm install express cors nodemon multer @aws-sdk/client-rekognition
I have created a separate folder for rekognition, to handle analyzing logic and create a file inside that folder.
//aws.rek.js import { RekognitionClient, DetectTextCommand, } from "@aws-sdk/client-rekognition"; const client = new RekognitionClient({}); export const Reko = async (params) => { try { const command = new DetectTextCommand( { Image: { Bytes:params //we are using Bytes directly instead of S3 } } ); const response = await client.send(command); return response } catch (error) { console.log(error.message); } };
Explanation
Creating the API
In the server folder, create a file index.js or what ever name you want.
//index.js import express from "express" import multer from "multer" import cors from "cors" import { Reko } from "./rekognition/aws.rek.js"; const app = express() app.use(cors()) const storage = multer.memoryStorage() const upload = multer() const texts = [] let data = [] app.post("/img", upload.single("img"), async(req,res) => { const file = req.file data = await Reko(file.buffer) data.TextDetections.map((item) => { texts.push(item.DetectedText) }) res.status(200).send(texts) }) app.listen(3000, () => { console.log("server started"); })
Explanation
Coming back to frontend
import axios from "axios"; import { useState } from "react"; import "./App.css"; function App() { const [img, setImg] = useState(null); const [pending, setPending] = useState(false); const [texts, setTexts] = useState([]); const handleImg = (e) => { setImg(e.target.files[0]); }; const handleSubmit = async (e) => { e.preventDefault(); if (!img) return; const formData = new FormData(); formData.append("img", img); try { setPending(true); const response = await axios.post("http://localhost:3000/img", formData); setTexts(response.data); } catch (error) { console.log("Error uploading image:", error); } finally { setPending(false); } }; return (); } export default App;{pending &&Loading...
} {texts.length > 0 && ({texts.map((text, index) => (
)}- {text}
))}
Final Output
After clicking the "Upload Image" button, the backend processes the image and returns the detected text, which is then displayed to the user.
For the complete code, check out my: GitHub Repo
Thank You!!!
Follow me on: Medium, GitHub, LinkedIn, X, Instagram
The above is the detailed content of Text Detection in Images Using AWS Rekognition and Node.js. For more information, please follow other related articles on the PHP Chinese website!