Is there a library for Voice recognition in Java? [closed]

1

I have already searched for my question to ask, and wanted to know if you know of any bookstore and how to use it?

If you could give me a tutorial, pdf, etc. I would really appreciate it.

    
asked by Jorge Anza 16.10.2016 в 06:06
source

1 answer

2

but from what I've seen the best use online services and are paid, for example the google. I opted for CMU Sphinx which is free, but I found it a little complex to start using it. In that sense I'll make a copy past of a couple of essential files of my project to serve as a "tutorial" to start. Since I did not find a valid updated tutorial and I got to go fighting me step by step.

First of all, create the project as a java maven project. And I added there the dependencies of Sphinx. In the pom.xml file of the maven configuration, add (within project, for example under the properties tag):

<dependencies>
    <dependency>
        <groupId>edu.cmu.sphinx</groupId>
        <artifactId>sphinx4-core</artifactId>
        <version>5prealpha-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>edu.cmu.sphinx</groupId>
        <artifactId>sphinx4-data</artifactId>
        <version>5prealpha-SNAPSHOT</version>
    </dependency> 
</dependencies>

I have needed to create this class to transform the wav into a valid file to recognize the library (the method used for voice recognition is to record an audio, and then read it with the library.) It is not "on demand" ).

import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.TargetDataLine;

/**
 *
 * @author idelcano
 */
public class WavTool {

    static long RECORD_TIME = 10000;  // 10 seconds

    // path of the wav file
    File wavFile = new File("test.wav");

    // format of audio file
    AudioFileFormat.Type fileType = AudioFileFormat.Type.WAVE;

    // the line from which audio data is captured
    TargetDataLine line;

    /**
     * Defines an audio format
     */
    AudioFormat getAudioFormat() {
        float sampleRate = 16000;
        int sampleSizeInBits = 16;
        int channels = 1;
        boolean signed = true;
        boolean bigEndian = true;
        AudioFormat format = new AudioFormat(sampleRate, sampleSizeInBits,
                channels, signed, bigEndian);
        return format;
    }

    /**
     * Captures the sound and record into a WAV file
     */
    void start() {
        try {
            AudioFormat format = getAudioFormat();

            DataLine.Info info;
            info = new DataLine.Info(TargetDataLine.class, format);
            line = (TargetDataLine) AudioSystem.getLine(info);
            line.open(format);
            line.start();   // start capturing

            System.out.println("Start capturing...");

            AudioInputStream ais = new AudioInputStream(line);

            System.out.println("Start recording...");

            // start recording
            AudioSystem.write(ais, fileType, wavFile);

        } catch (LineUnavailableException ex) {
            ex.printStackTrace();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }

    /**
     * Closes the target data line to finish capturing and recording
     */
    void finish() {
        line.stop();
        line.close();
        System.out.println("Finished");
    }

    /**
     * Entry to run the program
     */
    public static void record(int time) {
        RECORD_TIME=time*100;
        final WavTool recorder = new WavTool();

        // creates a new thread that waits for a specified
        // of time before stopping
        Thread stopper = new Thread(new Runnable() {
            public void run() {
                try {
                    Thread.sleep(RECORD_TIME);
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
                recorder.finish();
            }
        });

        stopper.start();

        // start recording
        recorder.start();
    }

}

And to prove it, create this method (the official example did not work for me because it was obsolete at least at the time I tried it, you have commented the necessary files for a correct speech recognition in English, but without commenting are the ones that download from the web that comes out in comments and placed in the folder is at the root of the project.):

public void tryProgram() {
    WavTool.record(30);
    try {
        Configuration configuration = new Configuration();

        //ENG
        //configuration.setAcousticModelPath("resource:/edu/cmu/sphinx/models/en-us/en-us");
        //configuration.setDictionaryPath("resource:/edu/cmu/sphinx/models/en-us/cmudict-en-us.dict");
        //configuration.setLanguageModelPath("resource:/edu/cmu/sphinx/models/en-us/en-us.lm.bin");
        //SPANISH downloaded from https://sourceforge.net/projects/cmusphinx/files/Acoustic%20and%20Language%20Models/Spanish/
        configuration.setAcousticModelPath("es/cmusphinx-es-5.2/model_parameters/voxforge_es_sphinx.cd_ptm_4000");
        configuration.setDictionaryPath("es/es.dict");
        configuration.setLanguageModelPath("es/es-20k.lm");

        StreamSpeechRecognizer recognizer = new StreamSpeechRecognizer(
                configuration);
        File file = new File("test.wav");
        InputStream stream = new FileInputStream(file);
        recognizer.startRecognition(stream);
        SpeechResult result; 
        while ((result = recognizer.getResult()) != null) {
            //recognized text
            System.out.format("Hypothesis: %s\n", Utils.fixEncode(result.getHypothesis())); 
        }
        recognizer.stopRecognition();
    } catch (IOException ex) {
        Logger.getLogger(MainApp.class.getName()).log(Level.SEVERE, null, ex);
    }
}

And the method to correct the encoding of the text recognized by the library in Spanish:

public static String fixEncode(String hypothesis) {
    try {
        byte ptext[] = hypothesis.getBytes("ISO-8859-1");
        String value2 = new String(ptext, "UTF-8");
        return value2;
    } catch (UnsupportedEncodingException ex) {
        Logger.getLogger(FXMLController.class.getName()).log(Level.SEVERE, null, ex);
    }
    return "";
}
    
answered by 16.10.2016 в 09:24