Merge branch 'master' into edit-node-recipe

This commit is contained in:
n1474335 2019-10-01 12:12:49 +01:00
commit 15c26a95c5
13 changed files with 459 additions and 34 deletions

View file

@ -383,6 +383,7 @@
"ops": [
"Render Image",
"Play Media",
"Optical Character Recognition",
"Remove EXIF",
"Extract EXIF",
"Split Colour Channels",

View file

@ -54,7 +54,7 @@ class ConvertDataUnits extends Operation {
const DATA_UNITS = [
"Bits (b)", "Nibbles", "Octets", "Bytes (B)",
"[Binary bits (2^n)]", "Kibibits (Kib)", "Mebibits (Mib)", "Gibibits (Gib)", "Tebibits (Tib)", "Pebibits (Pib)", "Exbibits (Eib)", "Zebibits (Zib)", "Yobibits (Yib)", "[/Binary bits (2^n)]",
"[Decimal bits (10^n)]", "Decabits", "Hectobits", "Kilobits (kb)", "Megabits (Mb)", "Gigabits (Gb)", "Terabits (Tb)", "Petabits (Pb)", "Exabits (Eb)", "Zettabits (Zb)", "Yottabits (Yb)", "[/Decimal bits (10^n)]",
"[Decimal bits (10^n)]", "Decabits", "Hectobits", "Kilobits (Kb)", "Megabits (Mb)", "Gigabits (Gb)", "Terabits (Tb)", "Petabits (Pb)", "Exabits (Eb)", "Zettabits (Zb)", "Yottabits (Yb)", "[/Decimal bits (10^n)]",
"[Binary bytes (8 x 2^n)]", "Kibibytes (KiB)", "Mebibytes (MiB)", "Gibibytes (GiB)", "Tebibytes (TiB)", "Pebibytes (PiB)", "Exbibytes (EiB)", "Zebibytes (ZiB)", "Yobibytes (YiB)", "[/Binary bytes (8 x 2^n)]",
"[Decimal bytes (8 x 10^n)]", "Kilobytes (KB)", "Megabytes (MB)", "Gigabytes (GB)", "Terabytes (TB)", "Petabytes (PB)", "Exabytes (EB)", "Zettabytes (ZB)", "Yottabytes (YB)", "[/Decimal bytes (8 x 10^n)]"
];

View file

@ -0,0 +1,87 @@
/**
* @author n1474335 [n1474335@gmail.com]
* @author mshwed [m@ttshwed.com]
* @copyright Crown Copyright 2019
* @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 { TesseractWorker } = 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 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({
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()) {
self.sendStatusMessage(`Status: ${progress.status} - ${(parseFloat(progress.progress)*100).toFixed(2)}%`);
}
});
if (showConfidence) {
return `Confidence: ${result.confidence}%\n\n${result.text}`;
} else {
return result.text;
}
} catch (err) {
throw new OperationError(`Error performing OCR on image. (${err})`);
}
}
}
export default OpticalCharacterRecognition;

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