From 98ecdbce3ccc71df4de4f646ae016ef772b458fc Mon Sep 17 00:00:00 2001 From: Steven Zhao Date: Sun, 23 Oct 2016 14:56:40 +0800 Subject: [PATCH] Support psm 0 --- lib/tesseract.js | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/lib/tesseract.js b/lib/tesseract.js index db9729a..3f0a541 100644 --- a/lib/tesseract.js +++ b/lib/tesseract.js @@ -56,6 +56,9 @@ var Tesseract = { // assemble tesseract command var command = [options.binary, image, output]; + if (options.psm == 0) { + command = [options.binary, image, 'stdout']; + } if (options.l !== null) { command.push('-l ' + options.l); @@ -74,7 +77,7 @@ var Tesseract = { var opts = options.env || {}; // Run the tesseract command - exec(command, opts, function(err) { + var childProcess = exec(command, opts, function(err) { if (err) { // Something went wrong executing the assembled command callback(err, null); @@ -98,15 +101,50 @@ var Tesseract = { fs.unlink(files[0]); + if (options.psm == 0) { + data = parseOsdOutput(data); + } callback(null, data) }); }) }); // end exec + if (options.psm == 0) { + childProcess.stderr.pipe(fs.createWriteStream(output + '.txt')); + } + } }; +function parseOsdOutput(string) { + return string + .split('\n') + .reduce(function(obj, line) { + var result; + + result = /Orientation in degrees: (\d+)/.exec(line); + if (result) { + obj.orientation = parseInt(result[1]); + return obj; + } + + result = /Orientation confidence: (\d+(\.\d+))/.exec(line); + if (result) { + obj.orientationConfidence = parseFloat(result[1]); + return obj; + } + + result = /Script confidence: (\d+(\.\d+))/.exec(line); + if (result) { + obj.scriptConfidence = parseFloat(result[1]); + return obj; + } + + return obj; + }, {}); +} + function gc() { for (var i = Tesseract.tmpFiles.length - 1; i >= 0; i--) { try {