-
Notifications
You must be signed in to change notification settings - Fork 5
/
Field.java
214 lines (179 loc) · 5.84 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
package org.genericsystem.cv.retriever;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Predicate;
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 Set<Field> children;
private List<double[]> shifts;
private final Predicate<Field> outsideParent = field -> rect.getInsider(field.getRect()).map(r -> !r.equals(field.getRect())).orElse(true);
private final Predicate<Field> overlapWithSiblings = field -> field.getSiblings().stream().filter(f -> !f.equals(field)).anyMatch(sibling -> field.getRect().isOverlappingStrict(sibling.getRect()));
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 HashSet<>();
this.shifts = new ArrayList<>();
checkOutsideParentConstraint();
}
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(": ");
sb.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 drawLockedField(Img display, Mat homography) {
if (locked)
drawRect(display, getRectPointsWithHomography(homography), new Scalar(255, 172, 0), 2);
}
public void drawTruncatedField(Img display, Mat homography) {
if (truncated)
drawRect(display, getRectPointsWithHomography(homography), new Scalar(0, 172, 255), 1);
}
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) {
// TODO add a constraint check, remove the check on the surface
// if (!this.equals(child) && !overlapsMoreThanThresh(child, 0.95) && !containsChild(child))
return children.add(child);
// return false;
}
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(Set<Field> children) {
this.children = children;
}
public Set<Field> getChildren() {
return children;
}
public Field getParent() {
return parent;
}
public Set<Field> getSiblings() {
if (isOrphan())
return Collections.emptySet();
return getParent().getChildren().stream().filter(child -> !this.equals(child)).collect(Collectors.toSet());
}
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 (getSiblings().stream().noneMatch(field -> rect.isOverlappingStrict(field.getRect()))) {
if (this.parent != null)
logger.error("Child already has a parent:\n{}\nParent:\n{}", this, this.parent);
this.parent = parent;
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("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;
}
public List<double[]> getShifts() {
return shifts;
}
public boolean registerShift(double[] delta) {
if (!isOrphan())
return parent.addShift(delta);
return false;
}
private boolean addShift(double[] delta) {
return shifts.add(delta);
}
public void clearShifts() {
shifts.clear();
}
@Override
void updateRect(GSRect rect) {
super.updateRect(rect);
checkOutsideParentConstraint();
}
private void checkOutsideParentConstraint() {
boolean ok = isOrphan() ? this.checkConstraint(outsideParent) : parent.checkConstraint(outsideParent);
if (!ok)
logger.error("Invalid constraint for:\n{}", this);
}
private boolean checkConstraint(Predicate<Field> constraint) {
// 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 (constraint.test(child))
return false;
}
// If false was not returned, apply the function to the children
for (Field child : children) {
if (!child.checkConstraint(constraint))
return false;
}
}
// At this stage, all the constraints should be verified
return true;
}
}