-
Notifications
You must be signed in to change notification settings - Fork 5
/
Field.java
140 lines (113 loc) · 2.86 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
package org.genericsystem.cv.retriever;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import org.genericsystem.cv.Img;
import org.opencv.core.Mat;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
public class Field extends AbstractField {
private Field parent;
private Set<Field> children;
private static final int LABELS_SIZE_THRESHOLD = 15;
private static final double CONFIDENCE_THRESHOLD = 0.92;
private boolean locked = false;
private boolean child;
public boolean isChild() {
return child;
}
public void setChild(boolean child) {
this.child = child;
}
public Field(Rect rect) {
super(rect);
this.parent = null;
this.children = new HashSet<>();
}
public Field(Field other) {
super(other);
if (other instanceof Field) {
this.parent = other.parent;
this.children = other.children;
setFinal();
}
}
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 merge(AbstractField field) {
super.merge(field);
if (field instanceof Field)
setFinal();
}
@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 setFinal() {
if (!locked)
if (getLabelsSize() > LABELS_SIZE_THRESHOLD && getConfidence() > CONFIDENCE_THRESHOLD)
this.locked = true;
}
public boolean isLocked() {
return locked;
}
public void setChildren(Set<Field> children) {
this.children = children;
}
public boolean addChild(Field child) {
if (!rect.equals(child.getRect()))
return children.add(child);
return false;
}
public boolean removeChild(Field child) {
return children.remove(child);
}
public boolean containsChild(Field field) {
return children.contains(field);
}
public boolean addChildren(Collection<Field> children) {
return this.children.addAll(children);
}
public Set<Field> getChildren() {
return children;
}
public Field getParent() {
return parent;
}
public void setParent(Field parent) {
this.parent = parent;
}
}