📄 ocrdemo.java
字号:
} private void readSymbolFile(String fname) throws java.io.IOException { double dubval; int symbol = -1; int token = 0; int row,col; double[] N=new double[4]; //we won't use the zeroth element // FileReader inputFile; InputStream inputFile; StreamTokenizer stkz; inputFile = ClassLoader.getSystemResourceAsStream(fname); if(inputFile==null){ System.out.println("File" + fname + " not found"); System.exit(-1); }// try{// inputFile = new FileReader(fname);// // } catch (java.io.FileNotFoundException e) {// System.out.println("File " + fname + " not found");// return ;// } stkz = new StreamTokenizer(inputFile); stkz.resetSyntax(); stkz.eolIsSignificant(true); stkz.whitespaceChars(0, ' '); stkz.wordChars(33, 255); stkz.parseNumbers(); while (stkz.nextToken() != stkz.TT_EOF) { token++; switch (stkz.ttype) { case StreamTokenizer.TT_EOL: token = 0; break; case StreamTokenizer.TT_WORD: if ( stkz.sval.equalsIgnoreCase("symbol") ) { if ( stkz.nextToken() == stkz.TT_NUMBER) { symbol = (int) stkz.nval; } } do{stkz.nextToken();} while (stkz.ttype != stkz.TT_EOL); token = 0; break; case StreamTokenizer.TT_NUMBER: N[token] = stkz.nval; if (token == 3) { if (symbol != -1) { row = (int) N[1]; col = (int) N[2]; dubval = N[3]; inputImages[symbol][row][col] = dubval; } do{stkz.nextToken();} while (stkz.ttype != stkz.TT_EOL); token = 0; break; } } } } public void initTrainPanel(JPanel trainPanel) { startTrainBtn.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent evt) { startTrainBtnActionPerformed(evt); } } ); stopTrainBtn.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent evt) { stopTrainBtnActionPerformed(evt); } } ); resetNetworkBtn.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent evt) { resetNetworkBtnActionPerformed(evt); } } ); resetInputBtn.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent evt) { resetInputBtnActionPerformed(evt); } } ); saveAllErrBtn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent evt){ saveAllErrActionPerformed(evt); } }); JPanel ctrlPanel = new JPanel(); ctrlPanel.setLayout(new GridLayout(0,1,3,3)); ctrlPanel.setBorder(bevelBorder); ctrlPanel.add(startTrainBtn); ctrlPanel.add(stopTrainBtn); ctrlPanel.add(resetNetworkBtn); ctrlPanel.add(resetInputBtn); ctrlPanel.add(saveAllErrBtn); s1Text = new JTextField(Integer.toString(s1),3); s2Text = new JTextField(Integer.toString(s2),3); errorText = new JTextField(12); epochText = new JTextField("0",4); epochText.setEditable(false); errorText.setEditable(false); JPanel infoPanel = new JPanel(); infoPanel.setLayout(new GridLayout(0,2,2,2)); infoPanel.setBorder( BorderFactory.createCompoundBorder(bevelBorder, emptyBorder) ); infoPanel.add(epochLabel); infoPanel.add(epochText); infoPanel.add(errorLabel); infoPanel.add(errorText); infoPanel.add(s1Label); infoPanel.add(s1Text); infoPanel.add(s2Label); infoPanel.add(s2Text); JPanel tempPane = new JPanel(); tempPane.setLayout(new BorderLayout()); tempPane.add(infoPanel, BorderLayout.CENTER); tempPane.add(ctrlPanel, BorderLayout.EAST); JPanel outputPane = new JPanel(); outputPane.setLayout(new BorderLayout()); outputPane.setBorder( BorderFactory.createCompoundBorder(bevelBorder, BorderFactory.createEmptyBorder(9,9,9,9)) ); networkOutput = new DigitMatrix(10,10,12,12,.01f,.8f,false,true); outputPane.add(networkOutput, BorderLayout.CENTER); trainPanel.setLayout(new BorderLayout()); trainPanel.setBorder(BorderFactory.createTitledBorder("训练多层感知机")); trainPanel.add(tempPane, BorderLayout.CENTER); trainPanel.add(networkOutput, BorderLayout.EAST); } private void startTrainBtnActionPerformed(ActionEvent evt) { double[] singlePattern = new double[rawdim]; double[][] inputPatterns = new double[rawdim][nSymbols]; for (int j=0; j<nSymbols; j++) { singlePattern = symbolSet[j].getVector(); for (int i=0; i<rawdim; i++) { inputPatterns[i][j] = singlePattern[i]; } } if (!timer.isRunning()) { timer.start(); } isTraining = network.trainInit(inputPatterns,targets); } private void stopTrainBtnActionPerformed(ActionEvent evt) { isTraining = false; if (timer.isRunning()) { timer.stop(); } } private void resetNetworkBtnActionPerformed(ActionEvent evt) { if (isTraining) return ; int new_S1,new_S2,old_S1,old_S2; old_S1 = s1; old_S2 = s2; try { new_S1 = Integer.parseInt(s1Text.getText()); new_S2 = Integer.parseInt(s2Text.getText()); } catch (java.lang.NumberFormatException exc) { new_S1 = old_S1; new_S2 = old_S2; } s1Text.setText(Integer.toString(new_S1)); s2Text.setText(Integer.toString(new_S2)); s1 = new_S1; s2 = new_S2; network = new MLP(rawdim,s1,s2,s3); epochText.setText("0"); errorText.setText(""); testAllPatterns(); } private void resetInputBtnActionPerformed(ActionEvent evt) { try { readSymbolFile(symbolfile); } catch (java.io.IOException ex) { System.out.println("IO Exception... file error"); } testAllPatterns(); errorText.setText(""); } //------------------------------------------------------- //将误差和存为一个文件 private void saveAllErrActionPerformed(ActionEvent evt){ try{ DataOutputStream sendout= new DataOutputStream( new BufferedOutputStream( new FileOutputStream("Error.txt"))); sendout.writeChars(ends); sendout.close(); } catch(IOException e) { System.err.println("End of stream"); } } //保存一个数据 private void saveOnePattern(DigitMatrix csymbol){ double a; String b,c; double[] oinputVector=csymbol.getVector(); try{ DataOutputStream output = new DataOutputStream( new BufferedOutputStream( new FileOutputStream("Data.txt"))); for(int i=0;i<16;i++){ for(int j=0;j<12;j++){ //double inputDigit=symbol.getcell(i,j); output.writeChars(Integer.toString(i)); output.writeChar(','); output.writeChars(Integer.toString(j)); output.writeChar(','); //output.writeDouble(inputDigit.doubleValue); a=csymbol.getCell(i,j); // System.out.println(a); b=Double.toString(a); c=b.substring(0,1); output.writeChars(b); output.writeChar('\n'); } } /* output.writeChars(Double.toString(3.14159)); output.writeChars("That was pi\n"); output.writeBytes("That was pi\n"); output.writeDouble(1.41413); output.writeUTF("Square root of 2");*/ output.close(); } catch(IOException e) { System.err.println("End of stream"); } } /*help------------------------------------------------------ private void saveOnePattern(DigitMatrix symbol){ // symbol.filter() try{ DataOutputStream output=new DataOutputStream( new FileOutputStream("client.txt")); output.writeChars("Symbol\n"); for(int i=0;i<16;i++){ for(int j=0;j<12;j++){ //double inputDigit=symbol.getcell(i,j); output.writeChars(Integer.toString(i)); output.writeChars(Integer.toString(j)); //output.writeDouble(inputDigit.doubleValue); double a=symbol.getCell(i,j); output.writeDouble(a); output.writeChar('\n'); } } } catch(IOException e){ System.err.println("File not opened\n"+e.toString()); System.exit(1); } }*/ //-------------------------------------------------------------------- private void testTwoPattern(String s){ // double[] result; // result=s.valueOf(s); double[] result={10,20,30,40,50,60,70,80,90,100,110}; graphl.plot(result,30,2); } private void testOnePattern(DigitMatrix symbol){ double[] inputVector = symbol.getVector(); double[] result = network.testNet(inputVector); graph.plot(result, 30, 2); vectorLight(result); } //-------------------------------------------------------------------- private void vectorLight(double[] outputVector){ //Although double[][], outputVector should have only one column. for(int i=0; i<s3; i++){ symbolSetLight[i].setLED(outputVector[i]); } } //-------------------------------------------------------------------- private void testAllPatterns() { double[] singlePattern = new double[rawdim]; double[][] inputPatterns = new double[rawdim][nSymbols]; for (int j=0; j<nSymbols; j++) { singlePattern = symbolSet[j].getVector(); for (int i=0; i<rawdim; i++){ inputPatterns[i][j] = singlePattern[i]; } } double[][] result = network.testNet(inputPatterns);// matrixLight(result); networkOutput.setMatrix(result); } /** private void matrixLight(double[][] outputMatrix) { networkOutput.setMatrix(outputMatrix); }*/ /** * @param args the command line arguments */ public static void main(String args[]) { JFrame frame = new ocrdemo("三层神经元网络的数字识别"); frame.setBounds(0,0,800,600); frame.setVisible(true); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -