Skip to content

Commit

Permalink
Merge pull request #22 from robertknight/node-demos
Browse files Browse the repository at this point in the history
Add Node examples
  • Loading branch information
robertknight authored May 29, 2022
2 parents 05bc564 + 2172589 commit 98b3327
Show file tree
Hide file tree
Showing 11 changed files with 2,872 additions and 3 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ runOCR();

## Examples

See the `examples/` directory for projects that show usage of the library.
See the `examples/` directory for projects that show usage of the library in
the browser and Node.

## Documentation

Expand Down
13 changes: 13 additions & 0 deletions examples/node-cli/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# tesseract-wasm Node CLI demo

This demo uses the low-level `OCREngine` API to synchronously perform OCR in
a command-line script.

## Usage

To set up the project and run OCR on `some-image.jpg`, run:

```sh
npm install
node ocr.js some-image.jpg
```
62 changes: 62 additions & 0 deletions examples/node-cli/ocr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { existsSync, readFileSync, writeFileSync } from "node:fs";
import { fileURLToPath } from "node:url";

import { Command } from "commander";
import fetch from "node-fetch";
import { createOCREngine } from "tesseract-wasm";
import { loadWasmBinary } from "tesseract-wasm/node";
import sharp from "sharp";

async function loadImage(path) {
const image = await sharp(path).ensureAlpha();
const { width, height } = await image.metadata();
return {
data: await image.raw().toBuffer(),
width,
height,
};
}

/** Resolve a URL relative to the current module. */
function resolve(path) {
return fileURLToPath(new URL(path, import.meta.url).href);
}

const program = new Command();
program.description("Extract text from an image");
program.argument("file");
program.parse();

// Initialize the OCR engine. In this demo we use the synchronous OCREngine
// API directly. In a server you would want to use the async OCRClient API
// instead.
const wasmBinary = await loadWasmBinary();
const engine = await createOCREngine({ wasmBinary });

// Fetch the text recognition model and cache it locally, then load it into
// the OCR engine.
const modelPath = "eng.traineddata";
if (!existsSync(modelPath)) {
process.stderr.write("Downloading text recognition model...\n");
const modelURL =
"https://github.com/tesseract-ocr/tessdata_fast/raw/main/eng.traineddata";
const response = await fetch(modelURL);
if (!response.ok) {
process.stderr.write(`Failed to download model from ${modelURL}`);
process.exit(1);
}
const data = await response.arrayBuffer();
writeFileSync(modelPath, new Uint8Array(data));
}
const model = readFileSync("eng.traineddata");
engine.loadModel(model);

// Load the image and perform OCR synchronously.
const image = await loadImage(program.args[0]);
engine.loadImage(image);

const text = engine.getText((progress) => {
process.stderr.write(`\rRecognizing text (${progress}% done)...`);
});
process.stderr.write("\n\n");
process.stdout.write(text);
Loading

0 comments on commit 98b3327

Please sign in to comment.