OCR operation now relies on local files

This commit is contained in:
n1474335 2019-09-13 17:40:20 +01:00
parent 3539e065fa
commit 70346bce35
8 changed files with 259 additions and 3 deletions

View file

@ -14,6 +14,8 @@ import { isWorkerEnvironment } from "../Utils.mjs";
import Tesseract from "tesseract.js";
const { TesseractWorker } = Tesseract;
import process from "process";
/**
* Optical Character Recognition operation
*/
@ -26,7 +28,7 @@ class OpticalCharacterRecognition extends Operation {
super();
this.name = "Optical Character Recognition";
this.module = "Image";
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";
@ -48,14 +50,22 @@ class OpticalCharacterRecognition extends Operation {
async run(input, args) {
const [showConfidence] = args;
if (!isWorkerEnvironment()) throw 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 {
const image = `data:${type};base64,${toBase64(input)}`;
const worker = new TesseractWorker();
const worker = new TesseractWorker({
workerPath: `${assetDir}/tesseract/worker.min.js`,
langPath: `${assetDir}/tesseract/lang-data/`,
corePath: `${assetDir}/tesseract/tesseract-core.wasm.js`,
});
const result = await worker.recognize(image)
.progress(progress => {
if (isWorkerEnvironment()) {

Binary file not shown.

File diff suppressed because one or more lines are too long

17
src/core/vendor/tesseract/worker.min.js vendored Normal file

File diff suppressed because one or more lines are too long