Skip to content

Commit

Permalink
Added interface for setting 'init only' options per #613
Browse files Browse the repository at this point in the history
  • Loading branch information
Balearica committed Sep 24, 2022
1 parent c0298ff commit b952488
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/createWorker.js
Expand Up @@ -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 },
}))
);

Expand Down
10 changes: 9 additions & 1 deletion src/index.d.ts
Expand Up @@ -20,7 +20,7 @@ declare namespace Tesseract {
removeText(path: string, jobId?: string): Promise<ConfigResult>
FS(method: string, args: any[], jobId?: string): Promise<ConfigResult>
loadLanguage(langs?: string, jobId?: string): Promise<ConfigResult>
initialize(langs?: string, oem?: OEM, jobId?: string): Promise<ConfigResult>
initialize(langs?: string, oem?: OEM, config?: string | Partial<InitOptions>, jobId?: string): Promise<ConfigResult>
setParameters(params: Partial<WorkerParams>, jobId?: string): Promise<ConfigResult>
getImage(type: imageType): string
recognize(image: ImageLike, options?: Partial<RecognizeOptions>, output?: Partial<OutputFormats>, jobId?: string): Promise<RecognizeResult>
Expand All @@ -29,6 +29,14 @@ declare namespace Tesseract {
getPDF(title?: string, textonly?: boolean, jobId?: string):Promise<GetPDFResult>
}

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
Expand Down
18 changes: 16 additions & 2 deletions src/worker-script/index.js
Expand Up @@ -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
Expand All @@ -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({
Expand Down

0 comments on commit b952488

Please sign in to comment.