@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.
Files changed (74) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/LICENSE +21 -0
  3. package/README.md +93 -1
  4. package/dist/bbox.d.ts +23 -0
  5. package/dist/bbox.d.ts.map +1 -0
  6. package/dist/bbox.js +76 -0
  7. package/dist/bbox.js.map +1 -0
  8. package/dist/engine.d.ts +40 -0
  9. package/dist/engine.d.ts.map +1 -0
  10. package/dist/engine.js +36 -0
  11. package/dist/engine.js.map +1 -0
  12. package/dist/index.d.ts +8 -0
  13. package/dist/index.d.ts.map +1 -0
  14. package/dist/index.js +11 -0
  15. package/dist/index.js.map +1 -0
  16. package/dist/module.d.ts +33 -0
  17. package/dist/module.d.ts.map +1 -0
  18. package/dist/module.js +84 -0
  19. package/dist/module.js.map +1 -0
  20. package/dist/parse.d.ts +8 -0
  21. package/dist/parse.d.ts.map +1 -0
  22. package/dist/parse.js +8 -0
  23. package/dist/parse.js.map +1 -0
  24. package/dist/procedures/ocr/image.d.ts +14 -0
  25. package/dist/procedures/ocr/image.d.ts.map +1 -0
  26. package/dist/procedures/ocr/image.js +19 -0
  27. package/dist/procedures/ocr/image.js.map +1 -0
  28. package/dist/procedures/ocr/index.d.ts +3 -0
  29. package/dist/procedures/ocr/index.d.ts.map +1 -0
  30. package/dist/procedures/ocr/index.js +3 -0
  31. package/dist/procedures/ocr/index.js.map +1 -0
  32. package/dist/procedures/ocr/page.d.ts +23 -0
  33. package/dist/procedures/ocr/page.d.ts.map +1 -0
  34. package/dist/procedures/ocr/page.js +74 -0
  35. package/dist/procedures/ocr/page.js.map +1 -0
  36. package/dist/programs.d.ts +66 -0
  37. package/dist/programs.d.ts.map +1 -0
  38. package/dist/programs.js +107 -0
  39. package/dist/programs.js.map +1 -0
  40. package/dist/register.d.ts +6 -0
  41. package/dist/register.d.ts.map +1 -0
  42. package/dist/register.js +8 -0
  43. package/dist/register.js.map +1 -0
  44. package/dist/run.d.ts +32 -0
  45. package/dist/run.d.ts.map +1 -0
  46. package/dist/run.js +48 -0
  47. package/dist/run.js.map +1 -0
  48. package/dist/text.d.ts +56 -0
  49. package/dist/text.d.ts.map +1 -0
  50. package/dist/text.js +77 -0
  51. package/dist/text.js.map +1 -0
  52. package/dist/tsv.d.ts +26 -0
  53. package/dist/tsv.d.ts.map +1 -0
  54. package/dist/tsv.js +91 -0
  55. package/dist/tsv.js.map +1 -0
  56. package/dist/types.d.ts +44 -0
  57. package/dist/types.d.ts.map +1 -0
  58. package/dist/types.js +64 -0
  59. package/dist/types.js.map +1 -0
  60. package/package.json +87 -5
  61. package/src/bbox.ts +76 -0
  62. package/src/engine.ts +59 -0
  63. package/src/index.ts +10 -0
  64. package/src/module.ts +103 -0
  65. package/src/parse.ts +8 -0
  66. package/src/procedures/ocr/image.ts +22 -0
  67. package/src/procedures/ocr/index.ts +2 -0
  68. package/src/procedures/ocr/page.ts +80 -0
  69. package/src/programs.ts +142 -0
  70. package/src/register.ts +10 -0
  71. package/src/run.ts +72 -0
  72. package/src/text.ts +120 -0
  73. package/src/tsv.ts +101 -0
  74. package/src/types.ts +122 -0
@@ -0,0 +1,74 @@
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 { existsSync, mkdtempSync, rmSync } from "node:fs";
9
+ import { tmpdir } from "node:os";
10
+ import { join, resolve } from "node:path";
11
+ import { parsePdftotextBbox } from "../../bbox.js";
12
+ import { PDFTOPPM, PDFTOTEXT } from "../../programs.js";
13
+ import { createOcrRunner, DEFAULT_TIMEOUT_MS } from "../../run.js";
14
+ import { scaleText } from "../../text.js";
15
+ /** The default resolution of the image of a page for OCR, in dots per inch. */
16
+ export const DEFAULT_DPI = 300;
17
+ /** The PDF points in one inch. */
18
+ const POINTS_PER_INCH = 72;
19
+ /** This function gives the argv of `pdftotext` for the text layer of one page, with the boxes. */
20
+ export function pdftotextArgs(file, page) {
21
+ return ["-f", String(page), "-l", String(page), "-bbox-layout", "-enc", "UTF-8", file, "-"];
22
+ }
23
+ /** This function gives the argv of `pdftoppm` for a PNG image of one page. The image is `<prefix>.png`. */
24
+ export function pdftoppmArgs(file, page, dpi, prefix) {
25
+ return ["-f", String(page), "-l", String(page), "-r", String(dpi), "-png", "-singlefile", file, prefix];
26
+ }
27
+ /**
28
+ * This function gives the text of one page of the PDF file of the input. The boxes are in PDF
29
+ * 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
+ */
32
+ export async function ocrPage(input, ctx, runner = createOcrRunner()) {
33
+ // An absolute path does not start with "-", so a program cannot read it as a flag
34
+ const file = resolve(input.file);
35
+ const page = input.page ?? 1;
36
+ const mode = input.mode ?? "auto";
37
+ const timeout = input.timeout ?? DEFAULT_TIMEOUT_MS;
38
+ // The text layer also gives the size of the page in points, so it runs in each mode
39
+ const layer = parsePdftotextBbox(await runner.run(PDFTOTEXT, pdftotextArgs(file, page), timeout, ctx));
40
+ if (mode === "text" || (mode === "auto" && layer.lines.length > 0))
41
+ return { page, source: "text-layer", ...layer };
42
+ const dpi = input.dpi ?? DEFAULT_DPI;
43
+ const { engine } = runner;
44
+ const folder = mkdtempSync(join(runner.tempFolder ?? tmpdir(), "procedures-ocr-"));
45
+ try {
46
+ const prefix = join(folder, "page");
47
+ await runner.run(PDFTOPPM, pdftoppmArgs(file, page, dpi, prefix), timeout, ctx);
48
+ const image = `${prefix}.png`;
49
+ if (!existsSync(image))
50
+ throw new Error(`pdftoppm made no image of page ${page}`);
51
+ const argv = engine.argv(image, { language: input.language, psm: input.psm, dpi });
52
+ const pixels = engine.parse(await runner.run(engine.program, argv, timeout, ctx));
53
+ const points = scaleText(pixels, POINTS_PER_INCH / dpi, "pt");
54
+ // The size of the page comes from the text layer, when it has one
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 };
57
+ }
58
+ finally {
59
+ removeFolder(folder);
60
+ }
61
+ }
62
+ /**
63
+ * This function removes the temporary folder. A removal that fails leaves the folder and does not
64
+ * throw, so it does not hide the result or the error of the call.
65
+ */
66
+ function removeFolder(folder) {
67
+ try {
68
+ rmSync(folder, { recursive: true, force: true, maxRetries: 3, retryDelay: 100 });
69
+ }
70
+ catch {
71
+ // The folder stays in the temporary folder of the system
72
+ }
73
+ }
74
+ //# sourceMappingURL=page.js.map
@@ -0,0 +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,IAAI,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAE1C,OAAO,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAC;AACnD,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC;AACxD,OAAO,EAAE,eAAe,EAAE,kBAAkB,EAAkB,MAAM,cAAc,CAAC;AACnF,OAAO,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC;AAG1C,+EAA+E;AAC/E,MAAM,CAAC,MAAM,WAAW,GAAG,GAAG,CAAC;AAE/B,kCAAkC;AAClC,MAAM,eAAe,GAAG,EAAE,CAAC;AAE3B,kGAAkG;AAClG,MAAM,UAAU,aAAa,CAAC,IAAY,EAAE,IAAY;IACtD,OAAO,CAAC,IAAI,EAAE,MAAM,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,MAAM,CAAC,IAAI,CAAC,EAAE,cAAc,EAAE,MAAM,EA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EAAE,CAAC;IAC1E,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,66 @@
1
+ /**
2
+ * The search for the programs of this package on the host: `tesseract` (the OCR engine), and
3
+ * `pdftotext` and `pdftoppm` of Poppler. The package does not download a program. When the search
4
+ * finds no program, `requireProgram` throws an `OcrToolMissingError`.
5
+ */
6
+ /** The environment of the search. */
7
+ export type ProgramEnv = Readonly<Record<string, string | undefined>>;
8
+ /** The description of a program for the search. */
9
+ export interface ProgramSpec {
10
+ /** The name of the program, without `.exe`, for example `tesseract`. */
11
+ readonly name: string;
12
+ /** The environment variables that give the path of the program, in order. */
13
+ readonly env: readonly string[];
14
+ /** The environment variables that give the folder of the program, in order. */
15
+ readonly folderEnv?: readonly string[] | undefined;
16
+ /** This function gives the install paths of the program on an operating system, in order. */
17
+ readonly installed: (platform: NodeJS.Platform, env: ProgramEnv) => readonly string[];
18
+ /** The text that tells how to install the program, for the error. */
19
+ readonly install: string;
20
+ }
21
+ /** The options of the search. Each one has a default from the host. */
22
+ export interface FindProgramOptions {
23
+ /** The environment (default: `process.env`). */
24
+ readonly env?: ProgramEnv | undefined;
25
+ /** The operating system (default: `process.platform`). */
26
+ readonly platform?: NodeJS.Platform | undefined;
27
+ /** The function that tells if a file exists (default: `existsSync`). */
28
+ readonly exists?: ((path: string) => boolean) | undefined;
29
+ }
30
+ /** The OCR engine tesseract. The installer for Windows puts it in `Tesseract-OCR`, and not on the `PATH`. */
31
+ export declare const TESSERACT: ProgramSpec;
32
+ /** The program of Poppler that gives the text layer of a PDF page. */
33
+ export declare const PDFTOTEXT: ProgramSpec;
34
+ /** The program of Poppler that makes an image of a PDF page. */
35
+ export declare const PDFTOPPM: ProgramSpec;
36
+ /** The programs of this package. */
37
+ export declare const OCR_PROGRAMS: readonly ProgramSpec[];
38
+ /** The error when a program of this package is not on the host. */
39
+ export declare class OcrToolMissingError extends Error {
40
+ /** The code of the error, for a check without `instanceof`. */
41
+ readonly code: "OCR_TOOL_MISSING";
42
+ /** The name of the program, for example `tesseract`. */
43
+ readonly tool: string;
44
+ /** The paths that the search examined, in order. */
45
+ readonly searched: readonly string[];
46
+ readonly name: string;
47
+ constructor(spec: ProgramSpec, searched: readonly string[], cause?: string);
48
+ }
49
+ /**
50
+ * This function gives the paths that the search examines for a program, in order. The order is
51
+ * the folders of the `folderEnv` variables, the install paths, then the folders of the `PATH`.
52
+ */
53
+ export declare function programCandidates(spec: ProgramSpec, platform: NodeJS.Platform, env: ProgramEnv): string[];
54
+ /**
55
+ * This function finds a program. The order is `executable`, then the `env` variables of the spec,
56
+ * then `programCandidates`. It gives `undefined` when it finds none. It does not examine the
57
+ * value of `executable` or of an `env` variable.
58
+ */
59
+ export declare function findProgram(spec: ProgramSpec, options?: FindProgramOptions & {
60
+ readonly executable?: string | undefined;
61
+ }): string | undefined;
62
+ /** This function finds a program, as `findProgram` does. It throws an `OcrToolMissingError` when it finds none. */
63
+ export declare function requireProgram(spec: ProgramSpec, options?: FindProgramOptions & {
64
+ readonly executable?: string | undefined;
65
+ }): string;
66
+ //# sourceMappingURL=programs.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"programs.d.ts","sourceRoot":"","sources":["../src/programs.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAKH,qCAAqC;AACrC,MAAM,MAAM,UAAU,GAAG,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,CAAC,CAAC;AAEtE,mDAAmD;AAC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Q,EAAE,SAAS,MAAM,EAAE,CAAC;IAErC,SAAkB,IAAI,EAAE,MAAM,CAAyB;IACvD,YAAY,IAAI,EAAE,WAAW,EAAE,QAAQ,EAAE,SAAS,MAAM,EAAE,EAAE,KAAK,CAAC,EAAE,MAAM,EAMzE;CACF;AASD;;;GAGG;AACH,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,CAAC,QAAQ,EAAE,GAAG,EAAE,UAAU,GAAG,MAAM,EAAE,CAMzG;AAED;;;;GAIG;AACH,wBAAgB,WAAW,CAAC,IAAI,EAAE,WAAW,EAAE,OAAO,GAAE,kBAAkB,GAAG;IAAE,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;CAAO,GAAG,MAAM,GAAG,SAAS,CASlJ;AAED,mHAAmH;AACnH,wBAAgB,cAAc,CAAC,IAAI,EAAE,WAAW,EAAE,OAAO,GAAE,kBAAkB,GAAG;IAAE,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;CAAO,GAAG,MAAM,CAIzI"}
@@ -0,0 +1,107 @@
1
+ /**
2
+ * The search for the programs of this package on the host: `tesseract` (the OCR engine), and
3
+ * `pdftotext` and `pdftoppm` of Poppler. The package does not download a program. When the search
4
+ * finds no program, `requireProgram` throws an `OcrToolMissingError`.
5
+ */
6
+ import { existsSync } from "node:fs";
7
+ import { posix, win32 } from "node:path";
8
+ /** The folders of Homebrew on macOS. An app that starts from the Finder does not have them on the `PATH`. */
9
+ const HOMEBREW_FOLDERS = ["/opt/homebrew/bin", "/usr/local/bin"];
10
+ function programFiles(env) {
11
+ return [env["ProgramFiles"] ?? env["PROGRAMFILES"] ?? "C:\\Program Files", env["ProgramFiles(x86)"] ?? env["PROGRAMFILES(X86)"] ?? "C:\\Program Files (x86)"];
12
+ }
13
+ /** The OCR engine tesseract. The installer for Windows puts it in `Tesseract-OCR`, and not on the `PATH`. */
14
+ export const TESSERACT = {
15
+ name: "tesseract",
16
+ env: ["TESSERACT_EXE"],
17
+ installed: (platform, env) => {
18
+ if (platform === "win32") {
19
+ const local = env["LOCALAPPDATA"];
20
+ return [
21
+ ...programFiles(env).map((folder) => win32.join(folder, "Tesseract-OCR", "tesseract.exe")),
22
+ ...(local !== undefined ? [win32.join(local, "Programs", "Tesseract-OCR", "tesseract.exe")] : []),
23
+ ];
24
+ }
25
+ return platform === "darwin" ? HOMEBREW_FOLDERS.map((folder) => posix.join(folder, "tesseract")) : [];
26
+ },
27
+ install: "the package tesseract-ocr on Linux, brew install tesseract on macOS, or the Tesseract installer on Windows",
28
+ };
29
+ function poppler(name, env) {
30
+ return {
31
+ name,
32
+ env: [env],
33
+ folderEnv: ["POPPLER_PATH"],
34
+ installed: (platform, environment) => {
35
+ if (platform === "win32")
36
+ return programFiles(environment).map((folder) => win32.join(folder, "poppler", "Library", "bin", `${name}.exe`));
37
+ return platform === "darwin" ? HOMEBREW_FOLDERS.map((folder) => posix.join(folder, name)) : [];
38
+ },
39
+ install: "the package poppler-utils on Linux, brew install poppler on macOS, or a Poppler build on Windows",
40
+ };
41
+ }
42
+ /** The program of Poppler that gives the text layer of a PDF page. */
43
+ export const PDFTOTEXT = poppler("pdftotext", "PDFTOTEXT_EXE");
44
+ /** The program of Poppler that makes an image of a PDF page. */
45
+ export const PDFTOPPM = poppler("pdftoppm", "PDFTOPPM_EXE");
46
+ /** The programs of this package. */
47
+ export const OCR_PROGRAMS = [TESSERACT, PDFTOTEXT, PDFTOPPM];
48
+ /** The error when a program of this package is not on the host. */
49
+ export class OcrToolMissingError extends Error {
50
+ /** The code of the error, for a check without `instanceof`. */
51
+ code = "OCR_TOOL_MISSING";
52
+ /** The name of the program, for example `tesseract`. */
53
+ tool;
54
+ /** The paths that the search examined, in order. */
55
+ searched;
56
+ name = "OcrToolMissingError";
57
+ constructor(spec, searched, cause) {
58
+ const variables = [...spec.env, ...(spec.folderEnv ?? [])].join(" or ");
59
+ const reason = cause === undefined ? `The search examined ${searched.length} paths.` : `It did not start: ${cause}.`;
60
+ super(`The program ${spec.name} is not on this host. ${reason} Install ${spec.install}, or set ${variables}.`);
61
+ this.tool = spec.name;
62
+ this.searched = searched;
63
+ }
64
+ }
65
+ /** This function gives the folders of the `PATH`, in order. */
66
+ function pathFolders(env, windows) {
67
+ const value = windows ? (env["Path"] ?? env["PATH"]) : env["PATH"];
68
+ if (value === undefined)
69
+ return [];
70
+ return value.split(windows ? ";" : ":").filter((folder) => folder.length > 0);
71
+ }
72
+ /**
73
+ * This function gives the paths that the search examines for a program, in order. The order is
74
+ * the folders of the `folderEnv` variables, the install paths, then the folders of the `PATH`.
75
+ */
76
+ export function programCandidates(spec, platform, env) {
77
+ const windows = platform === "win32";
78
+ const path = windows ? win32 : posix;
79
+ const file = windows ? `${spec.name}.exe` : spec.name;
80
+ const folders = (spec.folderEnv ?? []).map((name) => env[name]).filter((value) => value !== undefined && value.length > 0);
81
+ return [...folders.map((folder) => path.join(folder, file)), ...spec.installed(platform, env), ...pathFolders(env, windows).map((folder) => path.join(folder, file))];
82
+ }
83
+ /**
84
+ * This function finds a program. The order is `executable`, then the `env` variables of the spec,
85
+ * then `programCandidates`. It gives `undefined` when it finds none. It does not examine the
86
+ * value of `executable` or of an `env` variable.
87
+ */
88
+ export function findProgram(spec, options = {}) {
89
+ if (options.executable !== undefined && options.executable.length > 0)
90
+ return options.executable;
91
+ const env = options.env ?? process.env;
92
+ for (const name of spec.env) {
93
+ const value = env[name];
94
+ if (value !== undefined && value.length > 0)
95
+ return value;
96
+ }
97
+ const exists = options.exists ?? existsSync;
98
+ return programCandidates(spec, options.platform ?? process.platform, env).find((candidate) => exists(candidate));
99
+ }
100
+ /** This function finds a program, as `findProgram` does. It throws an `OcrToolMissingError` when it finds none. */
101
+ export function requireProgram(spec, options = {}) {
102
+ const found = findProgram(spec, options);
103
+ if (found !== undefined)
104
+ return found;
105
+ throw new OcrToolMissingError(spec, programCandidates(spec, options.platform ?? process.platform, options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B,MAAM,KAAK,GAAG,GAAG,CAAC,IAAI,CAAC,CAAC;QACxB,IAAI,KAAK,KAAK,SAAS,IAAI,KAAK,CAAC,MAAM,GAAG,CAAC;YAAE,OAAO,KAAK,CAAC;IAC5D,CAAC;IACD,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,IAAI,UAAU,CAAC;IAC5C,OAAO,iBAAiB,CAAC,IAAI,EAAE,OAAO,CAAC,QAAQ,IAAI,OAAO,CAAC,QAAQ,EAAE,GAAG,CAAC,CAAC,IAAI,CAAC,CAAC,SAAS,EAAE,EAAE,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC,CAAC;AACnH,CAAC;AAED,mHAAmH;AACnH,MAAM,UAAU,cAAc,CAAC,IAAiB,EAAE,OAAO,GAAsE,EAAE;IAC/H,MAAM,KAAK,GAAG,WAAW,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;IACzC,IAAI,KAAK,KAAK,SAAS;QAAE,OAAO,KAAK,CAAC;IACtC,MAAM,IAAI,mBAAmB,CAAC,IAAI,EAAE,iBAAiB,CAAC,IAAI,EAAE,OAAO,CAAC,QAAQ,IAAI,OAAO,CAAC,QAAQ,EAAE,OAAO,CAAC,GAAG,IAAI,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC;AACjI,CAAC"}
@@ -0,0 +1,6 @@
1
+ /**
2
+ * An import of this file registers the procedures of @procedures/ocr in the global registry:
3
+ * `import "@procedures/ocr/register"`. The package root registers nothing.
4
+ */
5
+ export * from "./module.js";
6
+ //# sourceMappingURL=register.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"register.d.ts","sourceRoot":"","sources":["../src/register.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAIH,cAAc,aAAa,CAAC"}
@@ -0,0 +1,8 @@
1
+ /**
2
+ * An import of this file registers the procedures of @procedures/ocr in the global registry:
3
+ * `import "@procedures/ocr/register"`. The package root registers nothing.
4
+ */
5
+ import { registerOcrProcedures } from "./module.js";
6
+ export * from "./module.js";
7
+ registerOcrProcedures();
8
+ //# sourceMappingURL=register.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"register.js","sourceRoot":"","sources":["../src/register.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AAEpD,cAAc,aAAa,CAAC;AAE5B,qBAAqB,EAAE,CAAC"}
package/dist/run.d.ts ADDED
@@ -0,0 +1,32 @@
1
+ /**
2
+ * The run of the programs of this package. Each program starts as argv through a nested
3
+ * `shell.run` call (`defineCliTool` of `@procedures/shell`). No shell starts. The engine of
4
+ * the host applies its rules to that call, and a cancel stops the program.
5
+ */
6
+ import type { ProcedureContext } from "@procedures/client";
7
+ import { type OcrEngine } from "./engine.js";
8
+ import { type FindProgramOptions, type ProgramSpec } from "./programs.js";
9
+ /** The default time limit of one program, in milliseconds. */
10
+ export declare const DEFAULT_TIMEOUT_MS = 120000;
11
+ /** The settings of the host for the procedures of this package. The procedure input cannot change them. */
12
+ export interface OcrHostOptions extends FindProgramOptions {
13
+ /**
14
+ * The path of each program, by name: `tesseract`, `pdftotext` or `pdftoppm`. A program with no
15
+ * path comes from the search of `findProgram`.
16
+ */
17
+ readonly executables?: Readonly<Record<string, string | undefined>> | undefined;
18
+ /** The OCR engine (default: `tesseractEngine`). */
19
+ readonly engine?: OcrEngine | undefined;
20
+ /** The folder of the temporary folders (default: the temporary folder of the system). */
21
+ readonly tempFolder?: string | undefined;
22
+ }
23
+ /** The programs of one host, ready to start. */
24
+ export interface OcrRunner {
25
+ readonly engine: OcrEngine;
26
+ readonly tempFolder: string | undefined;
27
+ /** This function starts a program and gives its standard output. */
28
+ run(spec: ProgramSpec, args: readonly string[], timeout: number, ctx: Pick<ProcedureContext, "client">): Promise<string>;
29
+ }
30
+ /** This function gives the runner of the programs with the settings of the host. */
31
+ export declare function createOcrRunner(options?: OcrHostOptions): OcrRunner;
32
+ //# sourceMappingURL=run.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"run.d.ts","sourceRoot":"","sources":["../src/run.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AAE3D,OAAO,EAAmB,KAAK,SAAS,EAAE,MAAM,aAAa,CAAC;AAC9D,OAAO,EAAuC,KAAK,kBAAkB,EAAE,KAAK,WAAW,EAAE,MAAM,eAAe,CAAC;AAE/G,8DAA8D;AAC9D,eAAO,MAAM,kBAAkB,SAAU,CAAC;AAE1C,2GAA2G;AAC3G,MAAM,WAAW,cAAe,SAAQ,kBAAkB;IACxD;;;OAGG;IACH,QAAQ,CAAC,WAAW,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,CAAC,GAAG,SAAS,CAAC;IAChF,mDAAmD;IACnD,QAAQ,CAAC,MAAM,CAAC,EAAE,SAAS,GAAG,SAAS,CAAC;IACxC,yFAAyF;IACzF,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;CAC1C;AAED,gDAAgD;AAChD,MAAM,WAAW,SAAS;IACxB,QAAQ,CAAC,MAAM,EAAE,SAAS,CAAC;IAC3B,QAAQ,CAAC,UAAU,EAAE,MAAM,GAAG,SAAS,CAAC;IACxC,oEAAoE;IACpE,GAAG,CAAC,IAAI,EAAE,WAAW,EAAE,IAAI,EAAE,SAAS,MAAM,EAAE,EAAE,OAAO,EAAE,MAAM,EAAE,GAAG,EAAE,IAAI,CAAC,gBAAgB,EAAE,QAAQ,CAAC,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;CAC1H;AAUD,oFAAoF;AACpF,wBAAgB,eAAe,CAAC,OAAO,GAAE,cAAmB,GAAG,SAAS,CA2BvE"}
package/dist/run.js ADDED
@@ -0,0 +1,48 @@
1
+ /**
2
+ * The run of the programs of this package. Each program starts as argv through a nested
3
+ * `shell.run` call (`defineCliTool` of `@procedures/shell`). No shell starts. The engine of
4
+ * the host applies its rules to that call, and a cancel stops the program.
5
+ */
6
+ import { CliToolError, defineCliTool } from "@procedures/shell/cli";
7
+ import { tesseractEngine } from "./engine.js";
8
+ import { OcrToolMissingError, requireProgram } from "./programs.js";
9
+ /** The default time limit of one program, in milliseconds. */
10
+ export const DEFAULT_TIMEOUT_MS = 120_000;
11
+ /**
12
+ * True when the output of `shell.run` tells that the program is not there. `shell.run` gives exit
13
+ * code 1 and the error of the start as the standard error, for example `spawn tesseract ENOENT`.
14
+ */
15
+ function notFound(result) {
16
+ return result.exitCode === 1 && result.stdout === "" && /^spawn .+ ENOENT$/.test(result.stderr.trim());
17
+ }
18
+ /** This function gives the runner of the programs with the settings of the host. */
19
+ export function createOcrRunner(options = {}) {
20
+ const engine = options.engine ?? tesseractEngine;
21
+ const find = { env: options.env, platform: options.platform, exists: options.exists };
22
+ const tools = new Map();
23
+ const toolOf = (spec) => {
24
+ let tool = tools.get(spec.name);
25
+ if (tool === undefined) {
26
+ tool = defineCliTool({ name: spec.name, resolve: () => requireProgram(spec, { ...find, executable: options.executables?.[spec.name] }) });
27
+ tools.set(spec.name, tool);
28
+ }
29
+ return tool;
30
+ };
31
+ return {
32
+ engine,
33
+ tempFolder: options.tempFolder,
34
+ async run(spec, args, timeout, ctx) {
35
+ const tool = toolOf(spec);
36
+ const input = tool.shellInput(args, { timeout });
37
+ const result = await tool.call(args, { timeout }, ctx);
38
+ if (notFound(result))
39
+ throw new OcrToolMissingError(spec, [input.command], result.stderr.trim());
40
+ if (result.exitCode !== 0) {
41
+ const text = result.stderr.trim() || result.stdout.trim();
42
+ throw new CliToolError(`${spec.name} failed (exit code ${result.exitCode}): ${text}`.trim(), result.exitCode, result.stderr);
43
+ }
44
+ return result.stdout;
45
+ },
46
+ };
47
+ }
48
+ //# sourceMappingURL=run.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"run.js","sourceRoot":"","sources":["../src/run.ts"],"names":[],"mappings":"AAAA;;;;GAIG;A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AAC,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;QAC7B,CAAC;QACD,OAAO,IAAI,CAAC;IACd,CAAC,CAAC;IACF,OAAO;QACL,MAAM;QACN,UAAU,EAAE,OAAO,CAAC,UAAU;QAC9B,KAAK,CAAC,GAAG,CAAC,IAAI,EAAE,IAAI,EAAE,OAAO,EAAE,GAAG;YAChC,MAAM,IAAI,GAAG,MAAM,CAAC,IAAI,CAAC,CAAC;YAC1B,MAAM,KAAK,GAAG,IAAI,CAAC,UAAU,CAAC,IAAI,EAAE,EAAE,OAAO,EAAE,CAAC,CAAC;YACjD,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,IAAI,CAAC,IAAI,EAAE,EAAE,OAAO,EAAE,EAAE,GAAG,CAAC,CAAC;YACvD,IAAI,QAAQ,CAAC,MAAM,CAAC;gBAAE,MAAM,IAAI,mBAAmB,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC,CAAC;YACjG,IAAI,MAAM,CAAC,QAAQ,KAAK,CAAC,EAAE,CAAC;gBAC1B,MAAM,IAAI,GAAG,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,IAAI,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC;gBAC1D,MAAM,IAAI,YAAY,CAAC,GAAG,IAAI,CAAC,IAAI,sBAAsB,MAAM,CAAC,QAAQ,MAAM,IAAI,EAAE,CAAC,IAAI,EAAE,EAAE,MAAM,CAAC,QAAQ,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC;YAC/H,CAAC;YACD,OAAO,MAAM,CAAC,MAAM,CAAC;QACvB,CAAC;KACF,CAAC;AACJ,CAAC"}
package/dist/text.d.ts ADDED
@@ -0,0 +1,56 @@
1
+ /**
2
+ * The records of a recognized text: each word and each line with its text, its confidence and its
3
+ * box. The box is the `Rect` record of `@mark1russell7/layout-contract`: the top-left corner and the
4
+ * size, with the y axis down. This file starts no process and reads no file.
5
+ */
6
+ import type { Rect } from "@mark1russell7/layout-contract";
7
+ export type { Rect };
8
+ /** The unit of the boxes: `px` is a pixel of the image, `pt` is a point of the PDF page (1/72 inch). */
9
+ export type OcrUnit = "px" | "pt";
10
+ /** One word: its text, its confidence from 0 to 1 and its box. */
11
+ export interface OcrWord {
12
+ readonly text: string;
13
+ /** The confidence of the engine, from 0 to 1. A word of a text layer has the confidence 1. */
14
+ readonly confidence: number;
15
+ readonly box: Rect;
16
+ }
17
+ /** One line: its text, its confidence, its box and its words, in the order of the line. */
18
+ export interface OcrLine {
19
+ /** The words of the line, with one space between each two words. */
20
+ readonly text: string;
21
+ /** The mean confidence of the words of the line, from 0 to 1. */
22
+ readonly confidence: number;
23
+ readonly box: Rect;
24
+ readonly words: readonly OcrWord[];
25
+ }
26
+ /** The text of a page or of an image, as lines of words with their boxes. */
27
+ export interface OcrText {
28
+ /** The unit of each box, and of `width` and `height`. */
29
+ readonly unit: OcrUnit;
30
+ /** The width of the page or of the image. */
31
+ readonly width: number;
32
+ /** The height of the page or of the image. */
33
+ readonly height: number;
34
+ /** The text of the lines, with one line end between each two lines. */
35
+ readonly text: string;
36
+ /** The mean confidence of all words, from 0 to 1. A text with no word has the confidence 0. */
37
+ readonly confidence: number;
38
+ readonly lines: readonly OcrLine[];
39
+ }
40
+ /** This function gives the smallest box that contains each box of the list. An empty list gives the box 0, 0, 0, 0. */
41
+ export declare function unionBox(boxes: readonly Rect[]): Rect;
42
+ /** This function gives the mean confidence of the words, or 0 for an empty list. */
43
+ export declare function meanConfidence(words: readonly OcrWord[]): number;
44
+ /**
45
+ * This function makes a line from its words. The box of the line is `box`, else the union of the
46
+ * boxes of the words.
47
+ */
48
+ export declare function makeLine(words: readonly OcrWord[], box?: Rect): OcrLine;
49
+ /** This function makes the text of a page or of an image from its lines. */
50
+ export declare function makeText(lines: readonly OcrLine[], unit: OcrUnit, width: number, height: number): OcrText;
51
+ /**
52
+ * This function multiplies each box, the width and the height by `factor`, and sets the unit. For
53
+ * example, the factor `72 / 300` changes the pixels of an image at 300 dots per inch to PDF points.
54
+ */
55
+ export declare function scaleText(text: OcrText, factor: number, unit: OcrUnit): OcrText;
56
+ //# sourceMappingURL=text.d.ts.map
@@ -0,0 +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;AAE3D,YAAY,EAAE,IAAI,EAAE,CAAC;AAErB,wGAAwG;AACxG,MAAM,MAAM,OAAO,GAAG,IAAI,GAAG,IAAI,CAAC;AAElC,kEAAkE;AAClE,MAAM,WAAW,OAAO;IACtB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,8FAA8F;IAC9F,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,QAAQ,CAAC,GAAG,EAAE,IAAI,CAAC;CACpB;AAED,2FAA2F;AAC3F,MAAM,WAAW,OAAO;IACtB,oEAAoE;IACpE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,iEAAiE;IACjE,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,QAAQ,CAAC,GAAG,EAAE,IAAI,CAAC;IACnB,QAAQ,CAAC,KAAK,EAAE,SAAS,OAAO,EAAE,CAAC;CACpC;AAED,6EAA6E;AAC7E,MAAM,WAAW,OAAO;IACtB,yDAAyD;IACzD,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC;IACvB,6CAA6C;IAC7C,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,8CAA8C;IAC9C,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,uEAAuE;IACvE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,+FAA+F;IAC/F,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,QAAQ,CAAC,KAAK,EAAE,SAAS,OAAO,EAAE,CAAC;CACpC;AAED,uHAAuH;AACvH,wBAAgB,QAAQ,CAAC,KAAK,EAAE,SAAS,IAAI,EAAE,GAAG,IAAI,CAarD;AAED,oFAAoF;AACpF,wBAAgB,cAAc,CAAC,KAAK,EAAE,SAAS,OAAO,EAAE,GAAG,MAAM,CAGhE;AAED;;;GAGG;AACH,wBAAgB,QAAQ,CAAC,KAAK,EAAE,SAAS,OAAO,EAAE,EAAE,GAAG,CAAC,EAAE,IAAI,GAAG,OAAO,CAOvE;AAED,4EAA4E;AAC5E,wBAAgB,QAAQ,CAAC,KAAK,EAAE,SAAS,OAAO,EAAE,EAAE,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CASzG;AAWD;;;GAGG;AACH,wBAAgB,SAAS,CAAC,IAAI,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,GAAG,OAAO,CAa/E"}
package/dist/text.js ADDED
@@ -0,0 +1,77 @@
1
+ /**
2
+ * The records of a recognized text: each word and each line with its text, its confidence and its
3
+ * box. The box is the `Rect` record of `@mark1russell7/layout-contract`: the top-left corner and the
4
+ * size, with the y axis down. This file starts no process and reads no file.
5
+ */
6
+ /** This function gives the smallest box that contains each box of the list. An empty list gives the box 0, 0, 0, 0. */
7
+ export function unionBox(boxes) {
8
+ if (boxes.length === 0)
9
+ return { x: 0, y: 0, w: 0, h: 0 };
10
+ let left = Infinity;
11
+ let top = Infinity;
12
+ let right = -Infinity;
13
+ let bottom = -Infinity;
14
+ for (const box of boxes) {
15
+ left = Math.min(left, box.x);
16
+ top = Math.min(top, box.y);
17
+ right = Math.max(right, box.x + box.w);
18
+ bottom = Math.max(bottom, box.y + box.h);
19
+ }
20
+ return { x: left, y: top, w: right - left, h: bottom - top };
21
+ }
22
+ /** This function gives the mean confidence of the words, or 0 for an empty list. */
23
+ export function meanConfidence(words) {
24
+ if (words.length === 0)
25
+ return 0;
26
+ return words.reduce((sum, word) => sum + word.confidence, 0) / words.length;
27
+ }
28
+ /**
29
+ * This function makes a line from its words. The box of the line is `box`, else the union of the
30
+ * boxes of the words.
31
+ */
32
+ export function makeLine(words, box) {
33
+ return {
34
+ text: words.map((word) => word.text).join(" "),
35
+ confidence: meanConfidence(words),
36
+ box: box ?? unionBox(words.map((word) => word.box)),
37
+ words,
38
+ };
39
+ }
40
+ /** This function makes the text of a page or of an image from its lines. */
41
+ export function makeText(lines, unit, width, height) {
42
+ return {
43
+ unit,
44
+ width,
45
+ height,
46
+ text: lines.map((line) => line.text).join("\n"),
47
+ confidence: meanConfidence(lines.flatMap((line) => line.words)),
48
+ lines,
49
+ };
50
+ }
51
+ /** This function rounds a value to 3 decimals, so a scale gives no long fractions. */
52
+ function round3(value) {
53
+ return Math.round(value * 1000) / 1000;
54
+ }
55
+ function scaleBox(box, factor) {
56
+ return { x: round3(box.x * factor), y: round3(box.y * factor), w: round3(box.w * factor), h: round3(box.h * factor) };
57
+ }
58
+ /**
59
+ * This function multiplies each box, the width and the height by `factor`, and sets the unit. For
60
+ * example, the factor `72 / 300` changes the pixels of an image at 300 dots per inch to PDF points.
61
+ */
62
+ export function scaleText(text, factor, unit) {
63
+ if (!(factor > 0) || !Number.isFinite(factor))
64
+ throw new RangeError(`The scale factor must be a positive number, not ${factor}`);
65
+ return {
66
+ ...text,
67
+ unit,
68
+ width: round3(text.width * factor),
69
+ height: round3(text.height * factor),
70
+ lines: text.lines.map((line) => ({
71
+ ...line,
72
+ box: scaleBox(line.box, factor),
73
+ words: line.words.map((word) => ({ ...word, box: scaleBox(word.box, factor) })),
74
+ })),
75
+ };
76
+ }
77
+ //# sourceMappingURL=text.js.map
@@ -0,0 +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,CA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AAC,EAAE,CAAC;AACxH,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,SAAS,CAAC,IAAa,EAAE,MAAc,EAAE,IAAa;IACpE,IAAI,CAAC,CAAC,MAAM,GAAG,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC;QAAE,MAAM,IAAI,UAAU,CAAC,mDAAmD,MAAM,EAAE,CAAC,CAAC;IACjI,OAAO;QACL,GAAG,IAAI;QACP,IAAI;QACJ,KAAK,EAAE,MAAM,CAAC,IAAI,CAAC,KAAK,GAAG,MAAM,CAAC;QAClC,MAAM,EAAE,MAAM,CAAC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACpC,KAAK,EAAE,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;YAC/B,GAAG,IAAI;YACP,GAAG,EAAE,QAAQ,CAAC,IAAI,CAAC,GAAG,EAAE,MAAM,CAAC;YAC/B,KAAK,EAAE,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,QAAQ,CAAC,IAAI,CAAC,GAAG,EAAE,MAAM,CAAC,EAAE,CAAC,CAAC;SAChF,CAAC,CAAC;KACJ,CAAC;AACJ,CAAC"}
package/dist/tsv.d.ts ADDED
@@ -0,0 +1,26 @@
1
+ /**
2
+ * The parse of the TSV output of tesseract (`tesseract <image> stdout tsv`). This file starts no
3
+ * process and reads no file.
4
+ *
5
+ * Each row of the TSV has 12 columns: `level`, `page_num`, `block_num`, `par_num`, `line_num`,
6
+ * `word_num`, `left`, `top`, `width`, `height`, `conf` and `text`. The level is 1 for the page, 2 for
7
+ * a block, 3 for a paragraph, 4 for a line and 5 for a word. Only a word has a confidence: the other
8
+ * rows have the confidence -1.
9
+ */
10
+ import { type OcrText } from "./text.js";
11
+ /** The columns of the TSV of tesseract, in order. */
12
+ export declare const TESSERACT_TSV_COLUMNS: readonly string[];
13
+ /** This function changes a confidence of tesseract (0 to 100, or -1 for no value) to a value from 0 to 1. */
14
+ export declare function tesseractConfidence(conf: number): number;
15
+ /**
16
+ * This function parses the TSV output of tesseract. It gives the lines of words in the order of
17
+ * the output, with the boxes in the pixels of the image.
18
+ *
19
+ * - The parse ignores the header row, an empty row and a row that is not valid.
20
+ * - A word with no text is not in the result. A line with no word is not in the result.
21
+ * - The box of a line is the box of its row of level 4, else the union of the boxes of its words.
22
+ * - The size is the size of the first row of level 1, else the union of all boxes.
23
+ * - With more than one page (a TIFF file with pages), the lines of all pages are in one result.
24
+ */
25
+ export declare function parseTesseractTsv(tsv: string): OcrText;
26
+ //# sourceMappingURL=tsv.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tsv.d.ts","sourceRoot":"","sources":["../src/tsv.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAAgC,KAAK,OAAO,EAA2B,MAAM,WAAW,CAAC;AAEhG,qDAAqD;AACrD,eAAO,MAAM,qBAAqB,EAAE,SAAS,MAAM,EAalD,CAAC;AAgCF,6GAA6G;AAC7G,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAGxD;AAED;;;;;;;;;GASG;AACH,wBAAgB,iBAAiB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CA0BtD"}