-
Notifications
You must be signed in to change notification settings - Fork 5
/
OcrVerticle.java
124 lines (107 loc) · 3.73 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package org.genericsystem.watch;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.genericsystem.common.Root;
import org.genericsystem.cv.Ocr;
import org.genericsystem.cv.comparator.FillModelWithData;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Vertx;
import io.vertx.core.VertxOptions;
import io.vertx.core.eventbus.MessageConsumer;
import io.vertx.core.logging.Logger;
import io.vertx.core.logging.LoggerFactory;
public class OcrVerticle extends AbstractVerticle {
private static Logger log = LoggerFactory.getLogger(ClassifierVerticle.class);
private Root engine;
public static void main(String[] args) {
VertxOptions options = new VertxOptions().setMaxWorkerExecuteTime(Long.MAX_VALUE);
// deployOcrVerticle(options);
deployTestVerticle(options);
}
public OcrVerticle() {
// TODO Auto-generated constructor stub
System.out.println(">>> OcrVerticle() called");
}
public OcrVerticle(Root engine) {
this.engine = engine;
System.out.println(">>> OcrVerticle(Root engine) called");
}
public void deployVerticle(VertxOptions options) {
Vertx vertx = Vertx.vertx(options);
OcrVerticle ocrVerticle = new OcrVerticle(engine);
vertx.deployVerticle(ocrVerticle, res -> {
if (res.failed())
throw new IllegalStateException("Deployment of verticles failed.", res.cause());
else
System.out.println("Verticle deployed");
});
}
public static void deployOcrVerticle(VertxOptions options) {
Vertx vertx = Vertx.vertx(options);
OcrVerticle ocrVerticle = new OcrVerticle();
vertx.deployVerticle(ocrVerticle, res -> {
if (res.failed())
throw new IllegalStateException("Deployment of verticles failed.", res.cause());
else
System.out.println("Verticle deployed");
});
}
public static void deployTestVerticle(VertxOptions options) {
Vertx vertx = Vertx.vertx(options);
vertx.deployVerticle(new AbstractVerticle() {
@Override
public void start() throws Exception {
Path imagePath = Paths.get(System.getProperty("user.dir") + "/../gs-cv/classes/id-fr-front/image6-0.png");
vertx.eventBus().publish(VerticleDeployer.ACCURATE_ZONES_FOUND, imagePath.toString());
}
}, res -> {
if (res.failed())
throw new IllegalStateException("Deployment of verticles failed.", res.cause());
else
System.out.println("Verticle deployed");
});
}
@Override
public void start() throws Exception {
MessageConsumer<String> consumer = vertx.eventBus().consumer(VerticleDeployer.ACCURATE_ZONES_FOUND);
// TODO: use a worker thread?
consumer.handler(message -> vertx.executeBlocking(future -> {
String imagePath = message.body();
System.out.println(">>>>> New image to OCR: " + imagePath);
int result = FillModelWithData.ERROR;
if (null != engine)
result = Ocr.ocrNewClassifiedImg(engine, Paths.get(imagePath));
else
result = Ocr.ocrNewClassifiedImg(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());
}));
}
public Root getEngine() {
return engine;
}
public void setEngine(Root engine) {
this.engine = engine;
}
}