Skip to content

Commit 9ed42bf

Browse files
committed
✨ add OCR output to CLI
1 parent b27c1bd commit 9ed42bf

File tree

1 file changed

+54
-20
lines changed

1 file changed

+54
-20
lines changed

src/main/java/com/mindee/CommandLineInterface.java

Lines changed: 54 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,20 @@
77
import com.mindee.input.PageOptions;
88
import com.mindee.input.PageOptionsOperation;
99
import com.mindee.parsing.common.AsyncPredictResponse;
10-
import com.mindee.parsing.common.Document;
1110
import com.mindee.parsing.common.Inference;
1211
import com.mindee.parsing.common.PredictResponse;
12+
import com.mindee.parsing.common.ocr.Ocr;
1313
import com.mindee.product.custom.CustomV1;
1414
import com.mindee.product.generated.GeneratedV1;
1515
import java.io.File;
1616
import java.io.IOException;
1717
import java.lang.reflect.Method;
1818
import java.util.ArrayList;
19+
import java.util.Arrays;
1920
import java.util.List;
2021
import java.util.Objects;
2122
import java.util.concurrent.Callable;
23+
import java.util.stream.Collectors;
2224
import picocli.CommandLine;
2325
import picocli.CommandLine.Command;
2426
import picocli.CommandLine.Model.CommandSpec;
@@ -66,7 +68,8 @@ private enum OutputChoices { summary, full, raw }
6668
description = "Output type, one of:\n"
6769
+ " summary - document predictions\n"
6870
+ " full - all predictions\n"
69-
+ " raw - raw response from the server"
71+
+ " raw - raw response from the server",
72+
defaultValue = "summary"
7073
)
7174
private OutputChoices outputType;
7275

@@ -109,7 +112,6 @@ public static void main(String[] args) {
109112

110113
/**
111114
* Adds all commands from CommandLineInterfaceProducts automatically.
112-
* Avoids using a Mixin, which I can't get to work.
113115
*/
114116
@CommandLine.Command(mixinStandardHelpOptions = true, description = "Auto-generated product command")
115117
public static class ProductCommandHandler implements Callable<Integer> {
@@ -138,7 +140,10 @@ public Integer call() throws Exception {
138140
}
139141
}
140142

141-
@Command(name = "custom", description = "Invokes a Custom API (API Builder only, use 'generated' for regular custom APIs)")
143+
@Command(
144+
name = "custom",
145+
description = "Invokes a Custom API (API Builder only, use 'generated' for regular custom APIs)"
146+
)
142147
void customMethod(
143148
@Option(
144149
names = {"-a", "--account"},
@@ -261,19 +266,29 @@ void generatedMethod(
261266
}
262267

263268
protected PageOptions getDefaultPageOptions() {
264-
265269
List<Integer> pageNumbers = new ArrayList<>();
266270
pageNumbers.add(0);
267271
pageNumbers.add(1);
268272
pageNumbers.add(2);
269273
pageNumbers.add(3);
270274
pageNumbers.add(4);
271-
272275
return new PageOptions(pageNumbers, PageOptionsOperation.KEEP_ONLY);
273276
}
274277

278+
private String wordsOutput(Ocr ocr) {
279+
StringBuilder output = new StringBuilder();
280+
output.append("\n#############\nDocument Text\n#############\n::\n ");
281+
output.append(
282+
Arrays.stream(ocr.toString().split(String.format("%n")))
283+
.collect(Collectors.joining(String.format("%n ")))
284+
);
285+
output.append("\n");
286+
return output.toString();
287+
}
288+
275289
@Override
276-
public <T extends Inference<?, ?>> String standardProductOutput(Class<T> productClass, File file) throws IOException {
290+
public <T extends Inference<?, ?>> String standardProductOutput(Class<T> productClass, File file)
291+
throws IOException {
277292
MindeeClient mindeeClient = new MindeeClient(apiKey);
278293
LocalInputSource inputSource = new LocalInputSource(file);
279294
PredictResponse<T> response;
@@ -284,18 +299,28 @@ protected PageOptions getDefaultPageOptions() {
284299
response = mindeeClient.parse(productClass, inputSource, predictOptions);
285300
}
286301

287-
if (outputType == OutputChoices.full) {
288-
return response.getDocument().toString();
302+
StringBuilder output = new StringBuilder();
303+
switch (outputType) {
304+
case full:
305+
output.append(response.getDocument().toString());
306+
break;
307+
case raw:
308+
output.append(response.getRawResponse());
309+
break;
310+
default:
311+
output.append(
312+
response.getDocument().getInference().getPrediction().toString()
313+
);
289314
}
290-
if (outputType == OutputChoices.raw) {
291-
return response.getRawResponse();
315+
if (words) {
316+
output.append(wordsOutput(response.getDocument().getOcr()));
292317
}
293-
Document<T> document = response.getDocument();
294-
return document.getInference().getPrediction().toString();
318+
return output.toString();
295319
}
296320

297321
@Override
298-
public <T extends Inference<?, ?>> String standardProductAsyncOutput(Class<T> productClass, File file) throws IOException, InterruptedException {
322+
public <T extends Inference<?, ?>> String standardProductAsyncOutput(Class<T> productClass, File file)
323+
throws IOException, InterruptedException {
299324
MindeeClient mindeeClient = new MindeeClient(apiKey);
300325
LocalInputSource inputSource = new LocalInputSource(file);
301326
AsyncPredictResponse<T> response;
@@ -310,13 +335,22 @@ productClass, inputSource, predictOptions, getDefaultPageOptions(), null
310335
);
311336
}
312337

313-
if (outputType == OutputChoices.full) {
314-
return response.getDocumentObj().toString();
338+
StringBuilder output = new StringBuilder();
339+
switch (outputType) {
340+
case full:
341+
output.append(response.getDocument().toString());
342+
break;
343+
case raw:
344+
output.append(response.getRawResponse());
345+
break;
346+
default:
347+
output.append(
348+
response.getDocumentObj().getInference().getPrediction().toString()
349+
);
315350
}
316-
if (outputType == OutputChoices.raw) {
317-
return response.getRawResponse();
351+
if (words) {
352+
output.append(wordsOutput(response.getDocumentObj().getOcr()));
318353
}
319-
Document<T> document = response.getDocumentObj();
320-
return document.getInference().getPrediction().toString();
354+
return output.toString();
321355
}
322356
}

0 commit comments

Comments
 (0)