-
Notifications
You must be signed in to change notification settings - Fork 5
/
Ocr.java
191 lines (164 loc) · 6.53 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
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
package org.genericsystem.cv;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
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 java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import javax.imageio.ImageIO;
import net.sourceforge.tess4j.ITessAPI.TessOcrEngineMode;
import net.sourceforge.tess4j.ITessAPI.TessPageSegMode;
import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfByte;
import org.opencv.core.MatOfPoint2f;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.RotatedRect;
import org.opencv.core.Size;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
public class Ocr {
private static Tesseract instance;
static {
instance = new Tesseract();
instance.setDatapath("/usr/share/tesseract-ocr/4.00/");
instance.setLanguage("fra");
instance.setHocr(false);
instance.setPageSegMode(TessPageSegMode.PSM_AUTO);
instance.setOcrEngineMode(TessOcrEngineMode.OEM_CUBE_ONLY);
// instance.setTessVariable("preserve_interword_spaces", "0");
// instance.setTessVariable("textord_space_size_is_variable", "1");
instance.setTessVariable("debug_file", "/dev/null");
instance.setTessVariable("tessedit_char_whitelist", "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789.-,<'");
instance.setTessVariable("tessedit_char_blacklist", "?{}_[]()À");
}
public static void main(String[] args) {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
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, Files::isRegularFile)) {
imagesStream.forEach(path -> {
System.out.println("------ Image: " + path);
ocrClassifiedImage(path);
});
}
}
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
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 + "%");
}
System.gc();
System.runFinalization();
}
public static String doWork(File imageFile) {
try {
return instance.doOCR(imageFile);
} catch (TesseractException e) {
throw new RuntimeException(e);
}
}
public static String doWork(Mat mat) {
return doWork(mat2bufferedImage(mat));
}
public static String doWork(Mat mat, Rect rect) {
return doWork(mat2bufferedImage(new Mat(mat, rect).clone()));
}
public static String doWork(Mat mat, RotatedRect rect) {
// Not tested !!!
Point[] points = new Point[4];
rect.points(points);
Mat result = new Mat();
MatOfPoint2f targets = new MatOfPoint2f(new Point(rect.size.width, 0), new Point(0, 0), new Point(0, rect.size.height), new Point(rect.size.width, rect.size.height));
Imgproc.warpAffine(mat, result, Imgproc.getAffineTransform(new MatOfPoint2f(points), targets), rect.size);
return doWork(mat2bufferedImage(result));
}
public static String doWork(BufferedImage image) {
try {
return instance.doOCR(image);
} catch (TesseractException e) {
throw new RuntimeException(e);
}
}
// public static String doWork(BufferedImage image, Rectangle rectangle) {
// try {
// return instance.doOCR(image, rectangle);
// } catch (TesseractException e) {
// throw new RuntimeException(e);
// }
//
// }
public static List<Rect> findBox(Mat mat) {
try {
return instance.getSegmentedRegions(Tools.mat2bufferedImage(mat), 2).stream().map(rectangle -> new Rect(new Point(rectangle.getX(), rectangle.getY()), new Size(rectangle.getWidth(), rectangle.getHeight()))).collect(Collectors.toList());
} catch (TesseractException e) {
throw new RuntimeException(e);
}
}
public static List<Rect> findBox(File imageFile) {
try {
return instance.getSegmentedRegions(ImageIO.read(imageFile), 2).stream().map(rectangle -> new Rect(new Point(rectangle.getX(), rectangle.getY()), new Size(rectangle.getWidth(), rectangle.getHeight()))).collect(Collectors.toList());
} catch (IOException | TesseractException e) {
throw new RuntimeException(e);
}
}
public static List<Rect> findBox(String hocrString) {
Pattern pattern = Pattern.compile("title=\"bbox \\d+ \\d+ \\d+ \\d+");
Matcher matcher = pattern.matcher(hocrString);
List<Rect> result = new ArrayList<>();
while (matcher.find()) {
String s = matcher.group(0);
s = s.replace("title=\"bbox ", "");
String[] v = s.split(" ");
List<Double> doubles = Arrays.stream(v).map(Double::valueOf).collect(Collectors.toList());
result.add(new Rect(new Point(doubles.get(0), doubles.get(1)), new Point(doubles.get(2), doubles.get(3))));
}
Arrays.asList(result);
return result;
}
public static BufferedImage mat2bufferedImage(Mat image) {
MatOfByte bytemat = new MatOfByte();
Imgcodecs.imencode(".png", image, bytemat);
try {
return ImageIO.read(new ByteArrayInputStream(bytemat.toArray()));
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
// public static BufferedImage mat2BufferedImage(Mat in) {
// BufferedImage image = new BufferedImage(in.width(), in.height(), BufferedImage.TYPE_3BYTE_BGR);
// WritableRaster raster = image.getRaster();
// DataBufferByte dataBuffer = (DataBufferByte) raster.getDataBuffer();
// byte[] data = dataBuffer.getData();
// in.get(0, 0, data);
// return image;
// }
}