mirror of
https://github.com/gchq/CyberChef.git
synced 2025-04-23 08:16:17 -04:00
OCR operation now relies on local files
This commit is contained in:
parent
3539e065fa
commit
70346bce35
8 changed files with 259 additions and 3 deletions
|
@ -14,6 +14,8 @@ import { isWorkerEnvironment } from "../Utils.mjs";
|
|||
import Tesseract from "tesseract.js";
|
||||
const { TesseractWorker } = Tesseract;
|
||||
|
||||
import process from "process";
|
||||
|
||||
/**
|
||||
* Optical Character Recognition operation
|
||||
*/
|
||||
|
@ -26,7 +28,7 @@ class OpticalCharacterRecognition extends Operation {
|
|||
super();
|
||||
|
||||
this.name = "Optical Character Recognition";
|
||||
this.module = "Image";
|
||||
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";
|
||||
|
@ -48,14 +50,22 @@ class OpticalCharacterRecognition extends Operation {
|
|||
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();
|
||||
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()) {
|
||||
|
|
BIN
src/core/vendor/tesseract/lang-data/eng.traineddata.gz
vendored
Normal file
BIN
src/core/vendor/tesseract/lang-data/eng.traineddata.gz
vendored
Normal file
Binary file not shown.
24
src/core/vendor/tesseract/tesseract-core.wasm.js
vendored
Normal file
24
src/core/vendor/tesseract/tesseract-core.wasm.js
vendored
Normal file
File diff suppressed because one or more lines are too long
17
src/core/vendor/tesseract/worker.min.js
vendored
Normal file
17
src/core/vendor/tesseract/worker.min.js
vendored
Normal file
File diff suppressed because one or more lines are too long
Loading…
Add table
Add a link
Reference in a new issue