Skip to content

Commit 0975f89

Browse files
committed
add OCR classes
1 parent d720d68 commit 0975f89

8 files changed

Lines changed: 192 additions & 3 deletions

File tree

src/main/java/com/mindee/v2/product/classification/ClassificationResult.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public final class ClassificationResult {
2626
@Override
2727
public String toString() {
2828
StringJoiner joiner = new StringJoiner("\n");
29-
joiner.add("Classification").add("==============");
29+
joiner.add("Classification\n==============");
3030
joiner.add(classification.toString());
3131

3232
return joiner.toString();

src/main/java/com/mindee/v2/product/crop/CropResult.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ public final class CropResult {
2727
@Override
2828
public String toString() {
2929
StringJoiner joiner = new StringJoiner("\n");
30-
joiner.add("Crops").add("=====");
31-
for (CropItem item : this.crops) {
30+
joiner.add("Crops\n=====");
31+
for (CropItem item : crops) {
3232
joiner.add(item.toString());
3333
}
3434
return joiner.toString();
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.mindee.v2.product.ocr;
2+
3+
import com.mindee.v2.parsing.BaseInference;
4+
5+
/**
6+
* The inference result for an OCR utility request.
7+
*/
8+
public class OcrInference extends BaseInference<OcrResult> {
9+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.mindee.v2.product.ocr;
2+
3+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
4+
import com.fasterxml.jackson.annotation.JsonProperty;
5+
import java.util.ArrayList;
6+
import lombok.AllArgsConstructor;
7+
import lombok.EqualsAndHashCode;
8+
import lombok.Getter;
9+
import lombok.NoArgsConstructor;
10+
11+
/**
12+
* OCR result for a single page.
13+
*/
14+
@Getter
15+
@EqualsAndHashCode
16+
@JsonIgnoreProperties(ignoreUnknown = true)
17+
@AllArgsConstructor
18+
@NoArgsConstructor
19+
public class OcrPage {
20+
/**
21+
* Full text content extracted from the document page.
22+
*/
23+
@JsonProperty("content")
24+
private String content;
25+
26+
/**
27+
* List of words extracted from the document page.
28+
*/
29+
@JsonProperty("words")
30+
private ArrayList<OcrWord> words;
31+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.mindee.v2.product.ocr;
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 an OCR utility inference.
10+
*/
11+
@Getter
12+
@JsonIgnoreProperties(ignoreUnknown = true)
13+
public class OcrResponse extends CommonResponse {
14+
15+
/**
16+
* The inference result for an OCR utility request.
17+
*/
18+
@JsonProperty("inference")
19+
private OcrInference 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.ocr;
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 the OCR utility inference.
14+
*/
15+
@Getter
16+
@EqualsAndHashCode
17+
@JsonIgnoreProperties(ignoreUnknown = true)
18+
@AllArgsConstructor
19+
@NoArgsConstructor
20+
public final class OcrResult {
21+
/**
22+
* List of OCR results for each page in the document.
23+
*/
24+
@JsonProperty("pages")
25+
private ArrayList<OcrPage> pages;
26+
27+
@Override
28+
public String toString() {
29+
StringJoiner joiner = new StringJoiner("\n");
30+
joiner.add("Pages\n======");
31+
for (OcrPage page : pages) {
32+
joiner.add(page.toString());
33+
}
34+
return joiner.toString();
35+
}
36+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.mindee.v2.product.ocr;
2+
3+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
4+
import com.fasterxml.jackson.annotation.JsonProperty;
5+
import com.mindee.geometry.Polygon;
6+
import lombok.AllArgsConstructor;
7+
import lombok.EqualsAndHashCode;
8+
import lombok.Getter;
9+
import lombok.NoArgsConstructor;
10+
11+
/**
12+
* OCR result for a single word extracted from the document page.
13+
*/
14+
@Getter
15+
@EqualsAndHashCode
16+
@JsonIgnoreProperties(ignoreUnknown = true)
17+
@AllArgsConstructor
18+
@NoArgsConstructor
19+
public class OcrWord {
20+
/**
21+
* Text content of the word.
22+
*/
23+
@JsonProperty("content")
24+
private String content;
25+
26+
/**
27+
* Position information as a list of points in clockwise order.
28+
*/
29+
@JsonProperty("polygon")
30+
private Polygon polygon;
31+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.mindee.v2.product;
2+
3+
import static com.mindee.TestingUtilities.getV2ResourcePath;
4+
import static org.junit.jupiter.api.Assertions.assertEquals;
5+
import static org.junit.jupiter.api.Assertions.assertNotNull;
6+
7+
import com.mindee.input.LocalResponse;
8+
import com.mindee.v2.product.ocr.OcrPage;
9+
import com.mindee.v2.product.ocr.OcrResponse;
10+
import java.io.IOException;
11+
import java.util.ArrayList;
12+
import org.junit.jupiter.api.DisplayName;
13+
import org.junit.jupiter.api.Nested;
14+
import org.junit.jupiter.api.Test;
15+
16+
@DisplayName("MindeeV2 - OCR Model Tests")
17+
public class OcrTest {
18+
private OcrResponse loadResponse(String filePath) throws IOException {
19+
LocalResponse localResponse = new LocalResponse(getV2ResourcePath(filePath));
20+
return localResponse.deserializeResponse(OcrResponse.class);
21+
}
22+
23+
@Nested
24+
@DisplayName("Result with single value")
25+
class SinglePredictionTest {
26+
@Test
27+
@DisplayName("all properties must be valid")
28+
void mustHaveValidProperties() throws IOException {
29+
OcrResponse response = loadResponse("products/ocr/ocr_single.json");
30+
assertNotNull(response.getInference());
31+
32+
ArrayList<OcrPage> pages = response.getInference().getResult().getPages();
33+
assertEquals(1, pages.size());
34+
assertEquals(305, pages.get(0).getWords().size());
35+
assertEquals("Shipper:", pages.get(0).getWords().get(0).getContent());
36+
}
37+
}
38+
39+
@Nested
40+
@DisplayName("Result with multiple values")
41+
class MultiPredictionTest {
42+
@Test
43+
@DisplayName("all properties must be valid")
44+
void mustHaveValidProperties() throws IOException {
45+
OcrResponse response = loadResponse("products/ocr/ocr_multiple.json");
46+
assertNotNull(response.getInference());
47+
48+
ArrayList<OcrPage> pages = response.getInference().getResult().getPages();
49+
assertEquals(3, pages.size());
50+
51+
OcrPage page1 = pages.get(0);
52+
assertNotNull(page1.getContent());
53+
assertEquals(295, page1.getWords().size());
54+
assertEquals("FICTIOCORP", page1.getWords().get(0).getContent());
55+
56+
OcrPage page2 = pages.get(1);
57+
assertNotNull(page2.getContent());
58+
assertEquals(450, page2.getWords().size());
59+
assertEquals("KEOLIO", page2.getWords().get(0).getContent());
60+
}
61+
}
62+
}

0 commit comments

Comments
 (0)