0.6.0 • Published 6 years ago
ocr-bulk v0.6.0
ocr-bulk
Perform OCR recognition on multiple images.
Note that this doesn't currently contain a normal functioning binary and needs adapting of source.
Installation
- Install tesseract (e.g., the Windows installer).
npm i -D ocr-bulk(for development use) ornpm i -P ocr-bulk(for production use)
For PDF conversion support:
- Follow the brew install instructions at https://github.com/aheckmann/gm#getting-started.
- I also needed
brew install ghostscript
Example
'use strict';
const {join} = require('path');
const ot = require('ocr-bulk');
(async () => {
await ot.writeFile({
start: 1,
end: 3,
outputPath: join(__dirname, 'ocr.txt'),
getImagePath (i) {
return `C:\\Users\\Brett\\Documents\\images\\image-${ot.pad(i, 4)}.jpg`;
},
concatenater (text, i) {
// eslint-disable-next-line no-console
console.log('Finishing ' + i);
return `\n\n{{page|${i}}}\n\n${text}`;
}
});
// eslint-disable-next-line no-console
console.log('Saved!');
})();Usage
Methods
writeFile({start: ..., end: ..., getImagePath: cb(i), readErrback: cb(err), outputPath: ..., concatenater: cb(text, i), tesseractOptions: {}, done: cb()})- This method will concatenate results based on the supplied arguments and output into a single file. Passed a single config object whosestartproperty indicates a number at which to begin reading images (defaults to1) andendis a required ending number.getImagePathis a required callback which is passed the current iteration number and should be used to return an image path.readErrbackis an optional errback which will be passed the file read error if one occurs.outputPathis a required string indicating the desired path to which to write the concatenated contents.concatenateris a callback which will be called with the OCR'd text and the current iteration count. It should return the value to be appended to any previously concatenated values (starting with an empty string).doneis an optional callback which will be called upon completion of appending OCR for all read files (those between the supplied start and end points); thePromisereturned fromwriteFilecan be used for the same purpose.readOCR({start: ..., end: ..., getImagePath: cb(i), readErrback: cb(err), processor: cb(text, i), tesseractOptions: {}, done: cb()})- Passed a single config object whosestartproperty indicates a number at which to begin reading images (defaults to1) andendis a required ending number.getImagePathis a required callback which is passed the current iteration number and should be used to return an image path.readErrbackis an optional errback which will be passed the file read error if one occurs.processoris a required callback argument which receives the current OCR'd text and the current iteration number as arguments.doneis an optional callback which will be called upon completion of reading all files between the supplied start and end points; thePromisereturned fromwriteFilecan be used for the same purpose.pad(val, len, chr)- A utility intended for use bygetImagePathto add padding in retrieval of predictable image path inputs. The requiredvalis the value to be padded, the requiredlenindicates the desired total length including padding, andchris an optional character to be used for padding (defaults to "0").
Todos
- Make as binary with flag to pass in (JavaScript) config file
- Add methods for zipping results and make a server with web app
- Could use tesseract.js for client-side conversion
- For imagemagick, could use http://manuels.github.io/unix-toolbox.js/.