-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gui.java
491 lines (389 loc) · 10.6 KB
/
Gui.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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Vector;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
/*
* 作成日: 2004/01/18
* $Id: Gui.java,v 1.4 2004/01/19 09:40:57 matsu Exp $
*/
/**
* @author matsu
*
* 文字認識GUIとControl
*/
public class Gui
extends JFrame
implements MouseMotionListener, ActionListener {
/** 認識文字表示領域*/
private JTextField text = new JTextField();
/** 文字認識パネル */
private JPanel field_panel;
/** mode */
private JButton learning_button = new JButton("Learning mode");
/** mode */
private JButton recognition_button = new JButton("Recognition mode");
/** リセットボタン */
private JButton reset_button = new JButton("RESET");
/** OKボタン */
private JButton ok_button = new JButton("OK");
/** 答え表示パネル */
private JPanel text_panel = new JPanel();
/** 文字選択パネル */
private JPanel select_panel;
/** セレクトボックス */
private JComboBox select;
/** Container */
private Container c;
/** NeuralNetwork */
private NeuralNetwork neural;
/** 学習データ */
Vector vector = new Vector();
// アルファベット配列
private static final String[] alpha_array =
new String[] {
"A",
"B",
"C",
"D",
"E",
"F",
"G",
"H",
"I",
"J",
"K",
"L",
"M",
"N",
"O",
"P",
"Q",
"R",
"S",
"T",
"U",
"V",
"W",
"X",
"Y",
"Z" };
/** 升目 */
private static final int x = 8;
/** 升目 */
private static final int y = 8;
/** 一升の大きさ */
private static final int x_width = 20;
/** 一升の大きさ */
private static final int y_width = 20;
/** 何個候補を表示するか */
private static final int DISP = 5;
/** 学習データ */
private static final String learning_file = "learning.txt";
/** 入力情報格納 */
private int[][] input = new int[x][y];
/** モード定義 */
public static final int LEARNING = 0;
public static final int RECOGNITION = 1;
/** モード */
private int mode = 0;
/** ファイルから学習するか */
private static boolean LEARN_FROM_FILE = false;
/**
* main
* @param args
*/
public static void main(String[] args) {
//Create and set up the window.
Gui frame = new Gui();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Display the window.
frame.setVisible(true);
}
/**
* GUI作成
*
*/
public Gui() {
super("Pattern learning.");
init();
// ニューラルネットワーク
neural = new NeuralNetwork();
if(LEARN_FROM_FILE == true)
study();
// サイズ設定
//setBounds(50, 50, 300, 500);
//setSize(300,500);
// Containerを取得
c = getContentPane();
c.setLayout(new BoxLayout(c, BoxLayout.Y_AXIS));
c.add(makeButtonPane());
c.add(makeSelectBox());
c.add(makeField());
c.add(makeButton());
makeDisplayLine();
//validate();
pack();
}
/**
* 初期化
* @return
*/
public void init() {
for (int i = 0; i < input.length; i++) {
for (int j = 0; j < input[i].length; j++)
input[i][j] = 0;
}
}
/**
* モード選択
* @return
*/
public Container makeButtonPane() {
JPanel panel = new JPanel();
learning_button.addActionListener(this);
recognition_button.addActionListener(this);
panel.add(learning_button);
panel.add(recognition_button);
return panel;
}
/**
* 学習させる文字選択
* @return
*/
public Container makeSelectBox() {
select_panel = new JPanel();
select = new JComboBox(alpha_array);
select_panel.add(select);
//panel.setPreferredSize(new Dimension(100, 50));
//panel.setBackground(Color.blue);
return select_panel;
}
/**
* 結果表示領域
* @return
*/
public Container makeDisplayLine() {
text.setPreferredSize(new Dimension(250, 20));
text_panel.add(this.text);
return text_panel;
}
/**
* 入力フィールド
* @return
*/
public Container makeField() {
field_panel = new JPanel() {
public void paintComponent(Graphics g) {
// 升目描画
for (int i = 0; i <= x; i++) {
// 縦線
g.drawLine(i * x_width, 0, i * x_width, y * y_width);
// 横線
g.drawLine(0, i * x_width, x * x_width, i * x_width);
}
// 色描画
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
if (input[i][j] == 0) {
//白
g.setColor(Color.WHITE);
} else {
g.setColor(Color.BLACK);
}
g.fillRect(
j * y_width + 1,
i * x_width + 1,
x_width - 1,
y_width - 1);
g.setColor(Color.BLACK);
}
}
}
};
field_panel.addMouseMotionListener(this);
field_panel.setPreferredSize(new Dimension(x * x_width, y * y_width));
return field_panel;
}
/**
* OKボタン
* @return
*/
public Container makeButton() {
JPanel panel = new JPanel();
reset_button.addActionListener(this);
ok_button.addActionListener(this);
panel.add(reset_button);
panel.add(ok_button);
return panel;
}
/**
* モードを変える
*
*/
public void changeMode(int next_mode) {
c.remove(1);
if (next_mode == LEARNING) {
c.add(select_panel, 1);
} else if (next_mode == RECOGNITION) {
c.add(text_panel, 1);
}
pack();
mode = next_mode;
}
/**
* 学習
*/
public void study() {
Log.info("<-- Learn from data");
Vector _vector = new Vector();
try {
// ファイルから読み込み
BufferedReader br =
new BufferedReader(
new InputStreamReader(new FileInputStream(learning_file)));
// 学習データ作成
String msg;
int i = 0;
while ((msg = br.readLine()) != null) {
for (int j = 0; j < msg.length(); j++) {
double[] _teach = new double[alpha_array.length];
double[] _input = new double[x * y];
//init
for (int h = 0; h < _teach.length; h++) {
_teach[h] = 0;
if (j == i)
_teach[h] = 1;
}
for (int h = 0; h < x * y; h++) {
_input[h] = Double.parseDouble(String.valueOf(msg.charAt(h)));
}
_vector.add(new Dataset(_input, _teach));
}
i++;
}
} catch (Exception e) {
e.printStackTrace();
}
neural.learn(_vector);
Log.info("-->");
}
/**
* マウスアクション
*/
public void mouseDragged(MouseEvent e) {
// クリック場所識別
if (e.getSource().equals(field_panel)) {
if (e.getX() < x * x_width
&& e.getY() < y * y_width
&& 0 < e.getX()
&& 0 < e.getY()) {
input[e.getY() / y_width][e.getX() / x_width] = 1;
field_panel.repaint();
//Log.debug(e.getX()/x_width+":"+e.getY()/y_width);
}
}
}
/**
* マウスアクション
*/
public void mouseMoved(MouseEvent e) {
//Log.debug(e.toString());
}
/**
* ボタンアクション
*/
public void actionPerformed(ActionEvent e) {
// Reset
if (e.getSource().equals(reset_button)) {
init();
field_panel.repaint();
}
// OK
else if (e.getSource().equals(ok_button)) {
if (mode == LEARNING) {
double[] _teach = new double[alpha_array.length];
double[] _input = new double[x * y];
// Inputデータ
for (int i = 0; i < x; i++)
for (int j = 0; j < y; j++) {
_input[i * x + j] = input[i][j];
System.out.print(input[i][j]);
}
// 教師データ
for (int i = 0; i < alpha_array.length; i++) {
_teach[i] = 0;
if (select.getSelectedIndex() == i)
_teach[i] = 1;
}
Dataset dataset = new Dataset(_input, _teach);
vector.add(dataset);
// 学習
neural.learn(vector);
init();
field_panel.repaint();
} else if (mode == RECOGNITION) {
double[] _input = new double[x * y];
// Inputデータ
for (int i = 0; i < x; i++)
for (int j = 0; j < y; j++)
_input[i * x + j] = input[i][j];
Dataset dataset = new Dataset(_input);
// 認識
neural.forward(dataset);
// 結果取得
double[] result = neural.getResult();
// Sort
int current = 0;
int[] key = new int[result.length];
// key init
for (int i = 0; i < result.length; i++) {
key[i] = i;
}
while (current < result.length) {
for (int search = current + 1;
search < result.length;
search++) {
if (result[current] < result[search]) {
double tmp = result[current];
result[current] = result[search];
result[search] = tmp;
int tmp_int = key[current];
key[current] = key[search];
key[search] = tmp_int;
}
}
current++;
}
StringBuffer disp_text = new StringBuffer();
for (int i = 0; i < DISP; i++) {
disp_text.append(alpha_array[key[i]] + " ");
}
text.setText(disp_text.toString());
pack();
Log.info(alpha_array[key[0]]);
}
}
// mode
else if (e.getSource().equals(learning_button)) {
changeMode(LEARNING);
}
// mode
else if (e.getSource().equals(recognition_button)) {
changeMode(RECOGNITION);
}
}
}