mindee 4.4.0 → 4.5.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 +8 -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 +8 -0
- package/src/imageOperations/extractedImage.js +2 -0
- package/src/imageOperations/extractedMultiReceiptImage.d.ts +10 -0
- package/src/imageOperations/extractedMultiReceiptImage.js +18 -0
- package/src/imageOperations/index.d.ts +2 -0
- package/src/imageOperations/index.js +7 -0
- package/src/imageOperations/internal.d.ts +3 -0
- package/src/imageOperations/internal.js +7 -0
- package/src/imageOperations/multiReceiptsExtractor.d.ts +4 -0
- package/src/imageOperations/multiReceiptsExtractor.js +66 -0
- package/src/index.d.ts +1 -0
- package/src/index.js +2 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
# CHANGELOG
|
|
2
2
|
|
|
3
|
+
## v4.5.0 - 2023-11-29
|
|
4
|
+
### Changes
|
|
5
|
+
* :sparkles: add Multi-Receipts auto-extraction feature
|
|
6
|
+
* :art: add example file to illustrate multi-receipt image extraction feature
|
|
7
|
+
* :recycle: add new imageOperations namespace
|
|
8
|
+
* :recycle: fix loose typing issues for inputsources
|
|
9
|
+
|
|
10
|
+
|
|
3
11
|
## v4.4.0 - 2023-11-17
|
|
4
12
|
### Changes
|
|
5
13
|
* :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,10 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { BufferInput } from "../input";
|
|
3
|
+
import { ExtractedImage } from "./extractedImage";
|
|
4
|
+
export declare class ExtractedMultiReceiptImage implements ExtractedImage {
|
|
5
|
+
readonly receiptId: number;
|
|
6
|
+
readonly pageId: number;
|
|
7
|
+
imageData: Buffer;
|
|
8
|
+
constructor(imageData: Uint8Array, pageId: number, receiptId: number);
|
|
9
|
+
asSource(): BufferInput;
|
|
10
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ExtractedMultiReceiptImage = void 0;
|
|
4
|
+
const input_1 = require("../input");
|
|
5
|
+
class ExtractedMultiReceiptImage {
|
|
6
|
+
constructor(imageData, pageId, receiptId) {
|
|
7
|
+
this.pageId = pageId;
|
|
8
|
+
this.imageData = Buffer.from(imageData);
|
|
9
|
+
this.receiptId = receiptId;
|
|
10
|
+
}
|
|
11
|
+
asSource() {
|
|
12
|
+
return new input_1.BufferInput({
|
|
13
|
+
buffer: this.imageData,
|
|
14
|
+
filename: `receipt_p${this.pageId}_${this.receiptId}.pdf`,
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
exports.ExtractedMultiReceiptImage = ExtractedMultiReceiptImage;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ExtractedMultiReceiptImage = 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 extractedMultiReceiptImage_1 = require("./extractedMultiReceiptImage");
|
|
7
|
+
Object.defineProperty(exports, "ExtractedMultiReceiptImage", { enumerable: true, get: function () { return extractedMultiReceiptImage_1.ExtractedMultiReceiptImage; } });
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ExtractedMultiReceiptImage = 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 extractedMultiReceiptImage_1 = require("./extractedMultiReceiptImage");
|
|
7
|
+
Object.defineProperty(exports, "ExtractedMultiReceiptImage", { enumerable: true, get: function () { return extractedMultiReceiptImage_1.ExtractedMultiReceiptImage; } });
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { MultiReceiptsDetectorV1 } from "../product";
|
|
2
|
+
import { ExtractedMultiReceiptImage } from "./extractedMultiReceiptImage";
|
|
3
|
+
import { LocalInputSource } from "../input/base";
|
|
4
|
+
export declare function extractReceipts(inputFile: LocalInputSource, inference: MultiReceiptsDetectorV1): Promise<ExtractedMultiReceiptImage[]>;
|
|
@@ -0,0 +1,66 @@
|
|
|
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
|
+
//Note: PDF-lib seems to invert y coordinates, giving us the following horror:
|
|
14
|
+
const croppedReceipt = await receiptPdf.embedPage(pdfPage, {
|
|
15
|
+
left: (0, geometry_1.getMinMaxX)(boundingBox).min * width,
|
|
16
|
+
right: (0, geometry_1.getMinMaxX)(boundingBox).max * width,
|
|
17
|
+
top: height - ((0, geometry_1.getMinMaxY)(boundingBox).min * height),
|
|
18
|
+
bottom: height - ((0, geometry_1.getMinMaxY)(boundingBox).max * height),
|
|
19
|
+
});
|
|
20
|
+
const receiptPage = receiptPdf.addPage([newWidth, newHeight]);
|
|
21
|
+
receiptPage.drawPage(croppedReceipt, {
|
|
22
|
+
width: newWidth,
|
|
23
|
+
height: newHeight,
|
|
24
|
+
});
|
|
25
|
+
const receiptBytes = await receiptPdf.save();
|
|
26
|
+
return new extractedMultiReceiptImage_1.ExtractedMultiReceiptImage(receiptBytes, pageId, receiptId);
|
|
27
|
+
}
|
|
28
|
+
async function loadPdfDoc(inputFile) {
|
|
29
|
+
let pdfDoc;
|
|
30
|
+
if (!["image/jpeg", "image/jpg", "image/png", "application/pdf"].includes(inputFile.mimeType)) {
|
|
31
|
+
throw new errors_1.MindeeMimeTypeError(`Unsupported file type "${inputFile.mimeType}" Currently supported types are .png, .jpg and .pdf`);
|
|
32
|
+
}
|
|
33
|
+
else if (inputFile.isPdf()) {
|
|
34
|
+
pdfDoc = await pdf_lib_1.PDFDocument.load(inputFile.fileObject);
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
pdfDoc = await pdf_lib_1.PDFDocument.create();
|
|
38
|
+
let image;
|
|
39
|
+
if (inputFile.mimeType === "image/png") {
|
|
40
|
+
image = await pdfDoc.embedPng(inputFile.fileObject);
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
image = await pdfDoc.embedJpg(inputFile.fileObject);
|
|
44
|
+
}
|
|
45
|
+
const imageDims = image.scale(1);
|
|
46
|
+
const pageImage = pdfDoc.addPage([imageDims.width, imageDims.height]);
|
|
47
|
+
pageImage.drawImage(image);
|
|
48
|
+
}
|
|
49
|
+
return pdfDoc;
|
|
50
|
+
}
|
|
51
|
+
async function extractReceipts(inputFile, inference) {
|
|
52
|
+
const images = [];
|
|
53
|
+
if (!inference.prediction.receipts) {
|
|
54
|
+
throw new errors_1.MindeeError("No possible receipts candidates found for MultiReceipts extraction.");
|
|
55
|
+
}
|
|
56
|
+
const pdfDoc = await loadPdfDoc(inputFile);
|
|
57
|
+
for (let pageId = 0; pageId < pdfDoc.getPageCount(); pageId++) {
|
|
58
|
+
const [page] = await pdfDoc.copyPages(pdfDoc, [pageId]);
|
|
59
|
+
for (let receiptId = 0; receiptId < inference.pages[pageId].prediction.receipts.length; receiptId++) {
|
|
60
|
+
const receipt = inference.pages[pageId].prediction.receipts[receiptId];
|
|
61
|
+
images.push(await addPage(page, receipt.boundingBox, pageId, receiptId));
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return images;
|
|
65
|
+
}
|
|
66
|
+
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"));
|