mindee 4.4.0 → 4.6.0
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/package.json +1 -1
- package/src/client.d.ts +7 -7
- package/src/errors/MindeeError.d.ts +3 -0
- package/src/errors/MindeeError.js +8 -1
- package/src/errors/index.d.ts +1 -1
- package/src/errors/index.js +2 -1
- package/src/imageOperations/extractedImage.d.ts +20 -0
- package/src/imageOperations/extractedImage.js +49 -0
- package/src/imageOperations/extractedInvoiceSplitterDocument.d.ts +6 -0
- package/src/imageOperations/extractedInvoiceSplitterDocument.js +12 -0
- package/src/imageOperations/extractedMultiReceiptImage.d.ts +6 -0
- package/src/imageOperations/extractedMultiReceiptImage.js +12 -0
- package/src/imageOperations/index.d.ts +4 -0
- package/src/imageOperations/index.js +11 -0
- package/src/imageOperations/internal.d.ts +5 -0
- package/src/imageOperations/internal.js +13 -0
- package/src/imageOperations/invoiceSplitterExtractor.d.ts +12 -0
- package/src/imageOperations/invoiceSplitterExtractor.js +73 -0
- package/src/imageOperations/multiReceiptsExtractor.d.ts +11 -0
- package/src/imageOperations/multiReceiptsExtractor.js +72 -0
- package/src/index.d.ts +1 -0
- package/src/index.js +2 -1
- package/src/input/base.d.ts +1 -1
- package/src/input/base.js +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,22 @@
|
|
|
1
1
|
# CHANGELOG
|
|
2
2
|
|
|
3
|
+
## v4.6.0 - 2023-12-11
|
|
4
|
+
### Changes
|
|
5
|
+
* :sparkles: add invoice-splitter auto-splitting feature
|
|
6
|
+
* :memo: add examples to illustrate auto-splitting feature
|
|
7
|
+
* :recycle: add unit tests for auto-splitting feature
|
|
8
|
+
* :recycle: rename `exemple` folder to `example`
|
|
9
|
+
* :arrow_up: update testing library
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
## v4.5.0 - 2023-11-29
|
|
13
|
+
### Changes
|
|
14
|
+
* :sparkles: add Multi-Receipts auto-extraction feature
|
|
15
|
+
* :art: add example file to illustrate multi-receipt image extraction feature
|
|
16
|
+
* :recycle: add new imageOperations namespace
|
|
17
|
+
* :recycle: fix loose typing issues for inputsources
|
|
18
|
+
|
|
19
|
+
|
|
3
20
|
## v4.4.0 - 2023-11-17
|
|
4
21
|
### Changes
|
|
5
22
|
* :sparkles: add support for Carte Grise V1
|
package/package.json
CHANGED
package/src/client.d.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
/// <reference types="node" />
|
|
3
3
|
/// <reference types="node" />
|
|
4
4
|
import { Readable } from "stream";
|
|
5
|
-
import { InputSource, PageOptions } from "./input";
|
|
5
|
+
import { Base64Input, BytesInput, InputSource, PathInput, StreamInput, PageOptions, UrlInput, BufferInput } from "./input";
|
|
6
6
|
import { Endpoint } from "./http";
|
|
7
7
|
import { AsyncPredictResponse, FeedbackResponse, Inference, PredictResponse, StringDict } from "./parsing/common";
|
|
8
8
|
/**
|
|
@@ -160,34 +160,34 @@ export declare class Client {
|
|
|
160
160
|
* Load an input document from a local path.
|
|
161
161
|
* @param inputPath
|
|
162
162
|
*/
|
|
163
|
-
docFromPath(inputPath: string):
|
|
163
|
+
docFromPath(inputPath: string): PathInput;
|
|
164
164
|
/**
|
|
165
165
|
* Load an input document from a base64 encoded string.
|
|
166
166
|
* @param inputString input content, as a string.
|
|
167
167
|
* @param filename file name.
|
|
168
168
|
*/
|
|
169
|
-
docFromBase64(inputString: string, filename: string):
|
|
169
|
+
docFromBase64(inputString: string, filename: string): Base64Input;
|
|
170
170
|
/**
|
|
171
171
|
* Load an input document from a `stream.Readable` object.
|
|
172
172
|
* @param inputStream input content, as a readable stream.
|
|
173
173
|
* @param filename file name.
|
|
174
174
|
*/
|
|
175
|
-
docFromStream(inputStream: Readable, filename: string):
|
|
175
|
+
docFromStream(inputStream: Readable, filename: string): StreamInput;
|
|
176
176
|
/**
|
|
177
177
|
* Load an input document from a bytes string.
|
|
178
178
|
* @param inputBytes input content, as readable bytes.
|
|
179
179
|
* @param filename file name.
|
|
180
180
|
*/
|
|
181
|
-
docFromBytes(inputBytes: string, filename: string):
|
|
181
|
+
docFromBytes(inputBytes: string, filename: string): BytesInput;
|
|
182
182
|
/**
|
|
183
183
|
* Load an input document from a URL.
|
|
184
184
|
* @param url input url. Must be HTTPS.
|
|
185
185
|
*/
|
|
186
|
-
docFromUrl(url: string):
|
|
186
|
+
docFromUrl(url: string): UrlInput;
|
|
187
187
|
/**
|
|
188
188
|
* Load an input document from a Buffer.
|
|
189
189
|
* @param buffer input content, as a buffer.
|
|
190
190
|
* @param filename file name.
|
|
191
191
|
*/
|
|
192
|
-
docFromBuffer(buffer: Buffer, filename: string):
|
|
192
|
+
docFromBuffer(buffer: Buffer, filename: string): BufferInput;
|
|
193
193
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.MindeeError = void 0;
|
|
3
|
+
exports.MindeeMimeTypeError = exports.MindeeError = void 0;
|
|
4
4
|
class MindeeError extends Error {
|
|
5
5
|
constructor(message) {
|
|
6
6
|
super(message);
|
|
@@ -8,3 +8,10 @@ class MindeeError extends Error {
|
|
|
8
8
|
}
|
|
9
9
|
}
|
|
10
10
|
exports.MindeeError = MindeeError;
|
|
11
|
+
class MindeeMimeTypeError extends MindeeError {
|
|
12
|
+
constructor(message) {
|
|
13
|
+
super(message);
|
|
14
|
+
this.name = "MindeeMimeTypeError";
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
exports.MindeeMimeTypeError = MindeeMimeTypeError;
|
package/src/errors/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { MindeeError } from "./MindeeError";
|
|
1
|
+
export { MindeeError, MindeeMimeTypeError } from "./MindeeError";
|
package/src/errors/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.MindeeError = void 0;
|
|
3
|
+
exports.MindeeMimeTypeError = exports.MindeeError = void 0;
|
|
4
4
|
var MindeeError_1 = require("./MindeeError");
|
|
5
5
|
Object.defineProperty(exports, "MindeeError", { enumerable: true, get: function () { return MindeeError_1.MindeeError; } });
|
|
6
|
+
Object.defineProperty(exports, "MindeeMimeTypeError", { enumerable: true, get: function () { return MindeeError_1.MindeeMimeTypeError; } });
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { Buffer } from "node:buffer";
|
|
3
|
+
import { BufferInput } from "../input";
|
|
4
|
+
export declare abstract class ExtractedImage {
|
|
5
|
+
buffer: Buffer;
|
|
6
|
+
protected internalFileName: string;
|
|
7
|
+
constructor(buffer: Uint8Array, fileName: string);
|
|
8
|
+
/**
|
|
9
|
+
* Saves the document to a file.
|
|
10
|
+
*
|
|
11
|
+
* @param outputPath Path to save the file to.
|
|
12
|
+
*/
|
|
13
|
+
saveToFile(outputPath: string): void;
|
|
14
|
+
/**
|
|
15
|
+
* Return the file as a Mindee-compatible BufferInput source.
|
|
16
|
+
*
|
|
17
|
+
* @returns A BufferInput source.
|
|
18
|
+
*/
|
|
19
|
+
asSource(): BufferInput;
|
|
20
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.ExtractedImage = void 0;
|
|
7
|
+
const node_buffer_1 = require("node:buffer");
|
|
8
|
+
const errors_1 = require("../errors");
|
|
9
|
+
const node_fs_1 = require("node:fs");
|
|
10
|
+
const node_path_1 = __importDefault(require("node:path"));
|
|
11
|
+
const logger_1 = require("../logger");
|
|
12
|
+
const input_1 = require("../input");
|
|
13
|
+
class ExtractedImage {
|
|
14
|
+
constructor(buffer, fileName) {
|
|
15
|
+
this.buffer = node_buffer_1.Buffer.from(buffer);
|
|
16
|
+
this.internalFileName = fileName;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Saves the document to a file.
|
|
20
|
+
*
|
|
21
|
+
* @param outputPath Path to save the file to.
|
|
22
|
+
*/
|
|
23
|
+
saveToFile(outputPath) {
|
|
24
|
+
try {
|
|
25
|
+
(0, node_fs_1.writeFileSync)(node_path_1.default.resolve(outputPath), this.buffer);
|
|
26
|
+
logger_1.logger.info(`File saved successfully to ${node_path_1.default.resolve(outputPath)}.`);
|
|
27
|
+
}
|
|
28
|
+
catch (e) {
|
|
29
|
+
if (e instanceof TypeError) {
|
|
30
|
+
throw new errors_1.MindeeError("Invalid path/filename provided.");
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
throw e;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Return the file as a Mindee-compatible BufferInput source.
|
|
39
|
+
*
|
|
40
|
+
* @returns A BufferInput source.
|
|
41
|
+
*/
|
|
42
|
+
asSource() {
|
|
43
|
+
return new input_1.BufferInput({
|
|
44
|
+
buffer: this.buffer,
|
|
45
|
+
filename: this.internalFileName,
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
exports.ExtractedImage = ExtractedImage;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ExtractedInvoiceSplitterImage = void 0;
|
|
4
|
+
const extractedImage_1 = require("./extractedImage");
|
|
5
|
+
class ExtractedInvoiceSplitterImage extends extractedImage_1.ExtractedImage {
|
|
6
|
+
constructor(buffer, pageIndices) {
|
|
7
|
+
super(buffer, `invoice_p_${pageIndices[0]}-${pageIndices[1]}.pdf`);
|
|
8
|
+
this.pageIdMin = pageIndices[0];
|
|
9
|
+
this.pageIdMax = pageIndices[1];
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
exports.ExtractedInvoiceSplitterImage = ExtractedInvoiceSplitterImage;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ExtractedMultiReceiptImage = void 0;
|
|
4
|
+
const extractedImage_1 = require("./extractedImage");
|
|
5
|
+
class ExtractedMultiReceiptImage extends extractedImage_1.ExtractedImage {
|
|
6
|
+
constructor(buffer, pageId, receiptId) {
|
|
7
|
+
super(buffer, `receipt_p${pageId}_${receiptId}.pdf`);
|
|
8
|
+
this.pageId = pageId;
|
|
9
|
+
this.receiptId = receiptId;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
exports.ExtractedMultiReceiptImage = ExtractedMultiReceiptImage;
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { extractReceipts } from "./multiReceiptsExtractor";
|
|
2
|
+
export { extractInvoices } from "./invoiceSplitterExtractor";
|
|
3
|
+
export { ExtractedMultiReceiptImage } from "./extractedMultiReceiptImage";
|
|
4
|
+
export { ExtractedInvoiceSplitterImage } from "./extractedInvoiceSplitterDocument";
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ExtractedInvoiceSplitterImage = exports.ExtractedMultiReceiptImage = exports.extractInvoices = exports.extractReceipts = void 0;
|
|
4
|
+
var multiReceiptsExtractor_1 = require("./multiReceiptsExtractor");
|
|
5
|
+
Object.defineProperty(exports, "extractReceipts", { enumerable: true, get: function () { return multiReceiptsExtractor_1.extractReceipts; } });
|
|
6
|
+
var invoiceSplitterExtractor_1 = require("./invoiceSplitterExtractor");
|
|
7
|
+
Object.defineProperty(exports, "extractInvoices", { enumerable: true, get: function () { return invoiceSplitterExtractor_1.extractInvoices; } });
|
|
8
|
+
var extractedMultiReceiptImage_1 = require("./extractedMultiReceiptImage");
|
|
9
|
+
Object.defineProperty(exports, "ExtractedMultiReceiptImage", { enumerable: true, get: function () { return extractedMultiReceiptImage_1.ExtractedMultiReceiptImage; } });
|
|
10
|
+
var extractedInvoiceSplitterDocument_1 = require("./extractedInvoiceSplitterDocument");
|
|
11
|
+
Object.defineProperty(exports, "ExtractedInvoiceSplitterImage", { enumerable: true, get: function () { return extractedInvoiceSplitterDocument_1.ExtractedInvoiceSplitterImage; } });
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { extractReceipts } from "./multiReceiptsExtractor";
|
|
2
|
+
export { extractInvoices } from "./invoiceSplitterExtractor";
|
|
3
|
+
export { ExtractedMultiReceiptImage } from "./extractedMultiReceiptImage";
|
|
4
|
+
export { ExtractedInvoiceSplitterImage } from "./extractedInvoiceSplitterDocument";
|
|
5
|
+
export { ExtractedImage } from "./extractedImage";
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ExtractedImage = exports.ExtractedInvoiceSplitterImage = exports.ExtractedMultiReceiptImage = exports.extractInvoices = exports.extractReceipts = void 0;
|
|
4
|
+
var multiReceiptsExtractor_1 = require("./multiReceiptsExtractor");
|
|
5
|
+
Object.defineProperty(exports, "extractReceipts", { enumerable: true, get: function () { return multiReceiptsExtractor_1.extractReceipts; } });
|
|
6
|
+
var invoiceSplitterExtractor_1 = require("./invoiceSplitterExtractor");
|
|
7
|
+
Object.defineProperty(exports, "extractInvoices", { enumerable: true, get: function () { return invoiceSplitterExtractor_1.extractInvoices; } });
|
|
8
|
+
var extractedMultiReceiptImage_1 = require("./extractedMultiReceiptImage");
|
|
9
|
+
Object.defineProperty(exports, "ExtractedMultiReceiptImage", { enumerable: true, get: function () { return extractedMultiReceiptImage_1.ExtractedMultiReceiptImage; } });
|
|
10
|
+
var extractedInvoiceSplitterDocument_1 = require("./extractedInvoiceSplitterDocument");
|
|
11
|
+
Object.defineProperty(exports, "ExtractedInvoiceSplitterImage", { enumerable: true, get: function () { return extractedInvoiceSplitterDocument_1.ExtractedInvoiceSplitterImage; } });
|
|
12
|
+
var extractedImage_1 = require("./extractedImage");
|
|
13
|
+
Object.defineProperty(exports, "ExtractedImage", { enumerable: true, get: function () { return extractedImage_1.ExtractedImage; } });
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { InvoiceSplitterV1 } from "../product";
|
|
2
|
+
import { LocalInputSource } from "../input/base";
|
|
3
|
+
import { ExtractedInvoiceSplitterImage } from "./extractedInvoiceSplitterDocument";
|
|
4
|
+
/**
|
|
5
|
+
* Extracts & cuts the pages of a main document invoice according to the provided indexes.
|
|
6
|
+
*
|
|
7
|
+
* @param inputFile File to extract sub-invoices from.
|
|
8
|
+
* @param indexes List of indexes to cut the document according to. Can be provided either as a InvoiceSplitterV1 inference, or a direct list of splits.
|
|
9
|
+
* @param strict If set to true, doesn't cut pages where the API isn't 100% confident.
|
|
10
|
+
* @returns A promise of extracted images, as an array of ExtractedInvoiceSplitterImage.
|
|
11
|
+
*/
|
|
12
|
+
export declare function extractInvoices(inputFile: LocalInputSource, indexes: InvoiceSplitterV1 | number[][], strict?: boolean): Promise<ExtractedInvoiceSplitterImage[]>;
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.extractInvoices = void 0;
|
|
4
|
+
const pdf_lib_1 = require("pdf-lib");
|
|
5
|
+
const errors_1 = require("../errors");
|
|
6
|
+
const product_1 = require("../product");
|
|
7
|
+
const extractedInvoiceSplitterDocument_1 = require("./extractedInvoiceSplitterDocument");
|
|
8
|
+
async function splitPdf(pdfDoc, invoicePageGroups) {
|
|
9
|
+
if (invoicePageGroups.length === 0) {
|
|
10
|
+
return [];
|
|
11
|
+
}
|
|
12
|
+
const generatedPdfs = [];
|
|
13
|
+
for (let i = 0; i < invoicePageGroups.length; i++) {
|
|
14
|
+
const subdocument = await pdf_lib_1.PDFDocument.create();
|
|
15
|
+
const fullIndexes = [];
|
|
16
|
+
for (let j = invoicePageGroups[i][0]; j <= invoicePageGroups[i][invoicePageGroups[i].length - 1]; j++) {
|
|
17
|
+
fullIndexes.push(j);
|
|
18
|
+
}
|
|
19
|
+
const copiedPages = await subdocument.copyPages(pdfDoc, fullIndexes);
|
|
20
|
+
copiedPages.map((page) => {
|
|
21
|
+
subdocument.addPage(page);
|
|
22
|
+
});
|
|
23
|
+
const subdocumentBytes = await subdocument.save();
|
|
24
|
+
generatedPdfs.push(new extractedInvoiceSplitterDocument_1.ExtractedInvoiceSplitterImage(subdocumentBytes, [invoicePageGroups[i][0], invoicePageGroups[i][invoicePageGroups[i].length - 1]]));
|
|
25
|
+
}
|
|
26
|
+
return generatedPdfs;
|
|
27
|
+
}
|
|
28
|
+
async function getPdfDoc(inputFile) {
|
|
29
|
+
await inputFile.init();
|
|
30
|
+
if (!inputFile.isPdf()) {
|
|
31
|
+
throw new errors_1.MindeeMimeTypeError("Invoice Splitter is only compatible with pdf documents.");
|
|
32
|
+
}
|
|
33
|
+
const pdfDoc = await pdf_lib_1.PDFDocument.load(inputFile.fileObject);
|
|
34
|
+
if (pdfDoc.getPageCount() < 2) {
|
|
35
|
+
throw new errors_1.MindeeError("Invoice Splitter is only compatible with multi-page-pdf documents.");
|
|
36
|
+
}
|
|
37
|
+
return pdfDoc;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Extracts & cuts the pages of a main document invoice according to the provided indexes.
|
|
41
|
+
*
|
|
42
|
+
* @param inputFile File to extract sub-invoices from.
|
|
43
|
+
* @param indexes List of indexes to cut the document according to. Can be provided either as a InvoiceSplitterV1 inference, or a direct list of splits.
|
|
44
|
+
* @param strict If set to true, doesn't cut pages where the API isn't 100% confident.
|
|
45
|
+
* @returns A promise of extracted images, as an array of ExtractedInvoiceSplitterImage.
|
|
46
|
+
*/
|
|
47
|
+
async function extractInvoices(inputFile, indexes, strict = false) {
|
|
48
|
+
if (!indexes) {
|
|
49
|
+
throw new errors_1.MindeeError("No possible receipts candidates found for MultiReceipts extraction.");
|
|
50
|
+
}
|
|
51
|
+
let customIndexes = [];
|
|
52
|
+
if (indexes instanceof product_1.InvoiceSplitterV1) {
|
|
53
|
+
indexes.prediction.invoicePageGroups.map((invoicePageGroup) => {
|
|
54
|
+
if (!strict || invoicePageGroup.confidence === 1) {
|
|
55
|
+
customIndexes.push(invoicePageGroup.pageIndexes);
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
customIndexes = indexes;
|
|
61
|
+
}
|
|
62
|
+
const pdfDoc = await getPdfDoc(inputFile);
|
|
63
|
+
const pageCount = pdfDoc.getPageCount();
|
|
64
|
+
customIndexes.forEach((pageGroup) => {
|
|
65
|
+
pageGroup.forEach((index) => {
|
|
66
|
+
if (index >= pageCount) {
|
|
67
|
+
throw new errors_1.MindeeError(`Given index ${index} doesn't exist in page range (0-${pageCount - 1})`);
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
return await splitPdf(pdfDoc, customIndexes);
|
|
72
|
+
}
|
|
73
|
+
exports.extractInvoices = extractInvoices;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { MultiReceiptsDetectorV1 } from "../product";
|
|
2
|
+
import { ExtractedMultiReceiptImage } from "./extractedMultiReceiptImage";
|
|
3
|
+
import { LocalInputSource } from "../input/base";
|
|
4
|
+
/**
|
|
5
|
+
* Extracts individual receipts from multi-receipts documents.
|
|
6
|
+
*
|
|
7
|
+
* @param inputFile File to extract sub-receipts from.
|
|
8
|
+
* @param inference Results of the inference.
|
|
9
|
+
* @returns Individual extracted receipts as an array of ExtractedMultiReceiptsImage.
|
|
10
|
+
*/
|
|
11
|
+
export declare function extractReceipts(inputFile: LocalInputSource, inference: MultiReceiptsDetectorV1): Promise<ExtractedMultiReceiptImage[]>;
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.extractReceipts = void 0;
|
|
4
|
+
const pdf_lib_1 = require("pdf-lib");
|
|
5
|
+
const errors_1 = require("../errors");
|
|
6
|
+
const geometry_1 = require("../geometry");
|
|
7
|
+
const extractedMultiReceiptImage_1 = require("./extractedMultiReceiptImage");
|
|
8
|
+
async function addPage(pdfPage, boundingBox, pageId, receiptId) {
|
|
9
|
+
const { width, height } = pdfPage.getSize();
|
|
10
|
+
const receiptPdf = await pdf_lib_1.PDFDocument.create();
|
|
11
|
+
const newWidth = width * ((0, geometry_1.getMinMaxX)(boundingBox).max - (0, geometry_1.getMinMaxX)(boundingBox).min);
|
|
12
|
+
const newHeight = height * ((0, geometry_1.getMinMaxY)(boundingBox).max - (0, geometry_1.getMinMaxY)(boundingBox).min);
|
|
13
|
+
const croppedReceipt = await receiptPdf.embedPage(pdfPage, {
|
|
14
|
+
left: (0, geometry_1.getMinMaxX)(boundingBox).min * width,
|
|
15
|
+
right: (0, geometry_1.getMinMaxX)(boundingBox).max * width,
|
|
16
|
+
top: height - ((0, geometry_1.getMinMaxY)(boundingBox).min * height),
|
|
17
|
+
bottom: height - ((0, geometry_1.getMinMaxY)(boundingBox).max * height),
|
|
18
|
+
});
|
|
19
|
+
const receiptPage = receiptPdf.addPage([newWidth, newHeight]);
|
|
20
|
+
receiptPage.drawPage(croppedReceipt, {
|
|
21
|
+
width: newWidth,
|
|
22
|
+
height: newHeight,
|
|
23
|
+
});
|
|
24
|
+
const receiptBytes = await receiptPdf.save();
|
|
25
|
+
return new extractedMultiReceiptImage_1.ExtractedMultiReceiptImage(receiptBytes, pageId, receiptId);
|
|
26
|
+
}
|
|
27
|
+
async function loadPdfDoc(inputFile) {
|
|
28
|
+
let pdfDoc;
|
|
29
|
+
if (!["image/jpeg", "image/jpg", "image/png", "application/pdf"].includes(inputFile.mimeType)) {
|
|
30
|
+
throw new errors_1.MindeeMimeTypeError(`Unsupported file type "${inputFile.mimeType}" Currently supported types are .png, .jpg and .pdf`);
|
|
31
|
+
}
|
|
32
|
+
else if (inputFile.isPdf()) {
|
|
33
|
+
pdfDoc = await pdf_lib_1.PDFDocument.load(inputFile.fileObject);
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
pdfDoc = await pdf_lib_1.PDFDocument.create();
|
|
37
|
+
let image;
|
|
38
|
+
if (inputFile.mimeType === "image/png") {
|
|
39
|
+
image = await pdfDoc.embedPng(inputFile.fileObject);
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
image = await pdfDoc.embedJpg(inputFile.fileObject);
|
|
43
|
+
}
|
|
44
|
+
const imageDims = image.scale(1);
|
|
45
|
+
const pageImage = pdfDoc.addPage([imageDims.width, imageDims.height]);
|
|
46
|
+
pageImage.drawImage(image);
|
|
47
|
+
}
|
|
48
|
+
return pdfDoc;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Extracts individual receipts from multi-receipts documents.
|
|
52
|
+
*
|
|
53
|
+
* @param inputFile File to extract sub-receipts from.
|
|
54
|
+
* @param inference Results of the inference.
|
|
55
|
+
* @returns Individual extracted receipts as an array of ExtractedMultiReceiptsImage.
|
|
56
|
+
*/
|
|
57
|
+
async function extractReceipts(inputFile, inference) {
|
|
58
|
+
const images = [];
|
|
59
|
+
if (!inference.prediction.receipts) {
|
|
60
|
+
throw new errors_1.MindeeError("No possible receipts candidates found for MultiReceipts extraction.");
|
|
61
|
+
}
|
|
62
|
+
const pdfDoc = await loadPdfDoc(inputFile);
|
|
63
|
+
for (let pageId = 0; pageId < pdfDoc.getPageCount(); pageId++) {
|
|
64
|
+
const [page] = await pdfDoc.copyPages(pdfDoc, [pageId]);
|
|
65
|
+
for (let receiptId = 0; receiptId < inference.pages[pageId].prediction.receipts.length; receiptId++) {
|
|
66
|
+
const receipt = inference.pages[pageId].prediction.receipts[receiptId];
|
|
67
|
+
images.push(await addPage(page, receipt.boundingBox, pageId, receiptId));
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return images;
|
|
71
|
+
}
|
|
72
|
+
exports.extractReceipts = extractReceipts;
|
package/src/index.d.ts
CHANGED
|
@@ -3,3 +3,4 @@ export { Client, PredictOptions } from "./client";
|
|
|
3
3
|
export { AsyncPredictResponse, PredictResponse, Document, Page, } from "./parsing/common";
|
|
4
4
|
export { InputSource, PageOptionsOperation } from "./input";
|
|
5
5
|
export * as internal from "./internal";
|
|
6
|
+
export * as imageOperations from "./imageOperations";
|
package/src/index.js
CHANGED
|
@@ -23,7 +23,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
23
23
|
return result;
|
|
24
24
|
};
|
|
25
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
exports.internal = exports.PageOptionsOperation = exports.InputSource = exports.Page = exports.Document = exports.PredictResponse = exports.AsyncPredictResponse = exports.Client = exports.product = void 0;
|
|
26
|
+
exports.imageOperations = exports.internal = exports.PageOptionsOperation = exports.InputSource = exports.Page = exports.Document = exports.PredictResponse = exports.AsyncPredictResponse = exports.Client = exports.product = void 0;
|
|
27
27
|
exports.product = __importStar(require("./product"));
|
|
28
28
|
var client_1 = require("./client");
|
|
29
29
|
Object.defineProperty(exports, "Client", { enumerable: true, get: function () { return client_1.Client; } });
|
|
@@ -36,3 +36,4 @@ var input_1 = require("./input");
|
|
|
36
36
|
Object.defineProperty(exports, "InputSource", { enumerable: true, get: function () { return input_1.InputSource; } });
|
|
37
37
|
Object.defineProperty(exports, "PageOptionsOperation", { enumerable: true, get: function () { return input_1.PageOptionsOperation; } });
|
|
38
38
|
exports.internal = __importStar(require("./internal"));
|
|
39
|
+
exports.imageOperations = __importStar(require("./imageOperations"));
|
package/src/input/base.d.ts
CHANGED