-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathocr.js
More file actions
28 lines (25 loc) · 903 Bytes
/
Copy pathocr.js
File metadata and controls
28 lines (25 loc) · 903 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
const Tesseract = require('tesseract.js');
const fs = require('fs'); // File System module to write to files
// Path to the image you want to extract text from
const imagePath = './randomimage.png'; // Update with your image path
// Run Tesseract.js OCR
Tesseract.recognize(
imagePath,
'eng', // Language code ('eng' for English)
{
logger: () => {} // Suppress the progress log by providing an empty function
}
).then(({ data: { text } }) => {
// Define the path where you want to save the extracted text
const outputPath = './extracted_text.txt';
// Write the extracted text to a file in the root directory
fs.writeFile(outputPath, text, (err) => {
if (err) {
console.error('Error writing to file:', err);
} else {
console.log('Text successfully written to extracted_text.txt');
}
});
}).catch((error) => {
console.error('OCR Error:', error);
});