?? 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 + -