Skip to content

Commit aa53297

Browse files
committed
rework file operations
1 parent 4a7fff8 commit aa53297

20 files changed

+119
-204
lines changed

example/AutoInvoiceSplitterExtractionExample.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
import com.mindee.input.LocalInputSource;
33
import com.mindee.extraction.ExtractedPDF;
44
import com.mindee.extraction.PDFExtractor;
5-
import com.mindee.parsing.common.AsyncPredictResponse;
6-
import com.mindee.product.invoice.InvoiceV4;
7-
import com.mindee.product.invoicesplitter.InvoiceSplitterV1;
5+
import com.mindee.v1.parsing.common.AsyncPredictResponse;
6+
import com.mindee.v1.product.invoice.InvoiceV4;
7+
import com.mindee.v1.product.invoicesplitter.InvoiceSplitterV1;
88

99
import java.io.File;
1010
import java.io.IOException;

example/AutoMultiReceiptExtractionExample.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
import com.mindee.input.LocalInputSource;
33
import com.mindee.extraction.ExtractedImage;
44
import com.mindee.extraction.ImageExtractor;
5-
import com.mindee.parsing.common.PredictResponse;
6-
import com.mindee.parsing.common.Page;
7-
import com.mindee.product.multireceiptsdetector.MultiReceiptsDetectorV1;
8-
import com.mindee.product.multireceiptsdetector.MultiReceiptsDetectorV1Document;
9-
import com.mindee.product.receipt.ReceiptV5;
5+
import com.mindee.v1.parsing.common.PredictResponse;
6+
import com.mindee.v1.parsing.common.Page;
7+
import com.mindee.v1.product.multireceiptsdetector.MultiReceiptsDetectorV1;
8+
import com.mindee.v1.product.multireceiptsdetector.MultiReceiptsDetectorV1Document;
9+
import com.mindee.v1.product.receipt.ReceiptV5;
1010

1111
import java.io.File;
1212
import java.io.IOException;

src/main/java/com/mindee/geometry/Polygon.java

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
44
import java.util.ArrayList;
5+
import java.util.Collection;
56
import java.util.List;
6-
import lombok.Builder;
7+
import java.util.stream.Collectors;
78
import lombok.Getter;
89

910
/**
@@ -17,7 +18,19 @@ public class Polygon {
1718
*/
1819
private List<Point> coordinates = new ArrayList<>();
1920

20-
@Builder
21+
/**
22+
* Create a Polygon from a list of a list of floats.
23+
*/
24+
public Polygon(Collection<List<Double>> coordinates) {
25+
this.coordinates = coordinates
26+
.stream()
27+
.map(coordinate -> new Point(coordinate.get(0), coordinate.get(1)))
28+
.collect(Collectors.toList());
29+
}
30+
31+
/**
32+
* Create a Polygon from a list of Points.
33+
*/
2134
public Polygon(List<Point> coordinates) {
2235
this.coordinates = coordinates;
2336
}
@@ -82,6 +95,37 @@ public String toStringPrecise() {
8295
return builder.toString();
8396
}
8497

98+
/**
99+
* Compare two polygons based on their Y coordinates.
100+
* Useful for sorting lists.
101+
*/
102+
public int compareOnY(Polygon polygon2) {
103+
double sort = this.getMinMaxY().getMin() - polygon2.getMinMaxY().getMin();
104+
if (sort == 0) {
105+
return 0;
106+
}
107+
return sort < 0 ? -1 : 1;
108+
}
109+
110+
/**
111+
* Compare two polygons based on their X coordinates.
112+
* Useful for sorting lists.
113+
*/
114+
public int compareOnX(Polygon polygon2) {
115+
double sort = this.getMinMaxX().getMin() - polygon2.getMinMaxX().getMin();
116+
if (sort == 0) {
117+
return 0;
118+
}
119+
return sort < 0 ? -1 : 1;
120+
}
121+
122+
/**
123+
* Merge the coordinates of the two polygons.
124+
*/
125+
public Polygon combine(Polygon target) {
126+
return PolygonUtils.combine(this, target);
127+
}
128+
85129
@Override
86130
public boolean equals(Object object) {
87131
if (this == object) {

src/main/java/com/mindee/geometry/PolygonDeserializer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,6 @@ public Polygon deserialize(
3535
};
3636
List<List<Double>> polygonList = mapper.readerFor(typeRef).readValue(node);
3737

38-
return PolygonUtils.getFrom(polygonList);
38+
return new Polygon(polygonList);
3939
}
4040
}

src/main/java/com/mindee/geometry/PolygonUtils.java

Lines changed: 1 addition & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,16 @@
33
import java.util.Arrays;
44
import java.util.Collections;
55
import java.util.List;
6-
import java.util.OptionalDouble;
76
import java.util.stream.Collectors;
87

98
/**
109
* Methods for working with Polygons.
10+
* Used internally, use at your own risk.
1111
*/
1212
public final class PolygonUtils {
1313
private PolygonUtils() {
1414
}
1515

16-
/**
17-
* Create a Polygon from a list of a list of floats.
18-
*/
19-
public static Polygon getFrom(List<List<Double>> polygon) {
20-
List<Point> coordinates = polygon
21-
.stream()
22-
.map(coordinate -> new Point(coordinate.get(0), coordinate.get(1)))
23-
.collect(Collectors.toList());
24-
return new Polygon(coordinates);
25-
}
26-
2716
/**
2817
* Get the central coordinates (centroid) of a list of Points.
2918
*/
@@ -35,30 +24,6 @@ public static Point getCentroid(List<Point> vertices) {
3524
return new Point(xSum / verticesSum, ySum / verticesSum);
3625
}
3726

38-
/**
39-
* Compare two polygons based on their Y coordinates.
40-
* Useful for sorting lists.
41-
*/
42-
public static int CompareOnY(Polygon polygon1, Polygon polygon2) {
43-
double sort = getMinYCoordinate(polygon1) - getMinYCoordinate(polygon2);
44-
if (sort == 0) {
45-
return 0;
46-
}
47-
return sort < 0 ? -1 : 1;
48-
}
49-
50-
/**
51-
* Compare two polygons based on their X coordinates.
52-
* Useful for sorting lists.
53-
*/
54-
public static int CompareOnX(Polygon polygon1, Polygon polygon2) {
55-
double sort = getMinXCoordinate(polygon1) - getMinXCoordinate(polygon2);
56-
if (sort == 0) {
57-
return 0;
58-
}
59-
return sort < 0 ? -1 : 1;
60-
}
61-
6227
/**
6328
* Get the maximum and minimum Y coordinates in a given list of Points.
6429
*/
@@ -90,75 +55,6 @@ public static boolean isPointInPolygonY(Point centroid, Polygon polygon) {
9055
return isPointInY(centroid, yCoords.getMin(), yCoords.getMax());
9156
}
9257

93-
public static Double getMinYCoordinate(Polygon polygon) {
94-
OptionalDouble min = polygon
95-
.getCoordinates()
96-
.stream()
97-
.map(Point::getY)
98-
.mapToDouble(Double::doubleValue)
99-
.min();
100-
101-
if (min.isPresent()) {
102-
return min.getAsDouble();
103-
}
104-
105-
throw new IllegalStateException(
106-
"The min Y could not be found "
107-
+ "because it seems that there is no coordinates in the current polygon."
108-
);
109-
}
110-
111-
public static Double getMaxYCoordinate(Polygon polygon) {
112-
OptionalDouble max = polygon
113-
.getCoordinates()
114-
.stream()
115-
.map(Point::getY)
116-
.mapToDouble(Double::doubleValue)
117-
.max();
118-
if (max.isPresent()) {
119-
return max.getAsDouble();
120-
}
121-
122-
throw new IllegalStateException(
123-
"The max Y could not be found "
124-
+ "because it seems that there is no coordinates in the current polygon."
125-
);
126-
}
127-
128-
public static Double getMinXCoordinate(Polygon polygon) {
129-
OptionalDouble min = polygon
130-
.getCoordinates()
131-
.stream()
132-
.map(Point::getX)
133-
.mapToDouble(Double::doubleValue)
134-
.min();
135-
if (min.isPresent()) {
136-
return min.getAsDouble();
137-
}
138-
139-
throw new IllegalStateException(
140-
"The min X could not be found "
141-
+ "because it seems that there is no coordinates in the current polygon."
142-
);
143-
}
144-
145-
public static Double getMaxXCoordinate(Polygon polygon) {
146-
OptionalDouble max = polygon
147-
.getCoordinates()
148-
.stream()
149-
.map(Point::getX)
150-
.mapToDouble(Double::doubleValue)
151-
.max();
152-
if (max.isPresent()) {
153-
return max.getAsDouble();
154-
}
155-
156-
throw new IllegalStateException(
157-
"The max X could not be found "
158-
+ "because it seems that there is no coordinates in the current polygon."
159-
);
160-
}
161-
16258
/**
16359
* Merge the coordinates of the two polygons.
16460
*/
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.mindee.geometry;
2+
3+
/**
4+
* A field with position data.
5+
*/
6+
public interface PositionDataField {
7+
Polygon getPolygon();
8+
}

src/main/java/com/mindee/extraction/ExtractedImage.java renamed to src/main/java/com/mindee/image/ExtractedImage.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.mindee.extraction;
1+
package com.mindee.image;
22

33
import com.mindee.input.LocalInputSource;
44
import java.awt.image.BufferedImage;
@@ -21,7 +21,7 @@ public class ExtractedImage {
2121

2222
/**
2323
* Default constructor.
24-
*
24+
*
2525
* @param image Buffered image object.
2626
* @param filename Name of the extracted image.
2727
* @param saveFormat Format to save the image as, defaults to PNG.
@@ -35,7 +35,7 @@ public ExtractedImage(BufferedImage image, String filename, String saveFormat) {
3535
/**
3636
* Write the image to a file.
3737
* Uses the default image format and filename.
38-
*
38+
*
3939
* @param outputPath the output directory (must exist).
4040
* @throws IOException Throws if the file can't be accessed.
4141
*/
@@ -48,7 +48,7 @@ public void writeToFile(String outputPath) throws IOException {
4848
/**
4949
* Write the image to a file.
5050
* Uses the default image format and filename.
51-
*
51+
*
5252
* @param outputPath the output directory (must exist).
5353
* @throws IOException Throws if the file can't be accessed.
5454
*/
@@ -60,7 +60,7 @@ public void writeToFile(Path outputPath) throws IOException {
6060

6161
/**
6262
* Return the image in a format suitable for sending to MindeeClient for parsing.
63-
*
63+
*
6464
* @return an instance of {@link LocalInputSource}
6565
* @throws IOException Throws if the file can't be accessed.
6666
*/

src/main/java/com/mindee/extraction/ImageExtractor.java renamed to src/main/java/com/mindee/image/ImageExtractor.java

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
package com.mindee.extraction;
1+
package com.mindee.image;
22

33
import com.mindee.geometry.Bbox;
4-
import com.mindee.geometry.BboxUtils;
54
import com.mindee.geometry.Polygon;
5+
import com.mindee.geometry.PositionDataField;
66
import com.mindee.input.InputSourceUtils;
77
import com.mindee.input.LocalInputSource;
88
import com.mindee.pdf.PDFUtils;
99
import com.mindee.pdf.PdfPageImage;
10-
import com.mindee.v1.parsing.standard.PositionData;
1110
import java.awt.image.BufferedImage;
1211
import java.io.ByteArrayInputStream;
1312
import java.io.IOException;
@@ -75,7 +74,7 @@ public int getPageCount() {
7574
* @param pageIndex The page index to extract, begins at 0.
7675
* @return A list of {@link ExtractedImage}.
7776
*/
78-
public <FieldT extends PositionData> List<ExtractedImage> extractImagesFromPage(
77+
public <FieldT extends PositionDataField> List<ExtractedImage> extractImagesFromPage(
7978
List<FieldT> fields,
8079
int pageIndex
8180
) {
@@ -91,7 +90,7 @@ public <FieldT extends PositionData> List<ExtractedImage> extractImagesFromPage(
9190
* @param outputName The base output filename, must have an image extension.
9291
* @return A list of {@link ExtractedImage}.
9392
*/
94-
public <FieldT extends PositionData> List<ExtractedImage> extractImagesFromPage(
93+
public <FieldT extends PositionDataField> List<ExtractedImage> extractImagesFromPage(
9594
List<FieldT> fields,
9695
int pageIndex,
9796
String outputName
@@ -106,7 +105,7 @@ public <FieldT extends PositionData> List<ExtractedImage> extractImagesFromPage(
106105
return extractFromPage(fields, pageIndex, filename);
107106
}
108107

109-
private <FieldT extends PositionData> List<ExtractedImage> extractFromPage(
108+
private <FieldT extends PositionDataField> List<ExtractedImage> extractFromPage(
110109
List<FieldT> fields,
111110
int pageIndex,
112111
String outputName
@@ -137,24 +136,27 @@ private <FieldT extends PositionData> List<ExtractedImage> extractFromPage(
137136
* @return The {@link ExtractedImage}, or <code>null</code> if the field does not have valid
138137
* position data.
139138
*/
140-
public <FieldT extends PositionData> ExtractedImage extractImage(
139+
public <FieldT extends PositionDataField> ExtractedImage extractImage(
141140
FieldT field,
142141
int pageIndex,
143142
int index,
144143
String filename
145144
) {
146145
String[] splitName = InputSourceUtils.splitNameStrict(filename);
147146
String saveFormat = splitName[1].toLowerCase();
148-
Polygon boundingBox = field.getBoundingBox();
149-
if (boundingBox == null) {
147+
Polygon polygon = field.getPolygon();
148+
if (polygon == null) {
150149
return null;
151150
}
152-
Bbox bbox = BboxUtils.generate(boundingBox);
153151
String fieldFilename = splitName[0]
154152
+ String.format("_%3s", index).replace(" ", "0")
155153
+ "."
156154
+ saveFormat;
157-
return new ExtractedImage(extractImage(bbox, pageIndex), fieldFilename, saveFormat);
155+
return new ExtractedImage(
156+
extractImage(polygon.getAsBbox(), pageIndex),
157+
fieldFilename,
158+
saveFormat
159+
);
158160
}
159161

160162
/**
@@ -167,7 +169,7 @@ public <FieldT extends PositionData> ExtractedImage extractImage(
167169
* @return The {@link ExtractedImage}, or <code>null</code> if the field does not have valid
168170
* position data.
169171
*/
170-
public <FieldT extends PositionData> ExtractedImage extractImage(
172+
public <FieldT extends PositionDataField> ExtractedImage extractImage(
171173
FieldT field,
172174
int pageIndex,
173175
int index

src/main/java/com/mindee/extraction/ExtractedPDF.java renamed to src/main/java/com/mindee/pdf/ExtractedPDF.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.mindee.extraction;
1+
package com.mindee.pdf;
22

33
import com.mindee.input.LocalInputSource;
44
import java.io.ByteArrayOutputStream;

src/main/java/com/mindee/extraction/PDFExtractor.java renamed to src/main/java/com/mindee/pdf/PDFExtractor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.mindee.extraction;
1+
package com.mindee.pdf;
22

33
import static com.mindee.pdf.PDFUtils.mergePdfPages;
44

0 commit comments

Comments
 (0)