-
Notifications
You must be signed in to change notification settings - Fork 5
/
Field.java
274 lines (227 loc) · 7.08 KB
/
Field.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
package org.genericsystem.cv.retriever;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import org.genericsystem.cv.Img;
import org.genericsystem.reinforcer.tools.GSRect;
import org.opencv.core.Mat;
import org.opencv.core.Scalar;
public class Field extends AbstractField {
private Field parent;
private List<Field> children;
private static final int LABELS_SIZE_THRESHOLD = 15;
private static final double CONFIDENCE_THRESHOLD = 0.92;
private boolean locked = false;
public Field(GSRect rect) {
super(rect);
this.parent = null;
this.children = new ArrayList<>();
checkConstraints();
}
public String recursiveToString() {
StringBuffer sb = new StringBuffer();
recursiveToString(this, sb, 0);
sb.append("\n");
return sb.toString();
}
private void recursiveToString(Field field, StringBuffer sb, int depth) {
if (depth > 8)
return;
sb.append("depth: ").append(depth).append(": ").append(field.getRect());
if (field.isConsolidated())
sb.append(" -> ").append(field.getConsolidated());
if (!field.getChildren().isEmpty()) {
depth++;
for (Field child : field.getChildren()) {
sb.append("\n");
for (int i = 0; i < depth; ++i)
sb.append(" ");
recursiveToString(child, sb, depth);
}
}
}
@Override
public void ocr(Img rootImg) {
super.ocr(rootImg);
if (attempts <= 3 || attempts % 5 == 0)
consolidateOcr(false);
}
@Override
public void resetDeadCounter() {
super.resetDeadCounter();
setFinal();
}
public void draw(Img display, Mat homography, Scalar color, int thickness) {
Scalar scalar = selectColor(color);
if (needRect())
drawRect(display, getRectPointsWithHomography(homography), deadCounter == 0 ? scalar : new Scalar(0, 0, 255), thickness);
// if (needText())
// drawText(display, getRectPointsWithHomography(homography), new Scalar(0, 64, 255), thickness);
}
private Scalar selectColor(Scalar defaultColor) {
// if (drawAsTruncated())
// return new Scalar(0, 0, 51);
if (drawAsLocked())
return new Scalar(255, 172, 0);
if (drawAsChild())
return new Scalar(255, 0, 0);
return defaultColor;
}
private boolean drawAsChild() {
return !isOrphan() && parent.deadCounter != 0;
}
private boolean drawAsLocked() {
return (deadCounter == 0 && isOrphan()) || (!isOrphan() && parent.getDeadCounter() != 0) ? this.locked : false;
}
// private boolean drawAsTruncated() {
// return this.truncated;
// }
private boolean needRect() {
return !isOrphan() && parent.getDeadCounter() == 0 ? false : true;
}
// private boolean needText() {
// return deadCounter == 0 && !truncated && needRect();
// }
public void setFinal() {
if (!locked)
if (getLabelsSize() > LABELS_SIZE_THRESHOLD && getConfidence() > CONFIDENCE_THRESHOLD && isOrphan())
this.locked = true;
}
public boolean isLocked() {
return locked;
}
public boolean addChildIfNotPresent(Field child) {
return children.add(child);
}
public boolean removeChild(Field child) {
return children.remove(child);
}
public boolean containsChild(Field field) {
return children.stream().anyMatch(child -> child.getRect().inclusiveArea(field.getRect()) > 0.95);
}
public boolean addChildren(Collection<Field> children) {
return this.children.addAll(children);
}
public void setChildren(List<Field> children) {
this.children = children;
}
public List<Field> getChildren() {
return children;
}
public Field getParent() {
return parent;
}
public List<Field> getSiblings() {
if(getParent()==null)
return Collections.emptyList();
return getParent().getChildren().stream().filter(child -> this!=child).collect(Collectors.toList());
}
public void setParent(Field parent) {
if (parent == null) {
if (this.parent == null)
return;
logger.info("Resetting parent for field: {}", this.rect);
this.parent.removeChild(this); // TODO can this method be called here, or should it be handled separately?
this.parent = null;
} else {
if (this.isOverlappingSiblings()) {
if (this.parent != null)
logger.error("Child already has a parent:\n{}\nParent:\n{}", this, this.parent);
this.parent = parent;
if (parent.addChildIfNotPresent(this)) // TODO can this method be called here, or should it be handled separately?
logger.info("Added {} as parent of {}", parent.getRect(), this.getRect());
else {
logger.error("Unable to add {} as a parent of {}, reverting", parent.getRect(), this.getRect());
this.parent = null;
}
} else
logger.error("New child overlaps with future siblings:\n{}\nSiblings:\n{}", this, this.getSiblings());
}
}
public boolean hasChildren() {
return children != null && !children.isEmpty();
}
public boolean hasSiblings() {
return !getSiblings().isEmpty();
}
public boolean isOrphan() {
return parent == null;
}
@Override
void updateRect(GSRect rect) {
super.updateRect(rect);
boolean ok = checkConstraints();
if (!ok) {
if (isOrphan()) {
this.repairTree();
} else {
parent.repairTree();
}
}
}
private boolean checkConstraints() {
boolean ok = isOrphan() ? this.checkConstraintsRecursive() : parent.checkConstraintsRecursive();
if (!ok)
logger.error("Invalid constraint for:\n{}Tree:\n{}", this, isOrphan() ? this.recursiveToString() : this.parent.recursiveToString());
return ok;
}
private boolean checkConstraintsRecursive() {
// If the field is orphan and has no children, it validates the constraints
if (isOrphan() && !hasChildren())
return true;
// If the field has children, they must meet the constraint
if (hasChildren()) {
for (Field child : children)
if(child.isOutsideParent() || child.isOverlappingSiblings())
return false;
// If false was not returned, apply the function to the children
for (Field child : children)
if (!child.checkConstraintsRecursive())
return false;
}
// At this stage, all the constraints should be verified
return true;
}
private boolean isOutsideParent() {
return !rect.equals(rect.isInsider(getParent().getRect()));
}
private boolean isOverlappingSiblings() {
return getSiblings().stream().anyMatch(sibling -> getRect().isOverlapping(sibling.getRect()));
}
public void repairTree() {
if (isOrphan() && !hasChildren()) {
logger.error("Unable to repair a single-membered tree");
return;
}
for (Field child : children) {
if (child.isOutsideParent()) {
if (child.getParent().getDeadCounter() == 0)
child.fitInParent();
else if (child.getDeadCounter() == 0)
child.getParent().accomodate(child);
}
if (child.hasChildren())
child.repairTree();
}
}
protected void adjustRect(GSRect rect) {
logger.warn("replaced {} => {}", this.rect, rect);
this.rect = rect;
}
private void accomodate(Field child) {
if (!hasChildren())
return;
logger.warn("need union");
adjustRect(rect.getUnion(child.getRect()));
}
private void fitInParent() {
if (isOrphan())
return;
GSRect intersection = rect.getIntersection(parent.getRect());
logger.warn("need intersection");
if(intersection!=null)
adjustRect(intersection);
}
}