@procedures/ocr 0.68.6 → 0.68.8
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/CHANGELOG.md +14 -0
- package/README.md +20 -1
- package/dist/engine.d.ts +17 -1
- package/dist/engine.d.ts.map +1 -1
- package/dist/engine.js +15 -1
- package/dist/engine.js.map +1 -1
- package/dist/osd.d.ts +26 -0
- package/dist/osd.d.ts.map +1 -0
- package/dist/osd.js +30 -0
- package/dist/osd.js.map +1 -0
- package/dist/parse.d.ts +1 -0
- package/dist/parse.d.ts.map +1 -1
- package/dist/parse.js +1 -0
- package/dist/parse.js.map +1 -1
- package/dist/procedures/ocr/image.d.ts +2 -1
- package/dist/procedures/ocr/image.d.ts.map +1 -1
- package/dist/procedures/ocr/image.js +6 -5
- package/dist/procedures/ocr/image.js.map +1 -1
- package/dist/procedures/ocr/page.d.ts +2 -1
- package/dist/procedures/ocr/page.d.ts.map +1 -1
- package/dist/procedures/ocr/page.js +9 -7
- package/dist/procedures/ocr/page.js.map +1 -1
- package/dist/procedures/ocr/recognize.d.ts +29 -0
- package/dist/procedures/ocr/recognize.d.ts.map +1 -0
- package/dist/procedures/ocr/recognize.js +27 -0
- package/dist/procedures/ocr/recognize.js.map +1 -0
- package/dist/text.d.ts +17 -0
- package/dist/text.d.ts.map +1 -1
- package/dist/text.js +43 -0
- package/dist/text.js.map +1 -1
- package/dist/types.d.ts +9 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +10 -0
- package/dist/types.js.map +1 -1
- package/package.json +3 -3
- package/src/engine.ts +29 -1
- package/src/osd.ts +42 -0
- package/src/parse.ts +1 -0
- package/src/procedures/ocr/image.ts +6 -5
- package/src/procedures/ocr/page.ts +9 -7
- package/src/procedures/ocr/recognize.ts +45 -0
- package/src/text.ts +47 -0
- package/src/types.ts +20 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
# @procedures/ocr
|
|
2
2
|
|
|
3
|
+
## 0.68.8
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- @procedures/client@0.68.8
|
|
8
|
+
- @procedures/shell@0.68.8
|
|
9
|
+
|
|
10
|
+
## 0.68.7
|
|
11
|
+
|
|
12
|
+
### Patch Changes
|
|
13
|
+
|
|
14
|
+
- @procedures/client@0.68.7
|
|
15
|
+
- @procedures/shell@0.68.7
|
|
16
|
+
|
|
3
17
|
## 0.68.6
|
|
4
18
|
|
|
5
19
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -54,6 +54,23 @@ mark ocr page report.pdf --page 2
|
|
|
54
54
|
mark ocr image photo.png --language eng+deu
|
|
55
55
|
```
|
|
56
56
|
|
|
57
|
+
## A page on its side
|
|
58
|
+
|
|
59
|
+
A scan of a wide table is often on its side. Give `upright: true` to `ocr.page` or to `ocr.image`. Then the engine finds the orientation of the page first:
|
|
60
|
+
|
|
61
|
+
1. Tesseract finds the turn that makes the page upright (`--psm 0`). This step needs the `osd` data of tesseract. The installers of tesseract include it.
|
|
62
|
+
2. For a page on its side, tesseract reads the page in the mode `--psm 1`, unless the input gives `psm`. In this mode, tesseract turns the page, but it gives the boxes in the frame of the scan.
|
|
63
|
+
3. The procedure turns each box into the frame of the upright page. A turn of 90 or 270 degrees changes the width and the height.
|
|
64
|
+
|
|
65
|
+
The result then has `orientation`: the `turn` in the clock direction (0, 90, 180 or 270 degrees) and the `confidence` of the engine. For tesseract, a confidence below 2 is weak. A page with too few letters gives the turn 0 and the confidence 0.
|
|
66
|
+
|
|
67
|
+
```ts
|
|
68
|
+
const scan = await outputValue(await invokePath(["ocr", "page"], { file: "auction.pdf", page: 3, upright: true }, { registry }));
|
|
69
|
+
// { source: "ocr", width: 792, height: 612, orientation: { turn: 90, confidence: 2 }, lines: [...] }
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
The function `turnText(text, turn)` of the parsers does step 3 for a text from another source.
|
|
73
|
+
|
|
57
74
|
## The result
|
|
58
75
|
|
|
59
76
|
Each result has `unit`, `width`, `height`, `text`, `confidence` and `lines`. Each line has `text`, `confidence`, `box` and `words`. Each word has `text`, `confidence` and `box`.
|
|
@@ -88,8 +105,10 @@ The parsers start no process and read no file. The root and the entry `@procedur
|
|
|
88
105
|
|
|
89
106
|
- `parseTesseractTsv(tsv)` reads the TSV output of tesseract (`tesseract <image> stdout tsv`).
|
|
90
107
|
- `parsePdftotextBbox(xhtml)` reads the output of `pdftotext -bbox-layout` for one page.
|
|
108
|
+
- `parseTesseractOsd(output)` reads the orientation output of tesseract (`tesseract <image> stdout --psm 0`).
|
|
109
|
+
- `turnText(text, turn)` turns each box, the width and the height by 0, 90, 180 or 270 degrees in the clock direction.
|
|
91
110
|
- `scaleText(text, factor, unit)` changes the unit of the boxes. For example, the factor `72 / 300` changes the pixels of an image at 300 dots per inch to PDF points.
|
|
92
111
|
|
|
93
112
|
## Other engines
|
|
94
113
|
|
|
95
|
-
An OCR engine is an `OcrEngine` adapter: a program, the argv for one image and the parse of its output. `tesseractEngine` is the first adapter. Give another adapter to `createOcrModule({ engine })`.
|
|
114
|
+
An OCR engine is an `OcrEngine` adapter: a program, the argv for one image and the parse of its output. An optional `orientation` step finds the orientation of a page. An engine with no step cannot read a page with `upright`. `tesseractEngine` is the first adapter. Give another adapter to `createOcrModule({ engine })`.
|
package/dist/engine.d.ts
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
* is the first engine. Another engine is one more adapter.
|
|
5
5
|
*/
|
|
6
6
|
import { type ProgramSpec } from "./programs.js";
|
|
7
|
+
import { type OcrOrientation } from "./osd.js";
|
|
7
8
|
import type { OcrText } from "./text.js";
|
|
8
9
|
/** The options of one recognition. */
|
|
9
10
|
export interface OcrEngineOptions {
|
|
@@ -14,6 +15,18 @@ export interface OcrEngineOptions {
|
|
|
14
15
|
/** The resolution of the image, in dots per inch, when the image file does not tell it. */
|
|
15
16
|
readonly dpi?: number | undefined;
|
|
16
17
|
}
|
|
18
|
+
/** The step of an engine that finds the orientation of a page. */
|
|
19
|
+
export interface OcrOrientationStep {
|
|
20
|
+
/** This function gives the argv of the program that finds the orientation of one image. */
|
|
21
|
+
argv(image: string, options: Pick<OcrEngineOptions, "dpi">): string[];
|
|
22
|
+
/** This function parses the standard output of the program. */
|
|
23
|
+
parse(stdout: string): OcrOrientation;
|
|
24
|
+
/**
|
|
25
|
+
* The page segmentation mode that reads a page on its side. The engine turns the page in this
|
|
26
|
+
* mode, but it gives the boxes in the frame of the image.
|
|
27
|
+
*/
|
|
28
|
+
readonly psm: number;
|
|
29
|
+
}
|
|
17
30
|
/** An OCR engine. */
|
|
18
31
|
export interface OcrEngine {
|
|
19
32
|
/** The name of the engine in the result, for example `tesseract`. */
|
|
@@ -24,6 +37,8 @@ export interface OcrEngine {
|
|
|
24
37
|
argv(image: string, options: OcrEngineOptions): string[];
|
|
25
38
|
/** This function parses the standard output of the program. The boxes are in the pixels of the image. */
|
|
26
39
|
parse(stdout: string): OcrText;
|
|
40
|
+
/** The step that finds the orientation of a page. An engine with no step cannot read a page on its side. */
|
|
41
|
+
readonly orientation?: OcrOrientationStep | undefined;
|
|
27
42
|
}
|
|
28
43
|
/** The default language of the recognition. */
|
|
29
44
|
export declare const DEFAULT_LANGUAGE = "eng";
|
|
@@ -34,7 +49,8 @@ export declare const DEFAULT_LANGUAGE = "eng";
|
|
|
34
49
|
export declare const LANGUAGE_PATTERN: RegExp;
|
|
35
50
|
/**
|
|
36
51
|
* The adapter of tesseract 4 or later. The argv is `<image> stdout -l <language> [--psm N]
|
|
37
|
-
* [--dpi N] tsv`: tesseract writes the TSV of the image to the standard output.
|
|
52
|
+
* [--dpi N] tsv`: tesseract writes the TSV of the image to the standard output. Its orientation step
|
|
53
|
+
* is `<image> stdout --psm 0 [--dpi N]`.
|
|
38
54
|
*/
|
|
39
55
|
export declare const tesseractEngine: OcrEngine;
|
|
40
56
|
//# sourceMappingURL=engine.d.ts.map
|
package/dist/engine.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"engine.d.ts","sourceRoot":"","sources":["../src/engine.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAa,KAAK,WAAW,EAAE,MAAM,eAAe,CAAC;AAC5D,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAGzC,sCAAsC;AACtC,MAAM,WAAW,gBAAgB;IAC/B,oGAAoG;IACpG,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IACvC,qFAAqF;IACrF,QAAQ,CAAC,GAAG,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IAClC,2FAA2F;IAC3F,QAAQ,CAAC,GAAG,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;CACnC;AAED,qBAAqB;AACrB,MAAM,WAAW,SAAS;IACxB,qEAAqE;IACrE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,6DAA6D;IAC7D,QAAQ,CAAC,OAAO,EAAE,WAAW,CAAC;IAC9B,8FAA8F;IAC9F,IAAI,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,gBAAgB,GAAG,MAAM,EAAE,CAAC;IACzD,yGAAyG;IACzG,KAAK,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC;
|
|
1
|
+
{"version":3,"file":"engine.d.ts","sourceRoot":"","sources":["../src/engine.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAa,KAAK,WAAW,EAAE,MAAM,eAAe,CAAC;AAC5D,OAAO,EAAE,KAAK,cAAc,EAAqB,MAAM,UAAU,CAAC;AAClE,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAGzC,sCAAsC;AACtC,MAAM,WAAW,gBAAgB;IAC/B,oGAAoG;IACpG,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IACvC,qFAAqF;IACrF,QAAQ,CAAC,GAAG,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IAClC,2FAA2F;IAC3F,QAAQ,CAAC,GAAG,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;CACnC;AAED,kEAAkE;AAClE,MAAM,WAAW,kBAAkB;IACjC,2FAA2F;IAC3F,IAAI,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,IAAI,CAAC,gBAAgB,EAAE,KAAK,CAAC,GAAG,MAAM,EAAE,CAAC;IACtE,+DAA+D;IAC/D,KAAK,CAAC,MAAM,EAAE,MAAM,GAAG,cAAc,CAAC;IACtC;;;OAGG;IACH,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;CACtB;AAED,qBAAqB;AACrB,MAAM,WAAW,SAAS;IACxB,qEAAqE;IACrE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,6DAA6D;IAC7D,QAAQ,CAAC,OAAO,EAAE,WAAW,CAAC;IAC9B,8FAA8F;IAC9F,IAAI,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,gBAAgB,GAAG,MAAM,EAAE,CAAC;IACzD,yGAAyG;IACzG,KAAK,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC;IAC/B,4GAA4G;IAC5G,QAAQ,CAAC,WAAW,CAAC,EAAE,kBAAkB,GAAG,SAAS,CAAC;CACvD;AAED,+CAA+C;AAC/C,eAAO,MAAM,gBAAgB,QAAQ,CAAC;AAEtC;;;GAGG;AACH,eAAO,MAAM,gBAAgB,EAAE,MAAsE,CAAC;AAEtG;;;;GAIG;AACH,eAAO,MAAM,eAAe,EAAE,SAwB7B,CAAC"}
|
package/dist/engine.js
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
* is the first engine. Another engine is one more adapter.
|
|
5
5
|
*/
|
|
6
6
|
import { TESSERACT } from "./programs.js";
|
|
7
|
+
import { parseTesseractOsd } from "./osd.js";
|
|
7
8
|
import { parseTesseractTsv } from "./tsv.js";
|
|
8
9
|
/** The default language of the recognition. */
|
|
9
10
|
export const DEFAULT_LANGUAGE = "eng";
|
|
@@ -14,7 +15,8 @@ export const DEFAULT_LANGUAGE = "eng";
|
|
|
14
15
|
export const LANGUAGE_PATTERN = /^[A-Za-z0-9_][A-Za-z0-9_/]*(\+[A-Za-z0-9_][A-Za-z0-9_/]*)*$/;
|
|
15
16
|
/**
|
|
16
17
|
* The adapter of tesseract 4 or later. The argv is `<image> stdout -l <language> [--psm N]
|
|
17
|
-
* [--dpi N] tsv`: tesseract writes the TSV of the image to the standard output.
|
|
18
|
+
* [--dpi N] tsv`: tesseract writes the TSV of the image to the standard output. Its orientation step
|
|
19
|
+
* is `<image> stdout --psm 0 [--dpi N]`.
|
|
18
20
|
*/
|
|
19
21
|
export const tesseractEngine = {
|
|
20
22
|
name: "tesseract",
|
|
@@ -32,5 +34,17 @@ export const tesseractEngine = {
|
|
|
32
34
|
return argv;
|
|
33
35
|
},
|
|
34
36
|
parse: parseTesseractTsv,
|
|
37
|
+
orientation: {
|
|
38
|
+
// The mode 0 finds only the orientation and the script. It needs the osd data of tesseract
|
|
39
|
+
argv(image, options) {
|
|
40
|
+
const argv = [image, "stdout", "--psm", "0"];
|
|
41
|
+
if (options.dpi !== undefined)
|
|
42
|
+
argv.push("--dpi", String(options.dpi));
|
|
43
|
+
return argv;
|
|
44
|
+
},
|
|
45
|
+
parse: parseTesseractOsd,
|
|
46
|
+
// The mode 1 finds the orientation, turns the page and then finds the blocks of the page
|
|
47
|
+
psm: 1,
|
|
48
|
+
},
|
|
35
49
|
};
|
|
36
50
|
//# sourceMappingURL=engine.js.map
|
package/dist/engine.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"engine.js","sourceRoot":"","sources":["../src/engine.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,SAAS,EAAoB,MAAM,eAAe,CAAC;
|
|
1
|
+
{"version":3,"file":"engine.js","sourceRoot":"","sources":["../src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AAG,CAAC,KAAK,EAAE,QAAQ,EAAE,OAAO,EAAE,GAAG,CAAC,CAAC;YAC7C,IAAI,OAAO,CAAC,GAAG,KAAK,SAAS;gBAAE,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC;YACvE,OAAO,IAAI,CAAC;QACd,CAAC;QACD,KAAK,EAAE,iBAAiB;QACxB,yFAAyF;QACzF,GAAG,EAAE,CAAC;KACP;CACF,CAAC"}
|
package/dist/osd.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The parse of the orientation output of tesseract (`tesseract <image> stdout --psm 0`). This file
|
|
3
|
+
* starts no process and reads no file.
|
|
4
|
+
*
|
|
5
|
+
* The output has lines, for example `Orientation in degrees: 270`, `Rotate: 90` and
|
|
6
|
+
* `Orientation confidence: 2.00`. `Rotate` is the turn in the clock direction that makes the page
|
|
7
|
+
* upright.
|
|
8
|
+
*/
|
|
9
|
+
import { type OcrTurn } from "./text.js";
|
|
10
|
+
/** The orientation of a page: the turn that makes it upright, and the confidence of the engine. */
|
|
11
|
+
export interface OcrOrientation {
|
|
12
|
+
/** The turn in the clock direction that makes the page upright, in degrees. */
|
|
13
|
+
readonly turn: OcrTurn;
|
|
14
|
+
/**
|
|
15
|
+
* The confidence of the engine in the turn. For tesseract, it is the margin of the best
|
|
16
|
+
* orientation over the next orientation. It has no upper limit, and a value below 2 is weak.
|
|
17
|
+
* A page with too few letters gives 0.
|
|
18
|
+
*/
|
|
19
|
+
readonly confidence: number;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* This function parses the orientation output of tesseract. An output with no valid `Rotate` line
|
|
23
|
+
* (for example, a page with too few letters) gives the turn 0 and the confidence 0.
|
|
24
|
+
*/
|
|
25
|
+
export declare function parseTesseractOsd(output: string): OcrOrientation;
|
|
26
|
+
//# sourceMappingURL=osd.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"osd.d.ts","sourceRoot":"","sources":["../src/osd.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAa,KAAK,OAAO,EAAE,MAAM,WAAW,CAAC;AAEpD,mGAAmG;AACnG,MAAM,WAAW,cAAc;IAC7B,+EAA+E;IAC/E,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC;IACvB;;;;OAIG;IACH,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;CAC7B;AAWD;;;GAGG;AACH,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,MAAM,GAAG,cAAc,CAKhE"}
|
package/dist/osd.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The parse of the orientation output of tesseract (`tesseract <image> stdout --psm 0`). This file
|
|
3
|
+
* starts no process and reads no file.
|
|
4
|
+
*
|
|
5
|
+
* The output has lines, for example `Orientation in degrees: 270`, `Rotate: 90` and
|
|
6
|
+
* `Orientation confidence: 2.00`. `Rotate` is the turn in the clock direction that makes the page
|
|
7
|
+
* upright.
|
|
8
|
+
*/
|
|
9
|
+
import { OCR_TURNS } from "./text.js";
|
|
10
|
+
/** This function gives the number after `<label>:` on a line of the output, or undefined. */
|
|
11
|
+
function field(output, label) {
|
|
12
|
+
// The label is a fixed word of this file, so it needs no escape
|
|
13
|
+
const match = new RegExp(String.raw `^\s*${label}\s*:\s*(-?[0-9.]+)\s*$`, "im").exec(output);
|
|
14
|
+
if (match === null)
|
|
15
|
+
return undefined;
|
|
16
|
+
const value = Number(match[1]);
|
|
17
|
+
return Number.isFinite(value) ? value : undefined;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* This function parses the orientation output of tesseract. An output with no valid `Rotate` line
|
|
21
|
+
* (for example, a page with too few letters) gives the turn 0 and the confidence 0.
|
|
22
|
+
*/
|
|
23
|
+
export function parseTesseractOsd(output) {
|
|
24
|
+
const rotate = field(output, "Rotate");
|
|
25
|
+
const turn = OCR_TURNS.find((value) => value === rotate);
|
|
26
|
+
if (turn === undefined)
|
|
27
|
+
return { turn: 0, confidence: 0 };
|
|
28
|
+
return { turn, confidence: Math.max(0, field(output, "Orientation confidence") ?? 0) };
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=osd.js.map
|
package/dist/osd.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"osd.js","sourceRoot":"","sources":["../src/osd.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,SAAS,EAAgB,MAAM,WAAW,CAAC;AAcpD,6FAA6F;AAC7F,SAAS,KAAK,CAAC,MAAc,EAAE,KAAa;IAC1C,gEAAgE;IAChE,MAAM,KAAK,GAAG,IAAI,MAAM,CAAC,MAAM,CAAC,GAAG,CAAA,OAAO,KAAK,wBAAwB,EAAE,IAAI,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IAC5F,IAAI,KAAK,KAAK,IAAI;QAAE,OAAO,SAAS,CAAC;IACrC,MAAM,KAAK,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;IAC/B,OAAO,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS,CAAC;AACpD,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,iBAAiB,CAAC,MAAc;IAC9C,MAAM,MAAM,GAAG,KAAK,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;IACvC,MAAM,IAAI,GAAG,SAAS,CAAC,IAAI,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,KAAK,MAAM,CAAC,CAAC;IACzD,IAAI,IAAI,KAAK,SAAS;QAAE,OAAO,EAAE,IAAI,EAAE,CAAC,EAAE,UAAU,EAAE,CAAC,EAAE,CAAC;IAC1D,OAAO,EAAE,IAAI,EAAE,UAAU,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,KAAK,CAAC,MAAM,EAAE,wBAAwB,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC;AACzF,CAAC"}
|
package/dist/parse.d.ts
CHANGED
package/dist/parse.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"parse.d.ts","sourceRoot":"","sources":["../src/parse.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,cAAc,WAAW,CAAC;AAC1B,cAAc,UAAU,CAAC;AACzB,cAAc,WAAW,CAAC"}
|
|
1
|
+
{"version":3,"file":"parse.d.ts","sourceRoot":"","sources":["../src/parse.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,cAAc,WAAW,CAAC;AAC1B,cAAc,UAAU,CAAC;AACzB,cAAc,WAAW,CAAC;AAC1B,cAAc,UAAU,CAAC"}
|
package/dist/parse.js
CHANGED
package/dist/parse.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"parse.js","sourceRoot":"","sources":["../src/parse.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,cAAc,WAAW,CAAC;AAC1B,cAAc,UAAU,CAAC;AACzB,cAAc,WAAW,CAAC"}
|
|
1
|
+
{"version":3,"file":"parse.js","sourceRoot":"","sources":["../src/parse.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,cAAc,WAAW,CAAC;AAC1B,cAAc,UAAU,CAAC;AACzB,cAAc,WAAW,CAAC;AAC1B,cAAc,UAAU,CAAC"}
|
|
@@ -8,7 +8,8 @@ import { type OcrRunner } from "../../run.js";
|
|
|
8
8
|
import type { OcrImageInput, OcrImageOutput } from "../../types.js";
|
|
9
9
|
/**
|
|
10
10
|
* This function reads the text of the image file of the input. It gives each line and each word
|
|
11
|
-
* with its text, its confidence and its box, in the pixels of the image.
|
|
11
|
+
* with its text, its confidence and its box, in the pixels of the image. With `upright`, the boxes
|
|
12
|
+
* are in the pixels of the upright image.
|
|
12
13
|
*/
|
|
13
14
|
export declare function ocrImage(input: OcrImageInput, ctx: Pick<ProcedureContext, "client">, runner?: OcrRunner): Promise<OcrImageOutput>;
|
|
14
15
|
//# sourceMappingURL=image.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"image.d.ts","sourceRoot":"","sources":["../../../src/procedures/ocr/image.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AAC3D,OAAO,EAAuC,KAAK,SAAS,EAAE,MAAM,cAAc,CAAC;AACnF,OAAO,KAAK,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;
|
|
1
|
+
{"version":3,"file":"image.d.ts","sourceRoot":"","sources":["../../../src/procedures/ocr/image.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AAC3D,OAAO,EAAuC,KAAK,SAAS,EAAE,MAAM,cAAc,CAAC;AACnF,OAAO,KAAK,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAGpE;;;;GAIG;AACH,wBAAsB,QAAQ,CAAC,KAAK,EAAE,aAAa,EAAE,GAAG,EAAE,IAAI,CAAC,gBAAgB,EAAE,QAAQ,CAAC,EAAE,MAAM,GAAE,SAA6B,GAAG,OAAO,CAAC,cAAc,CAAC,CAK1J"}
|
|
@@ -5,15 +5,16 @@
|
|
|
5
5
|
*/
|
|
6
6
|
import { resolve } from "node:path";
|
|
7
7
|
import { createOcrRunner, DEFAULT_TIMEOUT_MS } from "../../run.js";
|
|
8
|
+
import { recognize } from "./recognize.js";
|
|
8
9
|
/**
|
|
9
10
|
* This function reads the text of the image file of the input. It gives each line and each word
|
|
10
|
-
* with its text, its confidence and its box, in the pixels of the image.
|
|
11
|
+
* with its text, its confidence and its box, in the pixels of the image. With `upright`, the boxes
|
|
12
|
+
* are in the pixels of the upright image.
|
|
11
13
|
*/
|
|
12
14
|
export async function ocrImage(input, ctx, runner = createOcrRunner()) {
|
|
13
|
-
const { engine } = runner;
|
|
14
15
|
// An absolute path does not start with "-", so the engine cannot read it as a flag
|
|
15
|
-
const
|
|
16
|
-
const
|
|
17
|
-
return { engine: engine.name, ...
|
|
16
|
+
const options = { language: input.language, psm: input.psm, dpi: input.dpi, upright: input.upright, timeout: input.timeout ?? DEFAULT_TIMEOUT_MS };
|
|
17
|
+
const { text, orientation } = await recognize(runner, resolve(input.file), options, ctx);
|
|
18
|
+
return { engine: runner.engine.name, ...text, ...(orientation === undefined ? {} : { orientation }) };
|
|
18
19
|
}
|
|
19
20
|
//# sourceMappingURL=image.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"image.js","sourceRoot":"","sources":["../../../src/procedures/ocr/image.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAEpC,OAAO,EAAE,eAAe,EAAE,kBAAkB,EAAkB,MAAM,cAAc,CAAC;
|
|
1
|
+
{"version":3,"file":"image.js","sourceRoot":"","sources":["../../../src/procedures/ocr/image.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAEpC,OAAO,EAAE,eAAe,EAAE,kBAAkB,EAAkB,MAAM,cAAc,CAAC;AAEnF,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAE3C;;;;GAIG;AACH,MAAM,CAAC,KAAK,UAAU,QAAQ,CAAC,KAAoB,EAAE,GAAqC,EAAE,MAAM,GAAc,eAAe,EAAE;IAC/H,mFAAmF;IACnF,MAAM,OAAO,GAAG,EAAE,QAAQ,EAAE,KAAK,CAAC,QAAQ,EAAE,GAAG,EAAE,KAAK,CAAC,GAAG,EAAE,GAAG,EAAE,KAAK,CAAC,GAAG,EAAE,OAAO,EAAE,KAAK,CAAC,OAAO,EAAE,OAAO,EAAE,KAAK,CAAC,OAAO,IAAI,kBAAkB,EAAE,CAAC;IACnJ,MAAM,EAAE,IAAI,EAAE,WAAW,EAAE,GAAG,MAAM,SAAS,CAAC,MAAM,EAAE,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,GAAG,CAAC,CAAC;IACzF,OAAO,EAAE,MAAM,EAAE,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,GAAG,IAAI,EAAE,GAAG,CAAC,WAAW,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,WAAW,EAAE,CAAC,EAAE,CAAC;AACxG,CAAC"}
|
|
@@ -17,7 +17,8 @@ export declare function pdftoppmArgs(file: string, page: number, dpi: number, pr
|
|
|
17
17
|
/**
|
|
18
18
|
* This function gives the text of one page of the PDF file of the input. The boxes are in PDF
|
|
19
19
|
* points. In the mode `auto`, a page with a word in its text layer gives the text layer. A page
|
|
20
|
-
* with no word gives the text of OCR.
|
|
20
|
+
* with no word gives the text of OCR. With `upright`, OCR finds the orientation of the page first,
|
|
21
|
+
* and the boxes and the size are in the frame of the upright page.
|
|
21
22
|
*/
|
|
22
23
|
export declare function ocrPage(input: OcrPageInput, ctx: Pick<ProcedureContext, "client">, runner?: OcrRunner): Promise<OcrPageOutput>;
|
|
23
24
|
//# sourceMappingURL=page.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"page.d.ts","sourceRoot":"","sources":["../../../src/procedures/ocr/page.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAKH,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AAG3D,OAAO,EAAuC,KAAK,SAAS,EAAE,MAAM,cAAc,CAAC;AAEnF,OAAO,KAAK,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;
|
|
1
|
+
{"version":3,"file":"page.d.ts","sourceRoot":"","sources":["../../../src/procedures/ocr/page.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAKH,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AAG3D,OAAO,EAAuC,KAAK,SAAS,EAAE,MAAM,cAAc,CAAC;AAEnF,OAAO,KAAK,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAGlE,+EAA+E;AAC/E,eAAO,MAAM,WAAW,MAAM,CAAC;AAK/B,kGAAkG;AAClG,wBAAgB,aAAa,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,EAAE,CAElE;AAED,2GAA2G;AAC3G,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,CAE9F;AAED;;;;;GAKG;AACH,wBAAsB,OAAO,CAAC,KAAK,EAAE,YAAY,EAAE,GAAG,EAAE,IAAI,CAAC,gBAAgB,EAAE,QAAQ,CAAC,EAAE,MAAM,GAAE,SAA6B,GAAG,OAAO,CAAC,aAAa,CAAC,CA4BvJ"}
|
|
@@ -12,6 +12,7 @@ import { parsePdftotextBbox } from "../../bbox.js";
|
|
|
12
12
|
import { PDFTOPPM, PDFTOTEXT } from "../../programs.js";
|
|
13
13
|
import { createOcrRunner, DEFAULT_TIMEOUT_MS } from "../../run.js";
|
|
14
14
|
import { scaleText } from "../../text.js";
|
|
15
|
+
import { recognize } from "./recognize.js";
|
|
15
16
|
/** The default resolution of the image of a page for OCR, in dots per inch. */
|
|
16
17
|
export const DEFAULT_DPI = 300;
|
|
17
18
|
/** The PDF points in one inch. */
|
|
@@ -27,7 +28,8 @@ export function pdftoppmArgs(file, page, dpi, prefix) {
|
|
|
27
28
|
/**
|
|
28
29
|
* This function gives the text of one page of the PDF file of the input. The boxes are in PDF
|
|
29
30
|
* points. In the mode `auto`, a page with a word in its text layer gives the text layer. A page
|
|
30
|
-
* with no word gives the text of OCR.
|
|
31
|
+
* with no word gives the text of OCR. With `upright`, OCR finds the orientation of the page first,
|
|
32
|
+
* and the boxes and the size are in the frame of the upright page.
|
|
31
33
|
*/
|
|
32
34
|
export async function ocrPage(input, ctx, runner = createOcrRunner()) {
|
|
33
35
|
// An absolute path does not start with "-", so a program cannot read it as a flag
|
|
@@ -48,12 +50,12 @@ export async function ocrPage(input, ctx, runner = createOcrRunner()) {
|
|
|
48
50
|
const image = `${prefix}.png`;
|
|
49
51
|
if (!existsSync(image))
|
|
50
52
|
throw new Error(`pdftoppm made no image of page ${page}`);
|
|
51
|
-
const
|
|
52
|
-
const
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
const size = layer.width > 0 && layer.height > 0 ? { width: layer.width, height: layer.height } : {};
|
|
56
|
-
return { page, source: "ocr", engine: engine.name, ...points, ...size };
|
|
53
|
+
const { text, orientation } = await recognize(runner, image, { language: input.language, psm: input.psm, dpi, upright: input.upright, timeout }, ctx);
|
|
54
|
+
const points = scaleText(text, POINTS_PER_INCH / dpi, "pt");
|
|
55
|
+
// The size of the page comes from the text layer, when it has one. A turn on the side changes the width and the height
|
|
56
|
+
const side = orientation?.turn === 90 || orientation?.turn === 270;
|
|
57
|
+
const size = layer.width > 0 && layer.height > 0 ? (side ? { width: layer.height, height: layer.width } : { width: layer.width, height: layer.height }) : {};
|
|
58
|
+
return { page, source: "ocr", engine: engine.name, ...points, ...size, ...(orientation === undefined ? {} : { orientation }) };
|
|
57
59
|
}
|
|
58
60
|
finally {
|
|
59
61
|
removeFolder(folder);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"page.js","sourceRoot":"","sources":["../../../src/procedures/ocr/page.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AAC1D,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AACjC,OAAO,EAAE,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,EAAE,MAAM,EAAE,MAAM,CAAC,IAAI,EAAE,GAAG,MAAM,EAAE,GAAG,IAAI,EAAE,GAAG,CAAC,WAAW,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,WAAW,EAAE,CAAC,EAAE,CAAC;IACjI,CAAC;YAAS,CAAC;QACT,YAAY,CAAC,MAAM,CAAC,CAAC;IACvB,CAAC;AACH,CAAC;AAED;;;GAGG;AACH,SAAS,YAAY,CAAC,MAAc;IAClC,IAAI,CAAC;QACH,MAAM,CAAC,MAAM,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,UAAU,EAAE,CAAC,EAAE,UAAU,EAAE,GAAG,EAAE,CAAC,CAAC;IACnF,CAAC;IAAC,MAAM,CAAC;QACP,yDAAyD;IAC3D,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The recognition of one image by the engine, with the optional turn of a page on its side.
|
|
3
|
+
*/
|
|
4
|
+
import type { ProcedureContext } from "@procedures/client";
|
|
5
|
+
import type { OcrOrientation } from "../../osd.js";
|
|
6
|
+
import type { OcrRunner } from "../../run.js";
|
|
7
|
+
import { type OcrText } from "../../text.js";
|
|
8
|
+
/** The options of one recognition. */
|
|
9
|
+
export interface RecognizeOptions {
|
|
10
|
+
readonly language?: string | undefined;
|
|
11
|
+
readonly psm?: number | undefined;
|
|
12
|
+
readonly dpi?: number | undefined;
|
|
13
|
+
/** When true, the engine finds the orientation first, and the boxes are in the frame of the upright page. */
|
|
14
|
+
readonly upright?: boolean | undefined;
|
|
15
|
+
readonly timeout: number;
|
|
16
|
+
}
|
|
17
|
+
/** The text of one image, in pixels, and the orientation when the options ask for the upright page. */
|
|
18
|
+
export interface Recognition {
|
|
19
|
+
readonly text: OcrText;
|
|
20
|
+
readonly orientation?: OcrOrientation | undefined;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* This function reads the text of one image. `image` is an absolute path. With `upright`, the
|
|
24
|
+
* engine finds the orientation of the page first. The engine then reads a page on its side in the
|
|
25
|
+
* mode of its orientation step, unless the options give a mode. Then each box is turned into the
|
|
26
|
+
* frame of the upright page.
|
|
27
|
+
*/
|
|
28
|
+
export declare function recognize(runner: OcrRunner, image: string, options: RecognizeOptions, ctx: Pick<ProcedureContext, "client">): Promise<Recognition>;
|
|
29
|
+
//# sourceMappingURL=recognize.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"recognize.d.ts","sourceRoot":"","sources":["../../../src/procedures/ocr/recognize.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AAC3D,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AACnD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAC9C,OAAO,EAAE,KAAK,OAAO,EAAY,MAAM,eAAe,CAAC;AAEvD,sCAAsC;AACtC,MAAM,WAAW,gBAAgB;IAC/B,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IACvC,QAAQ,CAAC,GAAG,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IAClC,QAAQ,CAAC,GAAG,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IAClC,6GAA6G;IAC7G,QAAQ,CAAC,OAAO,CAAC,EAAE,OAAO,GAAG,SAAS,CAAC;IACvC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;CAC1B;AAED,uGAAuG;AACvG,MAAM,WAAW,WAAW;IAC1B,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC;IACvB,QAAQ,CAAC,WAAW,CAAC,EAAE,cAAc,GAAG,SAAS,CAAC;CACnD;AAED;;;;;GAKG;AACH,wBAAsB,SAAS,CAAC,MAAM,EAAE,SAAS,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,gBAAgB,EAAE,GAAG,EAAE,IAAI,CAAC,gBAAgB,EAAE,QAAQ,CAAC,GAAG,OAAO,CAAC,WAAW,CAAC,CAaxJ"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The recognition of one image by the engine, with the optional turn of a page on its side.
|
|
3
|
+
*/
|
|
4
|
+
import { turnText } from "../../text.js";
|
|
5
|
+
/**
|
|
6
|
+
* This function reads the text of one image. `image` is an absolute path. With `upright`, the
|
|
7
|
+
* engine finds the orientation of the page first. The engine then reads a page on its side in the
|
|
8
|
+
* mode of its orientation step, unless the options give a mode. Then each box is turned into the
|
|
9
|
+
* frame of the upright page.
|
|
10
|
+
*/
|
|
11
|
+
export async function recognize(runner, image, options, ctx) {
|
|
12
|
+
const { engine } = runner;
|
|
13
|
+
const read = async (psm) => {
|
|
14
|
+
const argv = engine.argv(image, { language: options.language, psm, dpi: options.dpi });
|
|
15
|
+
return engine.parse(await runner.run(engine.program, argv, options.timeout, ctx));
|
|
16
|
+
};
|
|
17
|
+
if (options.upright !== true)
|
|
18
|
+
return { text: await read(options.psm) };
|
|
19
|
+
const step = engine.orientation;
|
|
20
|
+
if (step === undefined)
|
|
21
|
+
throw new Error(`The OCR engine ${engine.name} cannot find the orientation of a page`);
|
|
22
|
+
const orientation = step.parse(await runner.run(engine.program, step.argv(image, { dpi: options.dpi }), options.timeout, ctx));
|
|
23
|
+
if (orientation.turn === 0)
|
|
24
|
+
return { text: await read(options.psm), orientation };
|
|
25
|
+
return { text: turnText(await read(options.psm ?? step.psm), orientation.turn), orientation };
|
|
26
|
+
}
|
|
27
|
+
//# sourceMappingURL=recognize.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"recognize.js","sourceRoot":"","sources":["../../../src/procedures/ocr/recognize.ts"],"names":[],"mappings":"AAAA;;GAEG;AAKH,OAAO,EAAgB,QAAQ,EAAE,MAAM,eAAe,CAAC;AAkBvD;;;;;GA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AAC,MAAM,IAAI,CAAC,OAAO,CAAC,GAAG,IAAI,IAAI,CAAC,GAAG,CAAC,EAAE,WAAW,CAAC,IAAI,CAAC,EAAE,WAAW,EAAE,CAAC;AAChG,CAAC"}
|
package/dist/text.d.ts
CHANGED
|
@@ -53,4 +53,21 @@ export declare function makeText(lines: readonly OcrLine[], unit: OcrUnit, width
|
|
|
53
53
|
* example, the factor `72 / 300` changes the pixels of an image at 300 dots per inch to PDF points.
|
|
54
54
|
*/
|
|
55
55
|
export declare function scaleText(text: OcrText, factor: number, unit: OcrUnit): OcrText;
|
|
56
|
+
/** A turn of a page in the clock direction, in degrees: the turn that makes a page upright. */
|
|
57
|
+
export type OcrTurn = 0 | 90 | 180 | 270;
|
|
58
|
+
/** The turns in the clock direction that a page can have. */
|
|
59
|
+
export declare const OCR_TURNS: readonly OcrTurn[];
|
|
60
|
+
/**
|
|
61
|
+
* This function gives the box in the frame of the page after a turn of the page in the clock
|
|
62
|
+
* direction. `width` and `height` are the size of the page before the turn.
|
|
63
|
+
*/
|
|
64
|
+
export declare function turnBox(box: Rect, turn: OcrTurn, width: number, height: number): Rect;
|
|
65
|
+
/**
|
|
66
|
+
* This function turns each box, the width and the height of a text by `turn` degrees in the clock
|
|
67
|
+
* direction. For example, an engine that reads a scan on its side can give the boxes in the frame
|
|
68
|
+
* of the scan. The turn that makes the scan upright then gives the boxes in the frame of the upright
|
|
69
|
+
* page. A turn of 90 or 270 degrees changes the width and the height. The text and the order of the
|
|
70
|
+
* lines do not change.
|
|
71
|
+
*/
|
|
72
|
+
export declare function turnText(text: OcrText, turn: OcrTurn): OcrText;
|
|
56
73
|
//# sourceMappingURL=text.d.ts.map
|
package/dist/text.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"text.d.ts","sourceRoot":"","sources":["../src/text.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,gCAAgC,CAAC;AAE3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
|
|
|
74
74
|
})),
|
|
75
75
|
};
|
|
76
76
|
}
|
|
77
|
+
/** The turns in the clock direction that a page can have. */
|
|
78
|
+
export const OCR_TURNS = [0, 90, 180, 270];
|
|
79
|
+
/**
|
|
80
|
+
* This function gives the box in the frame of the page after a turn of the page in the clock
|
|
81
|
+
* direction. `width` and `height` are the size of the page before the turn.
|
|
82
|
+
*/
|
|
83
|
+
export function turnBox(box, turn, width, height) {
|
|
84
|
+
switch (turn) {
|
|
85
|
+
case 0:
|
|
86
|
+
return box;
|
|
87
|
+
case 90:
|
|
88
|
+
return { x: height - (box.y + box.h), y: box.x, w: box.h, h: box.w };
|
|
89
|
+
case 180:
|
|
90
|
+
return { x: width - (box.x + box.w), y: height - (box.y + box.h), w: box.w, h: box.h };
|
|
91
|
+
case 270:
|
|
92
|
+
return { x: box.y, y: width - (box.x + box.w), w: box.h, h: box.w };
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* This function turns each box, the width and the height of a text by `turn` degrees in the clock
|
|
97
|
+
* direction. For example, an engine that reads a scan on its side can give the boxes in the frame
|
|
98
|
+
* of the scan. The turn that makes the scan upright then gives the boxes in the frame of the upright
|
|
99
|
+
* page. A turn of 90 or 270 degrees changes the width and the height. The text and the order of the
|
|
100
|
+
* lines do not change.
|
|
101
|
+
*/
|
|
102
|
+
export function turnText(text, turn) {
|
|
103
|
+
if (!OCR_TURNS.includes(turn))
|
|
104
|
+
throw new RangeError(`The turn must be 0, 90, 180 or 270 degrees, not ${String(turn)}`);
|
|
105
|
+
if (turn === 0)
|
|
106
|
+
return text;
|
|
107
|
+
const { width, height } = text;
|
|
108
|
+
const side = turn === 90 || turn === 270;
|
|
109
|
+
return {
|
|
110
|
+
...text,
|
|
111
|
+
width: side ? height : width,
|
|
112
|
+
height: side ? width : height,
|
|
113
|
+
lines: text.lines.map((line) => ({
|
|
114
|
+
...line,
|
|
115
|
+
box: turnBox(line.box, turn, width, height),
|
|
116
|
+
words: line.words.map((word) => ({ ...word, box: turnBox(word.box, turn, width, height) })),
|
|
117
|
+
})),
|
|
118
|
+
};
|
|
119
|
+
}
|
|
77
120
|
//# sourceMappingURL=text.js.map
|
package/dist/text.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"text.js","sourceRoot":"","sources":["../src/text.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AA0CH,uHAAuH;AACvH,MAAM,UAAU,QAAQ,CAAC,KAAsB;IAC7C,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC;IAC1D,IAAI,IAAI,GAAG,QAAQ,CAAC;IACpB,IAAI,GAAG,GAAG,QAAQ,CAAC;IACnB,IAAI,KAAK,GAAG,CAAC,QAAQ,CAAC;IACtB,IAAI,MAAM,GAAG,CAAC,QAAQ,CAAC;IACvB,KAAK,MAAM,GAAG,IAAI,KAAK,EAAE,CAAC;QACxB,IAAI,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC,CAAC,CAAC;QAC7B,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,CAAC,CAAC;QAC3B,KAAK,GAAG,IAAI,CAAC,GAAG,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC,GAAG,GAAG,CAAC,CAAC,CAAC,CAAC;QACvC,MAAM,GAAG,IAAI,CAAC,GAAG,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC,GAAG,GAAG,CAAC,CAAC,CAAC,CAAC;IAC3C,CAAC;IACD,OAAO,EAAE,CAAC,EAAE,IAAI,EAAE,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,KAAK,GAAG,IAAI,EAAE,CAAC,EAAE,MAAM,GAAG,GAAG,EAAE,CAAC;AAC/D,CAAC;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,CAAC,KAAK;QAC5B,MAAM,EAAE,IAAI,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,MAAM;QAC7B,KAAK,EAAE,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;YAC/B,GAAG,IAAI;YACP,GAAG,EAAE,OAAO,CAAC,IAAI,CAAC,GAAG,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,CAAC;YAC3C,KAAK,EAAE,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,OAAO,CAAC,IAAI,CAAC,GAAG,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,CAAC,EAAE,CAAC,CAAC;SAC5F,CAAC,CAAC;KACJ,CAAC;AACJ,CAAC"}
|
package/dist/types.d.ts
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
* The input and the output of the procedures of this package.
|
|
3
3
|
*/
|
|
4
4
|
import * as z from "zod";
|
|
5
|
+
import type { OcrOrientation } from "./osd.js";
|
|
5
6
|
import type { OcrText } from "./text.js";
|
|
6
7
|
/** How `ocr.page` gets the text: `auto` uses OCR only for a page with no text layer. */
|
|
7
8
|
export type OcrPageMode = "auto" | "text" | "ocr";
|
|
@@ -14,6 +15,8 @@ export interface OcrPageInput {
|
|
|
14
15
|
psm?: number | undefined;
|
|
15
16
|
dpi?: number | undefined;
|
|
16
17
|
timeout?: number | undefined;
|
|
18
|
+
/** When true, OCR finds the orientation of the page first, and the boxes are in the frame of the upright page. */
|
|
19
|
+
upright?: boolean | undefined;
|
|
17
20
|
}
|
|
18
21
|
export declare const OcrPageInputSchema: z.ZodType<OcrPageInput>;
|
|
19
22
|
/** The output of `ocr.page`. The boxes are in PDF points. */
|
|
@@ -24,6 +27,8 @@ export interface OcrPageOutput extends OcrText {
|
|
|
24
27
|
readonly source: "text-layer" | "ocr";
|
|
25
28
|
/** The OCR engine, when the source is `ocr`. */
|
|
26
29
|
readonly engine?: string | undefined;
|
|
30
|
+
/** The orientation that OCR found, when the input has `upright` and the source is `ocr`. */
|
|
31
|
+
readonly orientation?: OcrOrientation | undefined;
|
|
27
32
|
}
|
|
28
33
|
export declare const OcrPageOutputSchema: z.ZodType<OcrPageOutput>;
|
|
29
34
|
/** The input of `ocr.image`. */
|
|
@@ -33,12 +38,16 @@ export interface OcrImageInput {
|
|
|
33
38
|
psm?: number | undefined;
|
|
34
39
|
dpi?: number | undefined;
|
|
35
40
|
timeout?: number | undefined;
|
|
41
|
+
/** When true, OCR finds the orientation of the page first, and the boxes are in the frame of the upright page. */
|
|
42
|
+
upright?: boolean | undefined;
|
|
36
43
|
}
|
|
37
44
|
export declare const OcrImageInputSchema: z.ZodType<OcrImageInput>;
|
|
38
45
|
/** The output of `ocr.image`. The boxes are in the pixels of the image. */
|
|
39
46
|
export interface OcrImageOutput extends OcrText {
|
|
40
47
|
/** The OCR engine. */
|
|
41
48
|
readonly engine: string;
|
|
49
|
+
/** The orientation that OCR found, when the input has `upright`. */
|
|
50
|
+
readonly orientation?: OcrOrientation | undefined;
|
|
42
51
|
}
|
|
43
52
|
export declare const OcrImageOutputSchema: z.ZodType<OcrImageOutput>;
|
|
44
53
|
//# sourceMappingURL=types.d.ts.map
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,CAAC,MAAM,KAAK,CAAC;AAEzB,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,CAAC,MAAM,KAAK,CAAC;AAEzB,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,UAAU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CAAC;IACzB,GAAG,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IACzB,OAAO,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IAC7B,kHAAkH;IAClH,OAAO,CAAC,EAAE,OAAO,GAAG,SAAS,CAAC;CAC/B;AAED,eAAO,MAAM,mBAAmB,EAAE,CAAC,CAAC,OAAO,CAAC,aAAa,CAIvD,CAAC;AAEH,2EAA2E;AAC3E,MAAM,WAAW,cAAe,SAAQ,OAAO;IAC7C,sBAAsB;IACtB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,oEAAoE;IACpE,QAAQ,CAAC,WAAW,CAAC,EAAE,cAAc,GAAG,SAAS,CAAC;CACnD;AAED,eAAO,MAAM,oBAAoB,EAAE,CAAC,CAAC,OAAO,CAAC,cAAc,CAIzD,CAAC"}
|
package/dist/types.js
CHANGED
|
@@ -35,7 +35,15 @@ const engineFields = {
|
|
|
35
35
|
language: z.string().regex(LANGUAGE_PATTERN, "a language name of the engine, for example eng or eng+deu").optional().describe("The language of the text (default: eng)"),
|
|
36
36
|
psm: z.number().int().min(0).max(13).optional().describe("The page segmentation mode of tesseract, from 0 to 13"),
|
|
37
37
|
timeout: z.number().int().positive().optional().describe("The time limit of each program, in milliseconds (default: 120000)"),
|
|
38
|
+
upright: z
|
|
39
|
+
.boolean()
|
|
40
|
+
.optional()
|
|
41
|
+
.describe("When true, OCR finds the orientation of the page first, and the boxes are in the frame of the upright page (default: false)"),
|
|
38
42
|
};
|
|
43
|
+
const OrientationSchema = z.object({
|
|
44
|
+
turn: z.union([z.literal(0), z.literal(90), z.literal(180), z.literal(270)]).describe("The turn in the clock direction that makes the page upright, in degrees"),
|
|
45
|
+
confidence: z.number().min(0).describe("The confidence of the engine in the turn. For tesseract, a value below 2 is weak"),
|
|
46
|
+
});
|
|
39
47
|
export const OcrPageInputSchema = z.object({
|
|
40
48
|
file: z.string().min(1).describe("The path of the PDF file"),
|
|
41
49
|
page: z.number().int().min(1).optional().describe("The number of the page, from 1 (default: 1)"),
|
|
@@ -50,6 +58,7 @@ export const OcrPageOutputSchema = z.object({
|
|
|
50
58
|
page: z.number().describe("The number of the page"),
|
|
51
59
|
source: z.enum(["text-layer", "ocr"]).describe("Where the text comes from: the text layer, or OCR of an image of the page"),
|
|
52
60
|
engine: z.string().optional().describe("The OCR engine, when the source is ocr"),
|
|
61
|
+
orientation: OrientationSchema.optional().describe("The orientation that OCR found, when the input has upright and the source is ocr"),
|
|
53
62
|
...textFields,
|
|
54
63
|
});
|
|
55
64
|
export const OcrImageInputSchema = z.object({
|
|
@@ -59,6 +68,7 @@ export const OcrImageInputSchema = z.object({
|
|
|
59
68
|
});
|
|
60
69
|
export const OcrImageOutputSchema = z.object({
|
|
61
70
|
engine: z.string().describe("The OCR engine"),
|
|
71
|
+
orientation: OrientationSchema.optional().describe("The orientation that OCR found, when the input has upright"),
|
|
62
72
|
...textFields,
|
|
63
73
|
});
|
|
64
74
|
//# sourceMappingURL=types.js.map
|
package/dist/types.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,CAAC,MAAM,KAAK,CAAC;AACzB,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,CAAC,MAAM,KAAK,CAAC;AACzB,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAI/C,gFAAgF;AAChF,mBAAmB;AACnB,gFAAgF;AAEhF,MAAM,SAAS,GAAG,CAAC,CAAC,MAAM,CAAC;IACzB,CAAC,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,CAAC,0BAA0B,CAAC;IAClD,CAAC,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,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CAAC,CAAC,QAAQ,CAAC,kEAAkE,CAAC;IACpG,GAAG,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,QAAQ,EAAE,CAAC,QAAQ,CAAC,6DAA6D,CAAC;IACzH,GAAG,YAAY;CAChB,CAAC,CAAC;AAUH,MAAM,CAAC,MAAM,oBAAoB,GAA8B,CAAC,CAAC,MAAM,CAAC;IACtE,MAAM,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,CAAC,gBAAgB,CAAC;IAC7C,WAAW,EAAE,iBAAiB,CAAC,QAAQ,EAAE,CAAC,QAAQ,CAAC,4DAA4D,CAAC;IAChH,GAAG,UAAU;CACd,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://json.schemastore.org/package",
|
|
3
3
|
"name": "@procedures/ocr",
|
|
4
|
-
"version": "0.68.
|
|
4
|
+
"version": "0.68.8",
|
|
5
5
|
"description": "The text of a PDF page or an image, with the text, the confidence and the box of each line and word: the text layer of the page first (pdftotext), then OCR with a local engine (tesseract).",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"ocr",
|
|
@@ -81,8 +81,8 @@
|
|
|
81
81
|
},
|
|
82
82
|
"dependencies": {
|
|
83
83
|
"@mark1russell7/layout-contract": "^0.11.1",
|
|
84
|
-
"@procedures/client": "0.68.
|
|
85
|
-
"@procedures/shell": "0.68.
|
|
84
|
+
"@procedures/client": "0.68.8",
|
|
85
|
+
"@procedures/shell": "0.68.8",
|
|
86
86
|
"zod": "^4.6.5"
|
|
87
87
|
},
|
|
88
88
|
"scripts": {
|
package/src/engine.ts
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
7
|
import { TESSERACT, type ProgramSpec } from "./programs.js";
|
|
8
|
+
import { type OcrOrientation, parseTesseractOsd } from "./osd.js";
|
|
8
9
|
import type { OcrText } from "./text.js";
|
|
9
10
|
import { parseTesseractTsv } from "./tsv.js";
|
|
10
11
|
|
|
@@ -18,6 +19,19 @@ export interface OcrEngineOptions {
|
|
|
18
19
|
readonly dpi?: number | undefined;
|
|
19
20
|
}
|
|
20
21
|
|
|
22
|
+
/** The step of an engine that finds the orientation of a page. */
|
|
23
|
+
export interface OcrOrientationStep {
|
|
24
|
+
/** This function gives the argv of the program that finds the orientation of one image. */
|
|
25
|
+
argv(image: string, options: Pick<OcrEngineOptions, "dpi">): string[];
|
|
26
|
+
/** This function parses the standard output of the program. */
|
|
27
|
+
parse(stdout: string): OcrOrientation;
|
|
28
|
+
/**
|
|
29
|
+
* The page segmentation mode that reads a page on its side. The engine turns the page in this
|
|
30
|
+
* mode, but it gives the boxes in the frame of the image.
|
|
31
|
+
*/
|
|
32
|
+
readonly psm: number;
|
|
33
|
+
}
|
|
34
|
+
|
|
21
35
|
/** An OCR engine. */
|
|
22
36
|
export interface OcrEngine {
|
|
23
37
|
/** The name of the engine in the result, for example `tesseract`. */
|
|
@@ -28,6 +42,8 @@ export interface OcrEngine {
|
|
|
28
42
|
argv(image: string, options: OcrEngineOptions): string[];
|
|
29
43
|
/** This function parses the standard output of the program. The boxes are in the pixels of the image. */
|
|
30
44
|
parse(stdout: string): OcrText;
|
|
45
|
+
/** The step that finds the orientation of a page. An engine with no step cannot read a page on its side. */
|
|
46
|
+
readonly orientation?: OcrOrientationStep | undefined;
|
|
31
47
|
}
|
|
32
48
|
|
|
33
49
|
/** The default language of the recognition. */
|
|
@@ -41,7 +57,8 @@ export const LANGUAGE_PATTERN: RegExp = /^[A-Za-z0-9_][A-Za-z0-9_/]*(\+[A-Za-z0-
|
|
|
41
57
|
|
|
42
58
|
/**
|
|
43
59
|
* The adapter of tesseract 4 or later. The argv is `<image> stdout -l <language> [--psm N]
|
|
44
|
-
* [--dpi N] tsv`: tesseract writes the TSV of the image to the standard output.
|
|
60
|
+
* [--dpi N] tsv`: tesseract writes the TSV of the image to the standard output. Its orientation step
|
|
61
|
+
* is `<image> stdout --psm 0 [--dpi N]`.
|
|
45
62
|
*/
|
|
46
63
|
export const tesseractEngine: OcrEngine = {
|
|
47
64
|
name: "tesseract",
|
|
@@ -56,4 +73,15 @@ export const tesseractEngine: OcrEngine = {
|
|
|
56
73
|
return argv;
|
|
57
74
|
},
|
|
58
75
|
parse: parseTesseractTsv,
|
|
76
|
+
orientation: {
|
|
77
|
+
// The mode 0 finds only the orientation and the script. It needs the osd data of tesseract
|
|
78
|
+
argv(image, options) {
|
|
79
|
+
const argv = [image, "stdout", "--psm", "0"];
|
|
80
|
+
if (options.dpi !== undefined) argv.push("--dpi", String(options.dpi));
|
|
81
|
+
return argv;
|
|
82
|
+
},
|
|
83
|
+
parse: parseTesseractOsd,
|
|
84
|
+
// The mode 1 finds the orientation, turns the page and then finds the blocks of the page
|
|
85
|
+
psm: 1,
|
|
86
|
+
},
|
|
59
87
|
};
|
package/src/osd.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The parse of the orientation output of tesseract (`tesseract <image> stdout --psm 0`). This file
|
|
3
|
+
* starts no process and reads no file.
|
|
4
|
+
*
|
|
5
|
+
* The output has lines, for example `Orientation in degrees: 270`, `Rotate: 90` and
|
|
6
|
+
* `Orientation confidence: 2.00`. `Rotate` is the turn in the clock direction that makes the page
|
|
7
|
+
* upright.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { OCR_TURNS, type OcrTurn } from "./text.js";
|
|
11
|
+
|
|
12
|
+
/** The orientation of a page: the turn that makes it upright, and the confidence of the engine. */
|
|
13
|
+
export interface OcrOrientation {
|
|
14
|
+
/** The turn in the clock direction that makes the page upright, in degrees. */
|
|
15
|
+
readonly turn: OcrTurn;
|
|
16
|
+
/**
|
|
17
|
+
* The confidence of the engine in the turn. For tesseract, it is the margin of the best
|
|
18
|
+
* orientation over the next orientation. It has no upper limit, and a value below 2 is weak.
|
|
19
|
+
* A page with too few letters gives 0.
|
|
20
|
+
*/
|
|
21
|
+
readonly confidence: number;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** This function gives the number after `<label>:` on a line of the output, or undefined. */
|
|
25
|
+
function field(output: string, label: string): number | undefined {
|
|
26
|
+
// The label is a fixed word of this file, so it needs no escape
|
|
27
|
+
const match = new RegExp(String.raw`^\s*${label}\s*:\s*(-?[0-9.]+)\s*$`, "im").exec(output);
|
|
28
|
+
if (match === null) return undefined;
|
|
29
|
+
const value = Number(match[1]);
|
|
30
|
+
return Number.isFinite(value) ? value : undefined;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* This function parses the orientation output of tesseract. An output with no valid `Rotate` line
|
|
35
|
+
* (for example, a page with too few letters) gives the turn 0 and the confidence 0.
|
|
36
|
+
*/
|
|
37
|
+
export function parseTesseractOsd(output: string): OcrOrientation {
|
|
38
|
+
const rotate = field(output, "Rotate");
|
|
39
|
+
const turn = OCR_TURNS.find((value) => value === rotate);
|
|
40
|
+
if (turn === undefined) return { turn: 0, confidence: 0 };
|
|
41
|
+
return { turn, confidence: Math.max(0, field(output, "Orientation confidence") ?? 0) };
|
|
42
|
+
}
|
package/src/parse.ts
CHANGED
|
@@ -8,15 +8,16 @@ import { resolve } from "node:path";
|
|
|
8
8
|
import type { ProcedureContext } from "@procedures/client";
|
|
9
9
|
import { createOcrRunner, DEFAULT_TIMEOUT_MS, type OcrRunner } from "../../run.js";
|
|
10
10
|
import type { OcrImageInput, OcrImageOutput } from "../../types.js";
|
|
11
|
+
import { recognize } from "./recognize.js";
|
|
11
12
|
|
|
12
13
|
/**
|
|
13
14
|
* This function reads the text of the image file of the input. It gives each line and each word
|
|
14
|
-
* with its text, its confidence and its box, in the pixels of the image.
|
|
15
|
+
* with its text, its confidence and its box, in the pixels of the image. With `upright`, the boxes
|
|
16
|
+
* are in the pixels of the upright image.
|
|
15
17
|
*/
|
|
16
18
|
export async function ocrImage(input: OcrImageInput, ctx: Pick<ProcedureContext, "client">, runner: OcrRunner = createOcrRunner()): Promise<OcrImageOutput> {
|
|
17
|
-
const { engine } = runner;
|
|
18
19
|
// An absolute path does not start with "-", so the engine cannot read it as a flag
|
|
19
|
-
const
|
|
20
|
-
const
|
|
21
|
-
return { engine: engine.name, ...
|
|
20
|
+
const options = { language: input.language, psm: input.psm, dpi: input.dpi, upright: input.upright, timeout: input.timeout ?? DEFAULT_TIMEOUT_MS };
|
|
21
|
+
const { text, orientation } = await recognize(runner, resolve(input.file), options, ctx);
|
|
22
|
+
return { engine: runner.engine.name, ...text, ...(orientation === undefined ? {} : { orientation }) };
|
|
22
23
|
}
|
|
@@ -15,6 +15,7 @@ import { PDFTOPPM, PDFTOTEXT } from "../../programs.js";
|
|
|
15
15
|
import { createOcrRunner, DEFAULT_TIMEOUT_MS, type OcrRunner } from "../../run.js";
|
|
16
16
|
import { scaleText } from "../../text.js";
|
|
17
17
|
import type { OcrPageInput, OcrPageOutput } from "../../types.js";
|
|
18
|
+
import { recognize } from "./recognize.js";
|
|
18
19
|
|
|
19
20
|
/** The default resolution of the image of a page for OCR, in dots per inch. */
|
|
20
21
|
export const DEFAULT_DPI = 300;
|
|
@@ -35,7 +36,8 @@ export function pdftoppmArgs(file: string, page: number, dpi: number, prefix: st
|
|
|
35
36
|
/**
|
|
36
37
|
* This function gives the text of one page of the PDF file of the input. The boxes are in PDF
|
|
37
38
|
* points. In the mode `auto`, a page with a word in its text layer gives the text layer. A page
|
|
38
|
-
* with no word gives the text of OCR.
|
|
39
|
+
* with no word gives the text of OCR. With `upright`, OCR finds the orientation of the page first,
|
|
40
|
+
* and the boxes and the size are in the frame of the upright page.
|
|
39
41
|
*/
|
|
40
42
|
export async function ocrPage(input: OcrPageInput, ctx: Pick<ProcedureContext, "client">, runner: OcrRunner = createOcrRunner()): Promise<OcrPageOutput> {
|
|
41
43
|
// An absolute path does not start with "-", so a program cannot read it as a flag
|
|
@@ -56,12 +58,12 @@ export async function ocrPage(input: OcrPageInput, ctx: Pick<ProcedureContext, "
|
|
|
56
58
|
await runner.run(PDFTOPPM, pdftoppmArgs(file, page, dpi, prefix), timeout, ctx);
|
|
57
59
|
const image = `${prefix}.png`;
|
|
58
60
|
if (!existsSync(image)) throw new Error(`pdftoppm made no image of page ${page}`);
|
|
59
|
-
const
|
|
60
|
-
const
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
const size = layer.width > 0 && layer.height > 0 ? { width: layer.width, height: layer.height } : {};
|
|
64
|
-
return { page, source: "ocr", engine: engine.name, ...points, ...size };
|
|
61
|
+
const { text, orientation } = await recognize(runner, image, { language: input.language, psm: input.psm, dpi, upright: input.upright, timeout }, ctx);
|
|
62
|
+
const points = scaleText(text, POINTS_PER_INCH / dpi, "pt");
|
|
63
|
+
// The size of the page comes from the text layer, when it has one. A turn on the side changes the width and the height
|
|
64
|
+
const side = orientation?.turn === 90 || orientation?.turn === 270;
|
|
65
|
+
const size = layer.width > 0 && layer.height > 0 ? (side ? { width: layer.height, height: layer.width } : { width: layer.width, height: layer.height }) : {};
|
|
66
|
+
return { page, source: "ocr", engine: engine.name, ...points, ...size, ...(orientation === undefined ? {} : { orientation }) };
|
|
65
67
|
} finally {
|
|
66
68
|
removeFolder(folder);
|
|
67
69
|
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The recognition of one image by the engine, with the optional turn of a page on its side.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import type { ProcedureContext } from "@procedures/client";
|
|
6
|
+
import type { OcrOrientation } from "../../osd.js";
|
|
7
|
+
import type { OcrRunner } from "../../run.js";
|
|
8
|
+
import { type OcrText, turnText } from "../../text.js";
|
|
9
|
+
|
|
10
|
+
/** The options of one recognition. */
|
|
11
|
+
export interface RecognizeOptions {
|
|
12
|
+
readonly language?: string | undefined;
|
|
13
|
+
readonly psm?: number | undefined;
|
|
14
|
+
readonly dpi?: number | undefined;
|
|
15
|
+
/** When true, the engine finds the orientation first, and the boxes are in the frame of the upright page. */
|
|
16
|
+
readonly upright?: boolean | undefined;
|
|
17
|
+
readonly timeout: number;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** The text of one image, in pixels, and the orientation when the options ask for the upright page. */
|
|
21
|
+
export interface Recognition {
|
|
22
|
+
readonly text: OcrText;
|
|
23
|
+
readonly orientation?: OcrOrientation | undefined;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* This function reads the text of one image. `image` is an absolute path. With `upright`, the
|
|
28
|
+
* engine finds the orientation of the page first. The engine then reads a page on its side in the
|
|
29
|
+
* mode of its orientation step, unless the options give a mode. Then each box is turned into the
|
|
30
|
+
* frame of the upright page.
|
|
31
|
+
*/
|
|
32
|
+
export async function recognize(runner: OcrRunner, image: string, options: RecognizeOptions, ctx: Pick<ProcedureContext, "client">): Promise<Recognition> {
|
|
33
|
+
const { engine } = runner;
|
|
34
|
+
const read = async (psm: number | undefined): Promise<OcrText> => {
|
|
35
|
+
const argv = engine.argv(image, { language: options.language, psm, dpi: options.dpi });
|
|
36
|
+
return engine.parse(await runner.run(engine.program, argv, options.timeout, ctx));
|
|
37
|
+
};
|
|
38
|
+
if (options.upright !== true) return { text: await read(options.psm) };
|
|
39
|
+
|
|
40
|
+
const step = engine.orientation;
|
|
41
|
+
if (step === undefined) throw new Error(`The OCR engine ${engine.name} cannot find the orientation of a page`);
|
|
42
|
+
const orientation = step.parse(await runner.run(engine.program, step.argv(image, { dpi: options.dpi }), options.timeout, ctx));
|
|
43
|
+
if (orientation.turn === 0) return { text: await read(options.psm), orientation };
|
|
44
|
+
return { text: turnText(await read(options.psm ?? step.psm), orientation.turn), orientation };
|
|
45
|
+
}
|
package/src/text.ts
CHANGED
|
@@ -118,3 +118,50 @@ export function scaleText(text: OcrText, factor: number, unit: OcrUnit): OcrText
|
|
|
118
118
|
})),
|
|
119
119
|
};
|
|
120
120
|
}
|
|
121
|
+
|
|
122
|
+
/** A turn of a page in the clock direction, in degrees: the turn that makes a page upright. */
|
|
123
|
+
export type OcrTurn = 0 | 90 | 180 | 270;
|
|
124
|
+
|
|
125
|
+
/** The turns in the clock direction that a page can have. */
|
|
126
|
+
export const OCR_TURNS: readonly OcrTurn[] = [0, 90, 180, 270];
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* This function gives the box in the frame of the page after a turn of the page in the clock
|
|
130
|
+
* direction. `width` and `height` are the size of the page before the turn.
|
|
131
|
+
*/
|
|
132
|
+
export function turnBox(box: Rect, turn: OcrTurn, width: number, height: number): Rect {
|
|
133
|
+
switch (turn) {
|
|
134
|
+
case 0:
|
|
135
|
+
return box;
|
|
136
|
+
case 90:
|
|
137
|
+
return { x: height - (box.y + box.h), y: box.x, w: box.h, h: box.w };
|
|
138
|
+
case 180:
|
|
139
|
+
return { x: width - (box.x + box.w), y: height - (box.y + box.h), w: box.w, h: box.h };
|
|
140
|
+
case 270:
|
|
141
|
+
return { x: box.y, y: width - (box.x + box.w), w: box.h, h: box.w };
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* This function turns each box, the width and the height of a text by `turn` degrees in the clock
|
|
147
|
+
* direction. For example, an engine that reads a scan on its side can give the boxes in the frame
|
|
148
|
+
* of the scan. The turn that makes the scan upright then gives the boxes in the frame of the upright
|
|
149
|
+
* page. A turn of 90 or 270 degrees changes the width and the height. The text and the order of the
|
|
150
|
+
* lines do not change.
|
|
151
|
+
*/
|
|
152
|
+
export function turnText(text: OcrText, turn: OcrTurn): OcrText {
|
|
153
|
+
if (!OCR_TURNS.includes(turn)) throw new RangeError(`The turn must be 0, 90, 180 or 270 degrees, not ${String(turn)}`);
|
|
154
|
+
if (turn === 0) return text;
|
|
155
|
+
const { width, height } = text;
|
|
156
|
+
const side = turn === 90 || turn === 270;
|
|
157
|
+
return {
|
|
158
|
+
...text,
|
|
159
|
+
width: side ? height : width,
|
|
160
|
+
height: side ? width : height,
|
|
161
|
+
lines: text.lines.map((line) => ({
|
|
162
|
+
...line,
|
|
163
|
+
box: turnBox(line.box, turn, width, height),
|
|
164
|
+
words: line.words.map((word) => ({ ...word, box: turnBox(word.box, turn, width, height) })),
|
|
165
|
+
})),
|
|
166
|
+
};
|
|
167
|
+
}
|
package/src/types.ts
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
import * as z from "zod";
|
|
6
6
|
import { LANGUAGE_PATTERN } from "./engine.js";
|
|
7
|
+
import type { OcrOrientation } from "./osd.js";
|
|
7
8
|
import type { OcrText } from "./text.js";
|
|
8
9
|
|
|
9
10
|
// =============================================================================
|
|
@@ -43,8 +44,17 @@ const engineFields = {
|
|
|
43
44
|
language: z.string().regex(LANGUAGE_PATTERN, "a language name of the engine, for example eng or eng+deu").optional().describe("The language of the text (default: eng)"),
|
|
44
45
|
psm: z.number().int().min(0).max(13).optional().describe("The page segmentation mode of tesseract, from 0 to 13"),
|
|
45
46
|
timeout: z.number().int().positive().optional().describe("The time limit of each program, in milliseconds (default: 120000)"),
|
|
47
|
+
upright: z
|
|
48
|
+
.boolean()
|
|
49
|
+
.optional()
|
|
50
|
+
.describe("When true, OCR finds the orientation of the page first, and the boxes are in the frame of the upright page (default: false)"),
|
|
46
51
|
};
|
|
47
52
|
|
|
53
|
+
const OrientationSchema = z.object({
|
|
54
|
+
turn: z.union([z.literal(0), z.literal(90), z.literal(180), z.literal(270)]).describe("The turn in the clock direction that makes the page upright, in degrees"),
|
|
55
|
+
confidence: z.number().min(0).describe("The confidence of the engine in the turn. For tesseract, a value below 2 is weak"),
|
|
56
|
+
});
|
|
57
|
+
|
|
48
58
|
// =============================================================================
|
|
49
59
|
// ocr.page: the text of a PDF page
|
|
50
60
|
// =============================================================================
|
|
@@ -61,6 +71,8 @@ export interface OcrPageInput {
|
|
|
61
71
|
psm?: number | undefined;
|
|
62
72
|
dpi?: number | undefined;
|
|
63
73
|
timeout?: number | undefined;
|
|
74
|
+
/** When true, OCR finds the orientation of the page first, and the boxes are in the frame of the upright page. */
|
|
75
|
+
upright?: boolean | undefined;
|
|
64
76
|
}
|
|
65
77
|
|
|
66
78
|
export const OcrPageInputSchema: z.ZodType<OcrPageInput> = z.object({
|
|
@@ -82,12 +94,15 @@ export interface OcrPageOutput extends OcrText {
|
|
|
82
94
|
readonly source: "text-layer" | "ocr";
|
|
83
95
|
/** The OCR engine, when the source is `ocr`. */
|
|
84
96
|
readonly engine?: string | undefined;
|
|
97
|
+
/** The orientation that OCR found, when the input has `upright` and the source is `ocr`. */
|
|
98
|
+
readonly orientation?: OcrOrientation | undefined;
|
|
85
99
|
}
|
|
86
100
|
|
|
87
101
|
export const OcrPageOutputSchema: z.ZodType<OcrPageOutput> = z.object({
|
|
88
102
|
page: z.number().describe("The number of the page"),
|
|
89
103
|
source: z.enum(["text-layer", "ocr"]).describe("Where the text comes from: the text layer, or OCR of an image of the page"),
|
|
90
104
|
engine: z.string().optional().describe("The OCR engine, when the source is ocr"),
|
|
105
|
+
orientation: OrientationSchema.optional().describe("The orientation that OCR found, when the input has upright and the source is ocr"),
|
|
91
106
|
...textFields,
|
|
92
107
|
});
|
|
93
108
|
|
|
@@ -102,6 +117,8 @@ export interface OcrImageInput {
|
|
|
102
117
|
psm?: number | undefined;
|
|
103
118
|
dpi?: number | undefined;
|
|
104
119
|
timeout?: number | undefined;
|
|
120
|
+
/** When true, OCR finds the orientation of the page first, and the boxes are in the frame of the upright page. */
|
|
121
|
+
upright?: boolean | undefined;
|
|
105
122
|
}
|
|
106
123
|
|
|
107
124
|
export const OcrImageInputSchema: z.ZodType<OcrImageInput> = z.object({
|
|
@@ -114,9 +131,12 @@ export const OcrImageInputSchema: z.ZodType<OcrImageInput> = z.object({
|
|
|
114
131
|
export interface OcrImageOutput extends OcrText {
|
|
115
132
|
/** The OCR engine. */
|
|
116
133
|
readonly engine: string;
|
|
134
|
+
/** The orientation that OCR found, when the input has `upright`. */
|
|
135
|
+
readonly orientation?: OcrOrientation | undefined;
|
|
117
136
|
}
|
|
118
137
|
|
|
119
138
|
export const OcrImageOutputSchema: z.ZodType<OcrImageOutput> = z.object({
|
|
120
139
|
engine: z.string().describe("The OCR engine"),
|
|
140
|
+
orientation: OrientationSchema.optional().describe("The orientation that OCR found, when the input has upright"),
|
|
121
141
|
...textFields,
|
|
122
142
|
});
|