process = function (image, options, callback) { if (typeof options === 'function') { callback = options; options = null; } options = utils.merge(Tesseract.options, options); // generate output file name var output = path.resolve(tmpdir, 'node-tesseract-' + uuid.v4()); // add the tmp file to the list Tesseract.tmpFiles.push(output); // assemble tesseract command var command = [options.binary, image, output]; if (options.l !== null) { command.push('-l ' + options.l); } if (options.psm !== null) { command.push('-psm ' + options.psm); } if (options.config !== null) { command.push(options.config); } command = command.join(' '); var opts = options.env || {}; // Run the tesseract command exec(command, opts, function(err) { if (err) { // Something went wrong executing the assembled command callback(err, null); return; } // Find one of the three possible extension glob(output + '.+(html|hocr|txt)', function(err, files){ if (err) { callback(err, null); return; } fs.readFile(files[0], Tesseract.outputEncoding, function(err, data) { if (err) { callback(err, null); return; } var index = Tesseract.tmpFiles.indexOf(output); if (~index) Tesseract.tmpFiles.splice(index, 1); fs.unlink(files[0]); callback(null, data) }); }) }); // end exec }
...
## Usage
```JavaScript
var tesseract = require('node-tesseract');
// Recognize text of any language in any format
tesseract.process(__dirname + '/path/to/image.jpg',function(err, text) {
if(err) {
console.error(err);
} else {
console.log(text);
}
});
...
process = function (image, options, callback) { if (typeof options === 'function') { callback = options; options = null; } options = utils.merge(Tesseract.options, options); // generate output file name var output = path.resolve(tmpdir, 'node-tesseract-' + uuid.v4()); // add the tmp file to the list Tesseract.tmpFiles.push(output); // assemble tesseract command var command = [options.binary, image, output]; if (options.l !== null) { command.push('-l ' + options.l); } if (options.psm !== null) { command.push('-psm ' + options.psm); } if (options.config !== null) { command.push(options.config); } command = command.join(' '); var opts = options.env || {}; // Run the tesseract command exec(command, opts, function(err) { if (err) { // Something went wrong executing the assembled command callback(err, null); return; } // Find one of the three possible extension glob(output + '.+(html|hocr|txt)', function(err, files){ if (err) { callback(err, null); return; } fs.readFile(files[0], Tesseract.outputEncoding, function(err, data) { if (err) { callback(err, null); return; } var index = Tesseract.tmpFiles.indexOf(output); if (~index) Tesseract.tmpFiles.splice(index, 1); fs.unlink(files[0]); callback(null, data) }); }) }); // end exec }
...
## Usage
```JavaScript
var tesseract = require('node-tesseract');
// Recognize text of any language in any format
tesseract.process(__dirname + '/path/to/image.jpg',function(err, text) {
if(err) {
console.error(err);
} else {
console.log(text);
}
});
...
merge = function (defaults, options) { defaults = defaults || {}; if (options && typeof options === 'object') { var i = 0, keys = Object.keys(options); for (i = 0; i < keys.length; i += 1) { if (options[keys[i]] !== undefined) { defaults[keys[i]] = options[keys[i]]; } } } return defaults; }
...
process: function(image, options, callback) {
if (typeof options === 'function') {
callback = options;
options = null;
}
options = utils.merge(Tesseract.options, options);
// generate output file name
var output = path.resolve(tmpdir, 'node-tesseract-' + uuid.v4());
// add the tmp file to the list
Tesseract.tmpFiles.push(output);
...