forked from donaldlee2008/JavaVerify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIdenty.java
More file actions
114 lines (95 loc) · 2.74 KB
/
Identy.java
File metadata and controls
114 lines (95 loc) · 2.74 KB
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
package train;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import javax.imageio.ImageIO;
import Base.Base;
public class Identy{
private HashMap<Integer, String> labelMap = null;
public Identy(){
loadLabelMap();
}
private void loadLabelMap(){
labelMap = new HashMap<Integer, String>();
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(new File("svm/label.txt")));
String buff = null;
while((buff = reader.readLine()) != null){
String[] arr = buff.split(" ");
labelMap.put(Integer.parseInt(arr[1]), arr[0]);
}
System.out.println("load image label finish!");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private String getClassName(int label){
return labelMap.get(label);
}
/**
* 具体的预测,返回识别的文字
* @param file
* @return
* @throws IOException
*/
public String predict(File file) throws IOException{
BufferedImage sourceImage = ImageIO.read(file);
Preprocess preprocess = new Preprocess();
BufferedImage binaryImage = preprocess.getBinaryImage(sourceImage);
SegCfg segCfg = new SegCfg();
ArrayList<BufferedImage> interList = segCfg.cfs(binaryImage);
ArrayList<BufferedImage> imageList = new ArrayList<BufferedImage>();
SegWaterDrop segWaterDrop = new SegWaterDrop();
for (BufferedImage img : interList) {
ArrayList<BufferedImage> tmpList = segWaterDrop.drop(img);
for (BufferedImage sumImg : tmpList) {
imageList.add(ImageUtil.scaleImage(sumImg));
}
}
for (int i = 0; i < imageList.size(); i++) {
ImageIO.write(imageList.get(i), "JPG", new File("tmp/" + i + ".jpg"));
}
Predict.run(imageList);
String result = "";
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(new File("svm/result.txt")));
String buff = "";
while((buff = reader.readLine()) != null){
int label = (int)Double.parseDouble(buff);
String className = getClassName(label);
result += className + " ";
}
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
} finally{
if (reader != null) {
reader.close();
}
}
return result;
}
public static void main(String[] args) throws IOException{
Identy index = new Identy();
// index.predict(new File("download/1.jpg"));
index.predict(new File("tmp/test.jpg"));
}
}