2

我正在使用最新的 pocketsphinx android 演示(强大的计算机),它从麦克风获取输入。我想给一个wav文件作为输入。我尝试使用 decoder.processrow() 函数。但我不知道如何使用 hmm、lm 等配置解码器。

4

2 回答 2

3

在 pocketsphinx-java 中处理文件的代码

    Config c = Decoder.defaultConfig();
    c.setString("-hmm", "../../model/en-us/en-us");
    c.setString("-lm", "../../model/en-us/en-us.lm.dmp");
    c.setString("-dict", "../../model/en-us/cmudict-en-us.dict");
    Decoder d = new Decoder(c);

    URL testwav = new URL("file:../../test/data/goforward.wav");
    FileInputStream stream = new FileInputStream(new File(testwav)));

    d.startUtt();
    byte[] b = new byte[4096];
    try {
        int nbytes;
        while ((nbytes = stream.read(b)) >= 0) {
            ByteBuffer bb = ByteBuffer.wrap(b, 0, nbytes);

            // Not needed on desktop but required on android
            bb.order(ByteOrder.LITTLE_ENDIAN); 

            short[] s = new short[nbytes/2];
            bb.asShortBuffer().get(s);
            d.processRaw(s, nbytes/2, false, false);
        }
    } catch (IOException e) {
        fail("Error when reading goforward.wav" + e.getMessage());
    }
    d.endUtt();
    System.out.println(d.hyp().getHypstr());
    for (Segment seg : d.seg()) {
        System.out.println(seg.getWord());
    }
}
于 2015-03-28T11:54:42.560 回答
1

添加到 Nikolay 的答案中,这就是它可以在 Android 上完成的方法,调整此处找到的 SpeechRecognizer Android 实现示例:http: //cmusphinx.sourceforge.net/wiki/tutorialandroid

//statically load our library
static {
    System.loadLibrary("pocketsphinx_jni");
}

//convert an inputstream to text
private void convertToSpeech(final InputStream stream){
    new AsyncTask<Void, Void, Exception>() {
        @Override
        protected Exception doInBackground(Void... params) {
            try {
                Assets assets = new Assets(WearService.this);
                File assetsDir = assets.syncAssets();
                Config c = Decoder.defaultConfig();
                c.setString("-hmm", new File(assetsDir, "en-us-ptm").getPath());
                c.setString("-dict", new File(assetsDir, "cmudict-en-us.dict").getPath());
                c.setBoolean("-allphone_ci", true);
                c.setString("-lm", new File(assetsDir, "en-phone.dmp").getPath());
                Decoder d = new Decoder(c);

                d.startUtt();
                byte[] b = new byte[4096];
                try {
                    int nbytes;
                    while ((nbytes = stream.read(b)) >= 0) {
                        ByteBuffer bb = ByteBuffer.wrap(b, 0, nbytes);

                        // Not needed on desktop but required on android
                        bb.order(ByteOrder.LITTLE_ENDIAN);

                        short[] s = new short[nbytes/2];
                        bb.asShortBuffer().get(s);
                        d.processRaw(s, nbytes/2, false, false);
                    }
                } catch (IOException e) {
                    fail("Error when reading inputstream" + e.getMessage());
                }
                d.endUtt();
                System.out.println(d.hyp().getHypstr());
                for (Segment seg : d.seg()) {
                    //do something with the result here
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

    }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
于 2016-06-05T10:27:01.160 回答