@procedures/ocr 0.0.0-bootstrap.0 → 0.68.6
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 +17 -0
- package/LICENSE +21 -0
- package/README.md +93 -1
- package/dist/bbox.d.ts +23 -0
- package/dist/bbox.d.ts.map +1 -0
- package/dist/bbox.js +76 -0
- package/dist/bbox.js.map +1 -0
- package/dist/engine.d.ts +40 -0
- package/dist/engine.d.ts.map +1 -0
- package/dist/engine.js +36 -0
- package/dist/engine.js.map +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +11 -0
- package/dist/index.js.map +1 -0
- package/dist/module.d.ts +33 -0
- package/dist/module.d.ts.map +1 -0
- package/dist/module.js +84 -0
- package/dist/module.js.map +1 -0
- package/dist/parse.d.ts +8 -0
- package/dist/parse.d.ts.map +1 -0
- package/dist/parse.js +8 -0
- package/dist/parse.js.map +1 -0
- package/dist/procedures/ocr/image.d.ts +14 -0
- package/dist/procedures/ocr/image.d.ts.map +1 -0
- package/dist/procedures/ocr/image.js +19 -0
- package/dist/procedures/ocr/image.js.map +1 -0
- package/dist/procedures/ocr/index.d.ts +3 -0
- package/dist/procedures/ocr/index.d.ts.map +1 -0
- package/dist/procedures/ocr/index.js +3 -0
- package/dist/procedures/ocr/index.js.map +1 -0
- package/dist/procedures/ocr/page.d.ts +23 -0
- package/dist/procedures/ocr/page.d.ts.map +1 -0
- package/dist/procedures/ocr/page.js +74 -0
- package/dist/procedures/ocr/page.js.map +1 -0
- package/dist/programs.d.ts +66 -0
- package/dist/programs.d.ts.map +1 -0
- package/dist/programs.js +107 -0
- package/dist/programs.js.map +1 -0
- package/dist/register.d.ts +6 -0
- package/dist/register.d.ts.map +1 -0
- package/dist/register.js +8 -0
- package/dist/register.js.map +1 -0
- package/dist/run.d.ts +32 -0
- package/dist/run.d.ts.map +1 -0
- package/dist/run.js +48 -0
- package/dist/run.js.map +1 -0
- package/dist/text.d.ts +56 -0
- package/dist/text.d.ts.map +1 -0
- package/dist/text.js +77 -0
- package/dist/text.js.map +1 -0
- package/dist/tsv.d.ts +26 -0
- package/dist/tsv.d.ts.map +1 -0
- package/dist/tsv.js +91 -0
- package/dist/tsv.js.map +1 -0
- package/dist/types.d.ts +44 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +64 -0
- package/dist/types.js.map +1 -0
- package/package.json +87 -5
- package/src/bbox.ts +76 -0
- package/src/engine.ts +59 -0
- package/src/index.ts +10 -0
- package/src/module.ts +103 -0
- package/src/parse.ts +8 -0
- package/src/procedures/ocr/image.ts +22 -0
- package/src/procedures/ocr/index.ts +2 -0
- package/src/procedures/ocr/page.ts +80 -0
- package/src/programs.ts +142 -0
- package/src/register.ts +10 -0
- package/src/run.ts +72 -0
- package/src/text.ts +120 -0
- package/src/tsv.ts +101 -0
- package/src/types.ts +122 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# @procedures/ocr
|
|
2
|
+
|
|
3
|
+
## 0.68.6
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- f2c2fa2: A new package, `@procedures/ocr`: the text of a PDF page or of an image, as lines and words. Each line and each word has its text, its confidence from 0 to 1 and its box (the `Rect` record of `@mark1russell7/layout-contract`).
|
|
8
|
+
|
|
9
|
+
- `ocr.page` reads the text layer of a PDF page with `pdftotext -bbox-layout` first. Only a page with no text layer gets OCR: `pdftoppm` makes an image of the page, and tesseract reads it. The boxes are in PDF points. The modes `text` and `ocr` give only the text layer, or always OCR.
|
|
10
|
+
- `ocr.image` reads an image with tesseract. The boxes are in the pixels of the image.
|
|
11
|
+
- The programs start as argv through `shell.run`. The host finds each program (`executables`, then `TESSERACT_EXE`, `PDFTOTEXT_EXE`, `PDFTOPPM_EXE` or `POPPLER_PATH`, then the install folders and the `PATH`). A missing program gives an `OcrToolMissingError`.
|
|
12
|
+
- The parsers `parseTesseractTsv` and `parsePdftotextBbox` start no process. A browser can also load the entry `./parse`.
|
|
13
|
+
- An OCR engine is an `OcrEngine` adapter. `tesseractEngine` is the first one.
|
|
14
|
+
|
|
15
|
+
PATCH: before 1.0.0, a backward-compatible feature raises PATCH. The new package adds procedures and changes no procedure of another package.
|
|
16
|
+
- @procedures/client@0.68.6
|
|
17
|
+
- @procedures/shell@0.68.6
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025-2026 Mark Russell
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,95 @@
|
|
|
1
1
|
# @procedures/ocr
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://www.npmjs.com/package/@procedures/ocr)
|
|
4
|
+
|
|
5
|
+
The text of a PDF page or of an image, as lines and words. Each line and each word has its text, its confidence from 0 to 1 and its box. The package reads the text layer of a PDF page first. It uses OCR only for a page with no text layer.
|
|
6
|
+
|
|
7
|
+
The package uses the programs of the host. It does not download a program:
|
|
8
|
+
|
|
9
|
+
- `pdftotext` and `pdftoppm` of Poppler, for the PDF pages.
|
|
10
|
+
- `tesseract` 4 or later, the first OCR engine.
|
|
11
|
+
|
|
12
|
+
Each program starts as argv through the procedure `shell.run` of `@procedures/shell`. No shell starts.
|
|
13
|
+
|
|
14
|
+
## Install
|
|
15
|
+
|
|
16
|
+
```sh
|
|
17
|
+
pnpm add @procedures/ocr
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
Install the programs:
|
|
21
|
+
|
|
22
|
+
| Host | Poppler | Tesseract |
|
|
23
|
+
|---|---|---|
|
|
24
|
+
| Debian or Ubuntu | `apt install poppler-utils` | `apt install tesseract-ocr` |
|
|
25
|
+
| macOS | `brew install poppler` | `brew install tesseract` |
|
|
26
|
+
| Windows | A Poppler build, with `POPPLER_PATH` set to its `bin` folder | The Tesseract installer |
|
|
27
|
+
|
|
28
|
+
## The procedures
|
|
29
|
+
|
|
30
|
+
| Procedure | What it does | Effects |
|
|
31
|
+
|---|---|---|
|
|
32
|
+
| `ocr.page` | It gives the text of one page of a PDF file. The boxes are in PDF points. | It reads `fs` and `process`. It writes `fs` (a temporary image) and `process`. |
|
|
33
|
+
| `ocr.image` | It reads the text of an image file with the OCR engine. The boxes are in the pixels of the image. | It reads `fs` and `process`. It writes `process`. |
|
|
34
|
+
|
|
35
|
+
`ocr.page` has three modes:
|
|
36
|
+
|
|
37
|
+
- `auto` (the default): the text layer of the page. A page with no word in its text layer gets OCR.
|
|
38
|
+
- `text`: only the text layer.
|
|
39
|
+
- `ocr`: always OCR. `pdftoppm` makes an image of the page at `dpi` (default 300). Then the engine reads the image.
|
|
40
|
+
|
|
41
|
+
```ts
|
|
42
|
+
import { createRegistry, invokePath, outputValue } from "@procedures/client";
|
|
43
|
+
import { ocrModule } from "@procedures/ocr/module";
|
|
44
|
+
|
|
45
|
+
const registry = createRegistry(ocrModule);
|
|
46
|
+
const page = await outputValue(await invokePath(["ocr", "page"], { file: "report.pdf", page: 2 }, { registry }));
|
|
47
|
+
// { page: 2, source: "text-layer", unit: "pt", width: 612, height: 792, text: "...", confidence: 1, lines: [...] }
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
From the `mark` CLI:
|
|
51
|
+
|
|
52
|
+
```sh
|
|
53
|
+
mark ocr page report.pdf --page 2
|
|
54
|
+
mark ocr image photo.png --language eng+deu
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## The result
|
|
58
|
+
|
|
59
|
+
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`.
|
|
60
|
+
|
|
61
|
+
- A box is the `Rect` record of `@mark1russell7/layout-contract`: `x` and `y` are the top-left corner, `w` and `h` are the size. The y axis points down.
|
|
62
|
+
- A word of the text layer has the confidence 1. A word of OCR has the confidence of the engine, divided by 100.
|
|
63
|
+
- The confidence of a line is the mean of its words. The confidence of the result is the mean of all words.
|
|
64
|
+
|
|
65
|
+
## The programs on the host
|
|
66
|
+
|
|
67
|
+
The procedures find each program at each call. A host can set the path of each program with `createOcrModule`. The input of a call cannot choose a program.
|
|
68
|
+
|
|
69
|
+
```ts
|
|
70
|
+
import { createOcrModule } from "@procedures/ocr/module";
|
|
71
|
+
|
|
72
|
+
const registry = createRegistry(createOcrModule({ executables: { tesseract: "/opt/tesseract/bin/tesseract" } }));
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
The search examines these paths, in this order:
|
|
76
|
+
|
|
77
|
+
1. The `executables` option of `createOcrModule`.
|
|
78
|
+
2. The environment variable of the program: `TESSERACT_EXE`, `PDFTOTEXT_EXE` or `PDFTOPPM_EXE`.
|
|
79
|
+
3. For Poppler, the folder of `POPPLER_PATH`.
|
|
80
|
+
4. The install folders: `Tesseract-OCR` and `poppler\Library\bin` in the program files folders of Windows, and the Homebrew folders of macOS.
|
|
81
|
+
5. The folders of the `PATH`.
|
|
82
|
+
|
|
83
|
+
When the search finds no program, the call fails with an `OcrToolMissingError`. The error has the name of the program (`tool`), the paths that the search examined (`searched`) and the code `OCR_TOOL_MISSING`. A program that does not start gives the same error.
|
|
84
|
+
|
|
85
|
+
## The parsers
|
|
86
|
+
|
|
87
|
+
The parsers start no process and read no file. The root and the entry `@procedures/ocr/parse` export them. A browser can also load the entry `./parse`.
|
|
88
|
+
|
|
89
|
+
- `parseTesseractTsv(tsv)` reads the TSV output of tesseract (`tesseract <image> stdout tsv`).
|
|
90
|
+
- `parsePdftotextBbox(xhtml)` reads the output of `pdftotext -bbox-layout` for one page.
|
|
91
|
+
- `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
|
+
|
|
93
|
+
## Other engines
|
|
94
|
+
|
|
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 })`.
|
package/dist/bbox.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The parse of the text layer of a PDF page, in the XHTML output of `pdftotext -bbox-layout` of
|
|
3
|
+
* Poppler. This file starts no process and reads no file.
|
|
4
|
+
*
|
|
5
|
+
* The output has a `page` element with the size of the page, then `flow`, `block`, `line` and
|
|
6
|
+
* `word` elements. Each `line` and each `word` has its box as `xMin`, `yMin`, `xMax` and `yMax`, in
|
|
7
|
+
* PDF points from the top-left corner of the page.
|
|
8
|
+
*/
|
|
9
|
+
import { type OcrText } from "./text.js";
|
|
10
|
+
/** This function decodes the entities of an XML text: the five named entities and the number entities. */
|
|
11
|
+
export declare function decodeXmlText(text: string): string;
|
|
12
|
+
/**
|
|
13
|
+
* This function parses the output of `pdftotext -bbox-layout` for one page. Each word of the text
|
|
14
|
+
* layer has the confidence 1. The boxes are in PDF points.
|
|
15
|
+
*
|
|
16
|
+
* - The function reads the first `page` element. Give the output of one page (`-f N -l N`).
|
|
17
|
+
* - A word with no text, or with no valid box, is not in the result. A line with no word is not in the result.
|
|
18
|
+
* - A page with no text layer gives no line, and the size of the page.
|
|
19
|
+
*
|
|
20
|
+
* It throws when the output has no `page` element.
|
|
21
|
+
*/
|
|
22
|
+
export declare function parsePdftotextBbox(xhtml: string): OcrText;
|
|
23
|
+
//# sourceMappingURL=bbox.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bbox.d.ts","sourceRoot":"","sources":["../src/bbox.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAoC,KAAK,OAAO,EAA2B,MAAM,WAAW,CAAC;AAIpG,0GAA0G;AAC1G,wBAAgB,aAAa,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAMlD;AAyBD;;;;;;;;;GASG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAoBzD"}
|
package/dist/bbox.js
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The parse of the text layer of a PDF page, in the XHTML output of `pdftotext -bbox-layout` of
|
|
3
|
+
* Poppler. This file starts no process and reads no file.
|
|
4
|
+
*
|
|
5
|
+
* The output has a `page` element with the size of the page, then `flow`, `block`, `line` and
|
|
6
|
+
* `word` elements. Each `line` and each `word` has its box as `xMin`, `yMin`, `xMax` and `yMax`, in
|
|
7
|
+
* PDF points from the top-left corner of the page.
|
|
8
|
+
*/
|
|
9
|
+
import { makeLine, makeText } from "./text.js";
|
|
10
|
+
const ENTITIES = { amp: "&", lt: "<", gt: ">", quot: '"', apos: "'" };
|
|
11
|
+
/** This function decodes the entities of an XML text: the five named entities and the number entities. */
|
|
12
|
+
export function decodeXmlText(text) {
|
|
13
|
+
return text.replace(/&(#x[0-9a-fA-F]+|#[0-9]+|[a-z]+);/g, (entity, name) => {
|
|
14
|
+
if (name.startsWith("#x"))
|
|
15
|
+
return safeCodePoint(Number.parseInt(name.slice(2), 16), entity);
|
|
16
|
+
if (name.startsWith("#"))
|
|
17
|
+
return safeCodePoint(Number.parseInt(name.slice(1), 10), entity);
|
|
18
|
+
return ENTITIES[name] ?? entity;
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
function safeCodePoint(code, entity) {
|
|
22
|
+
return code >= 0 && code <= 0x10ffff ? String.fromCodePoint(code) : entity;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* This function gives the attributes of the start tag of an element, by name. Each attribute starts
|
|
26
|
+
* after a white space, so a match starts only at a white space. Thus the time is linear in the
|
|
27
|
+
* length of the tag, also for a long run of name characters with no `=`.
|
|
28
|
+
*/
|
|
29
|
+
function attributes(tag) {
|
|
30
|
+
const result = new Map();
|
|
31
|
+
for (const match of tag.matchAll(/\s([A-Za-z_:][\w:.-]*)="([^"]*)"/g))
|
|
32
|
+
result.set(match[1], match[2]);
|
|
33
|
+
return result;
|
|
34
|
+
}
|
|
35
|
+
/** This function gives the box of an element from its `xMin`, `yMin`, `xMax` and `yMax`, or undefined. */
|
|
36
|
+
function boxOf(tag) {
|
|
37
|
+
const values = attributes(tag);
|
|
38
|
+
const [xMin, yMin, xMax, yMax] = ["xMin", "yMin", "xMax", "yMax"].map((name) => Number(values.get(name)));
|
|
39
|
+
if (![xMin, yMin, xMax, yMax].every((value) => Number.isFinite(value)))
|
|
40
|
+
return undefined;
|
|
41
|
+
return { x: xMin, y: yMin, w: Math.max(0, xMax - xMin), h: Math.max(0, yMax - yMin) };
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* This function parses the output of `pdftotext -bbox-layout` for one page. Each word of the text
|
|
45
|
+
* layer has the confidence 1. The boxes are in PDF points.
|
|
46
|
+
*
|
|
47
|
+
* - The function reads the first `page` element. Give the output of one page (`-f N -l N`).
|
|
48
|
+
* - A word with no text, or with no valid box, is not in the result. A line with no word is not in the result.
|
|
49
|
+
* - A page with no text layer gives no line, and the size of the page.
|
|
50
|
+
*
|
|
51
|
+
* It throws when the output has no `page` element.
|
|
52
|
+
*/
|
|
53
|
+
export function parsePdftotextBbox(xhtml) {
|
|
54
|
+
const pageStart = /<page\b[^>]*>/.exec(xhtml);
|
|
55
|
+
if (pageStart === null)
|
|
56
|
+
throw new Error("The output of pdftotext -bbox-layout has no page element");
|
|
57
|
+
const size = attributes(pageStart[0]);
|
|
58
|
+
const width = Number(size.get("width"));
|
|
59
|
+
const height = Number(size.get("height"));
|
|
60
|
+
const pageEnd = xhtml.indexOf("</page>", pageStart.index);
|
|
61
|
+
const body = xhtml.slice(pageStart.index + pageStart[0].length, pageEnd === -1 ? undefined : pageEnd);
|
|
62
|
+
const lines = [];
|
|
63
|
+
for (const line of body.matchAll(/<line\b([^>]*)>([\s\S]*?)<\/line>/g)) {
|
|
64
|
+
const words = [];
|
|
65
|
+
for (const word of line[2].matchAll(/<word\b([^>]*)>([\s\S]*?)<\/word>/g)) {
|
|
66
|
+
const text = decodeXmlText(word[2]).trim();
|
|
67
|
+
const box = boxOf(word[1]);
|
|
68
|
+
if (text.length > 0 && box !== undefined)
|
|
69
|
+
words.push({ text, confidence: 1, box });
|
|
70
|
+
}
|
|
71
|
+
if (words.length > 0)
|
|
72
|
+
lines.push(makeLine(words, boxOf(line[1])));
|
|
73
|
+
}
|
|
74
|
+
return makeText(lines, "pt", Number.isFinite(width) ? width : 0, Number.isFinite(height) ? height : 0);
|
|
75
|
+
}
|
|
76
|
+
//# sourceMappingURL=bbox.js.map
|
package/dist/bbox.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bbox.js","sourceRoot":"","sources":["../src/bbox.ts"],"names":[],"mappings":"AAAA;;;;;;;GAO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,CAAC,CAAC,EAAE,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AACzG,CAAC"}
|
package/dist/engine.d.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The adapter of an OCR engine: the program, the argv for one image and the parse of the output.
|
|
3
|
+
* The adapter starts no process: the procedures start the program through `shell.run`. Tesseract
|
|
4
|
+
* is the first engine. Another engine is one more adapter.
|
|
5
|
+
*/
|
|
6
|
+
import { type ProgramSpec } from "./programs.js";
|
|
7
|
+
import type { OcrText } from "./text.js";
|
|
8
|
+
/** The options of one recognition. */
|
|
9
|
+
export interface OcrEngineOptions {
|
|
10
|
+
/** The language of the text, for example `eng`, or `eng+deu` for two languages (default: `eng`). */
|
|
11
|
+
readonly language?: string | undefined;
|
|
12
|
+
/** The page segmentation mode of the engine. For tesseract, a value from 0 to 13. */
|
|
13
|
+
readonly psm?: number | undefined;
|
|
14
|
+
/** The resolution of the image, in dots per inch, when the image file does not tell it. */
|
|
15
|
+
readonly dpi?: number | undefined;
|
|
16
|
+
}
|
|
17
|
+
/** An OCR engine. */
|
|
18
|
+
export interface OcrEngine {
|
|
19
|
+
/** The name of the engine in the result, for example `tesseract`. */
|
|
20
|
+
readonly name: string;
|
|
21
|
+
/** The program of the engine, for the search on the host. */
|
|
22
|
+
readonly program: ProgramSpec;
|
|
23
|
+
/** This function gives the argv of the program for one image. `image` is an absolute path. */
|
|
24
|
+
argv(image: string, options: OcrEngineOptions): string[];
|
|
25
|
+
/** This function parses the standard output of the program. The boxes are in the pixels of the image. */
|
|
26
|
+
parse(stdout: string): OcrText;
|
|
27
|
+
}
|
|
28
|
+
/** The default language of the recognition. */
|
|
29
|
+
export declare const DEFAULT_LANGUAGE = "eng";
|
|
30
|
+
/**
|
|
31
|
+
* The form of a language value: names of letters, digits, `_` and `/` (for example `script/Latin`),
|
|
32
|
+
* joined with `+`. A value cannot start with `-`, so the program cannot read it as a flag.
|
|
33
|
+
*/
|
|
34
|
+
export declare const LANGUAGE_PATTERN: RegExp;
|
|
35
|
+
/**
|
|
36
|
+
* 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.
|
|
38
|
+
*/
|
|
39
|
+
export declare const tesseractEngine: OcrEngine;
|
|
40
|
+
//# sourceMappingURL=engine.d.ts.map
|
|
@@ -0,0 +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;CAChC;AAED,+CAA+C;AAC/C,eAAO,MAAM,gBAAgB,QAAQ,CAAC;AAEtC;;;GAGG;AACH,eAAO,MAAM,gBAAgB,EAAE,MAAsE,CAAC;AAEtG;;;GAGG;AACH,eAAO,MAAM,eAAe,EAAE,SAa7B,CAAC"}
|
package/dist/engine.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The adapter of an OCR engine: the program, the argv for one image and the parse of the output.
|
|
3
|
+
* The adapter starts no process: the procedures start the program through `shell.run`. Tesseract
|
|
4
|
+
* is the first engine. Another engine is one more adapter.
|
|
5
|
+
*/
|
|
6
|
+
import { TESSERACT } from "./programs.js";
|
|
7
|
+
import { parseTesseractTsv } from "./tsv.js";
|
|
8
|
+
/** The default language of the recognition. */
|
|
9
|
+
export const DEFAULT_LANGUAGE = "eng";
|
|
10
|
+
/**
|
|
11
|
+
* The form of a language value: names of letters, digits, `_` and `/` (for example `script/Latin`),
|
|
12
|
+
* joined with `+`. A value cannot start with `-`, so the program cannot read it as a flag.
|
|
13
|
+
*/
|
|
14
|
+
export const LANGUAGE_PATTERN = /^[A-Za-z0-9_][A-Za-z0-9_/]*(\+[A-Za-z0-9_][A-Za-z0-9_/]*)*$/;
|
|
15
|
+
/**
|
|
16
|
+
* 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
|
+
*/
|
|
19
|
+
export const tesseractEngine = {
|
|
20
|
+
name: "tesseract",
|
|
21
|
+
program: TESSERACT,
|
|
22
|
+
argv(image, options) {
|
|
23
|
+
const language = options.language ?? DEFAULT_LANGUAGE;
|
|
24
|
+
if (!LANGUAGE_PATTERN.test(language))
|
|
25
|
+
throw new Error(`The language ${JSON.stringify(language)} is not a valid language name of tesseract`);
|
|
26
|
+
const argv = [image, "stdout", "-l", language];
|
|
27
|
+
if (options.psm !== undefined)
|
|
28
|
+
argv.push("--psm", String(options.psm));
|
|
29
|
+
if (options.dpi !== undefined)
|
|
30
|
+
argv.push("--dpi", String(options.dpi));
|
|
31
|
+
argv.push("tsv");
|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AC;IACd,CAAC;IACD,KAAK,EAAE,iBAAiB;CACzB,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export * from "./module.js";
|
|
2
|
+
export * from "./types.js";
|
|
3
|
+
export * from "./parse.js";
|
|
4
|
+
export * from "./engine.js";
|
|
5
|
+
export * from "./programs.js";
|
|
6
|
+
export { createOcrRunner, DEFAULT_TIMEOUT_MS, type OcrHostOptions, type OcrRunner } from "./run.js";
|
|
7
|
+
export { DEFAULT_DPI, ocrImage, ocrPage, pdftoppmArgs, pdftotextArgs } from "./procedures/ocr/index.js";
|
|
8
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC;AAE3B,cAAc,YAAY,CAAC;AAE3B,cAAc,aAAa,CAAC;AAC5B,cAAc,eAAe,CAAC;AAC9B,OAAO,EAAE,eAAe,EAAE,kBAAkB,EAAE,KAAK,cAAc,EAAE,KAAK,SAAS,EAAE,MAAM,UAAU,CAAC;AACpG,OAAO,EAAE,WAAW,EAAE,QAAQ,EAAE,OAAO,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// The root registers nothing: import "<package>/register", or give the module to createRegistry()
|
|
2
|
+
export * from "./module.js";
|
|
3
|
+
export * from "./types.js";
|
|
4
|
+
// The records and the parsers, with no process (also on the browser entry ./parse)
|
|
5
|
+
export * from "./parse.js";
|
|
6
|
+
// The adapter of an OCR engine, the search for the programs and the run through shell.run
|
|
7
|
+
export * from "./engine.js";
|
|
8
|
+
export * from "./programs.js";
|
|
9
|
+
export { createOcrRunner, DEFAULT_TIMEOUT_MS } from "./run.js";
|
|
10
|
+
export { DEFAULT_DPI, ocrImage, ocrPage, pdftoppmArgs, pdftotextArgs } from "./procedures/ocr/index.js";
|
|
11
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,kGAAkG;AAClG,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC;AAC3B,mFAAmF;AACnF,cAAc,YAAY,CAAC;AAC3B,0FAA0F;AAC1F,cAAc,aAAa,CAAC;AAC5B,cAAc,eAAe,CAAC;AAC9B,OAAO,EAAE,eAAe,EAAE,kBAAkB,EAAuC,MAAM,UAAU,CAAC;AACpG,OAAO,EAAE,WAAW,EAAE,QAAQ,EAAE,OAAO,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAC"}
|
package/dist/module.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The procedures of @procedures/ocr as data. An import of this file registers nothing. The module
|
|
3
|
+
* of `@procedures/shell` is necessary for this module: each procedure starts its programs through
|
|
4
|
+
* `shell.run`.
|
|
5
|
+
*
|
|
6
|
+
* Scaffold a procedure with: mark procedure new <name> --path packages/ocr
|
|
7
|
+
*/
|
|
8
|
+
import { type AnyProcedure, type PackageDefinition, type ProcedureModule } from "@procedures/client";
|
|
9
|
+
import { type OcrHostOptions } from "./run.js";
|
|
10
|
+
/**
|
|
11
|
+
* This function gives the procedures of this package with the settings of the host. The settings
|
|
12
|
+
* are the path of each program, the OCR engine and the folder of the temporary folders.
|
|
13
|
+
*/
|
|
14
|
+
export declare function createOcrProcedures(options?: OcrHostOptions): AnyProcedure[];
|
|
15
|
+
/** The procedures with the default settings: the search finds each program at each call. */
|
|
16
|
+
export declare const ocrProcedures: AnyProcedure[];
|
|
17
|
+
export declare const ocrModule: ProcedureModule;
|
|
18
|
+
export default ocrModule;
|
|
19
|
+
/**
|
|
20
|
+
* This function gives the module with the settings of the host. Without options, it gives
|
|
21
|
+
* `ocrModule`.
|
|
22
|
+
*
|
|
23
|
+
* ```ts
|
|
24
|
+
* createRegistry(createOcrModule({ executables: { tesseract: "/opt/tesseract/bin/tesseract" } }));
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
export declare function createOcrModule(options?: OcrHostOptions): ProcedureModule;
|
|
28
|
+
/**
|
|
29
|
+
* This function registers the procedures of @procedures/ocr in a registry. The default is the
|
|
30
|
+
* global registry.
|
|
31
|
+
*/
|
|
32
|
+
export declare const registerOcrProcedures: PackageDefinition["register"];
|
|
33
|
+
//# sourceMappingURL=module.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"module.d.ts","sourceRoot":"","sources":["../src/module.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAIL,KAAK,YAAY,EAGjB,KAAK,iBAAiB,EAEtB,KAAK,eAAe,EACrB,MAAM,oBAAoB,CAAC;AAI5B,OAAO,EAAmB,KAAK,cAAc,EAAE,MAAM,UAAU,CAAC;AAehE;;;GAGG;AACH,wBAAgB,mBAAmB,CAAC,OAAO,GAAE,cAAmB,GAAG,YAAY,EAAE,CAiChF;AAED,4FAA4F;AAC5F,eAAO,MAAM,aAAa,EAAE,YAAY,EAA0B,CAAC;AAInE,eAAO,MAAM,SAAS,EAAE,eAAmC,CAAC;eAE7C,SAAS;AAExB;;;;;;;GAOG;AACH,wBAAgB,eAAe,CAAC,OAAO,GAAE,cAAmB,GAAG,eAAe,CAG7E;AAED;;;GAGG;AACH,eAAO,MAAM,qBAAqB,EAAE,iBAAiB,CAAC,UAAU,CAAuB,CAAC"}
|
package/dist/module.js
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The procedures of @procedures/ocr as data. An import of this file registers nothing. The module
|
|
3
|
+
* of `@procedures/shell` is necessary for this module: each procedure starts its programs through
|
|
4
|
+
* `shell.run`.
|
|
5
|
+
*
|
|
6
|
+
* Scaffold a procedure with: mark procedure new <name> --path packages/ocr
|
|
7
|
+
*/
|
|
8
|
+
import { createProcedure, defineModule, definePackage, } from "@procedures/client";
|
|
9
|
+
import shellModule from "@procedures/shell/module";
|
|
10
|
+
import { ocrImage } from "./procedures/ocr/image.js";
|
|
11
|
+
import { ocrPage } from "./procedures/ocr/page.js";
|
|
12
|
+
import { createOcrRunner } from "./run.js";
|
|
13
|
+
import { OcrImageInputSchema, OcrImageOutputSchema, OcrPageInputSchema, OcrPageOutputSchema } from "./types.js";
|
|
14
|
+
/** The rule of the procedures under a root of the files: the file of the input is in the root. */
|
|
15
|
+
const FILE_IN_A_ROOT = { paths: ["file"] };
|
|
16
|
+
/**
|
|
17
|
+
* The effects of `ocr.page`. The programs read the PDF file. `pdftoppm` writes an image to a new
|
|
18
|
+
* temporary folder, and the procedure removes the folder. `shell.run` reads and writes "process".
|
|
19
|
+
*/
|
|
20
|
+
const PAGE_EFFECTS = { reads: ["fs", "process"], writes: ["fs", "process"] };
|
|
21
|
+
/** The effects of `ocr.image`. The engine reads the image file and writes only to its standard output. */
|
|
22
|
+
const IMAGE_EFFECTS = { reads: ["fs", "process"], writes: ["process"] };
|
|
23
|
+
/**
|
|
24
|
+
* This function gives the procedures of this package with the settings of the host. The settings
|
|
25
|
+
* are the path of each program, the OCR engine and the folder of the temporary folders.
|
|
26
|
+
*/
|
|
27
|
+
export function createOcrProcedures(options = {}) {
|
|
28
|
+
const runner = createOcrRunner(options);
|
|
29
|
+
const page = createProcedure()
|
|
30
|
+
.path(["ocr", "page"])
|
|
31
|
+
.input(OcrPageInputSchema)
|
|
32
|
+
.output(OcrPageOutputSchema)
|
|
33
|
+
.meta({
|
|
34
|
+
description: "Give the text of a PDF page with the box of each line and word: the text layer first, then OCR for a page with no text layer",
|
|
35
|
+
args: ["file"],
|
|
36
|
+
shorts: { page: "p", language: "l", mode: "m", timeout: "t" },
|
|
37
|
+
output: "json",
|
|
38
|
+
calls: ["shell.run"],
|
|
39
|
+
effects: PAGE_EFFECTS,
|
|
40
|
+
fsRoot: FILE_IN_A_ROOT,
|
|
41
|
+
})
|
|
42
|
+
.handler(async (input, ctx) => ocrPage(input, ctx, runner))
|
|
43
|
+
.build();
|
|
44
|
+
const image = createProcedure()
|
|
45
|
+
.path(["ocr", "image"])
|
|
46
|
+
.input(OcrImageInputSchema)
|
|
47
|
+
.output(OcrImageOutputSchema)
|
|
48
|
+
.meta({
|
|
49
|
+
description: "Read the text of an image with a local OCR engine (tesseract), with the confidence and the box of each line and word",
|
|
50
|
+
args: ["file"],
|
|
51
|
+
shorts: { language: "l", timeout: "t" },
|
|
52
|
+
output: "json",
|
|
53
|
+
calls: ["shell.run"],
|
|
54
|
+
effects: IMAGE_EFFECTS,
|
|
55
|
+
fsRoot: FILE_IN_A_ROOT,
|
|
56
|
+
})
|
|
57
|
+
.handler(async (input, ctx) => ocrImage(input, ctx, runner))
|
|
58
|
+
.build();
|
|
59
|
+
return [page, image];
|
|
60
|
+
}
|
|
61
|
+
/** The procedures with the default settings: the search finds each program at each call. */
|
|
62
|
+
export const ocrProcedures = createOcrProcedures();
|
|
63
|
+
const ocrPackage = definePackage("@procedures/ocr", ocrProcedures, [shellModule]);
|
|
64
|
+
export const ocrModule = ocrPackage.module;
|
|
65
|
+
export default ocrModule;
|
|
66
|
+
/**
|
|
67
|
+
* This function gives the module with the settings of the host. Without options, it gives
|
|
68
|
+
* `ocrModule`.
|
|
69
|
+
*
|
|
70
|
+
* ```ts
|
|
71
|
+
* createRegistry(createOcrModule({ executables: { tesseract: "/opt/tesseract/bin/tesseract" } }));
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
74
|
+
export function createOcrModule(options = {}) {
|
|
75
|
+
if (Object.values(options).every((value) => value === undefined))
|
|
76
|
+
return ocrModule;
|
|
77
|
+
return defineModule("@procedures/ocr", createOcrProcedures(options), [shellModule]);
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* This function registers the procedures of @procedures/ocr in a registry. The default is the
|
|
81
|
+
* global registry.
|
|
82
|
+
*/
|
|
83
|
+
export const registerOcrProcedures = ocrPackage.register;
|
|
84
|
+
//# sourceMappingURL=module.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"module.js","sourceRoot":"","sources":["../src/module.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EACL,eAAe,EACf,YAAY,EACZ,aAAa,GAOd,MAAM,oBAAoB,CAAC;AAC5B,OAAO,WAAW,MAAM,0BAA0B,CAAC;AACnD,OAAO,EAAE,QAAQ,EAAE,MAAM,2BAA2B,CAAC;AACrD,OAAO,EAAE,OAAO,EAAE,MAAM,0BAA0B,CAAC;AACnD,OAAO,EAAE,eAAe,EAAuB,MAAM,UAAU,CAAC;AAChE,OAAO,EAAE,mBAAmB,EAAE,oBAAoB,EAAE,kBAAkB,EAAE,mBAAmB,EAAkF,MAAM,YAAY,CAAC;AAEhM,kGAAkG;AAClG,MAAM,cAAc,GAAgB,EAAE,KAAK,EAAE,CAAC,MAAM,CAAC,EAAE,CAAC;AAExD;;;GAGG;AACH,MAAM,YAAY,GAAY,EAAE,KAAK,EAAE,CAAC,IAAI,EAAE,SAAS,CAAC,EAAE,MAAM,EAAE,CAAC,IAAI,EAAE,SAAS,CAAC,EAAE,CAAC;AAEtF,0GAA0G;AAC1G,MAAM,aAAa,GAAY,EAAE,KAAK,EAAE,CAAC,IAAI,EAAE,SAAS,CAAC,EAAE,MAAM,EAAE,CAAC,SAAS,CAAC,EAAE,CAAC;AAEjF;;;GAGG;AACH,MAAM,UAAU,mBAAmB,CAAC,OAAO,GAAmB,EAAE;IAC9D,MAAM,MAAM,GAAG,eAAe,CAAC,OAAO,CAAC,CAAC;IACxC,MAAM,IAAI,GAAiB,eAAe,EAAE;SACzC,IAAI,CAAC,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;SACrB,KAAK,CAAC,kBAAkB,CAAC;SACzB,MAAM,CAAC,mBAAmB,CAAC;SAC3B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,EAAE,CAAC,WAAW,CAAC,CAAC,CAAC;AAErG,MAAM,CAAC,MAAM,SAAS,GAAoB,UAAU,CAAC,MAAM,CAAC;AAE5D,eAAe,SAAS,CAAC;AAEzB;;;;;;;GAOG;AACH,MAAM,UAAU,eAAe,CAAC,OAAO,GAAmB,EAAE;IAC1D,IAAI,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,KAAK,SAAS,CAAC;QAAE,OAAO,SAAS,CAAC;IACnF,OAAO,YAAY,CAAC,iBAAiB,EAAE,mBAAmB,CAAC,OAAO,CAAC,EAAE,CAAC,WAAW,CAAC,CAAC,CAAC;AACtF,CAAC;AAED;;;GAGG;AACH,MAAM,CAAC,MAAM,qBAAqB,GAAkC,UAAU,CAAC,QAAQ,CAAC"}
|
package/dist/parse.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The entry `@procedures/ocr/parse`: the records and the parsers of this package. It starts no
|
|
3
|
+
* process, reads no file and imports no Node module, so a browser can also load it.
|
|
4
|
+
*/
|
|
5
|
+
export * from "./text.js";
|
|
6
|
+
export * from "./tsv.js";
|
|
7
|
+
export * from "./bbox.js";
|
|
8
|
+
//# sourceMappingURL=parse.d.ts.map
|
|
@@ -0,0 +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"}
|
package/dist/parse.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The entry `@procedures/ocr/parse`: the records and the parsers of this package. It starts no
|
|
3
|
+
* process, reads no file and imports no Node module, so a browser can also load it.
|
|
4
|
+
*/
|
|
5
|
+
export * from "./text.js";
|
|
6
|
+
export * from "./tsv.js";
|
|
7
|
+
export * from "./bbox.js";
|
|
8
|
+
//# sourceMappingURL=parse.js.map
|
|
@@ -0,0 +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"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ocr.image procedure
|
|
3
|
+
*
|
|
4
|
+
* This procedure reads the text of an image with the OCR engine of the host (tesseract first).
|
|
5
|
+
*/
|
|
6
|
+
import type { ProcedureContext } from "@procedures/client";
|
|
7
|
+
import { type OcrRunner } from "../../run.js";
|
|
8
|
+
import type { OcrImageInput, OcrImageOutput } from "../../types.js";
|
|
9
|
+
/**
|
|
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.
|
|
12
|
+
*/
|
|
13
|
+
export declare function ocrImage(input: OcrImageInput, ctx: Pick<ProcedureContext, "client">, runner?: OcrRunner): Promise<OcrImageOutput>;
|
|
14
|
+
//# sourceMappingURL=image.d.ts.map
|
|
@@ -0,0 +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;AAEpE;;;GAGG;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,CAM1J"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ocr.image procedure
|
|
3
|
+
*
|
|
4
|
+
* This procedure reads the text of an image with the OCR engine of the host (tesseract first).
|
|
5
|
+
*/
|
|
6
|
+
import { resolve } from "node:path";
|
|
7
|
+
import { createOcrRunner, DEFAULT_TIMEOUT_MS } from "../../run.js";
|
|
8
|
+
/**
|
|
9
|
+
* 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
|
+
*/
|
|
12
|
+
export async function ocrImage(input, ctx, runner = createOcrRunner()) {
|
|
13
|
+
const { engine } = runner;
|
|
14
|
+
// An absolute path does not start with "-", so the engine cannot read it as a flag
|
|
15
|
+
const argv = engine.argv(resolve(input.file), { language: input.language, psm: input.psm, dpi: input.dpi });
|
|
16
|
+
const stdout = await runner.run(engine.program, argv, input.timeout ?? DEFAULT_TIMEOUT_MS, ctx);
|
|
17
|
+
return { engine: engine.name, ...engine.parse(stdout) };
|
|
18
|
+
}
|
|
19
|
+
//# sourceMappingURL=image.js.map
|
|
@@ -0,0 +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;AAGnF;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,QAAQ,CAAC,KAAoB,EAAE,GAAqC,EAAE,MAAM,GAAc,eAAe,EAAE;IAC/H,MAAM,EAAE,MAAM,EAAE,GAAG,MAAM,CAAC;IAC1B,mFAAmF;IACnF,MAAM,IAAI,GAAG,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE,EAAE,QAAQ,EAAE,KAAK,CAAC,QAAQ,EAAE,GAAG,EAAE,KAAK,CAAC,GAAG,EAAE,GAAG,EAAE,KAAK,CAAC,GAAG,EAAE,CAAC,CAAC;IAC5G,MAAM,MAAM,GAAG,MAAM,MAAM,CAAC,GAAG,CAAC,MAAM,CAAC,OAAO,EAAE,IAAI,EAAE,KAAK,CAAC,OAAO,IAAI,kBAAkB,EAAE,GAAG,CAAC,CAAC;IAChG,OAAO,EAAE,MAAM,EAAE,MAAM,CAAC,IAAI,EAAE,GAAG,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,EAAE,CAAC;AAC1D,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/procedures/ocr/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,OAAO,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,WAAW,CAAC;AAC9E,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/procedures/ocr/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,OAAO,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,WAAW,CAAC;AAC9E,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ocr.page procedure
|
|
3
|
+
*
|
|
4
|
+
* This procedure gives the text of a PDF page. It reads the text layer of the page first, with
|
|
5
|
+
* `pdftotext`. Only for a page with no text layer, it makes an image of the page with `pdftoppm`
|
|
6
|
+
* and reads the image with the OCR engine.
|
|
7
|
+
*/
|
|
8
|
+
import type { ProcedureContext } from "@procedures/client";
|
|
9
|
+
import { type OcrRunner } from "../../run.js";
|
|
10
|
+
import type { OcrPageInput, OcrPageOutput } from "../../types.js";
|
|
11
|
+
/** The default resolution of the image of a page for OCR, in dots per inch. */
|
|
12
|
+
export declare const DEFAULT_DPI = 300;
|
|
13
|
+
/** This function gives the argv of `pdftotext` for the text layer of one page, with the boxes. */
|
|
14
|
+
export declare function pdftotextArgs(file: string, page: number): string[];
|
|
15
|
+
/** This function gives the argv of `pdftoppm` for a PNG image of one page. The image is `<prefix>.png`. */
|
|
16
|
+
export declare function pdftoppmArgs(file: string, page: number, dpi: number, prefix: string): string[];
|
|
17
|
+
/**
|
|
18
|
+
* This function gives the text of one page of the PDF file of the input. The boxes are in PDF
|
|
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.
|
|
21
|
+
*/
|
|
22
|
+
export declare function ocrPage(input: OcrPageInput, ctx: Pick<ProcedureContext, "client">, runner?: OcrRunner): Promise<OcrPageOutput>;
|
|
23
|
+
//# sourceMappingURL=page.d.ts.map
|
|
@@ -0,0 +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;AAElE,+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;;;;GAIG;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"}
|