mindee 4.36.0 → 4.36.1
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 +6 -0
- package/package.json +1 -1
- package/src/geometry/polygonUtils.d.ts +1 -0
- package/src/geometry/polygonUtils.js +13 -0
- package/src/imageOperations/common/imageExtractor.js +53 -8
- package/src/imageOperations/multiReceiptsExtractor/multiReceiptsExtractor.js +1 -0
- package/src/input/dataSchema.js +2 -2
package/CHANGELOG.md
CHANGED
package/package.json
CHANGED
|
@@ -44,3 +44,4 @@ export declare function getMinYCoordinate(polygon: Array<Point>): number;
|
|
|
44
44
|
export declare function getMinXCoordinate(polygon: Array<Point>): number;
|
|
45
45
|
export declare function compareOnY(polygon1: Array<Point>, polygon2: Array<Point>): number;
|
|
46
46
|
export declare function compareOnX(polygon1: Array<Point>, polygon2: Array<Point>): number;
|
|
47
|
+
export declare function adjustForRotation(polygon: Array<Point>, orientation: number): Array<Point>;
|
|
@@ -13,6 +13,7 @@ exports.getMinYCoordinate = getMinYCoordinate;
|
|
|
13
13
|
exports.getMinXCoordinate = getMinXCoordinate;
|
|
14
14
|
exports.compareOnY = compareOnY;
|
|
15
15
|
exports.compareOnX = compareOnX;
|
|
16
|
+
exports.adjustForRotation = adjustForRotation;
|
|
16
17
|
/**
|
|
17
18
|
* Get the central point (centroid) given a list of points.
|
|
18
19
|
*/
|
|
@@ -124,3 +125,15 @@ function compareOnX(polygon1, polygon2) {
|
|
|
124
125
|
}
|
|
125
126
|
return sort < 0 ? -1 : 1;
|
|
126
127
|
}
|
|
128
|
+
function adjustForRotation(polygon, orientation) {
|
|
129
|
+
if (orientation === 90) {
|
|
130
|
+
return polygon.map(([x, y]) => [y, 1 - x]);
|
|
131
|
+
}
|
|
132
|
+
if (orientation === 180) {
|
|
133
|
+
return polygon.map(([x, y]) => [1 - x, 1 - y]);
|
|
134
|
+
}
|
|
135
|
+
if (orientation === 270) {
|
|
136
|
+
return polygon.map(([x, y]) => [1 - y, x]);
|
|
137
|
+
}
|
|
138
|
+
return polygon;
|
|
139
|
+
}
|
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.extractFromPage = extractFromPage;
|
|
4
4
|
const pdf_lib_1 = require("@cantoo/pdf-lib");
|
|
5
5
|
const geometry_1 = require("../../geometry");
|
|
6
|
+
const polygonUtils_1 = require("../../geometry/polygonUtils");
|
|
6
7
|
/**
|
|
7
8
|
* Extracts elements from a page based off of a list of bounding boxes.
|
|
8
9
|
*
|
|
@@ -15,7 +16,9 @@ async function extractFromPage(pdfPage, polygons) {
|
|
|
15
16
|
// Manual upscale.
|
|
16
17
|
// Fixes issues with the OCR.
|
|
17
18
|
const qualityScale = 300 / 72;
|
|
18
|
-
|
|
19
|
+
const orientation = pdfPage.getRotation().angle;
|
|
20
|
+
for (const origPolygon of polygons) {
|
|
21
|
+
const polygon = (0, polygonUtils_1.adjustForRotation)(origPolygon, orientation);
|
|
19
22
|
const tempPdf = await pdf_lib_1.PDFDocument.create();
|
|
20
23
|
const newWidth = width * ((0, geometry_1.getMinMaxX)(polygon).max - (0, geometry_1.getMinMaxX)(polygon).min);
|
|
21
24
|
const newHeight = height * ((0, geometry_1.getMinMaxY)(polygon).max - (0, geometry_1.getMinMaxY)(polygon).min);
|
|
@@ -25,17 +28,59 @@ async function extractFromPage(pdfPage, polygons) {
|
|
|
25
28
|
top: height - ((0, geometry_1.getMinMaxY)(polygon).min * height),
|
|
26
29
|
bottom: height - ((0, geometry_1.getMinMaxY)(polygon).max * height),
|
|
27
30
|
});
|
|
28
|
-
|
|
31
|
+
// Determine the final page dimensions based on orientation
|
|
32
|
+
let finalWidth;
|
|
33
|
+
let finalHeight;
|
|
34
|
+
if (orientation === 90 || orientation === 270) {
|
|
35
|
+
// For 90/270 rotations, swap width and height
|
|
36
|
+
finalWidth = newHeight * qualityScale;
|
|
37
|
+
finalHeight = newWidth * qualityScale;
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
finalWidth = newWidth * qualityScale;
|
|
41
|
+
finalHeight = newHeight * qualityScale;
|
|
42
|
+
}
|
|
43
|
+
const samplePage = tempPdf.addPage([finalWidth, finalHeight]);
|
|
29
44
|
samplePage.drawRectangle({
|
|
30
45
|
x: 0,
|
|
31
46
|
y: 0,
|
|
32
|
-
width:
|
|
33
|
-
height:
|
|
34
|
-
});
|
|
35
|
-
samplePage.drawPage(cropped, {
|
|
36
|
-
width: newWidth * qualityScale,
|
|
37
|
-
height: newHeight * qualityScale,
|
|
47
|
+
width: finalWidth,
|
|
48
|
+
height: finalHeight,
|
|
38
49
|
});
|
|
50
|
+
// Draw the cropped page with rotation applied
|
|
51
|
+
if (orientation === 0) {
|
|
52
|
+
samplePage.drawPage(cropped, {
|
|
53
|
+
width: newWidth * qualityScale,
|
|
54
|
+
height: newHeight * qualityScale,
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
else if (orientation === 90) {
|
|
58
|
+
samplePage.drawPage(cropped, {
|
|
59
|
+
x: 0,
|
|
60
|
+
y: finalHeight,
|
|
61
|
+
width: newWidth * qualityScale,
|
|
62
|
+
height: newHeight * qualityScale,
|
|
63
|
+
rotate: (0, pdf_lib_1.degrees)(270),
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
else if (orientation === 180) {
|
|
67
|
+
samplePage.drawPage(cropped, {
|
|
68
|
+
x: finalWidth,
|
|
69
|
+
y: finalHeight,
|
|
70
|
+
width: newWidth * qualityScale,
|
|
71
|
+
height: newHeight * qualityScale,
|
|
72
|
+
rotate: (0, pdf_lib_1.degrees)(180),
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
else if (orientation === 270) {
|
|
76
|
+
samplePage.drawPage(cropped, {
|
|
77
|
+
x: finalWidth,
|
|
78
|
+
y: 0,
|
|
79
|
+
width: newWidth * qualityScale,
|
|
80
|
+
height: newHeight * qualityScale,
|
|
81
|
+
rotate: (0, pdf_lib_1.degrees)(90),
|
|
82
|
+
});
|
|
83
|
+
}
|
|
39
84
|
extractedElements.push(await tempPdf.save());
|
|
40
85
|
}
|
|
41
86
|
return extractedElements;
|
|
@@ -65,6 +65,7 @@ async function extractReceipts(inputFile, inference) {
|
|
|
65
65
|
const pdfDoc = await loadPdfDoc(inputFile);
|
|
66
66
|
for (let pageId = 0; pageId < pdfDoc.getPageCount(); pageId++) {
|
|
67
67
|
const [page] = await pdfDoc.copyPages(pdfDoc, [pageId]);
|
|
68
|
+
page.setRotation((0, pdf_lib_1.degrees)(inference.pages[pageId].orientation?.value ?? 0));
|
|
68
69
|
const receiptPositions = inference.pages[pageId].prediction.receipts.map((receipt) => receipt.boundingBox);
|
|
69
70
|
const extractedReceipts = await extractReceiptsFromPage(page, receiptPositions, pageId);
|
|
70
71
|
images.push(...extractedReceipts);
|
package/src/input/dataSchema.js
CHANGED
|
@@ -71,8 +71,8 @@ class DataSchema {
|
|
|
71
71
|
if (typeof dataSchema === "string") {
|
|
72
72
|
this.replace = new DataSchemaReplace(JSON.parse(dataSchema)["replace"]);
|
|
73
73
|
}
|
|
74
|
-
else if (dataSchema
|
|
75
|
-
this.replace = dataSchema
|
|
74
|
+
else if (dataSchema instanceof DataSchema) {
|
|
75
|
+
this.replace = dataSchema.replace;
|
|
76
76
|
}
|
|
77
77
|
else {
|
|
78
78
|
this.replace = new DataSchemaReplace(dataSchema["replace"]);
|