-
Notifications
You must be signed in to change notification settings - Fork 5
/
DezonerVerticle.java
67 lines (58 loc) · 2.34 KB
/
DezonerVerticle.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
package org.genericsystem.ir;
import java.lang.invoke.MethodHandles;
import java.nio.file.Paths;
import org.genericsystem.common.Root;
import org.genericsystem.cv.Img;
import org.genericsystem.cv.Zones;
import org.genericsystem.cv.model.ModelTools;
import org.opencv.core.Scalar;
import org.opencv.imgcodecs.Imgcodecs;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.vertx.core.AsyncResult;
import io.vertx.core.Future;
import io.vertx.core.json.JsonObject;
/**
* The DezonerVerticle receives a message from the event bus when a new image has been added to a class. If this class has already been dezoned, a message will be sent to the {@link OcrParametersVerticle}.
*
* @author Pierrik Lassalas
*/
public class DezonerVerticle extends ActionPersistentVerticle {
private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
public static final String ACTION = "dezoner";
public DezonerVerticle(Root engine) {
super(engine);
}
@Override
public String getAction() {
return ACTION;
}
@Override
protected void handle(Future<Object> future, JsonObject task) {
String imagePath = DistributedVerticle.BASE_PATH + task.getString(DistributedVerticle.FILENAME);
if (Zones.isZonesFilePresent(imagePath)) {
final Zones zones = Zones.load(Paths.get(imagePath).getParent().toString());
Img imgCopy = new Img(imagePath);
zones.draw(imgCopy, new Scalar(0, 255, 0), 3);
zones.writeNum(imgCopy, new Scalar(0, 0, 255), 3);
// TODO implement a filter mechanism to avoid creating duplicates in a public folder
String filenameExt = ModelTools.generateFileName(Paths.get(imagePath));
Imgcodecs.imwrite(DistributedVerticle.RESOURCES_FOLDER + filenameExt, imgCopy.getSrc());
imgCopy.close();
future.complete();
} else {
// No zones file was found, need to define the zones manually
// TODO: replace the future.fail by a notification to the system that a zone needs to be defined for this file
future.fail("No accurate zones found for " + imagePath);
}
}
@Override
protected void handleResult(AsyncResult<Object> res, JsonObject task) {
if (res.succeeded())
addTask(task.getString(DistributedVerticle.FILENAME), OcrParametersVerticle.ACTION);
else {
logger.info("No zones defined for file {}.", task.getString(DistributedVerticle.FILENAME));
res.cause().printStackTrace();
}
}
}