From c7c2d73d58fc258f3e2fb3ef24e50b7c939956ea Mon Sep 17 00:00:00 2001 From: Balearica Date: Thu, 13 Oct 2022 20:17:59 -0700 Subject: [PATCH] Added debug output option per #681 --- src/index.d.ts | 1 + src/worker-script/index.js | 17 ++++++++++++----- src/worker-script/utils/dump.js | 1 + 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/index.d.ts b/src/index.d.ts index a19e886..ed7567a 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -73,6 +73,7 @@ declare namespace Tesseract { imageColor: boolean; imageGrey: boolean; imageBinary: boolean; + debug: boolean; } interface RecognizeOptions { rectangle: Rectangle diff --git a/src/worker-script/index.js b/src/worker-script/index.js index 0d7d396..da7297a 100644 --- a/src/worker-script/index.js +++ b/src/worker-script/index.js @@ -281,11 +281,16 @@ const recognize = async ({ optionsTess[param] = options[param]; } } - if (Object.keys(optionsTess).length > 0) { - api.SaveParameters(); - for (const prop in optionsTess) { - api.SetVariable(prop, optionsTess[prop]); - } + } + if (output.debug) { + optionsTess["debug_file"] = "/debugInternal.txt"; + TessModule.FS.writeFile("/debugInternal.txt", ""); + } + // If any parameters are changed here they are changed back at the end + if (Object.keys(optionsTess).length > 0) { + api.SaveParameters(); + for (const prop in optionsTess) { + api.SetVariable(prop, optionsTess[prop]); } } @@ -345,6 +350,8 @@ const recognize = async ({ const result = dump(TessModule, api, workingOutput, {pdfTitle, pdfTextOnly}); result.rotateRadians = rotateRadiansFinal; + if (output.debug) TessModule.FS.unlink("/debugInternal.txt"); + if (Object.keys(optionsTess).length > 0) { api.RestoreParameters(); } diff --git a/src/worker-script/utils/dump.js b/src/worker-script/utils/dump.js index ce44f1c..e7f097b 100644 --- a/src/worker-script/utils/dump.js +++ b/src/worker-script/utils/dump.js @@ -219,5 +219,6 @@ module.exports = (TessModule, api, output, options) => { psm: enumToString(api.GetPageSegMode(), 'PSM'), oem: enumToString(api.oem(), 'OEM'), version: api.Version(), + debug: output.debug ? TessModule.FS.readFile('/debugInternal.txt', { encoding: 'utf8', flags:"a+" }) : null }; };