?? ocr.java
字號:
/**
* Introduction to Neural Networks with Java, 2nd Edition
* Copyright 2008 by Heaton Research, Inc.
* http://www.heatonresearch.com/books/java-neural-2/
*
* ISBN13: 978-1-60439-008-7
* ISBN: 1-60439-008-5
*
* This class is released under the:
* GNU Lesser General Public License (LGPL)
* http://www.gnu.org/copyleft/lesser.html
*/
package org.encog.examples.neural.gui.ocr;
import java.awt.Font;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.OutputStream;
import java.io.PrintStream;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import org.encog.neural.data.NeuralData;
import org.encog.neural.data.NeuralDataSet;
import org.encog.neural.data.basic.BasicNeuralData;
import org.encog.neural.data.basic.BasicNeuralDataPair;
import org.encog.neural.data.basic.BasicNeuralDataSet;
import org.encog.neural.networks.BasicNetwork;
import org.encog.neural.networks.layers.BasicLayer;
import org.encog.neural.networks.layers.SOMLayer;
import org.encog.neural.networks.training.som.TrainSelfOrganizingMap;
import org.encog.neural.networks.training.som.TrainSelfOrganizingMap.LearningMethod;
import org.encog.util.NormalizeInput.NormalizationType;
/**
* Chapter 12: OCR and the Self Organizing Map
*
* OCR: Main form that allows the user to interact with the OCR application.
*
* @author Jeff Heaton
* @version 2.1
*/
public class OCR extends JFrame implements Runnable {
class SymAction implements java.awt.event.ActionListener {
public void actionPerformed(final java.awt.event.ActionEvent event) {
final Object object = event.getSource();
if (object == OCR.this.downSample) {
downSample_actionPerformed(event);
} else if (object == OCR.this.clear) {
clear_actionPerformed(event);
} else if (object == OCR.this.add) {
add_actionPerformed(event);
} else if (object == OCR.this.del) {
del_actionPerformed(event);
} else if (object == OCR.this.load) {
load_actionPerformed(event);
} else if (object == OCR.this.save) {
save_actionPerformed(event);
} else if (object == OCR.this.train) {
train_actionPerformed(event);
} else if (object == OCR.this.recognize) {
recognize_actionPerformed(event);
}
}
}
class SymListSelection implements javax.swing.event.ListSelectionListener {
public void valueChanged(
final javax.swing.event.ListSelectionEvent event) {
final Object object = event.getSource();
if (object == OCR.this.letters) {
letters_valueChanged(event);
}
}
}
public class UpdateStats implements Runnable {
long _tries;
double _lastError;
double _bestError;
public void run() {
OCR.this.tries.setText("" + this._tries);
OCR.this.lastError.setText("" + this._lastError);
OCR.this.bestError.setText("" + this._bestError);
}
}
/**
* Serial id for this class.
*/
private static final long serialVersionUID = -6779380961875907013L;
/**
* The downsample width for the application.
*/
static final int DOWNSAMPLE_WIDTH = 5;
/**
* The down sample height for the application.
*/
static final int DOWNSAMPLE_HEIGHT = 7;
static final double MAX_ERROR = 0.01;
/**
* The main method.
*
* @param args
* Args not really used.
*/
public static void main(final String args[]) {
(new OCR()).setVisible(true);
}
boolean halt;
/**
* The entry component for the user to draw into.
*/
Entry entry;
/**
* The down sample component to display the drawing downsampled.
*/
Sample sample;
/**
* The letters that have been defined.
*/
DefaultListModel letterListModel = new DefaultListModel();
/**
* The neural network.
*/
BasicNetwork net;
/**
* The background thread used for training.
*/
Thread trainThread = null;
// {{DECLARE_CONTROLS
javax.swing.JLabel JLabel1 = new javax.swing.JLabel();
javax.swing.JLabel JLabel2 = new javax.swing.JLabel();
/**
* THe downsample button.
*/
javax.swing.JButton downSample = new javax.swing.JButton();
/**
* The add button.
*/
javax.swing.JButton add = new javax.swing.JButton();
/**
* The clear button
*/
javax.swing.JButton clear = new javax.swing.JButton();
/**
* The recognize button
*/
javax.swing.JButton recognize = new javax.swing.JButton();
javax.swing.JScrollPane JScrollPane1 = new javax.swing.JScrollPane();
/**
* The letters list box
*/
javax.swing.JList letters = new javax.swing.JList();
/**
* The delete button
*/
javax.swing.JButton del = new javax.swing.JButton();
/**
* The load button
*/
javax.swing.JButton load = new javax.swing.JButton();
/**
* The save button
*/
javax.swing.JButton save = new javax.swing.JButton();
/**
* The train button
*/
javax.swing.JButton train = new javax.swing.JButton();
javax.swing.JLabel JLabel3 = new javax.swing.JLabel();
javax.swing.JLabel JLabel4 = new javax.swing.JLabel();
/**
* How many tries
*/
javax.swing.JLabel tries = new javax.swing.JLabel();
/**
* The last error
*/
javax.swing.JLabel lastError = new javax.swing.JLabel();
/**
* The best error
*/
javax.swing.JLabel bestError = new javax.swing.JLabel();
javax.swing.JLabel JLabel8 = new javax.swing.JLabel();
javax.swing.JLabel JLabel5 = new javax.swing.JLabel();
// }}
// {{DECLARE_MENUS
// }}
/**
* The constructor.
*/
OCR() {
getContentPane().setLayout(null);
this.entry = new Entry();
this.entry.setLocation(168, 25);
this.entry.setSize(200, 128);
getContentPane().add(this.entry);
this.sample = new Sample(DOWNSAMPLE_WIDTH, DOWNSAMPLE_HEIGHT);
this.sample.setLocation(307, 210);
this.sample.setSize(65, 70);
this.entry.setSample(this.sample);
getContentPane().add(this.sample);
// {{INIT_CONTROLS
setTitle("Java Neural Network");
getContentPane().setLayout(null);
setSize(405, 382);
setVisible(false);
this.JLabel1.setText("Letters Known");
getContentPane().add(this.JLabel1);
this.JLabel1.setBounds(12, 12, 84, 12);
this.JLabel2.setText("Tries:");
getContentPane().add(this.JLabel2);
this.JLabel2.setBounds(12, 264, 72, 24);
this.downSample.setText("Down Sample");
this.downSample.setActionCommand("Down Sample");
getContentPane().add(this.downSample);
this.downSample.setBounds(252, 180, 120, 24);
this.add.setText("Add");
this.add.setActionCommand("Add");
getContentPane().add(this.add);
this.add.setBounds(168, 156, 84, 24);
this.clear.setText("Clear");
this.clear.setActionCommand("Clear");
getContentPane().add(this.clear);
this.clear.setBounds(168, 180, 84, 24);
this.recognize.setText("Recognize");
this.recognize.setActionCommand("Recognize");
getContentPane().add(this.recognize);
this.recognize.setBounds(252, 156, 120, 24);
this.JScrollPane1
.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
this.JScrollPane1.setOpaque(true);
getContentPane().add(this.JScrollPane1);
this.JScrollPane1.setBounds(12, 24, 144, 132);
this.JScrollPane1.getViewport().add(this.letters);
this.letters.setBounds(0, 0, 126, 129);
this.del.setText("Delete");
this.del.setActionCommand("Delete");
getContentPane().add(this.del);
this.del.setBounds(12, 156, 144, 24);
this.load.setText("Load");
this.load.setActionCommand("Load");
getContentPane().add(this.load);
this.load.setBounds(12, 180, 72, 24);
this.save.setText("Save");
this.save.setActionCommand("Save");
getContentPane().add(this.save);
this.save.setBounds(84, 180, 72, 24);
this.train.setText("Begin Training");
this.train.setActionCommand("Begin Training");
getContentPane().add(this.train);
this.train.setBounds(12, 204, 144, 24);
this.JLabel3.setText("Last Error:");
getContentPane().add(this.JLabel3);
this.JLabel3.setBounds(12, 288, 72, 24);
this.JLabel4.setText("Best Error:");
getContentPane().add(this.JLabel4);
this.JLabel4.setBounds(12, 312, 72, 24);
this.tries.setText("0");
getContentPane().add(this.tries);
this.tries.setBounds(96, 264, 72, 24);
this.lastError.setText("0");
getContentPane().add(this.lastError);
this.lastError.setBounds(96, 288, 72, 24);
this.bestError.setText("0");
getContentPane().add(this.bestError);
this.bestError.setBounds(96, 312, 72, 24);
this.JLabel8
.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
this.JLabel8.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
this.JLabel8.setText("Training Results");
getContentPane().add(this.JLabel8);
this.JLabel8.setFont(new Font("Dialog", Font.BOLD, 14));
this.JLabel8.setBounds(12, 240, 120, 24);
this.JLabel5.setText("Draw Letters Here");
getContentPane().add(this.JLabel5);
this.JLabel5.setBounds(204, 12, 144, 12);
// }}
// {{REGISTER_LISTENERS
final SymAction lSymAction = new SymAction();
this.downSample.addActionListener(lSymAction);
this.clear.addActionListener(lSymAction);
this.add.addActionListener(lSymAction);
this.del.addActionListener(lSymAction);
final SymListSelection lSymListSelection = new SymListSelection();
this.letters.addListSelectionListener(lSymListSelection);
this.load.addActionListener(lSymAction);
this.save.addActionListener(lSymAction);
this.train.addActionListener(lSymAction);
this.recognize.addActionListener(lSymAction);
// }}
this.letters.setModel(this.letterListModel);
// {{INIT_MENUS
// }}
}
/**
* Called to add the current image to the training set
*
* @param event
* The event
*/
@SuppressWarnings("unchecked")
void add_actionPerformed(final java.awt.event.ActionEvent event) {
int i;
final String letter = JOptionPane
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -