|
@@ -1,4 +1,5 @@
|
|
/**
|
|
/**
|
|
|
|
+ * @author n1474335 [n1474335@gmail.com]
|
|
* @author mshwed [m@ttshwed.com]
|
|
* @author mshwed [m@ttshwed.com]
|
|
* @copyright Crown Copyright 2019
|
|
* @copyright Crown Copyright 2019
|
|
* @license Apache-2.0
|
|
* @license Apache-2.0
|
|
@@ -7,30 +8,36 @@
|
|
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 { isWorkerEnvironment } from "../Utils.mjs";
|
|
import { isWorkerEnvironment } from "../Utils.mjs";
|
|
|
|
|
|
-import jimp from "jimp";
|
|
|
|
import Tesseract from "tesseract.js";
|
|
import Tesseract from "tesseract.js";
|
|
const { TesseractWorker } = Tesseract;
|
|
const { TesseractWorker } = Tesseract;
|
|
|
|
|
|
/**
|
|
/**
|
|
- * OCR operation
|
|
|
|
|
|
+ * Optical Character Recognition operation
|
|
*/
|
|
*/
|
|
-class OCR extends Operation {
|
|
|
|
|
|
+class OpticalCharacterRecognition extends Operation {
|
|
|
|
|
|
/**
|
|
/**
|
|
- * OCR constructor
|
|
|
|
|
|
+ * OpticalCharacterRecognition constructor
|
|
*/
|
|
*/
|
|
constructor() {
|
|
constructor() {
|
|
super();
|
|
super();
|
|
|
|
|
|
- this.name = "OCR";
|
|
|
|
- this.module = "Default";
|
|
|
|
- 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.";
|
|
|
|
- this.infoURL = "https://en.wikipedia.org/wiki/Optical_character_recognition";
|
|
|
|
|
|
+ this.name = "Optical Character Recognition";
|
|
|
|
+ this.module = "Image";
|
|
|
|
+ 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.inputType = "ArrayBuffer";
|
|
this.outputType = "string";
|
|
this.outputType = "string";
|
|
- this.args = [];
|
|
|
|
|
|
+ this.args = [
|
|
|
|
+ {
|
|
|
|
+ name: "Show confidence",
|
|
|
|
+ type: "boolean",
|
|
|
|
+ value: true
|
|
|
|
+ }
|
|
|
|
+ ];
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -39,34 +46,32 @@ class OCR extends Operation {
|
|
* @returns {string}
|
|
* @returns {string}
|
|
*/
|
|
*/
|
|
async run(input, args) {
|
|
async run(input, args) {
|
|
- if (!isImage(input)) {
|
|
|
|
|
|
+ const [showConfidence] = args;
|
|
|
|
+
|
|
|
|
+ const type = isImage(input);
|
|
|
|
+ if (!type) {
|
|
throw new OperationError("Invalid File Type");
|
|
throw new OperationError("Invalid File Type");
|
|
}
|
|
}
|
|
|
|
|
|
try {
|
|
try {
|
|
- if (isWorkerEnvironment())
|
|
|
|
- self.sendStatusMessage("Performing OCR on image...");
|
|
|
|
-
|
|
|
|
- let image;
|
|
|
|
- try {
|
|
|
|
- image = await jimp.read(input);
|
|
|
|
- image = await image.getBase64Async(jimp.AUTO);
|
|
|
|
- } catch (err) {
|
|
|
|
- throw new OperationError(`Error loading image. (${err})`);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
|
|
+ const image = `data:${type};base64,${toBase64(input)}`;
|
|
const worker = new TesseractWorker();
|
|
const worker = new TesseractWorker();
|
|
-
|
|
|
|
const result = await worker.recognize(image)
|
|
const result = await worker.recognize(image)
|
|
.progress(progress => {
|
|
.progress(progress => {
|
|
- if (isWorkerEnvironment()) self.sendStatusMessage(`${progress.status} - ${(parseFloat(progress.progress)*100).toFixed(2)}%`);
|
|
|
|
|
|
+ if (isWorkerEnvironment()) {
|
|
|
|
+ self.sendStatusMessage(`Status: ${progress.status} - ${(parseFloat(progress.progress)*100).toFixed(2)}%`);
|
|
|
|
+ }
|
|
});
|
|
});
|
|
|
|
|
|
- return result.text;
|
|
|
|
|
|
+ if (showConfidence) {
|
|
|
|
+ return `Confidence: ${result.confidence}%\n\n${result.text}`;
|
|
|
|
+ } else {
|
|
|
|
+ return result.text;
|
|
|
|
+ }
|
|
} catch (err) {
|
|
} catch (err) {
|
|
throw new OperationError(`Error performing OCR on image. (${err})`);
|
|
throw new OperationError(`Error performing OCR on image. (${err})`);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-export default OCR;
|
|
|
|
|
|
+export default OpticalCharacterRecognition;
|