Skip to content

Commit

Permalink
Fixed tesseract issue
Browse files Browse the repository at this point in the history
  • Loading branch information
mattnotmitt authored and d98762625 committed May 27, 2020
1 parent 5c35205 commit 5ce3cc1
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 50 deletions.
26 changes: 17 additions & 9 deletions src/core/operations/OpticalCharacterRecognition.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* @author n1474335 [n1474335@gmail.com]
* @author mshwed [m@ttshwed.com]
* @author Matt C [me@mitt.dev]
* @copyright Crown Copyright 2019
* @license Apache-2.0
*/
Expand All @@ -12,7 +13,7 @@ import { toBase64 } from "../lib/Base64.mjs";
import { isWorkerEnvironment } from "../Utils.mjs";

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

import process from "process";

Expand Down Expand Up @@ -60,23 +61,30 @@ class OpticalCharacterRecognition extends Operation {
const assetDir = isWorkerEnvironment() ? `${self.docURL}/assets/` : `${process.cwd()}/src/core/vendor/`;

try {
self.sendStatusMessage("Spinning up Tesseract worker...");
const image = `data:${type};base64,${toBase64(input)}`;
const worker = new TesseractWorker({
const worker = createWorker({
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 => {
logger: progress => {
if (isWorkerEnvironment()) {
self.sendStatusMessage(`Status: ${progress.status} - ${(parseFloat(progress.progress)*100).toFixed(2)}%`);
self.sendStatusMessage(`Status: ${progress.status}${progress.status === "recognizing text" ? ` - ${(parseFloat(progress.progress)*100).toFixed(2)}%`: "" }`);
}
});
}
});
await worker.load();
self.sendStatusMessage("Loading English language...");
await worker.loadLanguage("eng");
self.sendStatusMessage("Intialising Tesseract API...");
await worker.initialize("eng");
self.sendStatusMessage("Finding text...");
const result = await worker.recognize(image);

if (showConfidence) {
return `Confidence: ${result.confidence}%\n\n${result.text}`;
return `Confidence: ${result.data.confidence}%\n\n${result.data.text}`;
} else {
return result.text;
return result.data.text;
}
} catch (err) {
throw new OperationError(`Error performing OCR on image. (${err})`);
Expand Down
24 changes: 0 additions & 24 deletions src/core/vendor/tesseract/tesseract-core.wasm.js

This file was deleted.

17 changes: 0 additions & 17 deletions src/core/vendor/tesseract/worker.min.js

This file was deleted.

8 changes: 8 additions & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ module.exports = {
context: "src/core/vendor/",
from: "tesseract/**/*",
to: "assets/"
}, {
context: "node_modules/tesseract.js/",
from: "dist/worker.min.js",
to: "assets/tesseract"
}, {
context: "node_modules/tesseract.js-core/",
from: "tesseract-core.wasm.js",
to: "assets/tesseract"
}
])
],
Expand Down

0 comments on commit 5ce3cc1

Please sign in to comment.