Browse Source

Added interface for setting 'init only' options per #613

dev/v4
Balearica 2 years ago
parent
commit
b952488952
  1. 4
      src/createWorker.js
  2. 10
      src/index.d.ts
  3. 18
      src/worker-script/index.js

4
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({ startJob(createJob({
id: jobId, id: jobId,
action: 'initialize', action: 'initialize',
payload: { langs, oem }, payload: { langs, oem, config },
})) }))
); );

10
src/index.d.ts vendored

@ -20,7 +20,7 @@ declare namespace Tesseract {
removeText(path: string, jobId?: string): Promise<ConfigResult> removeText(path: string, jobId?: string): Promise<ConfigResult>
FS(method: string, args: any[], jobId?: string): Promise<ConfigResult> FS(method: string, args: any[], jobId?: string): Promise<ConfigResult>
loadLanguage(langs?: string, 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> setParameters(params: Partial<WorkerParams>, jobId?: string): Promise<ConfigResult>
getImage(type: imageType): string getImage(type: imageType): string
recognize(image: ImageLike, options?: Partial<RecognizeOptions>, output?: Partial<OutputFormats>, jobId?: string): Promise<RecognizeResult> recognize(image: ImageLike, options?: Partial<RecognizeOptions>, output?: Partial<OutputFormats>, jobId?: string): Promise<RecognizeResult>
@ -29,6 +29,14 @@ declare namespace Tesseract {
getPDF(title?: string, textonly?: boolean, jobId?: string):Promise<GetPDFResult> 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 { interface WorkerOptions {
corePath: string corePath: string
langPath: string langPath: string

18
src/worker-script/index.js

@ -176,7 +176,7 @@ const setParameters = async ({ payload: { params: _params } }, res) => {
const initialize = async ({ const initialize = async ({
workerId, workerId,
payload: { langs: _langs, oem }, payload: { langs: _langs, oem, config},
}, res) => { }, res) => {
const langs = (typeof _langs === 'string') const langs = (typeof _langs === 'string')
? _langs ? _langs
@ -189,8 +189,22 @@ const initialize = async ({
if (api !== null) { if (api !== null) {
api.End(); 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 = new TessModule.TessBaseAPI();
api.Init(null, langs, oem); api.Init(null, langs, oem, configFile);
params = defaultParams; params = defaultParams;
await setParameters({ payload: { params } }); await setParameters({ payload: { params } });
res.progress({ res.progress({

Loading…
Cancel
Save