From 7eabaf0de619bfcc1c3d90ae94b96d746321826b Mon Sep 17 00:00:00 2001 From: n1474335 Date: Fri, 13 Sep 2019 14:34:08 +0100 Subject: [PATCH] Cleaned up and improved OCR operation --- src/core/config/Categories.json | 4 +- ...CR.mjs => OpticalCharacterRecognition.mjs} | 55 ++++++++++--------- tests/operations/tests/Image.mjs | 17 +++++- 3 files changed, 48 insertions(+), 28 deletions(-) rename src/core/operations/{OCR.mjs => OpticalCharacterRecognition.mjs} (50%) diff --git a/src/core/config/Categories.json b/src/core/config/Categories.json index b990393c..94f7fd30 100755 --- a/src/core/config/Categories.json +++ b/src/core/config/Categories.json @@ -383,6 +383,7 @@ "ops": [ "Render Image", "Play Media", + "Optical Character Recognition", "Remove EXIF", "Extract EXIF", "Split Colour Channels", @@ -405,8 +406,7 @@ "Hex Density chart", "Scatter chart", "Series chart", - "Heatmap chart", - "OCR" + "Heatmap chart" ] }, { diff --git a/src/core/operations/OCR.mjs b/src/core/operations/OpticalCharacterRecognition.mjs similarity index 50% rename from src/core/operations/OCR.mjs rename to src/core/operations/OpticalCharacterRecognition.mjs index 5b4e6d79..ccfbcd1f 100644 --- a/src/core/operations/OCR.mjs +++ b/src/core/operations/OpticalCharacterRecognition.mjs @@ -1,4 +1,5 @@ /** + * @author n1474335 [n1474335@gmail.com] * @author mshwed [m@ttshwed.com] * @copyright Crown Copyright 2019 * @license Apache-2.0 @@ -7,30 +8,36 @@ 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 jimp from "jimp"; import Tesseract from "tesseract.js"; const { TesseractWorker } = Tesseract; /** - * OCR operation + * Optical Character Recognition operation */ -class OCR extends Operation { +class OpticalCharacterRecognition extends Operation { /** - * OCR constructor + * OpticalCharacterRecognition constructor */ constructor() { 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.

Supported image formats: png, jpg, bmp, pbm."; + this.infoURL = "https://wikipedia.org/wiki/Optical_character_recognition"; this.inputType = "ArrayBuffer"; this.outputType = "string"; - this.args = []; + this.args = [ + { + name: "Show confidence", + type: "boolean", + value: true + } + ]; } /** @@ -39,34 +46,32 @@ class OCR extends Operation { * @returns {string} */ async run(input, args) { - if (!isImage(input)) { + const [showConfidence] = args; + + const type = isImage(input); + if (!type) { throw new OperationError("Invalid File Type"); } 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 result = await worker.recognize(image) .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) { throw new OperationError(`Error performing OCR on image. (${err})`); } } } -export default OCR; +export default OpticalCharacterRecognition; diff --git a/tests/operations/tests/Image.mjs b/tests/operations/tests/Image.mjs index ea5c64a4..307420f2 100644 --- a/tests/operations/tests/Image.mjs +++ b/tests/operations/tests/Image.mjs @@ -247,5 +247,20 @@ TestRegister.addTests([ args: ["None"] } ] - } + }, + /*{ Commented out as it takes a while to run and drops a file to disk (eng.traineddata) + name: "Optical Character Recognition", + input: "iVBORw0KGgoAAAANSUhE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a7R2b5mD6/XSwdfg8snj4q21+W/L02dfzIxhQMDFyTm6Hd7m+JYP7rPKT5sRuzhOBywm91rUkYc3fV9ltchtr8VmzuGOdfDB9N1tFYefNfdXLmyGjNZkhoCLUQufVqd/7z7rUcLW/XieDvg0s9difNOdRV5ePibt5vTuazusWbF9rs2E5v4mH58LBFyMW7g5OID7s9cMuTygmt9rcNPb5MrAz0lHc3Z9Ht7XZsxqxO36ZtLR/c0+PpMEzLOc/4LhrwmYZ6lfywJ+JgHzJPr9DgLmi23/zdXvcwmYL7YKWL1PJ2AIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmAIJmCI9f7+G6yFxVg/GyYwAAAAAElFTkSuQmCC", + expectedOutput: "Tesseract.js\n", + recipeConfig: [ + { + "op": "From Base64", + "args": ["A-Za-z0-9+/=", true] + }, + { + "op": "Optical Character Recognition", + "args": [false] + } + ] + }*/ ]);