-
Notifications
You must be signed in to change notification settings - Fork 5
/
Fields.java
495 lines (446 loc) · 17.2 KB
/
Fields.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
package org.genericsystem.cv.retriever;
import java.lang.invoke.MethodHandles;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ThreadLocalRandom;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import org.genericsystem.cv.Img;
import org.genericsystem.cv.utils.ParallelTasks;
import org.genericsystem.cv.utils.Ransac;
import org.genericsystem.cv.utils.Ransac.Model;
import org.genericsystem.cv.utils.RectToolsMapper;
import org.genericsystem.reinforcer.tools.GSPoint;
import org.genericsystem.reinforcer.tools.GSRect;
import org.genericsystem.reinforcer.tools.RectangleTools;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.Point;
import org.opencv.core.Scalar;
import org.opencv.utils.Converters;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Fields extends AbstractFields<Field> {
private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
private static final int MAX_DELETE_UNMERGED = 5;
private static final int OCR_TIMEOUT = 50;
private static final double MIN_OVERLAP = 0.2;
public void reset() {
displayFieldsTree();
fields = new ArrayList<>();
}
@Override
public void drawOcrPerspectiveInverse(Img display, Mat homography, Scalar color, int thickness) {
// Only draw orphan fields
stream().filter(field -> field.isOrphan()).forEach(field -> field.drawOcrPerspectiveInverse(display, homography, color, thickness));
}
public void drawLockedFields(Img display, Mat homography) {
fields.forEach(field -> field.drawLockedField(display, homography));
fields.stream().filter(field -> !field.isOrphan()).forEach(field -> field.drawRect(display, field.getRectPointsWithHomography(homography), new Scalar(255, 128, 255), 1));
}
public void displayFieldsTree() {
StringBuffer sb = new StringBuffer();
sb.append("\n").append("--- FIELDS ---").append("\n");
fields.forEach(field -> {
sb.append(field.recursiveToString());
});
sb.append("\n").append("--- /FIELDS ---").append("\n");
System.out.println(sb.toString());
}
public void merge(RectDetector rectDetector) {
// Get the lists of rectangles
List<GSRect> rects = RectToolsMapper.rectToGSRect(rectDetector.getFilteredRects(1d));
List<GSRect> children = RectToolsMapper.rectToGSRect(rectDetector.getFilteredRects2(1d));
// Remove the duplicates of rects in children
rects.forEach(rect -> {
Iterator<GSRect> it = children.iterator();
while (it.hasNext()) {
if (RectangleTools.isInCluster(rect, it.next(), 0.1))
it.remove();
}
});
// Increment the dead counter of each field
fields.forEach(f -> f.incrementDeadCounter());
// Loop over all the rectangles and try to find any matching field
for (GSRect rect : rects) {
List<Field> matches = findPossibleMatches(rect, 0.1);
matches = cleanMatches(matches, rect);
if (!matches.isEmpty()) {
matches.forEach(f -> {
logger.info(formatLog(f, rect));
f.registerShift(RectangleTools.getShift(f.getRect(), rect));
f.updateRect(rect);
f.resetDeadCounter();
});
} else {
logger.info("No match for {}. Creating a new Field", rect);
Field f = new Field(rect);
fields.add(f);
}
}
mergeChildren(children);
removeUnmergedFields();
adjustUnmergedParents();
}
private void mergeChildren(List<GSRect> childrenRect) {
List<GSRect> list = mergeKnownChildren(childrenRect);
mergeNewChildren(list);
}
private List<GSRect> mergeKnownChildren(List<GSRect> childrenRect) {
if (childrenRect.isEmpty())
return Collections.emptyList();
fields.stream().filter(field -> field.hasChildren()).forEach(parent -> {
// Get the list of children for all parent fields
Set<Field> children = parent.getChildren();
// For each child, try to find a match in the list of childrenRect
children.forEach(child -> {
List<GSRect> matchingRects = childrenRect.stream().filter(rect -> child.isClusteredWith(rect, 0.1)).collect(Collectors.toList());
if (!matchingRects.isEmpty()) {
if (matchingRects.size() > 1) {
// Reduce to one element
matchingRects = Arrays.asList(matchingRects.stream().max((r1, r2) -> {
double area1 = r1.inclusiveArea(child.getRect());
double area2 = r2.inclusiveArea(child.getRect());
return Double.compare(area1, area2);
}).orElseThrow(IllegalStateException::new));
}
// Update the coordinates of the child with the new rect
GSRect newRect = matchingRects.get(0);
logger.warn(formatLog(child, newRect));
child.registerShift(RectangleTools.getShift(child.getRect(), newRect));
child.updateRect(newRect);
child.resetDeadCounter();
// Delete the rect from the children list
childrenRect.remove(newRect);
}
});
});
return childrenRect;
}
private void mergeNewChildren(List<GSRect> childrenRect) {
if (childrenRect.isEmpty())
return;
List<GSRect> fieldsRects = fields.stream().map(f -> f.getRect()).collect(Collectors.toList());
for (int i = 0; i < fieldsRects.size(); ++i) {
Field parent = fields.get(i);
List<GSRect> possibleChildrenRects = findChildrenRects(childrenRect, parent.getRect(), 0.95);
if (!possibleChildrenRects.isEmpty()) {
logger.info("Found possible child(ren) for {}: {}", parent.getRect(), possibleChildrenRects);
possibleChildrenRects.forEach(childRect -> {
List<Field> matches = findPossibleMatches(childRect, 0.1);
if (!matches.isEmpty()) {
matches.forEach(f -> {
logger.info(formatLog(f, childRect));
f.registerShift(RectangleTools.getShift(f.getRect(), childRect));
setLinks(parent, f);
f.updateRect(childRect);
f.resetDeadCounter();
});
} else {
logger.info("No match for child {}. Creating a new Field", childRect);
Field f = new Field(childRect);
setLinks(parent, f);
fields.add(f);
}
childrenRect.remove(childRect);
});
}
}
}
private List<GSRect> findChildrenRects(List<GSRect> children, GSRect putativeParent, double minArea) {
return children.stream().filter(child -> RectangleTools.commonArea(child, putativeParent)[0] > minArea).collect(Collectors.toList());
}
private void setLinks(Field parent, Field child) {
if (!child.isOrphan() && !child.getParent().equals(parent))
logger.error("\nchild's parent:\n{}\nparent:\n{}", child.getParent(), parent);
if (!parent.equals(child)) {
child.setParent(parent);
parent.addChildIfNotPresent(child);
}
}
private void adjustUnmergedParents() {
fields.stream().filter(field -> field.hasChildren() && field.getDeadCounter() != 0).forEach(field -> {
List<double[]> shifts = field.getShifts();
if (!shifts.isEmpty()) {
double[] mean = getMean(shifts);
logger.debug("Mean before Ransac: {}", Arrays.toString(mean));
if (shifts.size() > 3) {
try {
Ransac<double[]> ransac = new Ransac<>(shifts, getModelProvider(), 3, 50, 2, shifts.size() * 2 / 3);
List<double[]> newShifts = ransac.getBestDataSet().values().stream().collect(Collectors.toList());
mean = getMean(newShifts);
logger.debug("Mean after Ransac: {}", Arrays.toString(mean));
} catch (Exception e) {
logger.info("Unable to compute ransac on shifts for {}", field.getRect());
}
}
GSRect rect = field.getRect();
GSPoint tl = new GSPoint(rect.tl().getX() - mean[0], rect.tl().getY() - mean[1]);
GSPoint br = new GSPoint(rect.br().getX() - mean[2], rect.br().getY() - mean[3]);
field.updateRect(new GSRect(tl, br));
logger.info("Updated rect from {} to {}", rect, field.getRect());
field.clearShifts();
}
});
}
private void removeUnmergedFields() {
Predicate<Field> predicate = f -> !f.isLocked() && f.getDeadCounter() >= MAX_DELETE_UNMERGED;
// Clean the fields recursively from the 'root' of each tree
Set<Field> removes = new HashSet<>();
fields.stream().filter(field -> field.isOrphan()).forEach(field -> {
if (deadTree(field, predicate))
removes.addAll(killTree(field));
});
removes.forEach(field -> {
System.out.println("removing: " + field.getRect());
fields.remove(field);
});
// fields.stream().filter(field -> field.isOrphan()).forEach(field -> removes.addAll(deleteRecursive(field, predicate)));
fields.stream().filter(field -> field.isOrphan()).forEach(field -> removes.addAll(alternateDeleteRecursive(field, predicate)));
removes.forEach(field -> {
if (field.hasChildren())
field.getChildren().forEach(child -> child.setParent(null));
if (!field.isOrphan())
field.getParent().removeChild(field);
fields.remove(field);
});
}
private boolean deadTree(Field root, Predicate<Field> predicate) {
if (!root.hasChildren()) // Single element in the tree, use the predicate
return predicate.test(root);
for (Field child : root.getChildren())
if (!deadTree(child, predicate)) // Return false one of the element does not match the predicate
return false;
return true; // If false was not returned at this stage, the tree is dead
}
private Set<Field> killTree(Field root) {
Set<Field> res = new HashSet<>();
res.add(root);
if (root.hasChildren())
for (Field child : root.getChildren())
res.addAll(killTree(child));
return res;
}
private Set<Field> deleteRecursive(Field field, Predicate<Field> predicate) {
Set<Field> removes = new HashSet<>();
if (predicate.test(field)) { // FIXME: this test should be moved downwards
if (field.hasChildren()) {
// Call the method recursively
field.getChildren().forEach(f -> {
removes.addAll(deleteRecursive(f, predicate));
});
if (!field.isOrphan()) {
// attempt to merge text from child in parent (only if parent is not going to be deleted)
// siblings?
}
// add remove here to delete parent?
} else {
if (!field.isOrphan()) {
// attempt to merge text from child in parent (only if parent is not going to be deleted)
if (!predicate.test(field.getParent())) {
// check siblings to see if they need to be removed
if (field.hasSiblings()) {
// if all the siblings are removed, merge them all in the parent
// Set<Field> siblings = field.getSiblings();
} else {
// attempt to merge directly in parent
}
} // else parent gets deleted, so we don't care
}
}
removes.add(field);
}
return removes;
}
/**
* 1. If a field has children, it can't be removed <br>
* 2. If a field has a parent, it can't be removed <br>
* 3. A parent always contains its children <br>
* 4. Children are always enclosed in their parent <br>
*/
private Set<Field> alternateDeleteRecursive(Field field, Predicate<Field> predicate) {
Set<Field> removes = new HashSet<>();
if (field.hasChildren()) {
field.getChildren().forEach(f -> {
removes.addAll(alternateDeleteRecursive(f, predicate));
});
if (predicate.test(field)) {
// Update the field's coordinates to encompass all the children
List<GSRect> rects = field.getChildren().stream().map(f -> f.getRect()).collect(Collectors.toList());
GSRect union = rects.stream().reduce(rects.get(0), (r, total) -> r.getUnion(total));
field.updateRect(union);
}
} else {
if (field.isOrphan()) {
if (predicate.test(field))
removes.add(field);
} else {
if (predicate.test(field)) {
double[] area = RectangleTools.commonArea(field.getRect(), field.getParent().getRect());
if (area[0] < 0.90) // Need to adjust the size of the child
logger.error("area mismatch: {}", Arrays.toString(area));
// else
// logger.info("area ok: {}", Arrays.toString(area));
}
}
}
return removes;
}
// TODO: compare the consolidated text before merging?
public void removeOverlaps() {
for (Field field : new ArrayList<>(fields)) {
List<Field> matches = findPossibleMatches(field.getRect(), 0.1, 3).stream().filter(f -> !field.equals(f)).collect(Collectors.toList());
if (!matches.isEmpty()) {
logger.warn("Found {} matche(s) for {}", matches.size(), field.getRect());
for (Field f : matches) {
logger.warn("{}", f.getRect());
if (field.getRect().area() < f.getRect().area()) {
if (!f.isLocked()) {
boolean ok = fields.remove(f);
// System.err.println("removed field (" + ok + ")" + "\n" + f);
}
}
}
}
matches = findContainingFields(field).stream().filter(f -> !field.equals(f)).collect(Collectors.toList());
if (!matches.isEmpty()) {
logger.warn("Found {} inside {} other field(s)", field.getRect(), matches.size());
for (Field f : matches) {
logger.warn("{}", f.getRect());
if (!f.isLocked()) {
boolean ok = fields.remove(f);
// System.err.println("removed field (" + ok + ")" + "\n" + f);
}
}
}
}
}
private String formatLog(Field field, GSRect rect) {
double mergeArea = field.getRect().inclusiveArea(rect);
StringBuffer sb = new StringBuffer();
sb.append(String.format("Merging %s with %s (%.1f%% common area)", field.getRect(), rect, mergeArea * 100));
if (field.getConsolidated() != null)
sb.append(String.format(" -> %s", field.getConsolidated()));
return sb.toString();
}
private List<Field> cleanMatches(List<Field> matches, GSRect rect) {
List<Field> copy = new ArrayList<>(matches);
// Remove the false positives
copy = copy.stream().filter(f -> f.getRect().inclusiveArea(rect) > MIN_OVERLAP / 10).collect(Collectors.toList());
if (copy.isEmpty()) {
return Collections.emptyList();
} else {
// If there is more than one match, select only the best
if (copy.size() > 1) {
logger.debug("Multiple matches ({}), removing false positives", copy.size());
// Remove the overlaps with less than 10% common area
copy = copy.stream().filter(f -> f.getRect().inclusiveArea(rect) >= MIN_OVERLAP).collect(Collectors.toList());
if (copy.size() > 1) {
logger.warn("Still multiple matches ({}), selecting the maximum overlap", copy.size());
copy = Arrays.asList(copy.stream().max((f1, f2) -> {
double area1 = f1.getRect().inclusiveArea(rect);
double area2 = f2.getRect().inclusiveArea(rect);
return Double.compare(area1, area2);
}).orElseThrow(IllegalStateException::new));
}
}
return copy;
}
}
public void restabilizeFields(Mat homography) {
long start = System.nanoTime();
fields.forEach(field -> field.updateRect(findNewRect(field.getRect(), homography)));
long stop = System.nanoTime();
logger.info("Restabilized {} fields in {} ms", fields.size(), String.format("%.3f", ((double) (stop - start)) / 1_000_000));
}
private GSRect findNewRect(GSRect rect, Mat homography) {
List<Point> originals = RectToolsMapper.gsPointToPoint(Arrays.asList(rect.tl(), rect.br()));
List<GSPoint> points = RectToolsMapper.pointToGSPoint(restabilize(originals, homography));
return new GSRect(points.get(0), points.get(1));
}
private List<Point> restabilize(List<Point> originals, Mat homography) {
Mat original = Converters.vector_Point2d_to_Mat(originals);
Mat results = new Mat();
Core.perspectiveTransform(original, results, homography);
List<Point> res = new ArrayList<>();
Converters.Mat_to_vector_Point2d(results, res);
return res;
}
@Override
public void performOcr(Img rootImg) {
if (size() <= 0)
return;
long TS = System.currentTimeMillis();
while (System.currentTimeMillis() - TS <= OCR_TIMEOUT) {
runParallelOcr(rootImg);
// runSequentialOcr(rootImg);
}
}
private void runSequentialOcr(Img rootImg) {
int idx = ThreadLocalRandom.current().nextInt(size());
Field f = fields.get(idx);
if (!f.isLocked())
f.ocr(rootImg);
}
private void runParallelOcr(Img rootImg) {
ParallelTasks tasks = new ParallelTasks();
int limit = tasks.getCounter() * 2;
Set<Integer> indexes = new HashSet<>();
while (indexes.size() < limit && indexes.size() < size()) {
int idx = ThreadLocalRandom.current().nextInt(size());
if (indexes.add(idx)) {
Field f = fields.get(idx);
if (!f.isLocked())
tasks.add(() -> f.ocr(rootImg));
}
}
try {
tasks.run();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private Function<Collection<double[]>, Model<double[]>> getModelProvider() {
return datas -> {
double[] mean = getMean(datas);
return new Model<double[]>() {
@Override
public double computeError(double[] data) {
double error = 0;
for (int i = 0; i < mean.length; ++i)
error += Math.pow(mean[i] - data[i], 2);
return Math.sqrt(error);
}
@Override
public double computeGlobalError(List<double[]> datas, Collection<double[]> consensusDatas) {
double globalError = 0d;
for (double[] data : datas)
globalError += Math.pow(computeError(data), 2);
return Math.sqrt(globalError) / datas.size();
}
@Override
public Object[] getParams() {
return new Object[] { mean };
}
};
};
}
private double[] getMean(Collection<double[]> values) {
if (values.isEmpty())
return null;
double[] mean = new double[values.stream().findFirst().get().length];
for (double[] value : values)
for (int i = 0; i < mean.length; ++i)
mean[i] += value[i];
for (int i = 0; i < mean.length; ++i)
mean[i] /= values.size();
return mean;
}
}