Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace temporary Files with tesseract stdin/stdout #21

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 40 additions & 80 deletions lib/tesseract.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,10 @@
* Module dependencies.
*/
var utils = require('./utils');
var exec = require('child_process').exec;
var fs = require('fs');
var tmpdir = require('os').tmpdir(); // let the os take care of removing zombie tmp files
var uuid = require('node-uuid');
var path = require('path');
var spawn = require('child_process').spawn;
var createReadStream = require('fs').createReadStream;

var Tesseract = {

tmpFiles: [],

/**
* options default options passed to Tesseract binary
* @type {Object}
Expand All @@ -26,108 +20,74 @@ var Tesseract = {
},

/**
* outputEncoding
* @type {String}
* Create Stream from path, then run processStream
*
* @param {String} image
* @param {Object} options to pass to Tesseract binary
* @param {Function} callback
* @returns {Stream}
*/
outputEncoding: 'UTF-8',
process: function(image, options, callback) {
return this.processStream(createReadStream(image), options, callback);
},



/**
* Runs Tesseract binary with options
*
* @param {String} image
* @param {Stream} image
* @param {Object} options to pass to Tesseract binary
* @param {Function} callback
* @returns {Stream}
*/
process: function(image, options, callback) {
processStream: 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];
var command = 'stdin stdout';

if (options.l !== null) {
command.push('-l ' + options.l);
command += ' -l ' + options.l;
}

if (options.psm !== null) {
command.push('-psm ' + options.psm);
command += ' -psm ' + options.psm;
}

if (options.config !== null) {
command.push(options.config);
command += ' ' + 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;
}

var outputFile = output + '.txt';
fs.readFile(outputFile, function(err, data) {
if (!err) {
// There was no error, so get the text
data = data.toString(Tesseract.outputEncoding);
}
fs.unlink(outputFile);
// remove the file from the tmpFiles array
var index = Tesseract.tmpFiles.indexOf(output);
if (~index) Tesseract.tmpFiles.splice(index, 1);

callback(err, data);
}); // end reaFile

}); // end exec

var bin = spawn(Tesseract.options.binary, command.split(' ')),
body = '',
errbody = '',
done = false;

image.pipe(bin.stdin);

if (callback) {
bin.stdout.on('data', function(chunk) {
body += chunk;
});
bin.stderr.on('data', function(chunk) {
errbody += chunk;
});
bin.on('exit', function() {
callback(errbody, body);
});
}
return bin.stdout;
}

};

function gc() {
for (var i = Tesseract.tmpFiles.length - 1; i >= 0; i--) {
try {
fs.unlinkSync(Tesseract.tmpFiles[i] + '.txt');
} catch (err) {}

var index = Tesseract.tmpFiles.indexOf(Tesseract.tmpFiles[i]);
if (~index) Tesseract.tmpFiles.splice(index, 1);
};
}

var version = process.versions.node.split('.').map(function(value) {
return parseInt(value, 10);
});

if (version[0] === 0 && (version[1] < 9 || version[1] === 9 && version[2] < 5)) {
process.addListener('uncaughtException', function _uncaughtExceptionThrown(err) {
gc();
throw err;
});
}

// clean up the tmp files
process.addListener('exit', function _exit(code) {
gc();
});

/**
* Module exports.
*/
module.exports.process = Tesseract.process;
module.exports.processStream = Tesseract.processStream;
14 changes: 14 additions & 0 deletions test/tesseract.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,20 @@ describe('process', function(){
done();
});

});

it('should return the string "node-tesseract" when run with options', function(done){

var testImage = __dirname + '/test.png';

tesseract.process(testImage, {
psm:3,
l:'eng'
}, function(err, text) {
text.trim().should.equal('node-tesseract');
done();
});

})
})