-
Notifications
You must be signed in to change notification settings - Fork 1
/
OCR-and-bbox-export.js
71 lines (54 loc) · 2.27 KB
/
OCR-and-bbox-export.js
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
const { createWorker, createScheduler } = require('tesseract.js');
const path = require('path');
const fs = require('fs').promises;
async function processImages() {
const folderPath = path.resolve(__dirname, './input_images');
try {
// Read image files from the specified folder
console.log('Reading files from the specified folder:', folderPath);
const files = await fs.readdir(folderPath);
if (files.length === 0) {
throw new Error('No image files found in the specified folder.');
}
console.log('Number of image files found:', files.length);
const imageArr = files.map(file => path.join(folderPath, file));
// Create a scheduler and workers
const scheduler = createScheduler();
const workerGen = async () => {
const worker = await createWorker("hin", 1, { logger: m => console.log(m), cachePath: "." });
scheduler.addWorker(worker);
};
const workerN = 7; //Adjustable worker pool for a huge batch
await Promise.all(Array(workerN).fill(0).map(async () => await workerGen())); // Create workers concurrently
// Process images in parallel
console.log('Processing images and performing OCR:');
const jobPromises = imageArr.map(async (imagePath) => {
console.log(`Scheduling image processing for: ${imagePath}`);
return scheduler.addJob('recognize', imagePath)
.then(out => ({
imageName: path.basename(imagePath),
words: out.data.words.map(word => ({
text: word.text,
confidence: word.confidence.toFixed(2),
bbox: word.bbox,
})),
}))
.catch(error => ({
imageName: path.basename(imagePath),
error: error.message,
}));
});
const results = await Promise.all(jobPromises);
// Terminate workers and save results
await scheduler.terminate();
console.log('OCR processing completed.');
const jsonFilePath = path.resolve(__dirname, 'ocr_results.json');
console.log('Exporting OCR results to JSON file:', jsonFilePath);
await fs.writeFile(jsonFilePath, JSON.stringify(results, null, 2));
console.log('OCR results saved to:', jsonFilePath);
} catch (error) {
console.error('Error:', error.message);
}
}
// saves json file to input_images dir
processImages();