Search code examples
javaocrencoghandwriting-recognition

Handwriting recognition from image to string


I'm using Encog and I ran the ocr sample. It works fine. However, I want to pass an image file (png, jpg,...) as a parameter. This image contains the text to to be recognized. Then, the system should return a string with the "same" text.

Has someone already did something similar? How should I start?

Thanks!


Solution

  • Step 1: In GUI create file input and take file from user

    JFileChooser fc;
    JButton b, b1;
    JTextField tf;
    FileInputStream in;
    Socket s;
    DataOutputStream dout;
    DataInputStream din;
    int i;
    
    public void actionPerformed(ActionEvent e) {
    try {
        if (e.getSource() == b) {
            int x = fc.showOpenDialog(null);
            if (x == JFileChooser.APPROVE_OPTION) {
                fileToBeSent = fc.getSelectedFile();
                tf.setText(f1.getAbsolutePath());
                b1.setEnabled(true);
            } else {
                fileToBeSent = null;
                tf.setText(null;);
                b1.setEnabled(false);
            }
        }
        if (e.getSource() == b1) {
            send();
        }
    } catch (Exception ex) {
    }
    }
    
     public void copy() throws IOException {
        File f1 = fc.getSelectedFile();
        tf.setText(f1.getAbsolutePath());
        in = new FileInputStream(f1.getAbsolutePath());
        while ((i = in.read()) != -1) {
            System.out.print(i);
        }
    }
    
    public void send() throws IOException {
        dout.write(i);
        dout.flush();
    
    }
    

    Step 2: Down sample it

      private void processNetwork() throws IOException {
        System.out.println("Downsampling images...");
    
        for (final ImagePair pair : this.imageList) {
            final MLData ideal = new BasicMLData(this.outputCount);
            final int idx = pair.getIdentity();
            for (int i = 0; i < this.outputCount; i++) {
                if (i == idx) {
                    ideal.setData(i, 1);
                } else {
                    ideal.setData(i, -1);
                }
            }
    
            final Image img = ImageIO.read(fc.getFile());
            final ImageMLData data = new ImageMLData(img);
            this.training.add(data, ideal);
        }
    
        final String strHidden1 = getArg("hidden1");
        final String strHidden2 = getArg("hidden2");
    
        this.training.downsample(this.downsampleHeight, this.downsampleWidth);
    
        final int hidden1 = Integer.parseInt(strHidden1);
        final int hidden2 = Integer.parseInt(strHidden2);
    
        this.network = EncogUtility.simpleFeedForward(this.training
                .getInputSize(), hidden1, hidden2,
                this.training.getIdealSize(), true);
        System.out.println("Created network: " + this.network.toString());
    }
    

    Step 3: Begin Training with training set

     private void processTrain() throws IOException {
        final String strMode = getArg("mode");
        final String strMinutes = getArg("minutes");
        final String strStrategyError = getArg("strategyerror");
        final String strStrategyCycles = getArg("strategycycles");
    
        System.out.println("Training Beginning... Output patterns="
                + this.outputCount);
    
        final double strategyError = Double.parseDouble(strStrategyError);
        final int strategyCycles = Integer.parseInt(strStrategyCycles);
    
        final ResilientPropagation train = new ResilientPropagation(this.network, this.training);
        train.addStrategy(new ResetStrategy(strategyError, strategyCycles));
    
        if (strMode.equalsIgnoreCase("gui")) {
            TrainingDialog.trainDialog(train, this.network, this.training);
        } else {
            final int minutes = Integer.parseInt(strMinutes);
            EncogUtility.trainConsole(train, this.network, this.training,
                    minutes);
        }
        System.out.println("Training Stopped...");
    }
    

    Step 4: Give down sampled file to neural network

     public void processWhatIs() throws IOException {
        final String filename = getArg("image");
        final File file = new File(filename);
        final Image img = ImageIO.read(file);
        final ImageMLData input = new ImageMLData(img);
        input.downsample(this.downsample, false, this.downsampleHeight,
                this.downsampleWidth, 1, -1);
        final int winner = this.network.winner(input);
        System.out.println("What is: " + filename + ", it seems to be: "
                + this.neuron2identity.get(winner));
     }
    

    Step 5: Check Result