Update tesseract, fix API for tesseract 3.0

This commit is contained in:
Sean Marpo 2022-09-09 14:33:49 -07:00
parent 1b0ced9f9b
commit 1c8e37cb64
3 changed files with 305 additions and 419 deletions

483
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -154,7 +154,7 @@
"split.js": "^1.6.5", "split.js": "^1.6.5",
"ssdeep.js": "0.0.3", "ssdeep.js": "0.0.3",
"stream-browserify": "^3.0.0", "stream-browserify": "^3.0.0",
"tesseract.js": "2.1.5", "tesseract.js": "3.0.2",
"ua-parser-js": "^1.0.2", "ua-parser-js": "^1.0.2",
"unorm": "^1.6.0", "unorm": "^1.6.0",
"utf8": "^3.0.0", "utf8": "^3.0.0",

View File

@ -6,90 +6,155 @@
* @license Apache-2.0 * @license Apache-2.0
*/ */
import Operation from "../Operation.mjs"; import Operation from "../Operation.mjs";
import OperationError from "../errors/OperationError.mjs"; import OperationError from "../errors/OperationError.mjs";
import { isImage } from "../lib/FileType.mjs"; 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 Tesseract from "tesseract.js"; import { createWorker } from "tesseract.js";
const { createWorker } = Tesseract;
/**
import process from "process"; * Optical Character Recognition operation
*/
/** class OpticalCharacterRecognition extends Operation {
* Optical Character Recognition operation
*/ /**
class OpticalCharacterRecognition extends Operation { * OpticalCharacterRecognition constructor
*/
/** constructor() {
* OpticalCharacterRecognition constructor super();
*/
constructor() { this.name = "Optical Character Recognition";
super(); this.module = "OCR";
this.description = "Optical character recognition or optical character reader (OCR) is the mechanical or electronic conversion of images of typed, handwritten or printed text into machine-encoded text.<br><br>Supported image formats: png, jpg, bmp, pbm.";
this.name = "Optical Character Recognition"; this.infoURL = "https://wikipedia.org/wiki/Optical_character_recognition";
this.module = "OCR"; this.inputType = "ArrayBuffer";
this.description = "Optical character recognition or optical character reader (OCR) is the mechanical or electronic conversion of images of typed, handwritten or printed text into machine-encoded text.<br><br>Supported image formats: png, jpg, bmp, pbm."; this.outputType = "string";
this.infoURL = "https://wikipedia.org/wiki/Optical_character_recognition"; this.args = [
this.inputType = "ArrayBuffer"; {
this.outputType = "string"; name: "Show confidence",
this.args = [ type: "boolean",
{ value: true
name: "Show confidence", },
type: "boolean", {
value: true name: "Language",
} type: "editableOptionShort",
]; value: [
} { name: "Afrikaans", value: "afr" },
{ name: "Arabic", value: "ara" },
/** { name: "Azerbaijani", value: "aze" },
* @param {ArrayBuffer} input { name: "Belarusian", value: "bel" },
* @param {Object[]} args { name: "Bengali", value: "ben" },
* @returns {string} { name: "Bulgarian", value: "bul" },
*/ { name: "Catalan", value: "cat" },
async run(input, args) { { name: "Czech", value: "ces" },
const [showConfidence] = args; { name: "Chinese", value: "chi_sim" },
{ name: "Traditional Chinese", value: "chi_tra" },
if (!isWorkerEnvironment()) throw new OperationError("This operation only works in a browser"); { name: "Cherokee", value: "chr" },
{ name: "Danish", value: "dan" },
const type = isImage(input); { name: "German", value: "deu" },
if (!type) { { name: "Greek", value: "ell" },
throw new OperationError("Invalid File Type"); { name: "English", value: "eng" },
} { name: "English (Old)", value: "enm" },
{ name: "Internet Meme", value: "meme" },
const assetDir = isWorkerEnvironment() ? `${self.docURL}/assets/` : `${process.cwd()}/src/core/vendor/`; { name: "Esperanto", value: "epo" },
{ name: "Esperanto alternative", value: "epo_alt" },
try { { name: "Estonian", value: "est" },
self.sendStatusMessage("Spinning up Tesseract worker..."); { name: "Basque", value: "eus" },
const image = `data:${type};base64,${toBase64(input)}`; { name: "Finnish", value: "fin" },
const worker = createWorker({ { name: "French", value: "fra" },
workerPath: `${assetDir}tesseract/worker.min.js`, { name: "Frankish", value: "frk" },
langPath: `${assetDir}tesseract/lang-data`, { name: "French (Old)", value: "frm" },
corePath: `${assetDir}tesseract/tesseract-core.wasm.js`, { name: "Galician", value: "glg" },
logger: progress => { { name: "Ancient Greek", value: "grc" },
if (isWorkerEnvironment()) { { name: "Hebrew", value: "heb" },
self.sendStatusMessage(`Status: ${progress.status}${progress.status === "recognizing text" ? ` - ${(parseFloat(progress.progress)*100).toFixed(2)}%`: "" }`); { name: "Hindi", value: "hin" },
} { name: "Croatian", value: "hrv" },
} { name: "Hungarian", value: "hun" },
}); { name: "Indonesian", value: "ind" },
await worker.load(); { name: "Icelandic", value: "isl" },
self.sendStatusMessage("Loading English language..."); { name: "Italian", value: "ita" },
await worker.loadLanguage("eng"); { name: "Italian (Old)", value: "ita_old" },
self.sendStatusMessage("Intialising Tesseract API..."); { name: "Japanese", value: "jpn" },
await worker.initialize("eng"); { name: "Kannada", value: "kan" },
self.sendStatusMessage("Finding text..."); { name: "Korean", value: "kor" },
const result = await worker.recognize(image); { name: "Latvian", value: "lav" },
{ name: "Lithuanian", value: "lit" },
if (showConfidence) { { name: "Malayalam", value: "mal" },
return `Confidence: ${result.data.confidence}%\n\n${result.data.text}`; { name: "Macedonian", value: "mkd" },
} else { { name: "Maltese", value: "mlt" },
return result.data.text; { name: "Malay", value: "msa" },
} { name: "Dutch", value: "nld" },
} catch (err) { { name: "Norwegian", value: "nor" },
throw new OperationError(`Error performing OCR on image. (${err})`); { name: "Polish", value: "pol" },
} { name: "Portuguese", value: "por" },
} { name: "Romanian", value: "ron" },
} { name: "Russian", value: "rus" },
{ name: "Slovakian", value: "slk" },
export default OpticalCharacterRecognition; { 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
}
];
}
/**
* @param {ArrayBuffer} input
* @param {Object[]} args
* @returns {string}
*/
async run(input, args) {
const [showConfidence, language] = args;
if (!isWorkerEnvironment()) throw new OperationError("This operation only works in a browser");
const type = isImage(input);
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.");
}
try {
self.sendStatusMessage("Spinning up Tesseract worker...");
const image = `data:${type};base64,${toBase64(input)}`;
const worker = createWorker({
logger: progress => {
if (isWorkerEnvironment()) {
self.sendStatusMessage(`Status: ${progress.status}${progress.status === "recognizing text" ? ` - ${(parseFloat(progress.progress)*100).toFixed(2)}%`: "" }`);
}
}
});
await worker.load();
self.sendStatusMessage(`Loading ${language} language pack...`);
await worker.loadLanguage(language);
self.sendStatusMessage("Intialising Tesseract API...");
await worker.initialize(language);
self.sendStatusMessage("Finding text...");
const result = await worker.recognize(image);
if (showConfidence) {
return `Confidence: ${result.data.confidence}%\n\n${result.data.text}`;
} else {
return result.data.text;
}
} catch (err) {
throw new OperationError(`Error performing OCR on image. (${err})`);
}
}
}
export default OpticalCharacterRecognition;