@procedures/ocr 0.68.9 → 0.68.11
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 +25 -0
- package/README.md +35 -2
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/module.d.ts.map +1 -1
- package/dist/module.js +20 -2
- package/dist/module.js.map +1 -1
- package/dist/parse.d.ts +2 -0
- package/dist/parse.d.ts.map +1 -1
- package/dist/parse.js +2 -0
- package/dist/parse.js.map +1 -1
- package/dist/pdfinfo.d.ts +14 -0
- package/dist/pdfinfo.d.ts.map +1 -0
- package/dist/pdfinfo.js +20 -0
- package/dist/pdfinfo.js.map +1 -0
- package/dist/procedures/ocr/index.d.ts +1 -0
- package/dist/procedures/ocr/index.d.ts.map +1 -1
- package/dist/procedures/ocr/index.js +1 -0
- package/dist/procedures/ocr/index.js.map +1 -1
- package/dist/procedures/ocr/pageCount.d.ts +20 -0
- package/dist/procedures/ocr/pageCount.d.ts.map +1 -0
- package/dist/procedures/ocr/pageCount.js +28 -0
- package/dist/procedures/ocr/pageCount.js.map +1 -0
- package/dist/programs.d.ts +28 -2
- package/dist/programs.d.ts.map +1 -1
- package/dist/programs.js +30 -5
- package/dist/programs.js.map +1 -1
- package/dist/run.d.ts +2 -2
- package/dist/run.d.ts.map +1 -1
- package/dist/run.js +3 -0
- package/dist/run.js.map +1 -1
- package/dist/table.d.ts +59 -0
- package/dist/table.d.ts.map +1 -0
- package/dist/table.js +152 -0
- package/dist/table.js.map +1 -0
- package/dist/types.d.ts +12 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +7 -0
- package/dist/types.js.map +1 -1
- package/package.json +4 -3
- package/src/index.ts +1 -1
- package/src/module.ts +34 -2
- package/src/parse.ts +2 -0
- package/src/pdfinfo.ts +19 -0
- package/src/procedures/ocr/index.ts +1 -0
- package/src/procedures/ocr/pageCount.ts +32 -0
- package/src/programs.ts +44 -5
- package/src/run.ts +4 -2
- package/src/table.ts +188 -0
- package/src/types.ts +25 -0
package/dist/table.d.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The rows and the columns of a table, from the boxes of its words. This file starts no process and
|
|
3
|
+
* reads no file.
|
|
4
|
+
*
|
|
5
|
+
* OCR gives words and lines, not cells. A scan of a table needs its cells, for example to read
|
|
6
|
+
* one column of each row. The method uses only the boxes:
|
|
7
|
+
*
|
|
8
|
+
* 1. Two words are in one row when the vertical centers of their boxes are near.
|
|
9
|
+
* 2. In a row, a wide gap between two words starts a new cell.
|
|
10
|
+
* 3. A column starts at an x position where many rows start a cell.
|
|
11
|
+
* 4. Each word goes to the column that holds the center of its box.
|
|
12
|
+
*
|
|
13
|
+
* The lengths of the options are shares of the median height of the words, so one set of options
|
|
14
|
+
* works for pixels and for points.
|
|
15
|
+
*/
|
|
16
|
+
import { type OcrText, type OcrWord, type Rect } from "./text.js";
|
|
17
|
+
/** The options of `tableOfText`. Each length is a share of the median height of the words. */
|
|
18
|
+
export interface OcrTableOptions {
|
|
19
|
+
/** Two words are in one row when their vertical centers differ by less than this share. The default is 0.7. */
|
|
20
|
+
readonly rowTolerance?: number | undefined;
|
|
21
|
+
/** In a row, a gap of more than this share between two words starts a new cell. The default is 0.75. */
|
|
22
|
+
readonly cellGap?: number | undefined;
|
|
23
|
+
/** Two starts of cells are in one column when they differ by no more than this share. The default is 1. */
|
|
24
|
+
readonly columnTolerance?: number | undefined;
|
|
25
|
+
/** A column needs starts of cells in this share of the rows, from 0 to 1. The default is 0.25. */
|
|
26
|
+
readonly minShare?: number | undefined;
|
|
27
|
+
}
|
|
28
|
+
/** One cell: its words, their text with one space between words, the mean confidence and the union of the boxes. */
|
|
29
|
+
export interface OcrTableCell {
|
|
30
|
+
readonly text: string;
|
|
31
|
+
readonly confidence: number;
|
|
32
|
+
readonly box: Rect;
|
|
33
|
+
readonly words: readonly OcrWord[];
|
|
34
|
+
}
|
|
35
|
+
/** One row: the union of the boxes of its words, and one cell for each column. A column with no word in the row has `null`. */
|
|
36
|
+
export interface OcrTableRow {
|
|
37
|
+
readonly box: Rect;
|
|
38
|
+
readonly cells: readonly (OcrTableCell | null)[];
|
|
39
|
+
}
|
|
40
|
+
/** A table: the left edge of each column, from left to right, and the rows from top to bottom. */
|
|
41
|
+
export interface OcrTable {
|
|
42
|
+
readonly columns: readonly number[];
|
|
43
|
+
readonly rows: readonly OcrTableRow[];
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* This function gives the rows and the columns of a table from the boxes of its words. Give the
|
|
47
|
+
* words of one table only: a title or a note above the table becomes a row too.
|
|
48
|
+
*
|
|
49
|
+
* - A row has one cell for each column. A cell is `null` when the row has no word in its column.
|
|
50
|
+
* - A column that holds few starts of cells (less than `minShare` of the rows) is not a column. Its
|
|
51
|
+
* words go to the column at their left.
|
|
52
|
+
* - A word with an empty text is not a word of the table.
|
|
53
|
+
*
|
|
54
|
+
* @param source - A text from the OCR, or its words
|
|
55
|
+
* @param options - The tolerances, as shares of the median height of the words
|
|
56
|
+
* @throws RangeError when an option is not a finite number of 0 or more
|
|
57
|
+
*/
|
|
58
|
+
export declare function tableOfText(source: OcrText | readonly OcrWord[], options?: OcrTableOptions): OcrTable;
|
|
59
|
+
//# sourceMappingURL=table.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"table.d.ts","sourceRoot":"","sources":["../src/table.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAGH,OAAO,EAAY,KAAK,OAAO,EAAE,KAAK,OAAO,EAAE,KAAK,IAAI,EAAE,MAAM,WAAW,CAAC;AAE5E,8FAA8F;AAC9F,MAAM,WAAW,eAAe;IAC9B,+GAA+G;IAC/G,QAAQ,CAAC,YAAY,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IAC3C,wGAAwG;IACxG,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IACtC,2GAA2G;IAC3G,QAAQ,CAAC,eAAe,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IAC9C,kGAAkG;IAClG,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;CACxC;AAED,oHAAoH;AACpH,MAAM,WAAW,YAAY;IAC3B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,QAAQ,CAAC,GAAG,EAAE,IAAI,CAAC;IACnB,QAAQ,CAAC,KAAK,EAAE,SAAS,OAAO,EAAE,CAAC;CACpC;AAED,+HAA+H;AAC/H,MAAM,WAAW,WAAW;IAC1B,QAAQ,CAAC,GAAG,EAAE,IAAI,CAAC;IACnB,QAAQ,CAAC,KAAK,EAAE,SAAS,CAAC,YAAY,GAAG,IAAI,CAAC,EAAE,CAAC;CAClD;AAED,kGAAkG;AAClG,MAAM,WAAW,QAAQ;IACvB,QAAQ,CAAC,OAAO,EAAE,SAAS,MAAM,EAAE,CAAC;IACpC,QAAQ,CAAC,IAAI,EAAE,SAAS,WAAW,EAAE,CAAC;CACvC;AAuGD;;;;;;;;;;;;GAYG;AACH,wBAAgB,WAAW,CAAC,MAAM,EAAE,OAAO,GAAG,SAAS,OAAO,EAAE,EAAE,OAAO,GAAE,eAAoB,GAAG,QAAQ,CAsBzG"}
|
package/dist/table.js
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The rows and the columns of a table, from the boxes of its words. This file starts no process and
|
|
3
|
+
* reads no file.
|
|
4
|
+
*
|
|
5
|
+
* OCR gives words and lines, not cells. A scan of a table needs its cells, for example to read
|
|
6
|
+
* one column of each row. The method uses only the boxes:
|
|
7
|
+
*
|
|
8
|
+
* 1. Two words are in one row when the vertical centers of their boxes are near.
|
|
9
|
+
* 2. In a row, a wide gap between two words starts a new cell.
|
|
10
|
+
* 3. A column starts at an x position where many rows start a cell.
|
|
11
|
+
* 4. Each word goes to the column that holds the center of its box.
|
|
12
|
+
*
|
|
13
|
+
* The lengths of the options are shares of the median height of the words, so one set of options
|
|
14
|
+
* works for pixels and for points.
|
|
15
|
+
*/
|
|
16
|
+
import { median } from "@mark1russell7/rigor-statistics";
|
|
17
|
+
import { unionBox } from "./text.js";
|
|
18
|
+
/** This function gives the value of an option, or its default. It refuses a value that is not finite or less than 0. */
|
|
19
|
+
function option(name, value, fallback) {
|
|
20
|
+
if (value === undefined)
|
|
21
|
+
return fallback;
|
|
22
|
+
if (!Number.isFinite(value) || value < 0)
|
|
23
|
+
throw new RangeError(`tableOfText: the option ${name} is ${value}. It is a finite number of 0 or more.`);
|
|
24
|
+
return value;
|
|
25
|
+
}
|
|
26
|
+
const center = (box) => box.x + box.w / 2;
|
|
27
|
+
const middle = (box) => box.y + box.h / 2;
|
|
28
|
+
/** This function groups the words into rows, from top to bottom. Each row has its words from left to right. */
|
|
29
|
+
function groupRows(words, tolerance) {
|
|
30
|
+
const sorted = [...words].sort((a, b) => middle(a.box) - middle(b.box) || a.box.x - b.box.x);
|
|
31
|
+
const rows = [];
|
|
32
|
+
for (const word of sorted) {
|
|
33
|
+
const row = rows[rows.length - 1];
|
|
34
|
+
const value = middle(word.box);
|
|
35
|
+
if (row !== undefined && Math.abs(value - row.mean) < tolerance) {
|
|
36
|
+
row.words.push(word);
|
|
37
|
+
row.mean += (value - row.mean) / row.words.length;
|
|
38
|
+
}
|
|
39
|
+
else
|
|
40
|
+
rows.push({ mean: value, words: [word] });
|
|
41
|
+
}
|
|
42
|
+
return rows.map((row) => row.words.sort((a, b) => a.box.x - b.box.x));
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* This function gives the left edge of each column.
|
|
46
|
+
*
|
|
47
|
+
* - Each start of a cell goes to a cluster of starts that are near.
|
|
48
|
+
* - A column of numbers is often right-aligned. Then its starts are far apart, and they make
|
|
49
|
+
* two clusters or more. Two clusters next to each other can be one column.
|
|
50
|
+
* This is true when the median right edges of their cells are near, and no row has a cell in each cluster.
|
|
51
|
+
* Also, the left edge of the two clusters must be to the right of each cell of the column before them.
|
|
52
|
+
* Thus a long cell that starts inside the column before does not move the edge of the next column.
|
|
53
|
+
* - A cluster with starts in less than `minShare` of the rows is not a column.
|
|
54
|
+
*/
|
|
55
|
+
function findColumns(rows, gap, tolerance, minShare) {
|
|
56
|
+
const starts = [];
|
|
57
|
+
rows.forEach((words, row) => {
|
|
58
|
+
let first = 0;
|
|
59
|
+
words.forEach((word, index) => {
|
|
60
|
+
const next = words[index + 1];
|
|
61
|
+
if (next === undefined || next.box.x - (word.box.x + word.box.w) > gap) {
|
|
62
|
+
const start = words[first];
|
|
63
|
+
starts.push({ x: start.box.x, end: word.box.x + word.box.w, row });
|
|
64
|
+
first = index + 1;
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
starts.sort((a, b) => a.x - b.x);
|
|
69
|
+
const clusters = [];
|
|
70
|
+
for (const start of starts) {
|
|
71
|
+
const cluster = clusters[clusters.length - 1];
|
|
72
|
+
const last = cluster?.[cluster.length - 1];
|
|
73
|
+
if (cluster !== undefined && last !== undefined && start.x - last.x <= tolerance)
|
|
74
|
+
cluster.push(start);
|
|
75
|
+
else
|
|
76
|
+
clusters.push([start]);
|
|
77
|
+
}
|
|
78
|
+
const merged = [];
|
|
79
|
+
for (const cluster of clusters) {
|
|
80
|
+
const before = merged[merged.length - 1];
|
|
81
|
+
const rowsBefore = new Set(before?.map((start) => start.row));
|
|
82
|
+
const previous = merged[merged.length - 2];
|
|
83
|
+
const aligned = before !== undefined &&
|
|
84
|
+
(previous === undefined || Math.min(...before.map((start) => start.x)) >= Math.max(...previous.map((start) => start.end))) &&
|
|
85
|
+
Math.abs(median(before.map((start) => start.end)) - median(cluster.map((start) => start.end))) <= tolerance &&
|
|
86
|
+
cluster.every((start) => !rowsBefore.has(start.row));
|
|
87
|
+
if (aligned)
|
|
88
|
+
before.push(...cluster);
|
|
89
|
+
else
|
|
90
|
+
merged.push([...cluster]);
|
|
91
|
+
}
|
|
92
|
+
return merged
|
|
93
|
+
.filter((cluster) => new Set(cluster.map((start) => start.row)).size >= minShare * rows.length)
|
|
94
|
+
.map((cluster) => Math.min(...cluster.map((start) => start.x)));
|
|
95
|
+
}
|
|
96
|
+
/** This function gives the index of the column that holds the x position: the last column with its left edge at or before it. */
|
|
97
|
+
function columnOf(columns, x) {
|
|
98
|
+
let index = 0;
|
|
99
|
+
columns.forEach((edge, column) => {
|
|
100
|
+
if (edge <= x)
|
|
101
|
+
index = column;
|
|
102
|
+
});
|
|
103
|
+
return index;
|
|
104
|
+
}
|
|
105
|
+
/** This function makes a cell from its words, in their order from left to right. */
|
|
106
|
+
function makeCell(words) {
|
|
107
|
+
return {
|
|
108
|
+
text: words.map((word) => word.text).join(" "),
|
|
109
|
+
confidence: words.reduce((sum, word) => sum + word.confidence, 0) / words.length,
|
|
110
|
+
box: unionBox(words.map((word) => word.box)),
|
|
111
|
+
words,
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* This function gives the rows and the columns of a table from the boxes of its words. Give the
|
|
116
|
+
* words of one table only: a title or a note above the table becomes a row too.
|
|
117
|
+
*
|
|
118
|
+
* - A row has one cell for each column. A cell is `null` when the row has no word in its column.
|
|
119
|
+
* - A column that holds few starts of cells (less than `minShare` of the rows) is not a column. Its
|
|
120
|
+
* words go to the column at their left.
|
|
121
|
+
* - A word with an empty text is not a word of the table.
|
|
122
|
+
*
|
|
123
|
+
* @param source - A text from the OCR, or its words
|
|
124
|
+
* @param options - The tolerances, as shares of the median height of the words
|
|
125
|
+
* @throws RangeError when an option is not a finite number of 0 or more
|
|
126
|
+
*/
|
|
127
|
+
export function tableOfText(source, options = {}) {
|
|
128
|
+
const all = "lines" in source ? source.lines.flatMap((line) => line.words) : source;
|
|
129
|
+
const words = all.filter((word) => word.text.trim().length > 0);
|
|
130
|
+
const rowTolerance = option("rowTolerance", options.rowTolerance, 0.7);
|
|
131
|
+
const cellGap = option("cellGap", options.cellGap, 0.75);
|
|
132
|
+
const columnTolerance = option("columnTolerance", options.columnTolerance, 1);
|
|
133
|
+
const minShare = option("minShare", options.minShare, 0.25);
|
|
134
|
+
if (words.length === 0)
|
|
135
|
+
return { columns: [], rows: [] };
|
|
136
|
+
const heights = words.map((word) => word.box.h).sort((a, b) => a - b);
|
|
137
|
+
const height = heights[Math.floor(heights.length / 2)];
|
|
138
|
+
const rows = groupRows(words, rowTolerance * height);
|
|
139
|
+
const found = findColumns(rows, cellGap * height, columnTolerance * height, minShare);
|
|
140
|
+
// With no column that passes minShare, the table has one column, so that each word stays in a cell
|
|
141
|
+
const columns = found.length > 0 ? found : [Math.min(...words.map((word) => word.box.x))];
|
|
142
|
+
return {
|
|
143
|
+
columns,
|
|
144
|
+
rows: rows.map((row) => {
|
|
145
|
+
const cells = columns.map(() => []);
|
|
146
|
+
for (const word of row)
|
|
147
|
+
cells[columnOf(columns, center(word.box))].push(word);
|
|
148
|
+
return { box: unionBox(row.map((word) => word.box)), cells: cells.map((cell) => (cell.length === 0 ? null : makeCell(cell))) };
|
|
149
|
+
}),
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
//# sourceMappingURL=table.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"table.js","sourceRoot":"","sources":["../src/table.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,iCAAiC,CAAC;AACzD,OAAO,EAAE,QAAQ,EAAyC,MAAM,WAAW,CAAC;AAkC5E,wHAAwH;AACxH,SAAS,MAAM,CAAC,IAAY,EAAE,KAAyB,EAAE,QAAgB;IACvE,IAAI,KAAK,KAAK,SAAS;QAAE,OAAO,QAAQ,CAAC;IACzC,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,KAAK,GA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AQ,CAAC,OAAO,EAAE,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAe,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;YAC7F,OAAO,EAAE,GAAG,EAAE,QAAQ,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,KAAK,EAAE,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,IAAI,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC;QACjI,CAAC,CAAC;KACH,CAAC;AACJ,CAAC"}
|
package/dist/types.d.ts
CHANGED
|
@@ -31,6 +31,18 @@ export interface OcrPageOutput extends OcrText {
|
|
|
31
31
|
readonly orientation?: OcrOrientation | undefined;
|
|
32
32
|
}
|
|
33
33
|
export declare const OcrPageOutputSchema: z.ZodType<OcrPageOutput>;
|
|
34
|
+
/** The input of `ocr.pageCount`. */
|
|
35
|
+
export interface OcrPageCountInput {
|
|
36
|
+
file: string;
|
|
37
|
+
timeout?: number | undefined;
|
|
38
|
+
}
|
|
39
|
+
export declare const OcrPageCountInputSchema: z.ZodType<OcrPageCountInput>;
|
|
40
|
+
/** The output of `ocr.pageCount`. */
|
|
41
|
+
export interface OcrPageCountOutput {
|
|
42
|
+
/** The number of pages of the file. The pages of `ocr.page` are the numbers from 1 to this number. */
|
|
43
|
+
readonly pages: number;
|
|
44
|
+
}
|
|
45
|
+
export declare const OcrPageCountOutputSchema: z.ZodType<OcrPageCountOutput>;
|
|
34
46
|
/** The input of `ocr.image`. */
|
|
35
47
|
export interface OcrImageInput {
|
|
36
48
|
file: string;
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],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AAS,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
|
@@ -61,6 +61,13 @@ export const OcrPageOutputSchema = z.object({
|
|
|
61
61
|
orientation: OrientationSchema.optional().describe("The orientation that OCR found, when the input has upright and the source is ocr"),
|
|
62
62
|
...textFields,
|
|
63
63
|
});
|
|
64
|
+
export const OcrPageCountInputSchema = z.object({
|
|
65
|
+
file: z.string().min(1).describe("The path of the PDF file"),
|
|
66
|
+
timeout: engineFields.timeout,
|
|
67
|
+
});
|
|
68
|
+
export const OcrPageCountOutputSchema = z.object({
|
|
69
|
+
pages: z.number().int().min(0).describe("The number of pages of the file. The pages of ocr.page are the numbers from 1 to this number"),
|
|
70
|
+
});
|
|
64
71
|
export const OcrImageInputSchema = z.object({
|
|
65
72
|
file: z.string().min(1).describe("The path of the image file, for example a PNG, JPEG or TIFF file"),
|
|
66
73
|
dpi: z.number().int().min(1).max(4800).optional().describe("The resolution of the image, when the file does not tell it"),
|
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,gBAA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AO;CAC9B,CAAC,CAAC;AAQH,MAAM,CAAC,MAAM,wBAAwB,GAAkC,CAAC,CAAC,MAAM,CAAC;IAC9E,KAAK,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,8FAA8F,CAAC;CACxI,CAAC,CAAC;AAiBH,MAAM,CAAC,MAAM,mBAAmB,GAA6B,CAAC,CAAC,MAAM,CAAC;IACpE,IAAI,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,CAAC,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.11",
|
|
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,9 @@
|
|
|
81
81
|
},
|
|
82
82
|
"dependencies": {
|
|
83
83
|
"@mark1russell7/layout-contract": "^0.11.1",
|
|
84
|
-
"@
|
|
85
|
-
"@procedures/
|
|
84
|
+
"@mark1russell7/rigor-statistics": "^0.2.19",
|
|
85
|
+
"@procedures/client": "0.68.11",
|
|
86
|
+
"@procedures/shell": "0.68.11",
|
|
86
87
|
"zod": "^4.6.5"
|
|
87
88
|
},
|
|
88
89
|
"scripts": {
|
package/src/index.ts
CHANGED
|
@@ -7,4 +7,4 @@ export * from "./parse.js";
|
|
|
7
7
|
export * from "./engine.js";
|
|
8
8
|
export * from "./programs.js";
|
|
9
9
|
export { createOcrRunner, DEFAULT_TIMEOUT_MS, type OcrHostOptions, type OcrRunner } from "./run.js";
|
|
10
|
-
export { DEFAULT_DPI, ocrImage, ocrPage, pdftoppmArgs, pdftotextArgs } from "./procedures/ocr/index.js";
|
|
10
|
+
export { DEFAULT_DPI, ocrImage, ocrPage, ocrPageCount, pdfinfoArgs, pdftoppmArgs, pdftotextArgs } from "./procedures/ocr/index.js";
|
package/src/module.ts
CHANGED
|
@@ -20,8 +20,22 @@ import {
|
|
|
20
20
|
import shellModule from "@procedures/shell/module";
|
|
21
21
|
import { ocrImage } from "./procedures/ocr/image.js";
|
|
22
22
|
import { ocrPage } from "./procedures/ocr/page.js";
|
|
23
|
+
import { ocrPageCount } from "./procedures/ocr/pageCount.js";
|
|
23
24
|
import { createOcrRunner, type OcrHostOptions } from "./run.js";
|
|
24
|
-
import {
|
|
25
|
+
import {
|
|
26
|
+
OcrImageInputSchema,
|
|
27
|
+
OcrImageOutputSchema,
|
|
28
|
+
OcrPageCountInputSchema,
|
|
29
|
+
OcrPageCountOutputSchema,
|
|
30
|
+
OcrPageInputSchema,
|
|
31
|
+
OcrPageOutputSchema,
|
|
32
|
+
type OcrImageInput,
|
|
33
|
+
type OcrImageOutput,
|
|
34
|
+
type OcrPageCountInput,
|
|
35
|
+
type OcrPageCountOutput,
|
|
36
|
+
type OcrPageInput,
|
|
37
|
+
type OcrPageOutput,
|
|
38
|
+
} from "./types.js";
|
|
25
39
|
|
|
26
40
|
/** The rule of the procedures under a root of the files: the file of the input is in the root. */
|
|
27
41
|
const FILE_IN_A_ROOT: FsRootRules = { paths: ["file"] };
|
|
@@ -35,6 +49,9 @@ const PAGE_EFFECTS: Effects = { reads: ["fs", "process"], writes: ["fs", "proces
|
|
|
35
49
|
/** The effects of `ocr.image`. The engine reads the image file and writes only to its standard output. */
|
|
36
50
|
const IMAGE_EFFECTS: Effects = { reads: ["fs", "process"], writes: ["process"] };
|
|
37
51
|
|
|
52
|
+
/** The effects of `ocr.pageCount`. `pdfinfo` reads the PDF file and writes only to its standard output. */
|
|
53
|
+
const PAGE_COUNT_EFFECTS: Effects = { reads: ["fs", "process"], writes: ["process"] };
|
|
54
|
+
|
|
38
55
|
/**
|
|
39
56
|
* This function gives the procedures of this package with the settings of the host. The settings
|
|
40
57
|
* are the path of each program, the OCR engine and the folder of the temporary folders.
|
|
@@ -71,7 +88,22 @@ export function createOcrProcedures(options: OcrHostOptions = {}): AnyProcedure[
|
|
|
71
88
|
})
|
|
72
89
|
.handler(async (input: OcrImageInput, ctx: ProcedureContext): Promise<OcrImageOutput> => ocrImage(input, ctx, runner))
|
|
73
90
|
.build();
|
|
74
|
-
|
|
91
|
+
const pageCount: AnyProcedure = createProcedure()
|
|
92
|
+
.path(["ocr", "pageCount"])
|
|
93
|
+
.input(OcrPageCountInputSchema)
|
|
94
|
+
.output(OcrPageCountOutputSchema)
|
|
95
|
+
.meta({
|
|
96
|
+
description: "Give the number of pages of a PDF file (pdfinfo), so a caller can read each page with ocr.page",
|
|
97
|
+
args: ["file"],
|
|
98
|
+
shorts: { timeout: "t" },
|
|
99
|
+
output: "json",
|
|
100
|
+
calls: ["shell.run"],
|
|
101
|
+
effects: PAGE_COUNT_EFFECTS,
|
|
102
|
+
fsRoot: FILE_IN_A_ROOT,
|
|
103
|
+
})
|
|
104
|
+
.handler(async (input: OcrPageCountInput, ctx: ProcedureContext): Promise<OcrPageCountOutput> => ocrPageCount(input, ctx, runner))
|
|
105
|
+
.build();
|
|
106
|
+
return [page, image, pageCount];
|
|
75
107
|
}
|
|
76
108
|
|
|
77
109
|
/** The procedures with the default settings: the search finds each program at each call. */
|
package/src/parse.ts
CHANGED
package/src/pdfinfo.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The parse of the output of `pdfinfo` of Poppler. This file starts no process and reads no file.
|
|
3
|
+
*
|
|
4
|
+
* The output has one line for each field, for example `Title: A report` and
|
|
5
|
+
* `Pages: 3`.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* This function gives the number of pages from the output of `pdfinfo`: the value of its `Pages`
|
|
10
|
+
* line.
|
|
11
|
+
*
|
|
12
|
+
* @throws Error when the output has no `Pages` line with a whole number.
|
|
13
|
+
*/
|
|
14
|
+
export function parsePdfinfoPages(output: string): number {
|
|
15
|
+
const match = /^Pages:[ \t]*([0-9]+)[ \t]*\r?$/m.exec(output);
|
|
16
|
+
const pages = match === null ? Number.NaN : Number(match[1]);
|
|
17
|
+
if (!Number.isSafeInteger(pages)) throw new Error("The output of pdfinfo has no Pages line with a whole number");
|
|
18
|
+
return pages;
|
|
19
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ocr.pageCount procedure
|
|
3
|
+
*
|
|
4
|
+
* This procedure gives the number of pages of a PDF file, from `pdfinfo` of Poppler. A caller can
|
|
5
|
+
* then read each page with `ocr.page`, from page 1 to this number.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { resolve } from "node:path";
|
|
9
|
+
import type { ProcedureContext } from "@procedures/client";
|
|
10
|
+
import { parsePdfinfoPages } from "../../pdfinfo.js";
|
|
11
|
+
import { PDFINFO } from "../../programs.js";
|
|
12
|
+
import { createOcrRunner, DEFAULT_TIMEOUT_MS, type OcrRunner } from "../../run.js";
|
|
13
|
+
import type { OcrPageCountInput, OcrPageCountOutput } from "../../types.js";
|
|
14
|
+
|
|
15
|
+
/** This function gives the argv of `pdfinfo` for the fields of a PDF file. */
|
|
16
|
+
export function pdfinfoArgs(file: string): string[] {
|
|
17
|
+
return [file];
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* This function gives the number of pages of the PDF file of the input. The count comes from the
|
|
22
|
+
* same Poppler programs that `ocr.page` uses, so each page from 1 to the count is a valid page of
|
|
23
|
+
* `ocr.page`.
|
|
24
|
+
*
|
|
25
|
+
* @throws Error when the output of `pdfinfo` has no number of pages.
|
|
26
|
+
*/
|
|
27
|
+
export async function ocrPageCount(input: OcrPageCountInput, ctx: Pick<ProcedureContext, "client">, runner: OcrRunner = createOcrRunner()): Promise<OcrPageCountOutput> {
|
|
28
|
+
// An absolute path does not start with "-", so the program cannot read it as a flag
|
|
29
|
+
const file = resolve(input.file);
|
|
30
|
+
const timeout = input.timeout ?? DEFAULT_TIMEOUT_MS;
|
|
31
|
+
return { pages: parsePdfinfoPages(await runner.run(PDFINFO, pdfinfoArgs(file), timeout, ctx)) };
|
|
32
|
+
}
|
package/src/programs.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* The search for the programs of this package on the host: `tesseract` (the OCR engine), and
|
|
3
|
-
* `pdftotext` and `
|
|
3
|
+
* `pdftotext`, `pdftoppm` and `pdfinfo` of Poppler. The package does not download a program. When the search
|
|
4
4
|
* finds no program, `requireProgram` throws an `OcrToolMissingError`.
|
|
5
5
|
*/
|
|
6
6
|
|
|
@@ -10,6 +10,14 @@ import { posix, win32 } from "node:path";
|
|
|
10
10
|
/** The environment of the search. */
|
|
11
11
|
export type ProgramEnv = Readonly<Record<string, string | undefined>>;
|
|
12
12
|
|
|
13
|
+
/** A different program with the same name, for example the `pdftotext` of Xpdf. */
|
|
14
|
+
export interface OtherProgram {
|
|
15
|
+
/** The name of the other program, for the error, for example `Xpdf`. */
|
|
16
|
+
readonly name: string;
|
|
17
|
+
/** This function gives `true` when the output of a failed run comes from the other program. */
|
|
18
|
+
readonly matches: (output: string) => boolean;
|
|
19
|
+
}
|
|
20
|
+
|
|
13
21
|
/** The description of a program for the search. */
|
|
14
22
|
export interface ProgramSpec {
|
|
15
23
|
/** The name of the program, without `.exe`, for example `tesseract`. */
|
|
@@ -22,6 +30,11 @@ export interface ProgramSpec {
|
|
|
22
30
|
readonly installed: (platform: NodeJS.Platform, env: ProgramEnv) => readonly string[];
|
|
23
31
|
/** The text that tells how to install the program, for the error. */
|
|
24
32
|
readonly install: string;
|
|
33
|
+
/**
|
|
34
|
+
* The different programs with the same name. When a run fails and its output comes from one of
|
|
35
|
+
* them, the runner throws an `OcrToolMissingError`, not the error of the run.
|
|
36
|
+
*/
|
|
37
|
+
readonly others?: readonly OtherProgram[] | undefined;
|
|
25
38
|
}
|
|
26
39
|
|
|
27
40
|
/** The options of the search. Each one has a default from the host. */
|
|
@@ -58,6 +71,16 @@ export const TESSERACT: ProgramSpec = {
|
|
|
58
71
|
install: "the package tesseract-ocr on Linux, brew install tesseract on macOS, or the Tesseract installer on Windows",
|
|
59
72
|
};
|
|
60
73
|
|
|
74
|
+
/**
|
|
75
|
+
* The programs of Xpdf have the names of the programs of Poppler, but not their options: for
|
|
76
|
+
* example, the `pdftotext` of Xpdf has no `-bbox-layout`. Git for Windows puts Xpdf on the `PATH`.
|
|
77
|
+
* The usage text of Xpdf names Glyph & Cog. The usage text of Poppler names Glyph & Cog and Poppler.
|
|
78
|
+
*/
|
|
79
|
+
export const XPDF: OtherProgram = {
|
|
80
|
+
name: "Xpdf",
|
|
81
|
+
matches: (output) => /Glyph & Cog/.test(output) && !/poppler/i.test(output),
|
|
82
|
+
};
|
|
83
|
+
|
|
61
84
|
function poppler(name: string, env: string): ProgramSpec {
|
|
62
85
|
return {
|
|
63
86
|
name,
|
|
@@ -68,6 +91,7 @@ function poppler(name: string, env: string): ProgramSpec {
|
|
|
68
91
|
return platform === "darwin" ? HOMEBREW_FOLDERS.map((folder) => posix.join(folder, name)) : [];
|
|
69
92
|
},
|
|
70
93
|
install: "the package poppler-utils on Linux, brew install poppler on macOS, or a Poppler build on Windows",
|
|
94
|
+
others: [XPDF],
|
|
71
95
|
};
|
|
72
96
|
}
|
|
73
97
|
|
|
@@ -77,8 +101,11 @@ export const PDFTOTEXT: ProgramSpec = poppler("pdftotext", "PDFTOTEXT_EXE");
|
|
|
77
101
|
/** The program of Poppler that makes an image of a PDF page. */
|
|
78
102
|
export const PDFTOPPM: ProgramSpec = poppler("pdftoppm", "PDFTOPPM_EXE");
|
|
79
103
|
|
|
104
|
+
/** The program of Poppler that gives the fields of a PDF file, for example its number of pages. */
|
|
105
|
+
export const PDFINFO: ProgramSpec = poppler("pdfinfo", "PDFINFO_EXE");
|
|
106
|
+
|
|
80
107
|
/** The programs of this package. */
|
|
81
|
-
export const OCR_PROGRAMS: readonly ProgramSpec[] = [TESSERACT, PDFTOTEXT, PDFTOPPM];
|
|
108
|
+
export const OCR_PROGRAMS: readonly ProgramSpec[] = [TESSERACT, PDFTOTEXT, PDFTOPPM, PDFINFO];
|
|
82
109
|
|
|
83
110
|
/** The error when a program of this package is not on the host. */
|
|
84
111
|
export class OcrToolMissingError extends Error {
|
|
@@ -88,14 +115,26 @@ export class OcrToolMissingError extends Error {
|
|
|
88
115
|
readonly tool: string;
|
|
89
116
|
/** The paths that the search examined, in order. */
|
|
90
117
|
readonly searched: readonly string[];
|
|
118
|
+
/** The name of the different program that the search found, for example `Xpdf`, or `undefined`. */
|
|
119
|
+
readonly other: string | undefined;
|
|
91
120
|
|
|
92
121
|
override readonly name: string = "OcrToolMissingError";
|
|
93
|
-
|
|
122
|
+
/**
|
|
123
|
+
* The `cause` is the error of a start that failed. The `other` is the name of a different
|
|
124
|
+
* program with the same name at `searched[0]`, for example `Xpdf`.
|
|
125
|
+
*/
|
|
126
|
+
constructor(spec: ProgramSpec, searched: readonly string[], cause?: string, other?: string) {
|
|
94
127
|
const variables = [...spec.env, ...(spec.folderEnv ?? [])].join(" or ");
|
|
95
|
-
const
|
|
96
|
-
|
|
128
|
+
const fix = `Install ${spec.install}, or set ${variables}.`;
|
|
129
|
+
if (other !== undefined) {
|
|
130
|
+
super(`The program ${spec.name} at ${searched[0] ?? "the path of the search"} is the program of ${other}, and this package cannot use it. ${fix}`);
|
|
131
|
+
} else {
|
|
132
|
+
const reason = cause === undefined ? `The search examined ${searched.length} paths.` : `It did not start: ${cause}.`;
|
|
133
|
+
super(`The program ${spec.name} is not on this host. ${reason} ${fix}`);
|
|
134
|
+
}
|
|
97
135
|
this.tool = spec.name;
|
|
98
136
|
this.searched = searched;
|
|
137
|
+
this.other = other;
|
|
99
138
|
}
|
|
100
139
|
}
|
|
101
140
|
|
package/src/run.ts
CHANGED
|
@@ -15,8 +15,8 @@ export const DEFAULT_TIMEOUT_MS = 120_000;
|
|
|
15
15
|
/** The settings of the host for the procedures of this package. The procedure input cannot change them. */
|
|
16
16
|
export interface OcrHostOptions extends FindProgramOptions {
|
|
17
17
|
/**
|
|
18
|
-
* The path of each program, by name: `tesseract`, `pdftotext` or `
|
|
19
|
-
* path comes from the search of `findProgram`.
|
|
18
|
+
* The path of each program, by name: `tesseract`, `pdftotext`, `pdftoppm` or `pdfinfo`. A
|
|
19
|
+
* program with no path comes from the search of `findProgram`.
|
|
20
20
|
*/
|
|
21
21
|
readonly executables?: Readonly<Record<string, string | undefined>> | undefined;
|
|
22
22
|
/** The OCR engine (default: `tesseractEngine`). */
|
|
@@ -63,6 +63,8 @@ export function createOcrRunner(options: OcrHostOptions = {}): OcrRunner {
|
|
|
63
63
|
const result = await tool.call(args, { timeout }, ctx);
|
|
64
64
|
if (notFound(result)) throw new OcrToolMissingError(spec, [input.command], result.stderr.trim());
|
|
65
65
|
if (result.exitCode !== 0) {
|
|
66
|
+
const other = spec.others?.find((program) => program.matches(`${result.stderr}\n${result.stdout}`));
|
|
67
|
+
if (other !== undefined) throw new OcrToolMissingError(spec, [input.command], undefined, other.name);
|
|
66
68
|
const text = result.stderr.trim() || result.stdout.trim();
|
|
67
69
|
throw new CliToolError(`${spec.name} failed (exit code ${result.exitCode}): ${text}`.trim(), result.exitCode, result.stderr);
|
|
68
70
|
}
|