Acoustic model path not set right CMU Sphinx

2.7k Views Asked by At

The code written below is to convert audio to text using CMU Sphinx in Java 1.6 and Eclipse Helios.

import java.io.FileInputStream;
import java.io.IOException;
import java.io.FileNotFoundException;

import edu.cmu.sphinx.api.Configuration;
import edu.cmu.sphinx.api.SpeechResult;
import edu.cmu.sphinx.api.StreamSpeechRecognizer;

public class AudioToText {
    public static void main(String [] args) throws FileNotFoundException,IOException{
        Configuration configuration = new Configuration();

        // Set path to acoustic model.
        configuration.setAcousticModelPath("C:/Program Files/eclipse/sphinx4-5prealpha/models/acoustic");
        // Set path to dictionary.
        configuration.setDictionaryPath("C:/Program Files/eclipse/sphinx4-5prealpha/models/acoustic/wsj/dict/cmudict.0.6d");
        // Set language model.
        configuration.setLanguageModelPath("C:/Program Files/eclipse/sphinx4-5prealpha/models/language/en-us.lm.dmp");

        StreamSpeechRecognizer recognizer = new StreamSpeechRecognizer(configuration);
        //recognizer.startRecognition(new File("D:/audio.mp3").toURI().toURL());
        recognizer.startRecognition(new FileInputStream("D:/audio.mp3"));
        SpeechResult result;
        while ((result = recognizer.getResult()) != null) {
            System.out.println(result.getHypothesis());
        }
        recognizer.stopRecognition();
    }
}

Exceptions are arising because of not setting the path of acoustic model correctly as mentioned below:

   Exception in thread "main" Property exception component:'acousticModelLoader' property:'location' - Bad URL C:/Program Files/eclipse/sphinx4-5prealpha/models/acousticunknown protocol: c
edu.cmu.sphinx.util.props.InternalConfigurationException: Bad URL C:/Program Files/eclipse/sphinx4-5prealpha/models/acousticunknown protocol: c
    at edu.cmu.sphinx.util.props.ConfigurationManagerUtils.getResource(ConfigurationManagerUtils.java:479)
    at edu.cmu.sphinx.linguist.acoustic.tiedstate.Sphinx3Loader.newProperties(Sphinx3Loader.java:246)
    at edu.cmu.sphinx.util.props.PropertySheet.getOwner(PropertySheet.java:508)
    at edu.cmu.sphinx.util.props.PropertySheet.getComponent(PropertySheet.java:290)
    at edu.cmu.sphinx.linguist.acoustic.tiedstate.TiedStateAcousticModel.newProperties(TiedStateAcousticModel.java:102)
    at edu.cmu.sphinx.util.props.PropertySheet.getOwner(PropertySheet.java:508)
    at edu.cmu.sphinx.util.props.PropertySheet.getComponent(PropertySheet.java:290)
    at edu.cmu.sphinx.linguist.lextree.LexTreeLinguist.newProperties(LexTreeLinguist.java:301)
    at edu.cmu.sphinx.util.props.PropertySheet.getOwner(PropertySheet.java:508)
    at edu.cmu.sphinx.util.props.PropertySheet.getComponent(PropertySheet.java:290)
    at edu.cmu.sphinx.decoder.search.WordPruningBreadthFirstSearchManager.newProperties(WordPruningBreadthFirstSearchManager.java:199)
    at edu.cmu.sphinx.util.props.PropertySheet.getOwner(PropertySheet.java:508)
    at edu.cmu.sphinx.util.props.PropertySheet.getComponent(PropertySheet.java:290)
    at edu.cmu.sphinx.decoder.AbstractDecoder.newProperties(AbstractDecoder.java:71)
    at edu.cmu.sphinx.decoder.Decoder.newProperties(Decoder.java:37)
    at edu.cmu.sphinx.util.props.PropertySheet.getOwner(PropertySheet.java:508)
    at edu.cmu.sphinx.util.props.PropertySheet.getComponent(PropertySheet.java:290)
    at edu.cmu.sphinx.recognizer.Recognizer.newProperties(Recognizer.java:90)
    at edu.cmu.sphinx.util.props.PropertySheet.getOwner(PropertySheet.java:508)
    at edu.cmu.sphinx.util.props.ConfigurationManager.lookup(ConfigurationManager.java:161)
    at edu.cmu.sphinx.api.Context.<init>(Context.java:77)
    at edu.cmu.sphinx.api.Context.<init>(Context.java:49)
    at edu.cmu.sphinx.api.AbstractSpeechRecognizer.<init>(AbstractSpeechRecognizer.java:37)
    at edu.cmu.sphinx.api.StreamSpeechRecognizer.<init>(StreamSpeechRecognizer.java:33)
    at AudioToText.main(AudioToText.java:21)
Caused by: java.net.MalformedURLException: unknown protocol: c
    at java.net.URL.<init>(URL.java:574)
    at java.net.URL.<init>(URL.java:464)
    at java.net.URL.<init>(URL.java:413)
    at edu.cmu.sphinx.util.props.ConfigurationManagerUtils.resourceToURL(ConfigurationManagerUtils.java:495)
    at edu.cmu.sphinx.util.props.ConfigurationManagerUtils.getResource(ConfigurationManagerUtils.java:472)

I have specified path to acoustic folder. How to specify the correct path?

3

There are 3 best solutions below

0
Rajind Ruparathna On
private static final String ACOUSTIC_MODEL_PATH = 
        TextAligner.class.getResource("/resources/models/acoustic/wsj").toString();
configuration = new Configuration();
configuration.setAcousticModelPath(ACOUSTIC_MODEL_PATH);

I guess you can do it like this if you have added acoustic model to a resources folder in your project folder.

0
Vineet Kasat On

Change configuration.setAcousticModelPath("C:/Program Files/eclipse/sphinx4-5prealpha/models/acoustic");

to

configuration.setAcousticModelPath("file:C:\Program Files\eclips\sphinx4-5prealpha\models\\acoustic");

It should work then.

1
FieldMouse On
private static String ACOUSTIC_MODEL = "file:///C:/zero/zero_ru.cd_cont_4000";
private static String LANGUAGE_MODEL = "file:///C:/zero/ru.lm";
private static String DICTIONARY     = "file:///C:/zero/ru.dic";