@mrbilit/mrz-scanner 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +87 -0
- package/dist/index.d.ts +64 -0
- package/dist/index.js +26 -0
- package/package.json +32 -0
package/README.md
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
# @mrbilit/mrz-scanner
|
|
2
|
+
|
|
3
|
+
Top-level orchestrator. Combines `@mrbilit/mrz-detection`,
|
|
4
|
+
`@mrbilit/mrz-ocr`, and `@mrbilit/mrz-core` into a single `scanMrz(image)`
|
|
5
|
+
entry point.
|
|
6
|
+
|
|
7
|
+
This package is part of the [mrbilit/mrz-scanner](https://github.com/mrbilit/mrz-scanner)
|
|
8
|
+
fork of the original [alsenet-labs/mrz-scanner](https://github.com/alsenet-labs/mrz-scanner)
|
|
9
|
+
project.
|
|
10
|
+
|
|
11
|
+
## Install
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npm install @mrbilit/mrz-scanner image-js onnxruntime-web
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
import { decode } from 'image-js';
|
|
21
|
+
import { scanMrz } from '@mrbilit/mrz-scanner';
|
|
22
|
+
|
|
23
|
+
const image = decode(bytes);
|
|
24
|
+
|
|
25
|
+
const result = await scanMrz(image, {
|
|
26
|
+
modelPath: '/mrz-cnn.onnx',
|
|
27
|
+
onProgress: (stage) => console.log(stage), // 'detecting' | 'ocr' | 'parsing'
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
if (result.parsed?.valid) {
|
|
31
|
+
console.log(result.parsed.fields);
|
|
32
|
+
} else {
|
|
33
|
+
console.log('OCR lines:', result.ocrLines);
|
|
34
|
+
console.log('Error:', result.error);
|
|
35
|
+
}
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Pipeline
|
|
39
|
+
|
|
40
|
+
1. Lazy-initialize a shared `MrzOcr` instance on first call (cached between
|
|
41
|
+
calls — model is only loaded once).
|
|
42
|
+
2. `getMrz(image, { debug })` localizes and crops the MRZ region.
|
|
43
|
+
3. `ocr.recognize(crop)` produces `lines` + per-character confidence.
|
|
44
|
+
4. `parse(lines)` validates and, if necessary, applies OCR-confusion
|
|
45
|
+
corrections from `@mrbilit/mrz-core`.
|
|
46
|
+
|
|
47
|
+
### Fallback
|
|
48
|
+
|
|
49
|
+
If detection throws (e.g. the image is already a tight MRZ crop, or the
|
|
50
|
+
morphology pipeline rejects the region), `scanMrz` runs OCR directly on the
|
|
51
|
+
full input image and returns whatever it finds. Only if **that** also fails
|
|
52
|
+
is the detection error propagated back in `result.error`.
|
|
53
|
+
|
|
54
|
+
## Exports
|
|
55
|
+
|
|
56
|
+
```ts
|
|
57
|
+
interface ScanMrzOptions {
|
|
58
|
+
modelPath?: string;
|
|
59
|
+
onProgress?: ScanProgress;
|
|
60
|
+
debug?: boolean; // include detection debug images in the result
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
interface ScanMrzResult {
|
|
64
|
+
parsed?: ParseResult; // from @mrbilit/mrz-core (extends mrz v5 result)
|
|
65
|
+
ocrLines?: string[];
|
|
66
|
+
confidence?: number[][];
|
|
67
|
+
error?: Error;
|
|
68
|
+
debugImages?: Record<string, unknown>;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function scanMrz(image: Image, options?: ScanMrzOptions): Promise<ScanMrzResult>
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
Also re-exports `parse`, `ScanProgress`, `OcrResult`, `ParseResult` from
|
|
75
|
+
`@mrbilit/mrz-core`, and `MrzDetectionResult` / `MrzOcrResult` from the
|
|
76
|
+
other packages.
|
|
77
|
+
|
|
78
|
+
## Node.js
|
|
79
|
+
|
|
80
|
+
In Node.js the OCR sub-package needs the native runtime explicitly. Build
|
|
81
|
+
your own pipeline from the lower-level packages — see the top-level README
|
|
82
|
+
section "Node.js Usage" for an example — or reuse the CLI
|
|
83
|
+
(`@mrbilit/mrz-cli`) which wires it up for you.
|
|
84
|
+
|
|
85
|
+
## License
|
|
86
|
+
|
|
87
|
+
AGPL-3.0-or-later.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { Image as Image_2 } from 'image-js';
|
|
2
|
+
import { MrzDetectionResult } from '@mrbilit/mrz-detection';
|
|
3
|
+
import { MrzOcrResult } from '@mrbilit/mrz-ocr';
|
|
4
|
+
import { OcrResult } from '@mrbilit/mrz-core';
|
|
5
|
+
import { parse } from '@mrbilit/mrz-core';
|
|
6
|
+
import { ParseResult } from '@mrbilit/mrz-core';
|
|
7
|
+
import { ScanProgress } from '@mrbilit/mrz-core';
|
|
8
|
+
|
|
9
|
+
export { MrzDetectionResult }
|
|
10
|
+
|
|
11
|
+
export { MrzOcrResult }
|
|
12
|
+
|
|
13
|
+
export { OcrResult }
|
|
14
|
+
|
|
15
|
+
export { parse }
|
|
16
|
+
|
|
17
|
+
export { ParseResult }
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Scan a document image for MRZ data.
|
|
21
|
+
*
|
|
22
|
+
* @param image - An image-js Image of the document
|
|
23
|
+
* @param options - Configuration options
|
|
24
|
+
* @returns Parsed MRZ result with fields, or error details
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```ts
|
|
28
|
+
* import { Image } from 'image-js';
|
|
29
|
+
* import { scanMrz } from '@mrbilit/mrz-scanner';
|
|
30
|
+
*
|
|
31
|
+
* const image = await Image.load('passport.jpg');
|
|
32
|
+
* const result = await scanMrz(image);
|
|
33
|
+
* if (result.parsed?.valid) {
|
|
34
|
+
* console.log(result.parsed.fields);
|
|
35
|
+
* }
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
export declare function scanMrz(image: Image_2, options?: ScanMrzOptions): Promise<ScanMrzResult>;
|
|
39
|
+
|
|
40
|
+
export declare interface ScanMrzOptions {
|
|
41
|
+
/** URL or file path to the ONNX model */
|
|
42
|
+
modelPath?: string;
|
|
43
|
+
/** Progress callback for UI feedback */
|
|
44
|
+
onProgress?: ScanProgress;
|
|
45
|
+
/** Enable debug mode (returns intermediate images) */
|
|
46
|
+
debug?: boolean;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export declare interface ScanMrzResult {
|
|
50
|
+
/** Parsed and validated MRZ fields */
|
|
51
|
+
parsed?: ParseResult;
|
|
52
|
+
/** Raw OCR text lines */
|
|
53
|
+
ocrLines?: string[];
|
|
54
|
+
/** Per-character confidence scores */
|
|
55
|
+
confidence?: number[][];
|
|
56
|
+
/** Error if any stage failed */
|
|
57
|
+
error?: Error;
|
|
58
|
+
/** Debug images from the detection stage */
|
|
59
|
+
debugImages?: Record<string, unknown>;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export { ScanProgress }
|
|
63
|
+
|
|
64
|
+
export { }
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { getMrz as e } from "@mrbilit/mrz-detection";
|
|
2
|
+
import { MrzOcr as t } from "@mrbilit/mrz-ocr";
|
|
3
|
+
import { parse as n, parse as r } from "@mrbilit/mrz-core";
|
|
4
|
+
//#region src/scan-mrz.ts
|
|
5
|
+
var i = null;
|
|
6
|
+
async function a(n, a = {}) {
|
|
7
|
+
let { onProgress: o, debug: s = !1 } = a, c = {};
|
|
8
|
+
o?.("detecting"), i || (i = new t({ modelPath: a.modelPath }), await i.init());
|
|
9
|
+
try {
|
|
10
|
+
let t = e(n, { debug: s });
|
|
11
|
+
s && (c.debugImages = t.debugImages), o?.("ocr");
|
|
12
|
+
let a = await i.recognize(t.crop);
|
|
13
|
+
c.ocrLines = a.lines, c.confidence = a.confidence, o?.("parsing"), c.parsed = r(a.lines);
|
|
14
|
+
} catch (e) {
|
|
15
|
+
try {
|
|
16
|
+
o?.("ocr");
|
|
17
|
+
let t = await i.recognize(n);
|
|
18
|
+
t.lines.length > 0 ? (c.ocrLines = t.lines, c.confidence = t.confidence, o?.("parsing"), c.parsed = r(t.lines)) : c.error = e instanceof Error ? e : Error(String(e));
|
|
19
|
+
} catch {
|
|
20
|
+
c.error = e instanceof Error ? e : Error(String(e));
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return c;
|
|
24
|
+
}
|
|
25
|
+
//#endregion
|
|
26
|
+
export { n as parse, a as scanMrz };
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@mrbilit/mrz-scanner",
|
|
3
|
+
"version": "2.0.0",
|
|
4
|
+
"description": "Full MRZ scanning pipeline: detect, OCR, and parse",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": ["dist"],
|
|
15
|
+
"scripts": {
|
|
16
|
+
"build": "vite build",
|
|
17
|
+
"dev": "vite build --watch",
|
|
18
|
+
"typecheck": "tsc --noEmit"
|
|
19
|
+
},
|
|
20
|
+
"dependencies": {
|
|
21
|
+
"@mrbilit/mrz-core": "2.0.0",
|
|
22
|
+
"@mrbilit/mrz-detection": "2.0.0",
|
|
23
|
+
"@mrbilit/mrz-ocr": "2.0.0",
|
|
24
|
+
"image-js": "^1.5.0"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"typescript": "^5.8.0",
|
|
28
|
+
"vite": "^8.0.0",
|
|
29
|
+
"vite-plugin-dts": "^4.5.0"
|
|
30
|
+
},
|
|
31
|
+
"license": "AGPL-3.0-or-later"
|
|
32
|
+
}
|