-
Notifications
You must be signed in to change notification settings - Fork 5
/
Field.java
201 lines (164 loc) · 5.23 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
package org.genericsystem.cv.retriever;
import java.util.ArrayList;
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 final List<Field> children;
private static final int LABELS_SIZE_THRESHOLD = 10;
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<>();
}
public Field(GSRect rect, Field parent) {
super(rect);
setParent(parent);
this.children = new ArrayList<>();
}
public String recursiveToString() {
StringBuffer sb = new StringBuffer();
this.recursiveToString(sb, 0);
return sb.append("\n").toString();
}
public void recursiveToString(StringBuffer sb, int depth) {
if (depth > 8)
return;
sb.append("depth: ").append(depth).append(": ").append(rect);
if (isConsolidated())
sb.append(" -> ").append(getConsolidated());
if (!children.isEmpty()) {
depth++;
for (Field child : children) {
sb.append("\n");
for (int i = 0; i < depth; ++i)
sb.append(" ");
child.recursiveToString(sb, depth);
}
}
}
public Field findPotentialParent(GSRect rect) {
if (!rect.isInside(getRect()))
return null;
for (Field child : children) {
Field candidate = child.findPotentialParent(rect);
if (candidate != null)
return candidate;
}
return this;
}
@Override
public void ocr(Img rootImg) {
super.ocr(rootImg);
if (attempts <= 3 || attempts % 5 == 0)
consolidateOcr(false);
}
@Override
public void resetDeadCounter() {
super.resetDeadCounter();
tryLock();
}
public void draw(Img display, int thickness) {
if (needText(display))
drawDebugText(display, new Scalar(0, 64, 255), thickness);
drawRect(display, deadCounter == 0 ? new Scalar(0, 255, 0) : new Scalar(0, 0, 255), thickness);
}
public void drawWithHomography(Img display, Mat homography, int thickness) {
if (needRect(display)) {
drawRect(display, getRectPointsWithHomography(homography), drawAsLocked() ? new Scalar(0, 255, 0) : new Scalar(0, 0, 255), thickness);
drawText(display, getRectPointsWithHomography(homography), new Scalar(0, 255, 0), thickness);
}
}
private boolean drawAsLocked() {
return isOrphan() || (!isOrphan() && parent.getDeadCounter() != 0) ? isLocked() : false;
}
private boolean needRect(Img display) {
GSRect imgRect = new GSRect(0, 0, display.width(), display.height());
return !isOrphan() && parent.getRect().isInside(imgRect) ? false : rect.isInside(imgRect);
}
private boolean needText(Img display) {
return deadCounter == 0 && needRect(display);
}
public boolean isOnDisplay(Img display) {
GSRect imgRect = new GSRect(0, 0, display.width(), display.height());
return imgRect.isOverlapping(rect);
}
public void tryLock() {
if (!locked && getLabelsSize() > LABELS_SIZE_THRESHOLD && getConfidence() > CONFIDENCE_THRESHOLD)
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 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;
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));
double tlX = truncatedRect.getX();
double tlY = truncatedRect.getY();
double brX = tlX + getRect().getWidth();
double brY = tlY + getRect().getHeight();
GSRect updatedRect = 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()));
this.rect = updatedRect;
}
public boolean isVeryfyingConstraints() {
return false;
}
public boolean needOcr() {
return !isLocked() && deadCounter == 0;
}
public void resetChildrenDeadCounter() {
for (Field child : children) {
child.resetChildrenDeadCounter();
child.resetDeadCounter();
}
}
public void resetParentsDeadCounter() {
resetDeadCounter();
if (getParent() != null)
getParent().resetParentsDeadCounter();
}
}