Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: OCR #632

Merged
merged 2 commits into from
Sep 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 70 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@
"sortablejs": "^1.9.0",
"split.js": "^1.5.11",
"ssdeep.js": "0.0.2",
"tesseract.js": "^2.0.0-alpha.15",
"ua-parser-js": "^0.7.20",
"utf8": "^3.0.0",
"vkbeautify": "^0.99.3",
Expand Down
3 changes: 2 additions & 1 deletion src/core/config/Categories.json
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,8 @@
"Hex Density chart",
"Scatter chart",
"Series chart",
"Heatmap chart"
"Heatmap chart",
"OCR"
]
},
{
Expand Down
72 changes: 72 additions & 0 deletions src/core/operations/OCR.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/**
* @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 { isWorkerEnvironment } from "../Utils.mjs";

import jimp from "jimp";
import Tesseract from "tesseract.js";
const { TesseractWorker } = Tesseract;

/**
* OCR operation
*/
class OCR extends Operation {

/**
* OCR 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.inputType = "ArrayBuffer";
this.outputType = "string";
this.args = [];
}

/**
* @param {ArrayBuffer} input
* @param {Object[]} args
* @returns {string}
*/
async run(input, args) {
if (!isImage(input)) {
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 worker = new TesseractWorker();

const result = await worker.recognize(image)
.progress(progress => {
if (isWorkerEnvironment()) self.sendStatusMessage(`${progress.status} - ${(parseFloat(progress.progress)*100).toFixed(2)}%`);
});

return result.text;
} catch (err) {
throw new OperationError(`Error performing OCR on image. (${err})`);
}
}
}

export default OCR;