-
Notifications
You must be signed in to change notification settings - Fork 5
/
DezonerVerticle.java
71 lines (60 loc) · 2.44 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
68
69
70
71
package org.genericsystem.watch;
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 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 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 ActionVerticle {
public static final String ACTION = "dezoner";
private static final String RESOURCES_FOLDER = System.getProperty("user.dir") + "/../gs-watch/src/main/resources/";
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 DezonerVerticle(Root engine) {
this.engine = engine;
}
@Override
public String getAction() {
return ACTION;
}
@Override
protected void handle(Future<Object> future, JsonObject task) {
String imagePath = 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(RESOURCES_FOLDER + filenameExt, imgCopy.getSrc());
imgCopy.close();
future.complete(OcrParametersVerticle.ACTION);
} 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), (String) res.result());
else
System.out.println("No zones defined for file " + task.getString(DistributedVerticle.FILENAME));
}
}