Extracting text from images in Node.js using AWS Rekognition
Extracting text from images is a common requirement in modern applications, whether it is for processing scanned documents, enhancing accessibility, or automating data entry. AWS Rekognition provides robust text detection capabilities that can be seamlessly integrated into your Node.js applications. The service supports JPEG and PNG images up to 5MB when supplied as raw bytes, or 15MB when referenced in S3, and can detect up to 100 words per image.
Introduction to AWS Rekognition and text detection
AWS Rekognition is a powerful image and video analysis service that leverages deep learning to identify objects, scenes, text, and faces within images. Its text detection functionality is ideal for automating data entry, enhancing accessibility, and processing scanned documents. In this guide, you will learn how to integrate AWS Rekognition into your Node.js application for reliable text extraction.
Setting up AWS Rekognition
Before you begin, ensure that you set up your AWS credentials and have the necessary IAM
permissions. Use an IAM role or a configured local AWS profile with a policy like the following to
allow access to the rekognition:DetectText action:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": ["rekognition:DetectText"],
"Resource": "*"
}
]
}
Configure your AWS credentials using one of these methods:
- Environment variables (set
AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEY, andAWS_REGION, plusAWS_SESSION_TOKENfor temporary credentials). - AWS credentials file (located at
~/.aws/credentialson Linux/Mac orC:\Users\YourUser\.aws\credentialson Windows). - An IAM role attached to the AWS service running your application.
Installing the AWS SDK for Node.js
Use Node.js 22 or later and install AWS SDK v3 for Rekognition. Save the JavaScript examples as
.mjs files, or set "type": "module" in your project’s package.json:
npm install @aws-sdk/client-rekognition@3
Integrating AWS SDK into a Node.js application
Import the necessary classes and configure the region. Leaving credentials unset lets the
default credential provider chain
resolve profiles, temporary credentials, and IAM roles:
import { RekognitionClient, DetectTextCommand } from '@aws-sdk/client-rekognition'
const client = new RekognitionClient({
region: process.env.AWS_REGION || 'us-west-2',
})
Performing text detection with AWS Rekognition
You can detect text from both local files and images stored in S3. Below is an example function that loads a local image file and returns detected text lines with their confidence scores and bounding box information:
import fs from 'fs'
// Assumes 'client' is already configured as shown above
const detectTextFromImage = async (imagePath) => {
const imageBytes = fs.readFileSync(imagePath)
const params = {
Image: { Bytes: imageBytes },
}
try {
const data = await client.send(new DetectTextCommand(params))
const textLines = (data.TextDetections ?? []).filter((text) => text.Type === 'LINE').map((text) => ({
text: text.DetectedText,
confidence: text.Confidence,
boundingBox: text.Geometry?.BoundingBox,
}))
return textLines
} catch (err) {
if (err.name === 'InvalidImageFormatException') {
throw new Error('Invalid image format. Only JPEG and PNG are supported.', { cause: err })
} else if (err.name === 'ImageTooLargeException') {
throw new Error('Image size exceeds the 5MB limit for raw bytes.', { cause: err })
} else if (err.name === 'ThrottlingException') {
throw new Error('API request rate exceeded. Please retry with backoff.', { cause: err })
}
throw err
}
}
Analyzing images from S3
If your image is stored in an S3 bucket, reuse the configured client and DetectTextCommand
above. The bucket must be in the same region as the Rekognition client, and the calling identity
needs s3:GetObject permission for the image:
const detectTextFromS3Image = async (bucket, key) => {
const params = {
Image: {
S3Object: {
Bucket: bucket,
Name: key,
},
},
}
try {
const data = await client.send(new DetectTextCommand(params))
const textLines = (data.TextDetections ?? []).filter((text) => text.Type === 'LINE').map((text) => ({
text: text.DetectedText,
confidence: text.Confidence,
boundingBox: text.Geometry?.BoundingBox,
}))
return textLines
} catch (err) {
if (err.name === 'InvalidImageFormatException') {
throw new Error('Invalid image format. Only JPEG and PNG are supported.', { cause: err })
} else if (err.name === 'ImageTooLargeException') {
throw new Error('Image exceeds the 15MB limit for S3 objects.', { cause: err })
} else if (err.name === 'ThrottlingException') {
throw new Error('API request rate exceeded. Please retry with backoff.', { cause: err })
}
throw err
}
}
Practical example and code snippets
The following complete example processes a local image file. It verifies the existence of the file, sends the image to AWS Rekognition for text detection, and logs the detected text lines along with their confidence scores and positions.
import { RekognitionClient, DetectTextCommand } from '@aws-sdk/client-rekognition'
import fs from 'fs'
const client = new RekognitionClient({
region: process.env.AWS_REGION || 'us-west-2',
})
const processImage = async (imagePath) => {
if (!fs.existsSync(imagePath)) {
throw new Error('Image file not found.')
}
const imageBytes = fs.readFileSync(imagePath)
const params = {
Image: { Bytes: imageBytes },
}
try {
const data = await client.send(new DetectTextCommand(params))
console.log('Detected text lines:')
const detections = data.TextDetections ?? []
for (const text of detections.filter((text) => text.Type === 'LINE')) {
console.log(`- Text: ${text.DetectedText}`)
if (text.Confidence != null) console.log(` Confidence: ${text.Confidence.toFixed(2)}%`)
const box = text.Geometry?.BoundingBox
if (box?.Left != null && box.Top != null) {
console.log(` Position: (${box.Left.toFixed(2)}, ${box.Top.toFixed(2)})`)
}
}
return detections
} catch (err) {
if (err.name === 'InvalidImageFormatException') {
console.error('Error: Invalid image format. Only JPEG and PNG are supported.')
} else if (err.name === 'ImageTooLargeException') {
console.error('Error: Image size exceeds the 5MB limit for raw bytes.')
} else if (err.name === 'ThrottlingException') {
console.error('Error: API request rate exceeded. Please retry with backoff.')
} else {
console.error('Error detecting text:', err.message)
}
throw err
}
}
// Usage example
processImage('path/to/your/image.jpg')
.then((results) => {
console.log(`Found ${results.length} text elements`)
})
.catch((err) => {
console.error('Failed to process image:', err.message)
process.exitCode = 1
})
AWS Rekognition service limits and pricing
When using AWS Rekognition for text detection, keep these key points in mind:
- Raw byte input is limited to 5MB; S3 objects are limited to 15MB.
- Supported image formats are JPEG and PNG.
- The service can detect up to 100 words per image.
- Pricing depends on the API, region, volume, and your account’s free-tier eligibility.
For more details, refer to the AWS Rekognition Pricing page and the service limits documentation.
Conclusion
AWS Rekognition offers powerful text detection capabilities that can be easily integrated into your Node.js applications. By following the steps above, you can set up your AWS credentials, install the AWS SDK, and implement robust error handling for both local and S3-based images.
Interested in automating your image analysis workflow further? Check out how Transloadit's Image Describe Robot leverages similar technology to help you process images at scale.
