From b952488952a6cfa880d9df6665bca63c47c25123 Mon Sep 17 00:00:00 2001 From: Balearica Date: Fri, 23 Sep 2022 17:43:06 -0700 Subject: [PATCH] Added interface for setting 'init only' options per #613 --- src/createWorker.js | 4 ++-- src/index.d.ts | 10 +++++++++- src/worker-script/index.js | 18 ++++++++++++++++-- 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/src/createWorker.js b/src/createWorker.js index 2f04fe8..d5d4664 100644 --- a/src/createWorker.js +++ b/src/createWorker.js @@ -113,11 +113,11 @@ module.exports = async (_options = {}) => { })) ); - const initialize = (langs = 'eng', oem = defaultOEM, jobId) => ( + const initialize = (langs = 'eng', oem = defaultOEM, config, jobId) => ( startJob(createJob({ id: jobId, action: 'initialize', - payload: { langs, oem }, + payload: { langs, oem, config }, })) ); diff --git a/src/index.d.ts b/src/index.d.ts index db8fb22..a19e886 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -20,7 +20,7 @@ declare namespace Tesseract { removeText(path: string, jobId?: string): Promise FS(method: string, args: any[], jobId?: string): Promise loadLanguage(langs?: string, jobId?: string): Promise - initialize(langs?: string, oem?: OEM, jobId?: string): Promise + initialize(langs?: string, oem?: OEM, config?: string | Partial, jobId?: string): Promise setParameters(params: Partial, jobId?: string): Promise getImage(type: imageType): string recognize(image: ImageLike, options?: Partial, output?: Partial, jobId?: string): Promise @@ -29,6 +29,14 @@ declare namespace Tesseract { getPDF(title?: string, textonly?: boolean, jobId?: string):Promise } + interface InitOptions { + load_system_dawg: string + load_freq_dawg: string + load_unambig_dawg: string + load_punc_dawg: string + load_number_dawg: string + load_bigram_dawg: string + } interface WorkerOptions { corePath: string langPath: string diff --git a/src/worker-script/index.js b/src/worker-script/index.js index 1153a06..fc5e6e8 100644 --- a/src/worker-script/index.js +++ b/src/worker-script/index.js @@ -176,7 +176,7 @@ const setParameters = async ({ payload: { params: _params } }, res) => { const initialize = async ({ workerId, - payload: { langs: _langs, oem }, + payload: { langs: _langs, oem, config}, }, res) => { const langs = (typeof _langs === 'string') ? _langs @@ -189,8 +189,22 @@ const initialize = async ({ if (api !== null) { api.End(); } + let configFile = undefined; + let configStr = undefined; + // config argument may either be config file text, or object with key/value pairs + // In the latter case we convert to config file text here + if (typeof config === "object") { + configStr = JSON.stringify(config).replace(/,/g, "\n").replace(/:/g, " ").replace(/["'{}]/g, ""); + } else { + configStr = config; + } + if (typeof configStr === "string") { + configFile = "/config"; + TessModule.FS.writeFile(configFile, configStr); + } + api = new TessModule.TessBaseAPI(); - api.Init(null, langs, oem); + api.Init(null, langs, oem, configFile); params = defaultParams; await setParameters({ payload: { params } }); res.progress({