Skip to content

Commit d720d68

Browse files
committed
add crop classes
1 parent b00398f commit d720d68

6 files changed

Lines changed: 188 additions & 1 deletion

File tree

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.mindee.v2.product.crop;
2+
3+
import com.mindee.v2.parsing.BaseInference;
4+
5+
/**
6+
* The inference result for a classification utility request.
7+
*/
8+
public class CropInference extends BaseInference<CropResult> {
9+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.mindee.v2.product.crop;
2+
3+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
4+
import com.fasterxml.jackson.annotation.JsonProperty;
5+
import com.mindee.parsing.v2.field.FieldLocation;
6+
import lombok.AllArgsConstructor;
7+
import lombok.EqualsAndHashCode;
8+
import lombok.Getter;
9+
import lombok.NoArgsConstructor;
10+
11+
/**
12+
* Result of a cropped document region.
13+
*/
14+
@Getter
15+
@EqualsAndHashCode
16+
@JsonIgnoreProperties(ignoreUnknown = true)
17+
@AllArgsConstructor
18+
@NoArgsConstructor
19+
public class CropItem {
20+
/**
21+
* Type or classification of the detected object.
22+
*/
23+
@JsonProperty("object_type")
24+
private String objectType;
25+
/**
26+
* Location which includes cropping coordinates for the detected object, within the source
27+
* document.
28+
*/
29+
@JsonProperty("location")
30+
private FieldLocation location;
31+
32+
@Override
33+
public String toString() {
34+
return "* :Location: " + location + "\n :Object Type: " + objectType;
35+
}
36+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.mindee.v2.product.crop;
2+
3+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
4+
import com.fasterxml.jackson.annotation.JsonProperty;
5+
import com.mindee.parsing.v2.CommonResponse;
6+
import lombok.Getter;
7+
8+
/**
9+
* Response for a crop utility inference.
10+
*/
11+
@Getter
12+
@JsonIgnoreProperties(ignoreUnknown = true)
13+
public class CropResponse extends CommonResponse {
14+
15+
/**
16+
* The inference result for a crop utility request.
17+
*/
18+
@JsonProperty("inference")
19+
private CropInference inference;
20+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.mindee.v2.product.crop;
2+
3+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
4+
import com.fasterxml.jackson.annotation.JsonProperty;
5+
import java.util.ArrayList;
6+
import java.util.StringJoiner;
7+
import lombok.AllArgsConstructor;
8+
import lombok.EqualsAndHashCode;
9+
import lombok.Getter;
10+
import lombok.NoArgsConstructor;
11+
12+
/**
13+
* Result of a crop utility inference.
14+
*/
15+
@Getter
16+
@EqualsAndHashCode
17+
@JsonIgnoreProperties(ignoreUnknown = true)
18+
@AllArgsConstructor
19+
@NoArgsConstructor
20+
public final class CropResult {
21+
/**
22+
* List of objects and their cropping coordinates identified in the source document.
23+
*/
24+
@JsonProperty("crops")
25+
private ArrayList<CropItem> crops;
26+
27+
@Override
28+
public String toString() {
29+
StringJoiner joiner = new StringJoiner("\n");
30+
joiner.add("Crops").add("=====");
31+
for (CropItem item : this.crops) {
32+
joiner.add(item.toString());
33+
}
34+
return joiner.toString();
35+
}
36+
}

src/test/java/com/mindee/v2/product/ClassificationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ private ClassificationResponse loadResponse(String filePath) throws IOException
1919
}
2020

2121
@Nested
22-
@DisplayName("Classification with single value")
22+
@DisplayName("Result with single value")
2323
class SinglePredictionTest {
2424
@Test
2525
@DisplayName("all properties must be valid")
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package com.mindee.v2.product;
2+
3+
import static com.mindee.TestingUtilities.assertStringEqualsFile;
4+
import static com.mindee.TestingUtilities.getV2ResourcePath;
5+
import static org.junit.jupiter.api.Assertions.assertEquals;
6+
import static org.junit.jupiter.api.Assertions.assertNotNull;
7+
8+
import com.mindee.input.LocalResponse;
9+
import com.mindee.v2.product.crop.CropItem;
10+
import com.mindee.v2.product.crop.CropResponse;
11+
import java.io.IOException;
12+
import java.util.ArrayList;
13+
import org.junit.jupiter.api.DisplayName;
14+
import org.junit.jupiter.api.Nested;
15+
import org.junit.jupiter.api.Test;
16+
17+
@DisplayName("MindeeV2 - Crop Model Tests")
18+
public class CropTest {
19+
private CropResponse loadResponse(String filePath) throws IOException {
20+
LocalResponse localResponse = new LocalResponse(getV2ResourcePath(filePath));
21+
return localResponse.deserializeResponse(CropResponse.class);
22+
}
23+
24+
@Nested
25+
@DisplayName("Result with single value")
26+
class SinglePredictionTest {
27+
@Test
28+
@DisplayName("all properties must be valid")
29+
void mustHaveValidProperties() throws IOException {
30+
CropResponse response = loadResponse("products/crop/crop_single.json");
31+
assertNotNull(response.getInference());
32+
33+
ArrayList<CropItem> crops = response.getInference().getResult().getCrops();
34+
assertEquals(1, crops.size());
35+
36+
CropItem crop1 = crops.get(0);
37+
assertEquals("invoice", crop1.getObjectType());
38+
assertNotNull(crop1.getLocation().getPolygon());
39+
assertEquals(0, crop1.getLocation().getPage());
40+
}
41+
42+
@Test
43+
@DisplayName("RST output must be valid")
44+
void mustHaveValidDisplay() throws IOException {
45+
CropResponse response = loadResponse("products/crop/crop_single.json");
46+
assertStringEqualsFile(
47+
response.getInference().toString(),
48+
getV2ResourcePath("products/crop/crop_single.rst")
49+
);
50+
}
51+
}
52+
53+
@Nested
54+
@DisplayName("Result with multiple values")
55+
class MultiPredictionTest {
56+
@Test
57+
@DisplayName("all properties must be valid")
58+
void mustHaveValidProperties() throws IOException {
59+
CropResponse response = loadResponse("products/crop/crop_multiple.json");
60+
assertNotNull(response.getInference());
61+
62+
ArrayList<CropItem> crops = response.getInference().getResult().getCrops();
63+
assertEquals(2, crops.size());
64+
65+
CropItem crop1 = crops.get(0);
66+
assertEquals("invoice", crop1.getObjectType());
67+
assertNotNull(crop1.getLocation().getPolygon());
68+
assertEquals(0, crop1.getLocation().getPage());
69+
70+
CropItem crop2 = crops.get(1);
71+
assertEquals("invoice", crop2.getObjectType());
72+
assertNotNull(crop2.getLocation().getPolygon());
73+
assertEquals(0, crop2.getLocation().getPage());
74+
}
75+
76+
@Test
77+
@DisplayName("RST output must be valid")
78+
void mustHaveValidDisplay() throws IOException {
79+
CropResponse response = loadResponse("products/crop/crop_multiple.json");
80+
assertStringEqualsFile(
81+
response.getInference().toString(),
82+
getV2ResourcePath("products/crop/crop_multiple.rst")
83+
);
84+
}
85+
}
86+
}

0 commit comments

Comments
 (0)