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",
"ssdeep.js": "0.0.3",
"stream-browserify": "^3.0.0",
"tesseract.js": "2.1.5",
"tesseract.js": "3.0.2",
"ua-parser-js": "^1.0.2",
"unorm": "^1.6.0",
"utf8": "^3.0.0",

View File

@ -6,90 +6,155 @@
* @license Apache-2.0
*/
import Operation from "../Operation.mjs";
import OperationError from "../errors/OperationError.mjs";
import { isImage } from "../lib/FileType.mjs";
import { toBase64 } from "../lib/Base64.mjs";
import { isWorkerEnvironment } from "../Utils.mjs";
import Tesseract from "tesseract.js";
const { createWorker } = Tesseract;
import process from "process";
/**
* Optical Character Recognition operation
*/
class OpticalCharacterRecognition extends Operation {
/**
* OpticalCharacterRecognition constructor
*/
constructor() {
super();
this.name = "Optical Character Recognition";
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.infoURL = "https://wikipedia.org/wiki/Optical_character_recognition";
this.inputType = "ArrayBuffer";
this.outputType = "string";
this.args = [
{
name: "Show confidence",
type: "boolean",
value: true
}
];
}
/**
* @param {ArrayBuffer} input
* @param {Object[]} args
* @returns {string}
*/
async run(input, args) {
const [showConfidence] = args;
if (!isWorkerEnvironment()) throw new OperationError("This operation only works in a browser");
const type = isImage(input);
if (!type) {
throw new OperationError("Invalid File Type");
}
const assetDir = isWorkerEnvironment() ? `${self.docURL}/assets/` : `${process.cwd()}/src/core/vendor/`;
try {
self.sendStatusMessage("Spinning up Tesseract worker...");
const image = `data:${type};base64,${toBase64(input)}`;
const worker = createWorker({
workerPath: `${assetDir}tesseract/worker.min.js`,
langPath: `${assetDir}tesseract/lang-data`,
corePath: `${assetDir}tesseract/tesseract-core.wasm.js`,
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 English language...");
await worker.loadLanguage("eng");
self.sendStatusMessage("Intialising Tesseract API...");
await worker.initialize("eng");
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;
import Operation from "../Operation.mjs";
import OperationError from "../errors/OperationError.mjs";
import { isImage } from "../lib/FileType.mjs";
import { toBase64 } from "../lib/Base64.mjs";
import { isWorkerEnvironment } from "../Utils.mjs";
import { createWorker } from "tesseract.js";
/**
* Optical Character Recognition operation
*/
class OpticalCharacterRecognition extends Operation {
/**
* OpticalCharacterRecognition constructor
*/
constructor() {
super();
this.name = "Optical Character Recognition";
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.infoURL = "https://wikipedia.org/wiki/Optical_character_recognition";
this.inputType = "ArrayBuffer";
this.outputType = "string";
this.args = [
{
name: "Show confidence",
type: "boolean",
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
}
];
}
/**
* @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;