-
Notifications
You must be signed in to change notification settings - Fork 5
/
Ocr.java
78 lines (68 loc) · 2.75 KB
/
Ocr.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
package org.genericsystem.cv;
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.genericsystem.common.Root;
import org.genericsystem.cv.comparator.FillModelWithData;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.Size;
import org.opencv.text.OCRTesseract;
public class Ocr {
static {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
}
private final static OCRTesseract instance = OCRTesseract.create("/usr/share/tesseract-ocr/4.00/", "fra", "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789.-,<'", 1, 13);
public static void main(String[] args) {
try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(Paths.get(".", "classes"), Files::isDirectory)) {
for (Path directoryPath : directoryStream) {
System.out.println("--- Images of class: " + directoryPath);
try (DirectoryStream<Path> imagesStream = Files.newDirectoryStream(directoryPath, path -> Files.isRegularFile(path) && path.toString().endsWith(".png"))) {
imagesStream.forEach(path -> {
System.out.println("------ Image: " + path);
ocrClassifiedImage(path);
});
}
}
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
@Deprecated
public static void ocrClassifiedImage(Path imagePath) {
Path imgClassDirectory = imagePath.getParent();
Path zonesFile = imgClassDirectory.resolve("zones/zones.json");
Zones zones = null;
if (zonesFile.toFile().exists()) {
System.out.println("Precomputed zones found, file: " + zonesFile);
zones = Zones.load(zonesFile.toFile());
}
if (zones == null) {
ImgClass imgClass = ImgClass.fromDirectory(imgClassDirectory.toString());
imgClass.addMapper(img -> img.eraseCorners(0.1).dilateBlacks(86, 255, 76, new Size(15, 3)));
zones = Zones.get(imgClass.getClosedVarianceZones(new Size(9, 10)), 300, 6, 6);
}
for (Zone zone : zones) {
ZoneScorer scorer = zone.newUnsupervisedScorer(Tools.classImgsStream(imgClassDirectory + "/mask/" + imagePath.getFileName().toString().replace(".png", "")));
System.out.println("Image " + imagePath + ", found text: " + scorer.getBestText() + " " + Math.floor((scorer.getBestScore() * 10000)) / 100 + "%");
}
}
@Deprecated
public static int ocrNewClassifiedImg(Path imagePath) {
int result = FillModelWithData.doImgOcr(imagePath);
return result;
}
@Deprecated
public static int ocrNewClassifiedImg(Root engine, Path imagePath) {
int result = FillModelWithData.doImgOcr(engine, imagePath);
return result;
}
public static String doWork(Mat mat) {
return doWork(mat, 0);
}
public static String doWork(Mat mat, int minConfidence) {
return instance.run(mat, 50, 1).replace("\n", "").trim();
}
}