-
Notifications
You must be signed in to change notification settings - Fork 5
/
Fields.java
163 lines (144 loc) · 5.8 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
package org.genericsystem.cv.classifier;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import org.genericsystem.cv.Img;
import org.genericsystem.cv.utils.RectangleTools;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfPoint2f;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.utils.Converters;
@SuppressWarnings({ "unchecked", "rawtypes" })
public class Fields extends AbstractFields {
private Mat lastHomography;
private Mat lastRotation;
public void scanNewRects(Img display, List<Rect> newRects) {
if (!fields.isEmpty())
oldFields = fields;
fields = newRects.stream().map(Field::new).collect(Collectors.toList());
randomOcrStream().forEach(f -> ((Field) f).ocr(display));
}
public void merge() {
if (lastHomography != null) {
oldFields = (List) restabilizeFields();
System.out.println("oldFields transformed (" + oldFields.size() + ")");
for (int i = 0; i < fields.size(); i++) {
Field currentField = (Field) fields.get(i);
if (true || currentField.isConsolidated()) {
// String currentText = currentField.getConsolidated().get();
List<Field> matches = (List) findMatchingFieldsWithConfidence(currentField, 0.6); // Compare with fields in oldFields
System.out.println("matches.size() = " + matches.size());
if (!matches.isEmpty()) {
// matches.removeIf(f -> {
// // Remove the element from the list of matches if it was not consolidated, or if the text was not similar enough
// Optional<String> optional = f.getConsolidated();
// return optional.map(s -> !s.equals(currentText) && StringCompare.compare(s, currentText, SIMILARITY.COSINE_CHAR) <= MIN_SIMILARITY).orElse(true);
// });
System.out.println("Merge : " + currentField.getConsolidated());
matches.forEach(f -> currentField.merge(f));
boolean ok = oldFields.removeAll(matches);
System.out.println("ok: " + ok);
} else {
System.out.println("No matches found");
}
} else {
System.out.print(" . ");
}
}
// At this stage, add all the remaining fields still in oldFields
fields.addAll(oldFields);
}
// List<Field> oldFields = (List) fields;
// fields = rects.stream().map(Field::new).collect(Collectors.toList());
//
// if (lastHomography != null) {
// List<Field> virtualField = getVirtualFields(oldFields);
//
// for (int index = 0; index < virtualField.size(); index++) {
// if (virtualField.get(index).isConsolidated()) {
// Field virtualOldField = virtualField.get(index);
// List<Field> matches = (List) findMatchingFieldsWithConfidence(virtualOldField, 0.95);
//
// if (!matches.isEmpty()) {
// // TODO: if multiple matches are found, it might be better to remove the extra fields (NMS?)
// System.out.println("Merge : " + virtualOldField.getConsolidated());
// matches.forEach(f -> f.merge(virtualOldField));
// } else {
// System.out.println("No exact matches found");
// List<Field> containings = (List) findContainingFields(virtualOldField);
// if (!containings.isEmpty()) {
// // Check whether virtualOldField's text is found in the containing fields
// System.out.println("Found new fields containing the virtual old field");
// } else {
// System.out.println("No containing fields were found");
// List<Field> containeds = (List) findContainedFields(virtualOldField);
// if (!containeds.isEmpty()) {
// System.out.println("Found new fields contained in the virtual old field");
// } else {
// System.out.println("No contained fields were found");
// }
// fields.add(virtualOldField);
// }
// }
// }
// }
// }
}
@Override
protected Field getIntersection(AbstractField field1, AbstractField field2) {
Rect rect1 = field1.getRect();
Rect rect2 = field2.getRect();
Rect intersect = RectangleTools.getIntersection(rect1, rect2).orElseThrow(() -> new IllegalArgumentException("No intersecting rectangle was found"));
Field intersection = new Field(intersect);
intersection.merge(Arrays.asList(field1, field2));
return intersection;
}
@Override
protected Field getUnion(AbstractField field1, AbstractField field2) {
Rect rect1 = field1.getRect();
Rect rect2 = field2.getRect();
Field union = new Field(RectangleTools.getUnion(rect1, rect2));
union.merge(Arrays.asList(field1, field2));
return union;
}
private List<Field> restabilizeFields() {
// Apply the homography + rotation to the oldFields
List<Rect> virtualRects = oldFields.stream().map(AbstractField::getRect).map(rect -> findNewRect(rect)).collect(Collectors.toList());
return IntStream.range(0, oldFields.size()).mapToObj(i -> {
Field f = new Field(virtualRects.get(i));
f.merge(oldFields.get(i));
return f;
}).collect(Collectors.toList());
}
private Rect findNewRect(Rect rect) {
List<Point> points = restabilize(Arrays.asList(rect.tl(), rect.br()));
return new Rect(points.get(0), points.get(1));
}
private List<Point> restabilize(List<Point> originals) {
Mat original = Converters.vector_Point2f_to_Mat(originals);
MatOfPoint2f results = new MatOfPoint2f();
Core.perspectiveTransform(original, results, lastHomography);
MatOfPoint2f rotated = new MatOfPoint2f();
Core.transform(results, rotated, lastRotation);
List<Point> res = rotated.toList();
original.release();
results.release();
rotated.release();
return res;
}
public void storeLastHomography(Mat homography) {
this.lastHomography = homography;
}
public void storeLastRotation(Mat rotation) {
this.lastRotation = rotation;
}
@Override
public void consolidateOcr(Img rootImg) {
long TS = System.currentTimeMillis();
// XXX Replace needOCR with a random index
stream().filter(AbstractField::needOcr).filter(f -> System.currentTimeMillis() - TS <= 200).forEach(f -> f.ocr(rootImg));
}
}