-
Notifications
You must be signed in to change notification settings - Fork 5
/
OcrVerticle.java
104 lines (91 loc) · 3.51 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package org.genericsystem.watch;
import java.nio.file.Path;
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.AbstractVerticle;
import io.vertx.core.Vertx;
import io.vertx.core.VertxOptions;
import io.vertx.core.eventbus.MessageConsumer;
/**
* 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 AbstractVerticle {
private Root engine;
public static void main(String[] args) {
VertxOptions options = new VertxOptions().setMaxWorkerExecuteTime(Long.MAX_VALUE);
Vertx vertx = Vertx.vertx(options);
OcrVerticle ocrVerticle = new OcrVerticle(FillModelWithData.getEngine());
vertx.deployVerticle(ocrVerticle, res -> {
if (res.failed())
throw new IllegalStateException("Deployment of verticles failed.", res.cause());
else
System.out.println("Verticle deployed");
});
deployTestVerticle();
}
/**
* 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(Root engine) {
this.engine = engine;
}
/**
* Test method to deploy a test Verticle that will send a message to the OCR Verticle.
* <p>
* <b>This method should only be used for testing purposes.</b>
*/
public static void deployTestVerticle() {
AbstractVerticle testVerticle = new AbstractVerticle() {
@Override
public void start() throws Exception {
Path imagePath = Paths.get(System.getProperty("user.dir") + "/../gs-cv/classes/id-fr-front/image2-0.png");
vertx.eventBus().publish(VerticleDeployer.ACCURATE_ZONES_FOUND, imagePath.toString());
}
};
VerticleDeployerFromWatchApp.deployWorkerVerticle(testVerticle, "Deployment of verticles failed.");
}
// TODO: refactor the code, since the file is now stored before being sent to OCR (and thus, is always known)
@Override
public void start() throws Exception {
MessageConsumer<String> consumer = vertx.eventBus().consumer(VerticleDeployer.ACCURATE_ZONES_FOUND);
consumer.handler(message -> vertx.executeBlocking(future -> {
System.out.println(">>> ocr: " + Thread.currentThread().getName());
String imagePath = message.body();
System.out.println(">>>>> New image to OCR: " + imagePath);
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)");
vertx.eventBus().publish(VerticleDeployer.NEW_IMAGE_PROCESSED, imagePath);
future.complete();
break;
case FillModelWithData.KNOWN_FILE:
System.out.println("Known image (passed)");
vertx.eventBus().publish(VerticleDeployer.KNOWN_IMAGE_PASSED, imagePath);
future.complete();
break;
case FillModelWithData.KNOWN_FILE_UPDATED_FILTERS:
System.out.println("Known image (updated)");
vertx.eventBus().publish(VerticleDeployer.KNOWN_IMAGE_PROCESSED, imagePath);
future.complete();
break;
default:
future.fail("Unhandled case!");
break;
}
}, res -> {
if (res.failed())
throw new IllegalStateException(res.cause());
}));
}
}