-
Notifications
You must be signed in to change notification settings - Fork 5
/
OcrVerticle.java
80 lines (67 loc) · 2.5 KB
/
OcrVerticle.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package org.genericsystem.watch;
import java.nio.file.Paths;
import org.genericsystem.common.Root;
import org.genericsystem.cv.comparator.FillModelWithData;
import com.sun.xml.internal.ws.api.pipe.Engine;
import io.vertx.core.AsyncResult;
import io.vertx.core.Future;
import io.vertx.core.json.JsonObject;
/**
* The OcrVerticle receives a message from the event bus when an image has been successfully de-zoned. The image is then processed (e.g., transformed and OCR'd) and the results from the OCR are stored in Generic System.
*
* @author Pierrik Lassalas
*/
public class OcrVerticle extends ActionVerticle {
public static final String ACTION = "ocr";
public static final String NEW_IMAGE_PROCESSED = "app.ocr.newimage.processed";
public static final String KNOWN_IMAGE_PROCESSED = "app.ocr.knownimage.updated";
public static final String KNOWN_IMAGE_PASSED = "app.ocr.knownimage.passed";
@Override
public String getAction() {
return ACTION;
}
private Root engine;
/**
* Default constructor. A reference to an {@link Engine} must be provided to be able to save the results.
*
* @param engine - the engine used to store the data
*/
public OcrVerticle(String ip, Root engine) {
super(ip);
this.engine = engine;
}
// TODO: refactor the code, since the file is now stored before being sent to OCR (and thus, is always known)
@Override
protected void handle(Future<Object> future, JsonObject task) {
String imagePath = task.getString(DistributedVerticle.FILENAME);
int result = FillModelWithData.ERROR;
if (null != engine)
result = FillModelWithData.doImgOcr(engine, Paths.get(imagePath));
else
result = FillModelWithData.doImgOcr(Paths.get(imagePath));
switch (result) {
case FillModelWithData.NEW_FILE:
System.out.println("New image (processed)" + imagePath);
future.complete(NEW_IMAGE_PROCESSED);
break;
case FillModelWithData.KNOWN_FILE:
System.out.println("Known image (passed)" + imagePath);
future.complete(KNOWN_IMAGE_PASSED);
break;
case FillModelWithData.KNOWN_FILE_UPDATED_FILTERS:
System.out.println("Known image (updated)" + imagePath);
future.complete(KNOWN_IMAGE_PROCESSED);
break;
default:
future.fail("Unhandled case!");
break;
}
}
@Override
protected void handleResult(AsyncResult<Object> res, JsonObject task) {
if (res.succeeded())
addTask(task.getString(DistributedVerticle.FILENAME), getIp(), (String) res.result());
else
throw new IllegalStateException("Exception in OcrVerticle.", res.cause());
}
}