CMUSphinx:关键字未被识别

时间:2017-04-28 03:29:44

标签: java android speech-recognition pocketsphinx pocketsphinx-android

我几天前做了语音识别服务它工作正常,但现在当我再次在我的手机中运行时,当我说出它只是“onstart”和“onend”并且没有被识别但有时它的“onstart”并且听了对于关键字采取行动,我如何确保每次说出正确识别的关键字

public class VoiceService extends Service implements
    RecognitionListener{

private static final String LOG_TAG = VoiceService.class.getSimpleName();

private static final String KWS_SEARCH = "wakeup";
private static final String KEYPHRASE = "okay computer";
private SpeechRecognizer recognizer;

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
        runRecognizerSetup();
    }
    return super.onStartCommand(intent, flags, startId);
}

private void runRecognizerSetup() {
    // Recognizer initialization is a time-consuming and it involves IO,
    // so we execute it in async task
    new AsyncTask<Void, Void, Exception>() {
        @Override
        protected Exception doInBackground(Void... params) {
            try {
                Assets assets = new Assets(VoiceService.this);
                File assetDir = assets.syncAssets();
                setupRecognizer(assetDir);
            } catch (IOException e) {
                return e;
            }
            return null;
        }

        @Override
        protected void onPostExecute(Exception e) {
            if (e != null) {
                Log.i(LOG_TAG, "Failed to init recognizer ");
            } else {
              //  switchSearch(KWS_SEARCH);
                startListening(KWS_SEARCH);
            }
        }
    }.execute();
}

private void startListening(String kwsSearch) {
    recognizer.startListening(KWS_SEARCH);
    Log.i(LOG_TAG, "startedlistening");
}

@Override
public void onDestroy() {
}


@Override
public void onPartialResult(Hypothesis hypothesis) {
    if (hypothesis == null)
        return;

    String text = hypothesis.getHypstr();
    if (text.contains(KEYPHRASE)) {
        switchSearch(KWS_SEARCH);
    }
}

/**
 * This callback is called when we stop the recognizer.
 */
@Override
public void onResult(Hypothesis hypothesis) {
    if (hypothesis != null) {
        String text = hypothesis.getHypstr();
        Intent Intent = new Intent(this, ReceiverActivity.class);
        Intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(Intent);

    }

}

@Override
public void onBeginningOfSpeech() {
    Log.i(LOG_TAG, "onBeginningOfSpeech");
}


@Override
public void onEndOfSpeech() {
    if (!recognizer.getSearchName().contains(KWS_SEARCH))
        recognizer.stop();
        recognizer.startListening(KWS_SEARCH);
    Log.i(LOG_TAG, "onEndOfSpeech");
}

private void switchSearch(String searchName) {
    if (recognizer != null) {
        recognizer.stop();
        recognizer.startListening(searchName);

    }
}



private void setupRecognizer(File assetsDir) throws IOException {
    recognizer = SpeechRecognizerSetup.defaultSetup()

            .setAcousticModel(new File(assetsDir, "en-us-ptm"))
            .setDictionary(new File(assetsDir, "cmudict-en-us.dict"))
            .setRawLogDir(assetsDir) 
            .setKeywordThreshold(1e-45f) 
            .setBoolean("-allphone_ci", true) 
            .getRecognizer();
    recognizer.addKeyphraseSearch(KWS_SEARCH, KEYPHRASE);
    recognizer.addListener(this);
}

@Override
public void onError(Exception error) {
    Log.i(LOG_TAG, "onError " + error.getMessage());
}

@Override
public void onTimeout() {
    switchSearch(KWS_SEARCH);
    Log.i(LOG_TAG, "onTimeout");
}
}

Keyword.gram

okay computer /1e-30/

0 个答案:

没有答案
相关问题