mirror of
https://github.com/gchq/CyberChef.git
synced 2025-04-21 23:36:16 -04:00
Cleaned up and improved OCR operation
This commit is contained in:
parent
a8ad10757c
commit
7eabaf0de6
3 changed files with 48 additions and 28 deletions
|
@ -383,6 +383,7 @@
|
||||||
"ops": [
|
"ops": [
|
||||||
"Render Image",
|
"Render Image",
|
||||||
"Play Media",
|
"Play Media",
|
||||||
|
"Optical Character Recognition",
|
||||||
"Remove EXIF",
|
"Remove EXIF",
|
||||||
"Extract EXIF",
|
"Extract EXIF",
|
||||||
"Split Colour Channels",
|
"Split Colour Channels",
|
||||||
|
@ -405,8 +406,7 @@
|
||||||
"Hex Density chart",
|
"Hex Density chart",
|
||||||
"Scatter chart",
|
"Scatter chart",
|
||||||
"Series chart",
|
"Series chart",
|
||||||
"Heatmap chart",
|
"Heatmap chart"
|
||||||
"OCR"
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
|
@ -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.name = "Optical Character Recognition";
|
||||||
this.module = "Default";
|
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.";
|
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://en.wikipedia.org/wiki/Optical_character_recognition";
|
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())
|
const image = `data:${type};base64,${toBase64(input)}`;
|
||||||
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 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)}%`);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (showConfidence) {
|
||||||
|
return `Confidence: ${result.confidence}%\n\n${result.text}`;
|
||||||
|
} else {
|
||||||
return result.text;
|
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;
|
|
@ -247,5 +247,20 @@ TestRegister.addTests([
|
||||||
args: ["None"]
|
args: ["None"]
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
},
|
||||||
|
/*{ Commented out as it takes a while to run and drops a file to disk (eng.traineddata)
|
||||||
|
name: "Optical Character Recognition",
|
||||||
|
input: "iVBORw0KGgoAAAANSUhEUgAAAUAAAAC0CAIAAABqhmJGAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAASuSURBVHhe7dftVdswAIbRzsVAzMM0XabDUCOUxLYsWW4Jp+/pvf9w9GH76CHw4x2IJWAI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usWbF9rs2E5v4mH58LBFyMW7g5OID7s9cMuTygmt9rcNPb5MrAz0lHc3Z9Ht7XZsxqxO36ZtLR/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]
|
||||||
}
|
}
|
||||||
|
]
|
||||||
|
}*/
|
||||||
]);
|
]);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue