forked from GitLqr/JavaVerify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrain.java
More file actions
244 lines (192 loc) · 5.08 KB
/
Train.java
File metadata and controls
244 lines (192 loc) · 5.08 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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package train;
import java.awt.Label;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import javax.imageio.ImageIO;
import svmHelper.svm_scale;
import svmHelper.svm_train;
public class Train {
/**
* 类标号map,如:a=>1 b=>2
*/
private HashMap<String, Integer> labelMap = null;
/**
* 所有图像分类的map,key为当前类标号, value为对应的图片,图片以二维数组的形式保存
*/
private HashMap<String, ArrayList<Integer[][]>> imageMap = null;
public Train(){
init();
}
private void init(){
labelMap = new HashMap<String, Integer>();
imageMap = new HashMap<String, ArrayList<Integer[][]>>();
loadImageLabel();
loadImage();
svmFormat();
}
private void loadImageLabel(){
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(arr[0], Integer.parseInt(arr[1]));
}
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 void loadImage(){
File dir = new File("4_scale/");
//只列出jpg
File[] files = dir.listFiles(new FilenameFilter() {
public boolean isJpg(String file){
if (file.toLowerCase().endsWith(".jpg")){
return true;
}else{
return false;
}
}
@Override
public boolean accept(File dir, String name) {
// TODO Auto-generated method stub
return isJpg(name);
}
});
for (File file : files) {
try {
transferToMap(file);
} catch (Exception e) {
e.printStackTrace();
}
}
System.out.println("load mage end");
}
/**
* 将image 转换到 map中
* @param file
* @throws IOException
*/
private void transferToMap(File file) throws IOException{
BufferedImage image = ImageIO.read(file);
int width = image.getWidth();
int height = image.getHeight();
Integer[][] imgArr = new Integer[height][width];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
//黑色点标记为1
int value = ImageUtil.isBlack(image.getRGB(x, y)) ? 1 : 0;
imgArr[y][x] = value;
}
}
ArrayList<Integer[][]> imgList = null;
String className = ImageUtil.getImgClass(file.getName());
if (imageMap.containsKey(className)) {
imgList = imageMap.get(className);
imgList.add(imgArr);
}else{
imgList = new ArrayList<Integer[][]>();
imgList.add(imgArr);
imageMap.put(className, imgList);
}
}
/**
* 转换成svm预料的格式
*/
public void svmFormat(){
PrintWriter writer = null;
try {
writer = new PrintWriter(new File("svm/svm.train"));
Iterator<String> iterator = this.imageMap.keySet().iterator();
while(iterator.hasNext()){
String className = iterator.next();
int classLabel = labelMap.get(className);
ArrayList<Integer[][]> list = imageMap.get(className);
System.out.println(className);
for (Integer[][] img : list) {
String tmpLine = classLabel + " ";
int index = 1;
for (int i = 0; i < img.length; i++) {
for (int j = 0; j < img[i].length; j++) {
tmpLine += index + ":" + img[i][j] + " ";
index ++;
}
}
// System.out.println(tmpLine);
writer.write(tmpLine + "\r\n");
writer.flush();
}
}
} catch (Exception e) {
e.printStackTrace();
} finally{
if (writer != null) {
writer.close();
}
}
}
/**
* 生成模型
* @throws IOException
*/
public static void run() throws IOException{
//train参数
String[] arg = {"-t","0","svm/svm.train","svm/svm.model"};
//predict参数
String[] parg = {"svm/svmscale.test","svm/svm.model","svm/result.txt"};
System.out.println("训练开始");
svm_train.main(arg);
System.out.println("训练结束");
}
private static void produceLabel(){
FileWriter writer = null;
try {
writer = new FileWriter(new File("svm/label.txt"));
String charactor = "1234567890abcdefghijklmnopqrstuvwxyz";
for (int i = 0; i < charactor.length(); i++) {
char c = charactor.charAt(i);
String str = c + " " + (i+1) + "\r\n";
writer.write(str);
}
writer.flush();
} catch (IOException e) {
e.printStackTrace();
} finally{
if (writer != null) {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) throws IOException{
System.out.println("begin");
// Train model = new Train();
run();
System.out.println("end");
}
}