首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >返回的Decoder_setSearch -1异常

返回的Decoder_setSearch -1异常
EN

Stack Overflow用户
提问于 2016-11-23 05:20:54
回答 1查看 287关注 0票数 2

最近,我查看了由CMUSphinx为Android提供动力的CMUSphinx:https://github.com/cmusphinx/pocketsphinx-android-demo。我修改了一下,看看我能做些什么。

代码语言:javascript
复制
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();
}

}

当我加载它时,一切都很好,但是我一直在尝试并试图改变它所识别的单词。它似乎认识到“停止”很好,但其他的给我带来了问题。现在,我有以下语法设置:

代码语言:javascript
复制
#JSGF V1.0;

grammar menu;

public =(

我还将定制单词添加到我的字典文件中,以确保它们在字典文件中。

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

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-11-23 08:27:21

你混淆了“搜索”和“命令”。您已经添加了两个搜索- KWS_SEARCHMENU_SEARCH,您可以在它们之间切换。更恰当地说,MENU_SEARCH应该称为COMMAND_SEARCH。您不能切换到OVER_COMMAND,因为它是命令而不是搜索,这是异常告诉您的。您需要执行操作,转而切换到KWS_SEARCH

您的代码应该如下所示:

代码语言:javascript
复制
@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);
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40756612

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档