Skip to content

Commit d3c512e

Browse files
feat(image+ocr): add OCR quick-action on Render Image and OCR whitelist option
- Render Image (browser): overlay a small ✎ button linking to an OCR recipe for quick access - OCR: add optional "Character whitelist" arg and apply via tesseract parameter to improve accuracy on restricted alphabets Authored-by: Izai Alejandro Zalles Merino <zallesrene@gmail.com>
1 parent de2a83c commit d3c512e

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

src/core/operations/OpticalCharacterRecognition.mjs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { isWorkerEnvironment } from "../Utils.mjs";
1515
import { createWorker } from "tesseract.js";
1616

1717
const OEM_MODES = ["Tesseract only", "LSTM only", "Tesseract/LSTM Combined"];
18+
const OCR_DEFAULT_WHITELIST = "";
1819

1920
/**
2021
* Optical Character Recognition operation
@@ -34,6 +35,11 @@ class OpticalCharacterRecognition extends Operation {
3435
this.inputType = "ArrayBuffer";
3536
this.outputType = "string";
3637
this.args = [
38+
{
39+
name: "Character whitelist (optional)",
40+
type: "string",
41+
value: OCR_DEFAULT_WHITELIST
42+
},
3743
{
3844
name: "Show confidence",
3945
type: "boolean",
@@ -61,7 +67,7 @@ class OpticalCharacterRecognition extends Operation {
6167
* @returns {string}
6268
*/
6369
async run(input, args) {
64-
const [showConfidence, oemChoice] = args;
70+
const [whitelist, showConfidence, oemChoice] = args;
6571

6672
if (!isWorkerEnvironment()) throw new OperationError("This operation only works in a browser");
6773

@@ -86,6 +92,10 @@ class OpticalCharacterRecognition extends Operation {
8692
}
8793
}
8894
});
95+
self.sendStatusMessage("Configuring OCR parameters...");
96+
if (whitelist && whitelist.length) {
97+
await worker.setParameters({ /* eslint-disable camelcase */ tessedit_char_whitelist: whitelist /* eslint-enable camelcase */ });
98+
}
8999
self.sendStatusMessage("Finding text...");
90100
const result = await worker.recognize(image);
91101

src/core/operations/RenderImage.mjs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { fromBase64, toBase64 } from "../lib/Base64.mjs";
88
import { fromHex } from "../lib/Hex.mjs";
99
import Operation from "../Operation.mjs";
1010
import OperationError from "../errors/OperationError.mjs";
11-
import Utils from "../Utils.mjs";
11+
import Utils, { isWorkerEnvironment } from "../Utils.mjs";
1212
import {isImage} from "../lib/FileType.mjs";
1313

1414
/**
@@ -104,7 +104,12 @@ class RenderImage extends Operation {
104104
// Add image data to URI
105105
dataURI += "base64," + toBase64(data);
106106

107-
return "<img src='" + dataURI + "'>";
107+
let html = "<img src='" + dataURI + "'>";
108+
if (isWorkerEnvironment()) {
109+
const ocrLink = "#recipe=Optical_Character_Recognition('Show confidence',true,'OCR Engine Mode','LSTM only')";
110+
html = "<div style=\"position:relative; display:inline-block;\">" + html + "<a href=\"" + ocrLink + "\" title=\"Run OCR\" style=\"position:absolute; top:8px; right:8px; background:rgba(0,0,0,.6); color:#fff; padding:4px 6px; border-radius:4px; text-decoration:none; font-weight:bold;\">✎</a></div>";
111+
}
112+
return html;
108113
}
109114

110115
}

0 commit comments

Comments
 (0)