mirror of
https://github.com/gchq/CyberChef.git
synced 2024-11-16 17:08:31 +01:00
Linting fixes
This commit is contained in:
parent
1c8e37cb64
commit
c8eacb9942
1 changed files with 151 additions and 152 deletions
|
@ -6,155 +6,154 @@
|
||||||
* @license Apache-2.0
|
* @license Apache-2.0
|
||||||
*/
|
*/
|
||||||
|
|
||||||
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 { toBase64 } from "../lib/Base64.mjs";
|
||||||
import { isWorkerEnvironment } from "../Utils.mjs";
|
import { isWorkerEnvironment } from "../Utils.mjs";
|
||||||
|
|
||||||
import { createWorker } from "tesseract.js";
|
import { createWorker } from "tesseract.js";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Optical Character Recognition operation
|
* Optical Character Recognition operation
|
||||||
*/
|
*/
|
||||||
class OpticalCharacterRecognition extends Operation {
|
class OpticalCharacterRecognition extends Operation {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* OpticalCharacterRecognition constructor
|
* OpticalCharacterRecognition constructor
|
||||||
*/
|
*/
|
||||||
constructor() {
|
constructor() {
|
||||||
super();
|
super();
|
||||||
|
|
||||||
this.name = "Optical Character Recognition";
|
this.name = "Optical Character Recognition";
|
||||||
this.module = "OCR";
|
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.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.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",
|
name: "Show confidence",
|
||||||
type: "boolean",
|
type: "boolean",
|
||||||
value: true
|
value: true
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Language",
|
name: "Language",
|
||||||
type: "editableOptionShort",
|
type: "editableOptionShort",
|
||||||
value: [
|
value: [
|
||||||
{ name: "Afrikaans", value: "afr" },
|
{ name: "Afrikaans", value: "afr" },
|
||||||
{ name: "Arabic", value: "ara" },
|
{ name: "Arabic", value: "ara" },
|
||||||
{ name: "Azerbaijani", value: "aze" },
|
{ name: "Azerbaijani", value: "aze" },
|
||||||
{ name: "Belarusian", value: "bel" },
|
{ name: "Belarusian", value: "bel" },
|
||||||
{ name: "Bengali", value: "ben" },
|
{ name: "Bengali", value: "ben" },
|
||||||
{ name: "Bulgarian", value: "bul" },
|
{ name: "Bulgarian", value: "bul" },
|
||||||
{ name: "Catalan", value: "cat" },
|
{ name: "Catalan", value: "cat" },
|
||||||
{ name: "Czech", value: "ces" },
|
{ name: "Czech", value: "ces" },
|
||||||
{ name: "Chinese", value: "chi_sim" },
|
{ name: "Chinese", value: "chi_sim" },
|
||||||
{ name: "Traditional Chinese", value: "chi_tra" },
|
{ name: "Traditional Chinese", value: "chi_tra" },
|
||||||
{ name: "Cherokee", value: "chr" },
|
{ name: "Cherokee", value: "chr" },
|
||||||
{ name: "Danish", value: "dan" },
|
{ name: "Danish", value: "dan" },
|
||||||
{ name: "German", value: "deu" },
|
{ name: "German", value: "deu" },
|
||||||
{ name: "Greek", value: "ell" },
|
{ name: "Greek", value: "ell" },
|
||||||
{ name: "English", value: "eng" },
|
{ name: "English", value: "eng" },
|
||||||
{ name: "English (Old)", value: "enm" },
|
{ name: "English (Old)", value: "enm" },
|
||||||
{ name: "Internet Meme", value: "meme" },
|
{ name: "Internet Meme", value: "meme" },
|
||||||
{ name: "Esperanto", value: "epo" },
|
{ name: "Esperanto", value: "epo" },
|
||||||
{ name: "Esperanto alternative", value: "epo_alt" },
|
{ name: "Esperanto alternative", value: "epo_alt" },
|
||||||
{ name: "Estonian", value: "est" },
|
{ name: "Estonian", value: "est" },
|
||||||
{ name: "Basque", value: "eus" },
|
{ name: "Basque", value: "eus" },
|
||||||
{ name: "Finnish", value: "fin" },
|
{ name: "Finnish", value: "fin" },
|
||||||
{ name: "French", value: "fra" },
|
{ name: "French", value: "fra" },
|
||||||
{ name: "Frankish", value: "frk" },
|
{ name: "Frankish", value: "frk" },
|
||||||
{ name: "French (Old)", value: "frm" },
|
{ name: "French (Old)", value: "frm" },
|
||||||
{ name: "Galician", value: "glg" },
|
{ name: "Galician", value: "glg" },
|
||||||
{ name: "Ancient Greek", value: "grc" },
|
{ name: "Ancient Greek", value: "grc" },
|
||||||
{ name: "Hebrew", value: "heb" },
|
{ name: "Hebrew", value: "heb" },
|
||||||
{ name: "Hindi", value: "hin" },
|
{ name: "Hindi", value: "hin" },
|
||||||
{ name: "Croatian", value: "hrv" },
|
{ name: "Croatian", value: "hrv" },
|
||||||
{ name: "Hungarian", value: "hun" },
|
{ name: "Hungarian", value: "hun" },
|
||||||
{ name: "Indonesian", value: "ind" },
|
{ name: "Indonesian", value: "ind" },
|
||||||
{ name: "Icelandic", value: "isl" },
|
{ name: "Icelandic", value: "isl" },
|
||||||
{ name: "Italian", value: "ita" },
|
{ name: "Italian", value: "ita" },
|
||||||
{ name: "Italian (Old)", value: "ita_old" },
|
{ name: "Italian (Old)", value: "ita_old" },
|
||||||
{ name: "Japanese", value: "jpn" },
|
{ name: "Japanese", value: "jpn" },
|
||||||
{ name: "Kannada", value: "kan" },
|
{ name: "Kannada", value: "kan" },
|
||||||
{ name: "Korean", value: "kor" },
|
{ name: "Korean", value: "kor" },
|
||||||
{ name: "Latvian", value: "lav" },
|
{ name: "Latvian", value: "lav" },
|
||||||
{ name: "Lithuanian", value: "lit" },
|
{ name: "Lithuanian", value: "lit" },
|
||||||
{ name: "Malayalam", value: "mal" },
|
{ name: "Malayalam", value: "mal" },
|
||||||
{ name: "Macedonian", value: "mkd" },
|
{ name: "Macedonian", value: "mkd" },
|
||||||
{ name: "Maltese", value: "mlt" },
|
{ name: "Maltese", value: "mlt" },
|
||||||
{ name: "Malay", value: "msa" },
|
{ name: "Malay", value: "msa" },
|
||||||
{ name: "Dutch", value: "nld" },
|
{ name: "Dutch", value: "nld" },
|
||||||
{ name: "Norwegian", value: "nor" },
|
{ name: "Norwegian", value: "nor" },
|
||||||
{ name: "Polish", value: "pol" },
|
{ name: "Polish", value: "pol" },
|
||||||
{ name: "Portuguese", value: "por" },
|
{ name: "Portuguese", value: "por" },
|
||||||
{ name: "Romanian", value: "ron" },
|
{ name: "Romanian", value: "ron" },
|
||||||
{ name: "Russian", value: "rus" },
|
{ name: "Russian", value: "rus" },
|
||||||
{ name: "Slovakian", value: "slk" },
|
{ name: "Slovakian", value: "slk" },
|
||||||
{ name: "Slovenian", value: "slv" },
|
{ name: "Slovenian", value: "slv" },
|
||||||
{ name: "Spanish", value: "spa" },
|
{ name: "Spanish", value: "spa" },
|
||||||
{ name: "Old Spanish", value: "spa_old" },
|
{ name: "Old Spanish", value: "spa_old" },
|
||||||
{ name: "Albanian", value: "sqi" },
|
{ name: "Albanian", value: "sqi" },
|
||||||
{ name: "Serbian (Latin)", value: "srp" },
|
{ name: "Serbian (Latin)", value: "srp" },
|
||||||
{ name: "Swahili", value: "swa" },
|
{ name: "Swahili", value: "swa" },
|
||||||
{ name: "Swedish", value: "swe" },
|
{ name: "Swedish", value: "swe" },
|
||||||
{ name: "Tamil", value: "tam" },
|
{ name: "Tamil", value: "tam" },
|
||||||
{ name: "Telugu", value: "tel" },
|
{ name: "Telugu", value: "tel" },
|
||||||
{ name: "Tagalog", value: "tgl" },
|
{ name: "Tagalog", value: "tgl" },
|
||||||
{ name: "Thai", value: "tha" },
|
{ name: "Thai", value: "tha" },
|
||||||
{ name: "Turkish", value: "tur" },
|
{ name: "Turkish", value: "tur" },
|
||||||
{ name: "Ukrainian", value: "ukr" },
|
{ name: "Ukrainian", value: "ukr" },
|
||||||
{ name: "Vietnamese", value: "vie" },
|
{ name: "Vietnamese", value: "vie" },
|
||||||
],
|
],
|
||||||
defaultIndex: 14
|
defaultIndex: 14
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {ArrayBuffer} input
|
* @param {ArrayBuffer} input
|
||||||
* @param {Object[]} args
|
* @param {Object[]} args
|
||||||
* @returns {string}
|
* @returns {string}
|
||||||
*/
|
*/
|
||||||
async run(input, args) {
|
async run(input, args) {
|
||||||
const [showConfidence, language] = args;
|
const [showConfidence, language] = args;
|
||||||
|
|
||||||
if (!isWorkerEnvironment()) throw new OperationError("This operation only works in a browser");
|
if (!isWorkerEnvironment()) throw new OperationError("This operation only works in a browser");
|
||||||
|
|
||||||
const type = isImage(input);
|
const type = isImage(input);
|
||||||
if (!type) {
|
if (!type) {
|
||||||
throw new OperationError("Unsupported file type (supported: jpg,png,pbm,bmp) or no file provided\nOpen an image as input using the toolbar.");
|
throw new OperationError("Unsupported file type (supported: jpg,png,pbm,bmp) or no file provided\nOpen an image as input using the toolbar.");
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
self.sendStatusMessage("Spinning up Tesseract worker...");
|
self.sendStatusMessage("Spinning up Tesseract worker...");
|
||||||
const image = `data:${type};base64,${toBase64(input)}`;
|
const image = `data:${type};base64,${toBase64(input)}`;
|
||||||
const worker = createWorker({
|
const worker = createWorker({
|
||||||
logger: progress => {
|
logger: progress => {
|
||||||
if (isWorkerEnvironment()) {
|
if (isWorkerEnvironment()) {
|
||||||
self.sendStatusMessage(`Status: ${progress.status}${progress.status === "recognizing text" ? ` - ${(parseFloat(progress.progress)*100).toFixed(2)}%`: "" }`);
|
self.sendStatusMessage(`Status: ${progress.status}${progress.status === "recognizing text" ? ` - ${(parseFloat(progress.progress)*100).toFixed(2)}%`: "" }`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
await worker.load();
|
await worker.load();
|
||||||
self.sendStatusMessage(`Loading ${language} language pack...`);
|
self.sendStatusMessage(`Loading ${language} language pack...`);
|
||||||
await worker.loadLanguage(language);
|
await worker.loadLanguage(language);
|
||||||
self.sendStatusMessage("Intialising Tesseract API...");
|
self.sendStatusMessage("Intialising Tesseract API...");
|
||||||
await worker.initialize(language);
|
await worker.initialize(language);
|
||||||
self.sendStatusMessage("Finding text...");
|
self.sendStatusMessage("Finding text...");
|
||||||
const result = await worker.recognize(image);
|
const result = await worker.recognize(image);
|
||||||
|
|
||||||
if (showConfidence) {
|
if (showConfidence) {
|
||||||
return `Confidence: ${result.data.confidence}%\n\n${result.data.text}`;
|
return `Confidence: ${result.data.confidence}%\n\n${result.data.text}`;
|
||||||
} else {
|
} else {
|
||||||
return result.data.text;
|
return result.data.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 OpticalCharacterRecognition;
|
|
||||||
|
|
||||||
|
export default OpticalCharacterRecognition;
|
||||||
|
|
Loading…
Reference in a new issue