📄 ocrdemo.java
字号:
/* * ocrdemo.java * THIS is a swing-based OCR application with MLP neural network *//** * * @author wwwang */import javax.swing.*;import javax.swing.event.*;import java.awt.event.*;import java.awt.Container;import java.awt.BorderLayout;import java.awt.GridLayout;import java.io.*;import java.awt.Component;import javax.swing.border.Border;import java.util.*;public class ocrdemo extends JFrame{ private static final String symbolfile = "ochre.txt"; double[][] targets; float[] errall; boolean isTraining = false; private int rawdim,s1,s2,s3,nSymbols; MLP network; // The neural network. private double[][][] inputImages; private DigitMatrix[] symbolSet; private DigitMatrix curDigit; private DigitMatrix customDigit; private LedPanel[] symbolSetLight; private JPanel[] miniTestPanels; private XYPlot graph,graphl; private JButton startTrainBtn = new JButton("开始训练"); private JButton stopTrainBtn = new JButton("训练停止"); private JButton resetNetworkBtn = new JButton("重设网络"); private JButton resetInputBtn = new JButton("重新输入"); private JButton saveAllErrBtn = new JButton("误差输出"); private JButton clearBtn = new JButton("清除"); private JButton blurBtn = new JButton("淡化"); private JButton sharpBtn = new JButton("锐化"); private JButton recognizeBtn = new JButton("识别"); private JButton saveBtn=new JButton("保存"); private JLabel s1Label = new JLabel("第一隐含层的神经元:"); private JLabel s2Label = new JLabel("第二隐含层的神经元:"); private JLabel epochLabel = new JLabel("训练数:"); private JLabel errorLabel = new JLabel("分类误差和:"); private Border emptyBorder = BorderFactory.createEmptyBorder(4,4,4,4); private Border bevelBorder = BorderFactory.createLoweredBevelBorder(); private Border etchedBorder = BorderFactory.createEtchedBorder(); private JTextField s1Text, s2Text, errorText, epochText; private DigitMatrix networkOutput; private Vector v=new Vector(1); private String ends; private javax.swing.Timer timer; private static final int START_INDEX = 9; private String[] digitstrs = { "0", "1", "2", "3", "4", "5", "6", "7" , "8", "9" }; private JComboBox digitChoices; public ocrdemo(String title) { super(title); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); initContentPane(); testAllPatterns(); //Set up a timer that calls this object's action handler. timer = new javax.swing.Timer(100, new ActionListener() { public void actionPerformed(ActionEvent e) { timerAction(e); } } ); timer.setInitialDelay(0); timer.setCoalesce(true); } private void timerAction(ActionEvent e) { String errorval; int p=0; if (isTraining == false) { return ; } else { epochText.setText(Integer.toString(network.getepoch())); // errall[p]=(float)network.trainNet(); errorval = Float.toString((float)network.trainNet()); // System.out.println(errall[i]); errorText.setText(errorval); ends=addErrData(errorval); testAllPatterns(); } return ; }/**通过这个方法取得所有的均方误差值,并将他们用String类型返回。在前面建立了vector型的可变数组。 2个print是为了测试是否读入了误差值,数组的长度是否增长。 要是可以调用valueOf方法直接返回double将更好! */ public String addErrData(String errorval){ // System.out.println(errorval); v.addElement(errorval); String s=v.toString(); // int numb=v.size(); // System.out.println(numb); // System.out.println(s); // double[] allvalue=valueOf(s); // System.out.println(allvalue); return(s); } /** 如何让它能够继承上面的方法? public String addErr(){ super(errorval); }*/ private void initContentPane(){ rawdim = 192; s1 = 8; s2 = 6; s3 = 10; nSymbols = s3; targets = new double[s3][nSymbols]; for (int i=0; i<s3; i++) { for (int j=0; j<nSymbols; j++) { if (i==j) { targets[i][j]=1; } else { targets[i][j]=0; } } } /* Initialize a new MLP object */ network = new MLP(rawdim,s1,s2,s3); inputImages = new double[nSymbols][16][12]; /* Load the symbol forms from digitURL into symbolSet objects */ try { readSymbolFile(symbolfile); } catch (java.io.IOException e) { System.out.println("IO Exception... file error"); } JPanel testPanel, trainPanel,showPlot; testPanel = new JPanel(); initTestPanel(testPanel); trainPanel = new JPanel(); initTrainPanel(trainPanel); // showPlot=new JPanel(); // initShowPanel(showPlot); JPanel contentPane = (JPanel)getContentPane(); contentPane.setLayout(new GridLayout(2,1,5,5)); contentPane.setBorder( BorderFactory.createCompoundBorder(etchedBorder, emptyBorder) ); contentPane.add(trainPanel); contentPane.add(testPanel); //contentPane.add(showPlot); } //plot 误差输出图的机构和初始化。 /** public void initShowPanel(JPanel showPlot){ graphl = new XYPlot(); graphl.setBackground(java.awt.Color.black); graphl.setBorder(new javax.swing.border.LineBorder( new java.awt.Color(0,0,255), 1) ); JPanel showPanel = new JPanel(); showPanel.setLayout(new BorderLayout()); showPanel.setBorder( BorderFactory.createCompoundBorder( BorderFactory.createTitledBorder("误差输出图"), emptyBorder) ); showPanel.add(graphl, BorderLayout.CENTER); }*/ public void initTestPanel(JPanel testPanel) { digitChoices = new JComboBox(digitstrs); digitChoices.setSelectedIndex(START_INDEX); digitChoices.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent evt) { digitChoicesAction(evt); } } ); symbolSet = new DigitMatrix[nSymbols]; symbolSetLight = new LedPanel[nSymbols]; miniTestPanels = new JPanel[nSymbols]; for (int i=0;i<nSymbols;i++) { symbolSet[i] = new DigitMatrix(16,12,6,6,.1f,.6f,true,false); symbolSet[i].addMouseListener(symbolSet[i]); symbolSet[i].addMouseMotionListener(symbolSet[i]); symbolSet[i].addKeyListener(symbolSet[i]); symbolSet[i].setMatrix(inputImages[i]); symbolSetLight[i] = new LedPanel(); miniTestPanels[i] = new JPanel(); miniTestPanels[i].setLayout(new BorderLayout()); miniTestPanels[i].setBorder(etchedBorder); miniTestPanels[i].add( new JLabel(Integer.toString(i)), BorderLayout.NORTH ); miniTestPanels[i].add(symbolSetLight[i], BorderLayout.CENTER); } JPanel ledPane = new JPanel(); ledPane.setLayout( new GridLayout(0, nSymbols, 2, 2) ); for(int i=0; i<nSymbols; i++) { ledPane.add(miniTestPanels[i]); } curDigit = symbolSet[START_INDEX]; JPanel predefinedDigitPane = new JPanel(); predefinedDigitPane.add(curDigit); predefinedDigitPane.add(digitChoices); customDigit = new DigitMatrix(16,12,6,6,.1f,.6f,true,false); customDigit.addMouseListener(customDigit); customDigit.addMouseMotionListener(customDigit); clearBtn.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent evt) { clearBtnAction(evt); } } ); blurBtn.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent evt) { blurBtnAction(evt); } } ); sharpBtn.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent evt) { sharpBtnAction(evt); } } ); recognizeBtn.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent evt) { recognizeBtnAction(evt); } } ); saveBtn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent evt){ saveBtnAction(evt); } } ); JPanel custumCtrlPane = new JPanel(); custumCtrlPane.setLayout(new GridLayout(5,0,1,1)); custumCtrlPane.setBorder(emptyBorder); custumCtrlPane.add(clearBtn); custumCtrlPane.add(blurBtn); custumCtrlPane.add(sharpBtn); custumCtrlPane.add(recognizeBtn); custumCtrlPane.add(saveBtn); JPanel customDigitPane = new JPanel(); customDigitPane.setLayout(new BorderLayout()); customDigitPane.setBorder(BorderFactory.createCompoundBorder( BorderFactory.createTitledBorder("画图"), emptyBorder)); customDigitPane.add(customDigit, BorderLayout.CENTER); customDigitPane.add(custumCtrlPane, BorderLayout.EAST); JPanel displayPanel = new JPanel(); displayPanel.setBorder(BorderFactory.createCompoundBorder( BorderFactory.createTitledBorder("图像输入"), emptyBorder)); displayPanel.setLayout( new GridLayout(0,2,3,3) ); displayPanel.add(predefinedDigitPane); displayPanel.add(customDigitPane); JPanel leftPane = new JPanel(); leftPane.setLayout(new BorderLayout()); leftPane.add(displayPanel, BorderLayout.CENTER); leftPane.add(ledPane, BorderLayout.SOUTH); graph = new XYPlot(); graph.setBackground(java.awt.Color.black); graph.setBorder(new javax.swing.border.LineBorder( new java.awt.Color(0,0,255), 1) ); JPanel outputPane = new JPanel(); outputPane.setLayout(new BorderLayout()); outputPane.setBorder( BorderFactory.createCompoundBorder( BorderFactory.createTitledBorder("网络输出"), emptyBorder) ); outputPane.add(graph, BorderLayout.CENTER); testPanel.setLayout( new GridLayout(1,1,4,4)); testPanel.setBorder(emptyBorder); testPanel.add(leftPane); testPanel.add(outputPane); } private void digitChoicesAction( ActionEvent e) { int digitIndex = -1; if ("comboBoxChanged".equals(e.getActionCommand())) { digitIndex = digitChoices.getSelectedIndex(); curDigit.setMatrix(inputImages[digitIndex]); testOnePattern( symbolSet[digitIndex] ); } return ; } private void clearBtnAction(ActionEvent e) { customDigit.clear(); } private void blurBtnAction(ActionEvent e) { customDigit.filter(DigitMatrix.BLUR); } private void sharpBtnAction(ActionEvent e) { customDigit.filter(DigitMatrix.SHARPEN); } private void recognizeBtnAction(ActionEvent e) { testOnePattern(customDigit); testTwoPattern(ends); } //save private void saveBtnAction(ActionEvent e){ saveOnePattern(customDigit);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -