From 163c108e8d4ba4f33e4adff7615741c406f3e885 Mon Sep 17 00:00:00 2001 From: Robert Knight Date: Sun, 28 Apr 2024 09:40:31 +0100 Subject: [PATCH] Add package.json and README.md file for npm package Relocate the build outputs and examples for the WASM build under `js/dist`. This is needed so that `js/` can serve as the root of an npm package with its own README. It is now possible to run the WASM build again by first runing the `ocrs` CLI tool to download models, and then running: ``` make wasm node js/examples/ocr-node/index.js ~/.cache/ocrs/text-detection.rten ~/.cache/ocrs/text-recognition.rten ocrs-cli/test-data/why-rust.png ``` --- Makefile | 6 +++--- js/README.md | 9 +++++++++ {js-examples => js/examples}/ocr-node/.gitignore | 0 {js-examples => js/examples}/ocr-node/index.js | 8 +++++++- .../examples}/ocr-node/package-lock.json | 0 {js-examples => js/examples}/ocr-node/package.json | 0 js/package.json | 14 ++++++++++++++ 7 files changed, 33 insertions(+), 4 deletions(-) create mode 100644 js/README.md rename {js-examples => js/examples}/ocr-node/.gitignore (100%) rename {js-examples => js/examples}/ocr-node/index.js (92%) rename {js-examples => js/examples}/ocr-node/package-lock.json (100%) rename {js-examples => js/examples}/ocr-node/package.json (100%) create mode 100644 js/package.json diff --git a/Makefile b/Makefile index 66e4253..be80f72 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ build: .PHONY: clean clean: - rm -rf dist/* + rm -rf js/dist/* rm -rf target/ .PHONY: check @@ -38,8 +38,8 @@ test-e2e: .PHONY: wasm wasm: RUSTFLAGS="-C target-feature=+simd128" cargo build --release --target wasm32-unknown-unknown --package ocrs - wasm-bindgen target/wasm32-unknown-unknown/release/ocrs.wasm --out-dir dist/ --target web --reference-types --weak-refs - tools/optimize-wasm.sh dist/ocrs_bg.wasm + wasm-bindgen target/wasm32-unknown-unknown/release/ocrs.wasm --out-dir js/dist/ --target web --reference-types --weak-refs + tools/optimize-wasm.sh js/dist/ocrs_bg.wasm .PHONY: wasm-all wasm-all: wasm wasm-nosimd diff --git a/js/README.md b/js/README.md new file mode 100644 index 0000000..b010d98 --- /dev/null +++ b/js/README.md @@ -0,0 +1,9 @@ +# Ocrs + +Ocrs is a library for extracting text from images. + +This is an alpha WebAssembly build of the library for use in Node, the browser +and other WebAssembly runtimes. + +See the [Ocrs GitHub repository](https://github.com/robertknight/ocrs) for JS +examples and updates. diff --git a/js-examples/ocr-node/.gitignore b/js/examples/ocr-node/.gitignore similarity index 100% rename from js-examples/ocr-node/.gitignore rename to js/examples/ocr-node/.gitignore diff --git a/js-examples/ocr-node/index.js b/js/examples/ocr-node/index.js similarity index 92% rename from js-examples/ocr-node/index.js rename to js/examples/ocr-node/index.js index 9336ffd..d340a7f 100644 --- a/js-examples/ocr-node/index.js +++ b/js/examples/ocr-node/index.js @@ -1,3 +1,6 @@ +import { dirname } from "node:path"; +import { fileURLToPath } from "node:url"; + import { readFile } from "fs/promises"; import { program } from "commander"; @@ -84,10 +87,13 @@ program .option("-j, --json", "Output JSON") .action( async (detectionModelPath, recognitionModelPath, imagePath, options) => { + const scriptDir = dirname(fileURLToPath(import.meta.url)); + const wasmPath = `${scriptDir}/../../dist/ocrs_bg.wasm`; + // Concurrently load the OCR library, text detection and recognition models, // and input image. const [_, detectionModel, recognitionModel, image] = await Promise.all([ - readFile("dist/ocrs_bg.wasm").then(initOcrLib), + readFile(wasmPath).then(initOcrLib), readFile(detectionModelPath).then((data) => new Uint8Array(data)), readFile(recognitionModelPath).then((data) => new Uint8Array(data)), loadImage(imagePath), diff --git a/js-examples/ocr-node/package-lock.json b/js/examples/ocr-node/package-lock.json similarity index 100% rename from js-examples/ocr-node/package-lock.json rename to js/examples/ocr-node/package-lock.json diff --git a/js-examples/ocr-node/package.json b/js/examples/ocr-node/package.json similarity index 100% rename from js-examples/ocr-node/package.json rename to js/examples/ocr-node/package.json diff --git a/js/package.json b/js/package.json new file mode 100644 index 0000000..b32ecc6 --- /dev/null +++ b/js/package.json @@ -0,0 +1,14 @@ +{ + "name": "ocrs", + "version": "0.1.0", + "description": "Ocrs is a library for extracting text from images", + "type": "module", + "main": "dist/ocrs.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "files": ["dist/**"], + "keywords": ["OCR"], + "author": "Robert Knight ", + "license": "MIT" +}