Reverted to local copies of Tesseract trainddata in order to remain self-contained.

This commit is contained in:
n1474335 2022-09-16 14:15:54 +01:00
parent c8eacb9942
commit cdb197a9c3

View File

@ -12,6 +12,7 @@ import { isImage } from "../lib/FileType.mjs";
import { toBase64 } from "../lib/Base64.mjs"; import { toBase64 } from "../lib/Base64.mjs";
import { isWorkerEnvironment } from "../Utils.mjs"; import { isWorkerEnvironment } from "../Utils.mjs";
import process from "process";
import { createWorker } from "tesseract.js"; import { createWorker } from "tesseract.js";
/** /**
@ -36,78 +37,6 @@ class OpticalCharacterRecognition extends Operation {
name: "Show confidence", name: "Show confidence",
type: "boolean", type: "boolean",
value: true value: true
},
{
name: "Language",
type: "editableOptionShort",
value: [
{ name: "Afrikaans", value: "afr" },
{ name: "Arabic", value: "ara" },
{ name: "Azerbaijani", value: "aze" },
{ name: "Belarusian", value: "bel" },
{ name: "Bengali", value: "ben" },
{ name: "Bulgarian", value: "bul" },
{ name: "Catalan", value: "cat" },
{ name: "Czech", value: "ces" },
{ name: "Chinese", value: "chi_sim" },
{ name: "Traditional Chinese", value: "chi_tra" },
{ name: "Cherokee", value: "chr" },
{ name: "Danish", value: "dan" },
{ name: "German", value: "deu" },
{ name: "Greek", value: "ell" },
{ name: "English", value: "eng" },
{ name: "English (Old)", value: "enm" },
{ name: "Internet Meme", value: "meme" },
{ name: "Esperanto", value: "epo" },
{ name: "Esperanto alternative", value: "epo_alt" },
{ name: "Estonian", value: "est" },
{ name: "Basque", value: "eus" },
{ name: "Finnish", value: "fin" },
{ name: "French", value: "fra" },
{ name: "Frankish", value: "frk" },
{ name: "French (Old)", value: "frm" },
{ name: "Galician", value: "glg" },
{ name: "Ancient Greek", value: "grc" },
{ name: "Hebrew", value: "heb" },
{ name: "Hindi", value: "hin" },
{ name: "Croatian", value: "hrv" },
{ name: "Hungarian", value: "hun" },
{ name: "Indonesian", value: "ind" },
{ name: "Icelandic", value: "isl" },
{ name: "Italian", value: "ita" },
{ name: "Italian (Old)", value: "ita_old" },
{ name: "Japanese", value: "jpn" },
{ name: "Kannada", value: "kan" },
{ name: "Korean", value: "kor" },
{ name: "Latvian", value: "lav" },
{ name: "Lithuanian", value: "lit" },
{ name: "Malayalam", value: "mal" },
{ name: "Macedonian", value: "mkd" },
{ name: "Maltese", value: "mlt" },
{ name: "Malay", value: "msa" },
{ name: "Dutch", value: "nld" },
{ name: "Norwegian", value: "nor" },
{ name: "Polish", value: "pol" },
{ name: "Portuguese", value: "por" },
{ name: "Romanian", value: "ron" },
{ name: "Russian", value: "rus" },
{ name: "Slovakian", value: "slk" },
{ name: "Slovenian", value: "slv" },
{ name: "Spanish", value: "spa" },
{ name: "Old Spanish", value: "spa_old" },
{ name: "Albanian", value: "sqi" },
{ name: "Serbian (Latin)", value: "srp" },
{ name: "Swahili", value: "swa" },
{ name: "Swedish", value: "swe" },
{ name: "Tamil", value: "tam" },
{ name: "Telugu", value: "tel" },
{ name: "Tagalog", value: "tgl" },
{ name: "Thai", value: "tha" },
{ name: "Turkish", value: "tur" },
{ name: "Ukrainian", value: "ukr" },
{ name: "Vietnamese", value: "vie" },
],
defaultIndex: 14
} }
]; ];
} }
@ -118,19 +47,24 @@ class OpticalCharacterRecognition extends Operation {
* @returns {string} * @returns {string}
*/ */
async run(input, args) { async run(input, args) {
const [showConfidence, language] = args; const [showConfidence] = args;
if (!isWorkerEnvironment()) throw new OperationError("This operation only works in a browser"); if (!isWorkerEnvironment()) throw new OperationError("This operation only works in a browser");
const type = isImage(input); const type = isImage(input);
if (!type) { if (!type) {
throw new OperationError("Unsupported file type (supported: jpg,png,pbm,bmp) or no file provided\nOpen an image as input using the toolbar."); throw new OperationError("Unsupported file type (supported: jpg,png,pbm,bmp) or no file provided");
} }
const assetDir = isWorkerEnvironment() ? `${self.docURL}/assets/` : `${process.cwd()}/src/core/vendor/`;
try { try {
self.sendStatusMessage("Spinning up Tesseract worker..."); self.sendStatusMessage("Spinning up Tesseract worker...");
const image = `data:${type};base64,${toBase64(input)}`; const image = `data:${type};base64,${toBase64(input)}`;
const worker = createWorker({ const worker = createWorker({
workerPath: `${assetDir}tesseract/worker.min.js`,
langPath: `${assetDir}tesseract/lang-data`,
corePath: `${assetDir}tesseract/tesseract-core.wasm.js`,
logger: progress => { logger: progress => {
if (isWorkerEnvironment()) { if (isWorkerEnvironment()) {
self.sendStatusMessage(`Status: ${progress.status}${progress.status === "recognizing text" ? ` - ${(parseFloat(progress.progress)*100).toFixed(2)}%`: "" }`); self.sendStatusMessage(`Status: ${progress.status}${progress.status === "recognizing text" ? ` - ${(parseFloat(progress.progress)*100).toFixed(2)}%`: "" }`);
@ -138,10 +72,10 @@ class OpticalCharacterRecognition extends Operation {
} }
}); });
await worker.load(); await worker.load();
self.sendStatusMessage(`Loading ${language} language pack...`); self.sendStatusMessage(`Loading English language pack...`);
await worker.loadLanguage(language); await worker.loadLanguage("eng");
self.sendStatusMessage("Intialising Tesseract API..."); self.sendStatusMessage("Intialising Tesseract API...");
await worker.initialize(language); await worker.initialize("eng");
self.sendStatusMessage("Finding text..."); self.sendStatusMessage("Finding text...");
const result = await worker.recognize(image); const result = await worker.recognize(image);