-
Notifications
You must be signed in to change notification settings - Fork 5
/
ComputeBestTextPerZone.java
178 lines (159 loc) · 7.83 KB
/
ComputeBestTextPerZone.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package org.genericsystem.cv.comparator;
import java.lang.invoke.MethodHandles;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.genericsystem.api.core.Snapshot;
import org.genericsystem.common.Generic;
import org.genericsystem.common.Root;
import org.genericsystem.cv.docPattern.OCRPlasty;
import org.genericsystem.cv.model.Doc;
import org.genericsystem.cv.model.Doc.DocInstance;
import org.genericsystem.cv.model.DocClass;
import org.genericsystem.cv.model.DocClass.DocClassInstance;
import org.genericsystem.cv.model.ImgFilter;
import org.genericsystem.cv.model.ImgFilter.ImgFilterInstance;
import org.genericsystem.cv.model.MeanLevenshtein;
import org.genericsystem.cv.model.ModelTools;
import org.genericsystem.cv.model.Score;
import org.genericsystem.cv.model.Score.ScoreInstance;
import org.genericsystem.cv.model.ZoneGeneric;
import org.genericsystem.cv.model.ZoneGeneric.ZoneInstance;
import org.genericsystem.cv.model.ZoneText;
import org.genericsystem.cv.model.ZoneText.ZoneTextInstance;
import org.genericsystem.kernel.Engine;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ComputeBestTextPerZone {
private static final String gsPath = System.getenv("HOME") + "/genericsystem/gs-cv_model3/";
private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
public static void main(String[] mainArgs) {
final Root engine = FillModelWithData.getEngine(gsPath);
engine.newCache().start();
compute(engine);
engine.close();
}
public static void compute(Root engine) {
final String docType = "id-fr-front";
compute(engine, docType);
}
@SuppressWarnings({ "unchecked", "rawtypes" })
public static void compute(Root engine, String docType) {
Generic currentDocClass = engine.find(DocClass.class).getInstance(docType);
List<DocInstance> docInstances = (List) currentDocClass.getHolders(engine.find(Doc.class)).toList();
for (DocInstance docInstance : docInstances) {
computeOneFile(engine, docInstance, docType);
}
}
public static void computeOneFile(String filename, String docType) {
final Engine engine = new Engine(gsPath, Doc.class, ImgFilter.class, ZoneGeneric.class, ZoneText.class, Score.class, MeanLevenshtein.class);
engine.newCache().start();
computeOneFile(engine, filename, docType);
engine.close();
}
public static void computeOneFile(String filename) {
final Engine engine = new Engine(gsPath, Doc.class, ImgFilter.class, ZoneGeneric.class, ZoneText.class, Score.class, MeanLevenshtein.class);
final String docType = "id-fr-front";
engine.newCache().start();
computeOneFile(engine, filename, docType);
engine.close();
}
public static void computeOneFile(Root engine, String filename, String docType) {
Doc doc = engine.find(Doc.class);
Generic currentDocClass = engine.find(DocClass.class).getInstance(docType);
DocInstance docInstance = doc.getDoc(filename, (DocClassInstance) currentDocClass);
computeOneFile(engine, docInstance, docType);
}
// TODO: pass docClassInstance as a parameter instead of docType?
@SuppressWarnings({ "unchecked", "rawtypes" })
public static void computeOneFile(Root engine, DocInstance docInstance, String docType) {
Generic currentDocClass = engine.find(DocClass.class).getInstance(docType);
ImgFilter imgFilter = engine.find(ImgFilter.class);
ZoneText zoneText = engine.find(ZoneText.class);
Snapshot<ZoneInstance> zoneInstances = (Snapshot) currentDocClass.getHolders(engine.find(ZoneGeneric.class));
ImgFilterInstance realityInstance = imgFilter.getImgFilter("reality");
ImgFilterInstance bestInstance = imgFilter.setImgFilter("best");
logger.debug("Processing doc: {}", docInstance.getValue());
for (ZoneInstance zoneInstance : zoneInstances) {
logger.debug("Zone n°{}", zoneInstance.getValue());
ZoneTextInstance realTextInstance = zoneText.getZoneText(docInstance, zoneInstance, realityInstance);
if (realTextInstance == null || realTextInstance.getValue().toString().isEmpty()) {
// If not supervised, compute the best text
String bestText = computeBestTextOcrPlasty(engine, docInstance, docType, zoneInstance);
if (null != bestText) {
zoneText.setZoneText(bestText, docInstance, zoneInstance, bestInstance).setZoneTimestamp(ModelTools.getCurrentDate());
} else {
logger.debug("No OCR data found for {}", docInstance.getValue());
}
} else {
// If supervised, set the supervised text to best
zoneText.setZoneText(realTextInstance.getValue().toString(), docInstance, zoneInstance, bestInstance).setZoneTimestamp(ModelTools.getCurrentDate());
}
engine.getCurrentCache().flush();
}
}
@SuppressWarnings({ "unchecked", "rawtypes" })
private static String computeBestTextOcrPlasty(Root engine, DocInstance docInstance, String docType, ZoneInstance zoneInstance) {
ImgFilter imgFilter = engine.find(ImgFilter.class);
ZoneText zoneText = engine.find(ZoneText.class);
Snapshot<ImgFilterInstance> imgFilterInstances = (Snapshot) imgFilter.getInstances().filter(f -> !"reality".equals(f.getValue()) && !"best".equals(f.getValue()));
List<String> ocrTexts = new ArrayList<>();
imgFilterInstances.forEach(imgFilterInstance -> {
ZoneTextInstance zti = zoneText.getZoneText(docInstance, zoneInstance, imgFilterInstance);
if (zti == null) {
logger.debug("No text found for {} => zone n°{}, {}", docInstance.getValue(), zoneInstance.getValue(), imgFilterInstance.getValue());
} else {
String text = zti.getValue().toString();
ocrTexts.add(text);
}
});
String bestText = OCRPlasty.ocrPlasty(ocrTexts);
logger.debug("Best text: {}", bestText);
return bestText;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
private static String computeBestText(Root engine, DocInstance docInstance, String docType, ZoneInstance zoneInstance) {
// TODO: deal with the case where a field must be left blank
ImgFilter imgFilter = engine.find(ImgFilter.class);
ZoneText zoneText = engine.find(ZoneText.class);
Score score = engine.find(Score.class);
Snapshot<ImgFilterInstance> imgFilterInstances = (Snapshot) imgFilter.getInstances().filter(f -> !"reality".equals(f.getValue()) && !"best".equals(f.getValue()));
// Map containing the distinct OCR texts as a key, and the names of the imgFilters that gave this OCR
Map<String, List<String>> ocrResults = new ConcurrentHashMap<>();
for (ImgFilterInstance imgFilterInstance : imgFilterInstances) {
ZoneTextInstance zti = zoneText.getZoneText(docInstance, zoneInstance, imgFilterInstance);
if (zti == null) {
// TODO case where zti doesn't exist == filter has not been applied
logger.debug("No text found for {} => zone n°{}, {}", docInstance.getValue(), zoneInstance.getValue(), imgFilterInstance.getValue());
} else {
String text = zti.getValue().toString();
List<String> filters = ocrResults.get(text);
if (filters == null) {
filters = new ArrayList<>();
}
filters.add(imgFilterInstance.getValue().toString());
ocrResults.put(text, filters);
}
}
// Map containing each distinct OCR text as key, and the corresponding ponderation as a value (i.e., the sum of the individual scores of each filter that gave this string)
Map<String, Float> ocrElection = new ConcurrentHashMap<>();
ocrResults.entrySet().forEach(entry -> {
Float ocrWeight = 0f;
for (String filter : entry.getValue()) {
ScoreInstance scoreInstance = score.getScore(zoneInstance, imgFilter.getImgFilter(filter));
if (scoreInstance == null) {
logger.debug("No score found for zone n°{} and filter {}", zoneInstance.getValue(), filter);
} else {
ocrWeight += (Float) scoreInstance.getValue();
}
}
ocrElection.put(entry.getKey(), ocrWeight);
});
if (null != ocrElection && !ocrElection.isEmpty()) {
return ocrElection.entrySet().stream().max(Map.Entry.comparingByValue()).get().getKey();
} else {
return null;
}
}
}