android - Decoder_setSearch 返回 -1 异常

标签 android speech-recognition cmusphinx

我最近查看了由 CMUSphinx 提供支持的 Android 版 PocketSphinx,在这里找到:https://github.com/cmusphinx/pocketsphinx-android-demo 。我对它做了一些修改,看看我能做什么。

public class PocketSphinxActivity extends Activity implements
    RecognitionListener {

/* Named searches allow to quickly reconfigure the decoder */
private static final String KWS_SEARCH = "wakeup";
private static final String OVER_COMMAND = "overshield";
private static final String INVISIBLE_COMMAND = "invis";
private static final String POWER_COMMAND = "power";
private static final String MENU_SEARCH = "menu";

/* Keyword we are looking for to activate menu */
private static final String KEYPHRASE = "ready coach";
private boolean isGameStarted = false;

/* Used to handle permission request */
private static final int PERMISSIONS_REQUEST_RECORD_AUDIO = 1;

private SpeechRecognizer recognizer;
private HashMap<String, Integer> captions;

@Override
public void onCreate(Bundle state) {
    super.onCreate(state);

    // Prepare the data for UI
    captions = new HashMap<String, Integer>();
    captions.put(KWS_SEARCH, R.string.kws_caption);
    captions.put(MENU_SEARCH, R.string.menu_caption);
    captions.put(INVISIBLE_COMMAND, R.string.invis_caption);
    captions.put(OVER_COMMAND, R.string.os_caption);

    setContentView(R.layout.main);
    ((TextView) findViewById(R.id.caption_text))
            .setText("Preparing the recognizer");

    // Check if user has given permission to record audio
    int permissionCheck = ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.RECORD_AUDIO);
    if (permissionCheck == PackageManager.PERMISSION_DENIED) {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.RECORD_AUDIO}, PERMISSIONS_REQUEST_RECORD_AUDIO);
        return;
    }
    runRecognizerSetup();
}

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(PocketSphinxActivity.this);
                File assetDir = assets.syncAssets();
                setupRecognizer(assetDir);
            } catch (IOException e) {
                return e;
            }
            return null;
        }

        @Override
        protected void onPostExecute(Exception result) {
            if (result != null) {
                ((TextView) findViewById(R.id.caption_text))
                        .setText("Failed to init recognizer " + result);
            } else {
                switchSearch(KWS_SEARCH);
            }
        }
    }.execute();
}

@Override
public void onRequestPermissionsResult(int requestCode,
                                       String[] permissions, int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    if (requestCode == PERMISSIONS_REQUEST_RECORD_AUDIO) {
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            runRecognizerSetup();
        } else {
            finish();
        }
    }
}

@Override
public void onDestroy() {
    super.onDestroy();

    if (recognizer != null) {
        recognizer.cancel();
        recognizer.shutdown();
    }
}

/**
 * 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();
    if (text.equals(KEYPHRASE))
        switchSearch(MENU_SEARCH);
        //Invisible Mentioned
    else if (text.equals(INVISIBLE_COMMAND))
        switchSearch(INVISIBLE_COMMAND);
        //Over Mentioned
    else if (text.equals(OVER_COMMAND))
        switchSearch(OVER_COMMAND);
    else if (text.equals(POWER_COMMAND))
        ((TextView) findViewById(R.id.result_text)).setText(text);
    else
        ((TextView) findViewById(R.id.result_text)).setText(text);
}

/**
 * This callback is called when we stop the recognizer.
 */
@Override
public void onResult(Hypothesis hypothesis) {
    ((TextView) findViewById(R.id.result_text)).setText("");
    if (hypothesis != null) {
        String text = hypothesis.getHypstr();
        makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();
    }
}

@Override
public void onBeginningOfSpeech() {
}

/**
 * We stop recognizer here to get a final result
 */
@Override
public void onEndOfSpeech() {
    if (!recognizer.getSearchName().equals(KWS_SEARCH))
        switchSearch(KWS_SEARCH);
}

private void switchSearch(String searchName) {
    recognizer.stop();

    // If we are not spotting, start listening with timeout (10000 ms or 10 seconds).
    if (searchName.equals(KEYPHRASE)) {
        isGameStarted = true;
        startTimer();

    } else {

        if (isGameStarted)
            recognizer.startListening(searchName);
        else
            recognizer.startListening(searchName, 10000);

        String caption = getResources().getString(captions.get(searchName));
        ((TextView) findViewById(R.id.caption_text)).setText(caption);
    }
}

private void setupRecognizer(File assetsDir) throws IOException {
    // The recognizer can be configured to perform multiple searches
    // of different kind and switch between them

    recognizer = SpeechRecognizerSetup.defaultSetup()
            .setAcousticModel(new File(assetsDir, "en-us-ptm"))
            .setDictionary(new File(assetsDir, "cmudict-en-us.dict"))

            //            .setRawLogDir(assetsDir) // To disable logging of raw audio comment out this call (takes a lot of space on the device)

            .getRecognizer();
    recognizer.addListener(this);

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

    // Create grammar-based search for selection between demos
    File menuGrammar = new File(assetsDir, "menu.gram");
    recognizer.addGrammarSearch(MENU_SEARCH, menuGrammar);
}

@Override
public void onError(Exception error) {
    ((TextView) findViewById(R.id.caption_text)).setText(error.getMessage());
}

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

public void startTimer() {
    new CountDownTimer(30000, 1000) {

        public void onTick(long millisUntilFinished) {
            ((TextView) findViewById(R.id.caption_text))
                    .setText("seconds remaining: " + millisUntilFinished / 1000);
        }

        public void onFinish() {
            isGameStarted = false;
            // mTextField.setText("done!");
        }
    }.start();
}

}

当我加载它时一切正常,但我一直在尝试并尝试更改它识别的单词。它似乎可以很好地识别“停止”,但其他的却给我带来了问题。现在我有以下语法设置:

#JSGF V1.0;

grammar menu;

public = (invis | 隐形 | overshield | 停止 | 电源 | 关闭);

我还将自定义单词添加到我的词典文件中,只是为了确保它们位于其中。

这是我收到的错误日志,我还需要做什么才能将这些单词合并到我的项目中?

11-22 23:17:02.812 6856-6856/edu.cmu.sphinx.pocketsphinx I/SpeechRecognizer: Stop recognition
11-22 23:17:02.812 6856-6856/edu.cmu.sphinx.pocketsphinx I/SpeechRecognizer: Start recognition "overshield"
11-22 23:17:02.812 6856-6856/edu.cmu.sphinx.pocketsphinx D/AndroidRuntime: Shutting down VM
11-22 23:17:02.812 6856-6856/edu.cmu.sphinx.pocketsphinx E/AndroidRuntime: FATAL EXCEPTION: main
                                                                           Process: edu.cmu.sphinx.pocketsphinx, PID: 6856
                                                                           java.lang.RuntimeException: Decoder_setSearch returned -1
                                                                               at edu.cmu.pocketsphinx.PocketSphinxJNI.Decoder_setSearch(Native Method)
                                                                               at edu.cmu.pocketsphinx.Decoder.setSearch(Decoder.java:181)
                                                                               at edu.cmu.pocketsphinx.SpeechRecognizer.startListening(SpeechRecognizer.java:141)
                                                                               at edu.cmu.pocketsphinx.demo.PocketSphinxActivity.switchSearch(PocketSphinxActivity.java:215)
                                                                               at edu.cmu.pocketsphinx.demo.PocketSphinxActivity.onPartialResult(PocketSphinxActivity.java:170)
                                                                               at edu.cmu.pocketsphinx.SpeechRecognizer$ResultEvent.execute(SpeechRecognizer.java:427)
                                                                               at edu.cmu.pocketsphinx.SpeechRecognizer$RecognitionEvent.run(SpeechRecognizer.java:391)
                                                                               at android.os.Handler.handleCallback(Handler.java:739)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                               at android.os.Looper.loop(Looper.java:158)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:7229)
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)


                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

最佳答案

您混淆了“搜索”和“命令”。您已添加两个搜索 - KWS_SEARCHMENU_SEARCH,您可以在它们之间切换。 MENU_SEARCH 更正确的名称应该是 COMMAND_SEARCH。您无法切换到 OVER_COMMAND ,因为它是命令而不是搜索,这就是异常告诉您的内容。您需要执行操作并切换到 KWS_SEARCH

您的代码应如下所示:

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

    String text = hypothesis.getHypstr();
    if (text.equals(KEYPHRASE))
        switchSearch(MENU_SEARCH);
}


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

    String text = hypothesis.getHypstr();
    if (text.equals(OVER_COMMAND)) {
        // RUN ACTIONS FOR OVER_COMMAND_HERE
    }
}

@Override
public void onBeginningOfSpeech() {
}

@Override
public void onEndOfSpeech() {
    if (!recognizer.getSearchName().equals(KWS_SEARCH))
        switchSearch(KWS_SEARCH);
}

关于android - Decoder_setSearch 返回 -1 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40756612/

相关文章:

python - pocketsphinx python gstreamer 音频速率

java - 如果数据不可用,则在开始时禁用 Edittexts 并导航到另一个 Activity

python - Python 上麦克风的 Wav 文件

android - Android 上的关键字激活语音识别

python - 数据增强: What proportion of training dataset needs to be augmented?

java - sphinx java中的hello.gram文件有多大

android - Android 版 PocketSphinx 与谷歌语音识别冲突

java - 如何创建此自定义 View

android - 如何将 wordpress 集成到我的 Android 应用程序中

android - Android TextView 中的重叠问题