?? ocr.java
字號(hào):
.showInputDialog("Please enter a letter you would like to assign this sample to.");
if (letter == null) {
return;
}
if (letter.length() > 1) {
JOptionPane.showMessageDialog(this,
"Please enter only a single letter.", "Error",
JOptionPane.ERROR_MESSAGE);
return;
}
this.entry.downSample();
final SampleData sampleData = (SampleData) this.sample.getData()
.clone();
sampleData.setLetter(letter.charAt(0));
for (i = 0; i < this.letterListModel.size(); i++) {
final Comparable str = (Comparable) this.letterListModel
.getElementAt(i);
if (str.equals(letter)) {
JOptionPane.showMessageDialog(this,
"That letter is already defined, delete it first!",
"Error", JOptionPane.ERROR_MESSAGE);
return;
}
if (str.compareTo(sampleData) > 0) {
this.letterListModel.add(i, sampleData);
return;
}
}
this.letterListModel.add(this.letterListModel.size(), sampleData);
this.letters.setSelectedIndex(i);
this.entry.clear();
this.sample.repaint();
}
/**
* Called to clear the image.
*
* @param event
* The event
*/
void clear_actionPerformed(final java.awt.event.ActionEvent event) {
this.entry.clear();
this.sample.getData().clear();
this.sample.repaint();
}
/**
* Called when the del button is pressed.
*
* @param event
* The event.
*/
void del_actionPerformed(final java.awt.event.ActionEvent event) {
final int i = this.letters.getSelectedIndex();
if (i == -1) {
JOptionPane.showMessageDialog(this,
"Please select a letter to delete.", "Error",
JOptionPane.ERROR_MESSAGE);
return;
}
this.letterListModel.remove(i);
}
/**
* Called to downsample the image.
*
* @param event
* The event
*/
void downSample_actionPerformed(final java.awt.event.ActionEvent event) {
this.entry.downSample();
}
/**
* Called when a letter is selected from the list box.
*
* @param event
* The event
*/
void letters_valueChanged(final javax.swing.event.ListSelectionEvent event) {
if (this.letters.getSelectedIndex() == -1) {
return;
}
final SampleData selected = (SampleData) this.letterListModel
.getElementAt(this.letters.getSelectedIndex());
this.sample.setData((SampleData) selected.clone());
this.sample.repaint();
this.entry.clear();
}
/**
* Called when the load button is pressed.
*
* @param event
* The event
*/
void load_actionPerformed(final java.awt.event.ActionEvent event) {
try {
FileReader f;// the actual file stream
BufferedReader r;// used to read the file line by line
f = new FileReader(new File("./sample.dat"));
r = new BufferedReader(f);
String line;
int i = 0;
this.letterListModel.clear();
while ((line = r.readLine()) != null) {
final SampleData ds = new SampleData(line.charAt(0),
OCR.DOWNSAMPLE_WIDTH, OCR.DOWNSAMPLE_HEIGHT);
this.letterListModel.add(i++, ds);
int idx = 2;
for (int y = 0; y < ds.getHeight(); y++) {
for (int x = 0; x < ds.getWidth(); x++) {
ds.setData(x, y, line.charAt(idx++) == '1');
}
}
}
r.close();
f.close();
clear_actionPerformed(null);
JOptionPane.showMessageDialog(this, "Loaded from 'sample.dat'.",
"Training", JOptionPane.PLAIN_MESSAGE);
} catch (final Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(this, "Error: " + e, "Training",
JOptionPane.ERROR_MESSAGE);
}
}
/**
* Used to map neurons to actual letters.
*
* @return The current mapping between neurons and letters as an array.
*/
char[] mapNeurons() {
final char map[] = new char[this.letterListModel.size()];
for (int i = 0; i < map.length; i++) {
map[i] = '?';
}
for (int i = 0; i < this.letterListModel.size(); i++) {
final NeuralData input = new BasicNeuralData(5 * 7);
int idx = 0;
final SampleData ds = (SampleData) this.letterListModel
.getElementAt(i);
for (int y = 0; y < ds.getHeight(); y++) {
for (int x = 0; x < ds.getWidth(); x++) {
input.setData(idx++, ds.getData(x, y) ? .5 : -.5);
}
}
final int best = this.net.winner(input);
map[best] = ds.getLetter();
}
return map;
}
/**
* Called when the recognize button is pressed.
*
* @param event
* The event.
*/
void recognize_actionPerformed(final java.awt.event.ActionEvent event) {
if (this.net == null) {
JOptionPane.showMessageDialog(this, "I need to be trained first!",
"Error", JOptionPane.ERROR_MESSAGE);
return;
}
this.entry.downSample();
final NeuralData input = new BasicNeuralData(5 * 7);
int idx = 0;
final SampleData ds = this.sample.getData();
for (int y = 0; y < ds.getHeight(); y++) {
for (int x = 0; x < ds.getWidth(); x++) {
input.setData(idx++, ds.getData(x, y) ? .5 : -.5);
}
}
final int best = this.net.winner(input);
final char map[] = mapNeurons();
JOptionPane
.showMessageDialog(this, " " + map[best] + " (Neuron #"
+ best + " fired)", "That Letter Is",
JOptionPane.PLAIN_MESSAGE);
clear_actionPerformed(null);
}
/**
* Run method for the background training thread.
*/
public void run() {
try {
final int inputNeuron = OCR.DOWNSAMPLE_HEIGHT
* OCR.DOWNSAMPLE_WIDTH;
final int outputNeuron = this.letterListModel.size();
NeuralDataSet trainingSet = new BasicNeuralDataSet();
for (int t = 0; t < this.letterListModel.size(); t++) {
NeuralData item = new BasicNeuralData(inputNeuron);
int idx = 0;
final SampleData ds = (SampleData) this.letterListModel
.getElementAt(t);
for (int y = 0; y < ds.getHeight(); y++) {
for (int x = 0; x < ds.getWidth(); x++) {
item.setData(idx++,ds.getData(x, y) ? .5 : -.5);
}
}
trainingSet.add(new BasicNeuralDataPair(item,null));
}
this.net = new BasicNetwork();
this.net.addLayer(new SOMLayer(inputNeuron,NormalizationType.MULTIPLICATIVE));
this.net.addLayer(new BasicLayer(outputNeuron));
this.net.reset();
final TrainSelfOrganizingMap train = new TrainSelfOrganizingMap(
this.net, trainingSet,LearningMethod.SUBTRACTIVE,0.5);
int tries = 1;
do {
train.iteration();
update(tries++, train.getTotalError(), train.getBestError());
} while ((train.getTotalError() > MAX_ERROR) && !this.halt);
this.halt = true;
update(tries, train.getTotalError(), train.getBestError());
} catch (final Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(this, "Error: " + e, "Training",
JOptionPane.ERROR_MESSAGE);
}
}
/**
* Called when the save button is clicked.
*
* @param event
* The event
*/
void save_actionPerformed(final java.awt.event.ActionEvent event) {
try {
OutputStream os;// the actual file stream
PrintStream ps;// used to read the file line by line
os = new FileOutputStream("./sample.dat", false);
ps = new PrintStream(os);
for (int i = 0; i < this.letterListModel.size(); i++) {
final SampleData ds = (SampleData) this.letterListModel
.elementAt(i);
ps.print(ds.getLetter() + ":");
for (int y = 0; y < ds.getHeight(); y++) {
for (int x = 0; x < ds.getWidth(); x++) {
ps.print(ds.getData(x, y) ? "1" : "0");
}
}
ps.println("");
}
ps.close();
os.close();
clear_actionPerformed(null);
JOptionPane.showMessageDialog(this, "Saved to 'sample.dat'.",
"Training", JOptionPane.PLAIN_MESSAGE);
} catch (final Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(this, "Error: " + e, "Training",
JOptionPane.ERROR_MESSAGE);
}
}
/**
* Called when the train button is pressed.
*
* @param event
* The event.
*/
void train_actionPerformed(final java.awt.event.ActionEvent event) {
if (this.trainThread == null) {
this.train.setText("Stop Training");
this.train.repaint();
this.trainThread = new Thread(this);
this.trainThread.start();
} else {
this.halt = true;
}
}
/**
* Called to update the stats, from the neural network.
*
* @param trial
* How many tries.
* @param error
* The current error.
* @param best
* The best error.
*/
public void update(final int retry, final double totalError,
final double bestError) {
if (this.halt) {
this.trainThread = null;
this.train.setText("Begin Training");
JOptionPane.showMessageDialog(this, "Training has completed.",
"Training", JOptionPane.PLAIN_MESSAGE);
}
final UpdateStats stats = new UpdateStats();
stats._tries = retry;
stats._lastError = totalError;
stats._bestError = bestError;
try {
SwingUtilities.invokeAndWait(stats);
} catch (final Exception e) {
JOptionPane.showMessageDialog(this, "Error: " + e, "Training",
JOptionPane.ERROR_MESSAGE);
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -