Skip to content
Permalink
Browse files Browse the repository at this point in the history
exec() to execFile()
  • Loading branch information
rona-dinihari committed Mar 6, 2023
1 parent f72e97b commit 81d1664
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 30 deletions.
30 changes: 16 additions & 14 deletions lib/tesseract.js
Expand Up @@ -4,12 +4,12 @@
* Module dependencies.
*/
var utils = require('./utils');
var exec = require('child_process').exec;
var execFile = require('child_process').execFile;
var fs = require('fs');
var tmpdir = require('os').tmpdir(); // let the os take care of removing zombie tmp files
var uuid = require('uuid');
var path = require('path');
var { glob, } = require("glob");
var { globSync, } = require("glob");

var Tesseract = {

Expand Down Expand Up @@ -51,7 +51,7 @@ var Tesseract = {
callback(Error('Invalid image path'));
return;
}

var defaultOptions = utils.merge({}, Tesseract.options);
options = utils.merge(defaultOptions, options);

Expand All @@ -65,23 +65,23 @@ var Tesseract = {
var command = [options.binary, image, output];

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

if (options.psm !== null) {
command.push('--psm ' + options.psm);
command.push('--psm')
command.push(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, stdout, stderr) {
execFile(command[0], command.slice(1), opts, function(err, stdout, stderr) {
if (err) {
// Something went wrong executing the assembled command
callback(err, null);
Expand All @@ -98,11 +98,9 @@ var Tesseract = {
}

// Find one of the three possible extension
glob(output + '.+(html|hocr|txt)', function(err, files){
if (err) {
callback(err, null);
return;
}
try {
const files = globSync(output + '.{html,hocr,txt}');
const readFiles = function(files) {
fs.readFile(files[0], Tesseract.outputEncoding, function(err, data) {
if (err) {
callback(err, null);
Expand All @@ -116,7 +114,11 @@ var Tesseract = {

callback(null, data, extra);
});
})
};
readFiles(files);
} catch (err) {
callback(err, null);
}
}); // end exec

}
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
@@ -1,12 +1,12 @@
{
"name": "dawnsparks-node-tesseract",
"version": "0.4.0",
"version": "0.4.1",
"author": "Rona Dini Hari <hari.ronadini@gmail.com>",
"description": "A fork of a simple wrapper for the Tesseract OCR package",
"main": "index.js",
"repository": {
"type": "git",
"url": "https://rona_dinihari@bitbucket.org/dawnsparks/dawnsparks-node-tesseract.git"
"url": "https://github.com/rona-dinihari/dawnsparks-node-tesseract.git"
},
"keywords": [
"tesseract",
Expand Down
24 changes: 14 additions & 10 deletions test/tesseract.js
Expand Up @@ -9,28 +9,32 @@ const fs = require('fs');
describe('process', function(){
it('should return the string "node-tesseract"', function(done){

var testImage = "\"" + __dirname + '/test.png' + "\"";
var testImage = path.join(__dirname, 'test with spaces.png');

tesseract.process(testImage, function(err, text) {
text.trim().should.equal('node-tesseract');
done();
if (err) {
done(err);
} else {
text.trim().should.equal('node-tesseract');
done();
}
});

done();
})
})

describe('orientation', function(){
it('returns orientation equal to 0 using psm 1', function(done){

var testImage = "\"" + __dirname + '/test.png' + "\"";
var testImage = path.join(__dirname, 'test with spaces.png');

tesseract.process(testImage, { psm: 1 }, function(err, text, extra) {
extra.orientation.should.equal(0);
done();
if (err) {
done(err);
} else {
extra.orientation.should.equal(0);
done();
}
});

done();
})
})

Expand Down
File renamed without changes

0 comments on commit 81d1664

Please sign in to comment.