-
Notifications
You must be signed in to change notification settings - Fork 5
/
Fields.java
286 lines (245 loc) · 9.36 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
package org.genericsystem.cv.retriever;
import java.lang.invoke.MethodHandles;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ThreadLocalRandom;
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.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.core.Size;
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) {
stream().forEach(field -> field.draw(display, homography, color, thickness));
}
public void drawFieldsOnStabilized(Img stabilized) {
stream().forEach(f -> f.drawRect(stabilized, f.getDeadCounter() == 0 ? new Scalar(0, 255, 0) : new Scalar(0, 0, 255), 1));
}
// 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 List<Field> getRoots() {
return fields.stream().filter(field -> field.isOrphan()).collect(Collectors.toList());
}
public void consolidate(Img img) {
RectMerger rm = new RectMerger(img);
List<GSRect> rects = rm.mergeRectsList();
// Increment the dead counter of each field
fields.forEach(Field::incrementDeadCounter);
mergeRects(rects, img.width(), img.height());
removeDeadTrees();
cleanRelationships();
}
private static class RectMerger {
private RectDetector rd;
public RectMerger(Img img) {
this.rd = new RectDetector(img);
}
public List<GSRect> mergeRectsList() {
List<GSRect> rects = RectToolsMapper.rectToGSRect(rd.getRects(200, 11, 3, new Size(11, 3)));
List<GSRect> children = RectToolsMapper.rectToGSRect(rd.getRects(40, 17, 3, new Size(7, 3)));
// Remove the duplicates of rects in children
children.removeIf(child -> rects.stream().anyMatch(parent -> RectangleTools.isInCluster(parent, child, 0.1)));
rects.addAll(children);
return rects;
}
}
// TODO re-arrange the fields to match all the constraints (no overlap, children strictly contained in parents)
private void cleanRelationships() {
// // 1. Each tree must validate the constraints
// for (Field parent : getRoots()) {
// if (!parent.checkConstraints()) {
// System.err.println(parent.recursiveToString());
// parent.repairTree();
// }
// }
// // 2. Two overlapping trees can't coexist and must be merged
}
private void mergeRects(List<GSRect> rects, int width, int height) {
for (GSRect rect : rects) {
Field match = findMatch(rect, 0.4, width, height);
if (match != null)
updateNode(rect, match);
else
createNode(rect, findPotentialParent(rect, getRoots()));
}
}
private Field findPotentialParent(GSRect rect, List<Field> roots) {
for (Field root : getRoots()) {
Field parent = findPotentialParent(rect, root);
if (parent != null)
return parent;
}
return null;
}
private Field findPotentialParent(GSRect rect, Field root) {
if (rect.equals(rect.isInsider(root.getRect())))
return null;
for (Field child : root.getChildren()) {
Field candidate = findPotentialParent(rect, child);
if (candidate != null)
return candidate;
}
return root;
}
private void createNode(GSRect rect, Field parent) {
logger.info("Creating a new node for {}", rect);
Field f = new Field(rect);
if (parent != null)
f.setParent(parent);
fields.add(f);
}
private void updateNode(GSRect rect, Field field) {
logger.info("Updating node {} with {}", field.getRect(), rect);
field.updateRect(rect);
field.resetDeadCounter();
}
private void removeNode(Field field) {
logger.info("Removing node: {}", field.getRect());
fields.remove(field);
}
private void removeDeadTrees() {
Predicate<Field> predicate = f -> !f.isLocked() && f.getDeadCounter() >= MAX_DELETE_UNMERGED;
getRoots().stream().filter(field -> isDeadTree(field, predicate)).flatMap(field -> listTree(field).stream()).forEach(this::removeNode);
}
private boolean isDeadTree(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 (!isDeadTree(child, predicate)) // Return false if 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 List<Field> listTree(Field root) {
List<Field> res = new ArrayList<>();
res.add(root);
for (Field child : root.getChildren())
res.addAll(listTree(child));
return res;
}
private Field findMatch(GSRect rect, double areaOverlap, int width, int height) {
GSRect frameRect = new GSRect(0,0,width, height);
List<Field> matches = fields.stream().filter(f -> rect.inclusiveArea(f.getRect().getIntersection(frameRect))>areaOverlap).collect(Collectors.toList());
if(matches.isEmpty()){
System.out.println("AAAAAAAAAAAAAAA");
return null;
}
if(matches.size()>1){
StringBuilder sb = new StringBuilder(matches.size()+ " matches were detected.\n");
for(Field field : matches){
sb.append(field+"\n");
}
//throw new IllegalStateException(sb.toString());
logger.warn(sb.toString());
}
System.out.println("ZZZZZZZZZZZZZZZZZZZ");
return matches.get(0);
}
// private Field findMatch(GSRect rect, double eps, int width, int height) {
// List<Field> matches = findPossibleMatches(rect, eps);
// // Remove the false positives
// matches.removeIf(f -> f.getRect().inclusiveArea(rect) <= MIN_OVERLAP / 10);
// if (matches.isEmpty())
// return null;
// // If there is more than one match, select only the best
// if (matches.size() > 1) {
// logger.debug("Multiple matches ({}), removing false positives", matches.size());
// // Remove the overlaps with less than 10% common area
// matches.removeIf(f -> f.getRect().inclusiveArea(rect) < MIN_OVERLAP);
// if (matches.size() > 1) {
// logger.warn("Still multiple matches ({}), selecting the maximum overlap", matches.size());
// matches = Arrays.asList(matches.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 matches.isEmpty() ? null : matches.get(0);
//
// }
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();
}
}
}