-
Notifications
You must be signed in to change notification settings - Fork 5
/
Field.java
214 lines (179 loc) · 5.81 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.List;
import java.util.stream.Collectors;
import org.genericsystem.cv.Img;
import org.genericsystem.reinforcer.tools.GSPoint;
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), scalar, thickness);
if (needText())
drawText(display, getRectPointsWithHomography(homography), new Scalar(0, 64, 255), thickness);
}
private Scalar selectColor(Scalar defaultColor) {
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 isOrphan() || (!isOrphan() && parent.getDeadCounter() != 0) ? this.locked : false;
}
private boolean needRect() {
return !isOrphan() && parent.getDeadCounter() == 0 ? false : isLocked() ? true : deadCounter == 0;
}
private boolean needText() {
return deadCounter == 0 && 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 (parent == null)
return Collections.emptyList();
return parent.getChildren().stream().filter(child -> this != child).collect(Collectors.toList());
}
public void setParent(Field parent) {
this.parent = parent;
if (parent != null)
this.parent.addChildIfNotPresent(this);
}
public boolean hasChildren() {
return children != null && !children.isEmpty();
}
public boolean hasSiblings() {
return !getSiblings().isEmpty();
}
public boolean isOrphan() {
return parent == null;
}
public boolean isDead(int maxDeadCount) {
return !locked && deadCounter >= maxDeadCount;
}
void updateRect(GSRect rect, int width, int height) {
GSRect truncatedRect = getRect().getIntersection(new GSRect(0, 0, width, height));
if (truncatedRect != this.rect) {
double tlX = truncatedRect.getX();
double tlY = truncatedRect.getY();
double brX = tlX + truncatedRect.getWidth();
double brY = tlY + truncatedRect.getHeight();
this.rect = new GSRect(new GSPoint(tlX <= 0 ? this.rect.tl().getX() : rect.tl().getX(), tlY <= 0 ? this.rect.tl().getY() : rect.tl().getY()),
new GSPoint(brX >= width ? this.rect.br().getX() : rect.br().getX(), brY >= height ? this.rect.br().getY() : rect.br().getY()));
} else
this.rect = rect;
}
// 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 -> rect.isOverlapping(sibling.getRect()));
// }
}