PocketSphinx AndrodDemo

PocketSphinx总的来说识别效果不是很好,很容易出现不说话时也出现识别范围内的词。目前只用到关键词识别功能。
具体的demo在https://github.com/cmusphinx/pocketsphinx-android-demo链接下载。
用到的资源都在demo中取。
libs/pocketsphinx-android-5prealpha-nolib.jar
assets/sync
assets/sync/cmudict-en-us.dict
assets/sync/cmudict-en-us.dict.md5
assets/sync/en-us-ptm
assets/sync/assets.lst
jniLibs/armeabi-v7a/libpocketsphinx_jni.so

直接调用
initPocketSphinxRecognizer()接口就可启动PocketSphinx关键词识别。

代码如下:

/* Named searches allow to quickly reconfigure the decoder */
private static final String KWS_SEARCH = "wakeup";
/* Keyword we are looking for to activate menu */
private static final String KEYPHRASE = "mighty computer";
private edu.cmu.pocketsphinx.SpeechRecognizer mPocketSphinxRecognizer;
private void setupPocketSphinxRecognizer(File assetsDir) throws IOException {
    // The recognizer can be configured to perform multiple searches
    // of different kind and switch between them

    mPocketSphinxRecognizer = defaultSetup()
            .setAcousticModel(new File(assetsDir, "en-us-ptm"))
            .setDictionary(new File(assetsDir, "cmudict-en-us.dict"))
            // To disable logging of raw audio comment out this call (takes a lot of space on the device)
            //.setRawLogDir(assetsDir)

            // Threshold to tune for keyphrase to balance between false alarms and misses
            .setKeywordThreshold(1e-35f)

                    // Use context-independent phonetic search, context-dependent is too slow for mobile
            .setBoolean("-allphone_ci", true)

            .getRecognizer();
    mPocketSphinxRecognizer.addListener(mRecognitionListener);

    // Create keyword-activation search.
    mPocketSphinxRecognizer.addKeyphraseSearch(KWS_SEARCH, KEYPHRASE);

}
private void switchToPocketSphinxRecognizer(String searchName) {
    if(mPocketSphinxRecognizer != null) {
        mPocketSphinxRecognizer.stop();
        // If we are not spotting, start listening with timeout (10000 ms or 10 seconds).
        mPocketSphinxRecognizer.startListening(searchName);
    }
}

private void initPocketSphinxRecognizer(){
    new AsyncTask<Void, Void, Exception>() {
        @Override
        protected Exception doInBackground(Void... params) {
            try {
                Assets assets = new Assets(SearchActivity.this);
                File assetDir = assets.syncAssets();
                setupPocketSphinxRecognizer(assetDir);
            } catch (IOException e) {
                return e;
            }
            return null;
        }

        @Override
        protected void onPostExecute(Exception result) {
            if (result != null) {
                ToastUtil.showShortToast(SearchActivity.this,  "Failed to init pocketsphinx recognizer " + result);
            }else{
                switchToPocketSphinxRecognizer(KWS_SEARCH);
            }
        }
    }.execute();
}
edu.cmu.pocketsphinx.RecognitionListener mRecognitionListener = new edu.cmu.pocketsphinx.RecognitionListener(){
    @Override
    public void onBeginningOfSpeech() {

    }
    /**
     * We stop recognizer here to get a final result
     */
    @Override
    public void onEndOfSpeech() {
    }

    /**
     * In partial result we get quick updates about current hypothesis. In
     * keyword spotting mode we can react here, in other modes we need to wait
     * for final result in onResult.
     */
    @Override
    public void onPartialResult(Hypothesis hypothesis) {
        if (hypothesis == null)
            return;
        String text = hypothesis.getHypstr();
        Log.e("TAG", "text:" + text);
        if (text.equals(KEYPHRASE)) {
            mPocketSphinxRecognizer.stop();
            
        }
    }
    /**
     * This callback is called when we stop the recognizer.
     */
    @Override
    public void onResult(Hypothesis hypothesis) {

    }

    @Override
    public void onError(Exception error) {
        switchToPocketSphinxRecognizer(KWS_SEARCH);
    }

    @Override
    public void onTimeout() {
        switchToPocketSphinxRecognizer(KWS_SEARCH);
    }
};
/* Named searches allow to quickly reconfigure the decoder */
private static final String KWS_SEARCH = "wakeup";
/* Keyword we are looking for to activate menu */
private static final String KEYPHRASE = "mighty computer";
private edu.cmu.pocketsphinx.SpeechRecognizer mPocketSphinxRecognizer;
private void setupPocketSphinxRecognizer(File assetsDir) throws IOException {
    // The recognizer can be configured to perform multiple searches
    // of different kind and switch between them

    mPocketSphinxRecognizer = defaultSetup()
            .setAcousticModel(new File(assetsDir, "en-us-ptm"))
            .setDictionary(new File(assetsDir, "cmudict-en-us.dict"))
            // To disable logging of raw audio comment out this call (takes a lot of space on the device)
            //.setRawLogDir(assetsDir)

            // Threshold to tune for keyphrase to balance between false alarms and misses
            .setKeywordThreshold(1e-35f)

                    // Use context-independent phonetic search, context-dependent is too slow for mobile
            .setBoolean("-allphone_ci", true)

            .getRecognizer();
    mPocketSphinxRecognizer.addListener(mRecognitionListener);

    // Create keyword-activation search.
    mPocketSphinxRecognizer.addKeyphraseSearch(KWS_SEARCH, KEYPHRASE);

}
private void switchToPocketSphinxRecognizer(String searchName) {
    if(mPocketSphinxRecognizer != null) {
        mPocketSphinxRecognizer.stop();
        // If we are not spotting, start listening with timeout (10000 ms or 10 seconds).
        mPocketSphinxRecognizer.startListening(searchName);
    }
}

private void initPocketSphinxRecognizer(){
    new AsyncTask<Void, Void, Exception>() {
        @Override
        protected Exception doInBackground(Void... params) {
            try {
                Assets assets = new Assets(SearchActivity.this);
                File assetDir = assets.syncAssets();
                setupPocketSphinxRecognizer(assetDir);
            } catch (IOException e) {
                return e;
            }
            return null;
        }

        @Override
        protected void onPostExecute(Exception result) {
            if (result != null) {
                ToastUtil.showShortToast(SearchActivity.this,  "Failed to init pocketsphinx recognizer " + result);
            }else{
                switchToPocketSphinxRecognizer(KWS_SEARCH);
            }
        }
    }.execute();
}
edu.cmu.pocketsphinx.RecognitionListener mRecognitionListener = new edu.cmu.pocketsphinx.RecognitionListener(){
    @Override
    public void onBeginningOfSpeech() {

    }
    /**
     * We stop recognizer here to get a final result
     */
    @Override
    public void onEndOfSpeech() {
    }

    /**
     * In partial result we get quick updates about current hypothesis. In
     * keyword spotting mode we can react here, in other modes we need to wait
     * for final result in onResult.
     */
    @Override
    public void onPartialResult(Hypothesis hypothesis) {
        if (hypothesis == null)
            return;
        String text = hypothesis.getHypstr();
        Log.e("TAG", "text:" + text);
        if (text.equals(KEYPHRASE)) {
            mPocketSphinxRecognizer.stop();
            
        }
    }
    /**
     * This callback is called when we stop the recognizer.
     */
    @Override
    public void onResult(Hypothesis hypothesis) {

    }

    @Override
    public void onError(Exception error) {
        switchToPocketSphinxRecognizer(KWS_SEARCH);
    }

    @Override
    public void onTimeout() {
        switchToPocketSphinxRecognizer(KWS_SEARCH);
    }
};


作者:gsldqtan
来源:CSDN
原文:https://blog.csdn.net/gsldqtan/article/details/54945749
版权声明:本文为博主原创文章,转载请附上博文链接!

可以实现录音,有效率超过百分之九十九 package edu.cmu.pocketsphinx.demo; import java.util.Date; import android.app.Activity; import android.app.ProgressDialog; import android.content.Intent; import android.os.Bundle; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class PocketSphinxIntent extends Activity implements OnTouchListener, RecognitionListener { static { System.loadLibrary("pocketsphinx_jni"); } public static final String EXTRA_RESULTS = "PockectSphinxExtraResults"; /** * Recognizer task, which runs in a worker thread. */ RecognizerTask rec; /** * Thread in which the recognizer task runs. */ Thread rec_thread; /** * Time at which current recognition started. */ Date start_date; /** * Number of seconds of speech. */ float speech_dur; /** * Are we listening? */ boolean listening; /** * Progress dialog for final recognition. */ ProgressDialog rec_dialog; /** * Performance counter view. */ TextView performance_text; /** * Editable text view. */ EditText edit_text; Intent intent; /** * Respond to touch events on the Speak button. * * This allows the Speak button to function as a "push and hold" button, by * triggering the start of recognition when it is first pushed, and the end * of recognition when it is released. * * @param v * View on which this event is called * @param event * Event that was triggered. */ public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: start_date = new Date(); this.listening = true; this.rec.start(); break; case MotionEvent.ACTION_UP: Date end_date = new Date();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值