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

Add a function to enable tesseract stdin and stdout argument parameters #31

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
39 changes: 39 additions & 0 deletions lib/tesseract.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ var tmpdir = require('os').tmpdir(); // let the os take care of removing zombie
var uuid = require('node-uuid');
var path = require('path');
var glob = require("glob");
var spawn = require('child_process').spawn;


var Tesseract = {

Expand All @@ -32,6 +34,42 @@ var Tesseract = {
*/
outputEncoding: 'UTF-8',

/**
* Runs Tesseract binary with options
*
* @param {String} stream
* @param {Object} options to pass to Tesseract binary
* @param {Function} callback
*/
processStream: function(stream, options, callback) {
if (typeof options === 'function') {
callback = options;
options = null;
}

options = utils.merge(Tesseract.options, options);

var tesseract = spawn('tesseract', ['stdin', 'stdout', '-l', options.l, '-psm', options.psm])
stream.pipe(tesseract.stdin);

var data = '';
tesseract.stdout.on('data', function(chunk) {
data += chunk;
});
tesseract.stderr.on('data', function(chunk) {
data += chunk;
});
tesseract.on('exit', function(chunk) {
callback(null, data);
});
tesseract.on('error', function(chunk) {
if (tesseract) {
tesseract.kill();
}
callback('An error has occured', null);
});
},

/**
* Runs Tesseract binary with options
*
Expand Down Expand Up @@ -138,3 +176,4 @@ process.addListener('exit', function _exit(code) {
* Module exports.
*/
module.exports.process = Tesseract.process;
module.exports.processStream = Tesseract.processStream;
17 changes: 15 additions & 2 deletions test/tesseract.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

var tesseract = require('../lib/tesseract');
var should = require('should');
var fs = require('fs');


describe('process', function(){
Expand All @@ -14,6 +15,18 @@ describe('process', function(){
done();
});

})
})
});

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

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

tesseract.processStream(fs.createReadStream(testImage), function(err, text) {
text.trim().should.equal('node-tesseract');
done();
});

});

});