-
Notifications
You must be signed in to change notification settings - Fork 5
/
Field.java
58 lines (49 loc) · 1.75 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
package org.genericsystem.layout;
import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;
import java.util.stream.IntStream;
import org.genericsystem.cv.Img;
import org.genericsystem.cv.Ocr;
import org.genericsystem.cv.utils.OCRPlasty;
import org.genericsystem.cv.utils.OCRPlasty.RANSAC;
import org.genericsystem.cv.utils.OCRPlasty.Tuple;
import org.opencv.core.Mat;
import org.opencv.core.Rect;
public class Field extends AbstractField {
public Field(Rect rect) {
super(rect);
}
public void merge(Field field) {
field.getLabels().entrySet().forEach(entry -> labels.merge(entry.getKey(), entry.getValue(), Integer::sum));
consolidated = field.getConsolidated();
attempts = field.getAttempts();
}
public Field saveForLater(Rect newRect) {
Field f = new Field(newRect);
f.merge(this);
return f;
}
@Override
public void ocr(Img rootImg) {
String ocr = Ocr.doWork(new Mat(rootImg.getSrc(), getLargeRect(rootImg, 0.03, 0.1)));
if (!ocr.isEmpty()) {
labels.merge(ocr, 1, Integer::sum);
attempts++;
}
// if (getLabelsSize() == 0 || getLabelsSize() % 10 == 0)
consolidateOcr();
}
@Override
protected void consolidateOcr() {
List<String> strings = labels.entrySet().stream().sorted(Entry.<String, Integer>comparingByValue().reversed()).limit(20).collect(ArrayList<String>::new, (list, e) -> IntStream.range(0, e.getValue()).forEach(count -> list.add(e.getKey())),
List::addAll);
if (getLabelsSize() > 2) {
Tuple res = OCRPlasty.correctStringsAndGetOutliers(strings, RANSAC.NORM_LEVENSHTEIN);
consolidated = res.getString().orElse(labels.entrySet().stream().map(e -> e.getKey()).findFirst().orElse(null));
res.getOutliers().forEach(outlier -> labels.remove(outlier));
} else {
consolidated = null;
}
}
}