为了账号安全,请及时绑定邮箱和手机立即绑定

使用PocketSphinx识别多个关键字

使用PocketSphinx识别多个关键字

开心每一天1111 2019-10-12 09:34:21
我已经安装了PocketSphinx演示程序,并且在Ubuntu和Eclipse下运行良好,但是尽管尝试了一下,但仍无法弄清楚如何添加多个单词的识别。我只想让代码识别单个单词,然后我就可以在代码中识别它们switch(),例如“上”,“下”,“左”,“右”。我不想识别句子,只能识别单个单词。任何帮助,将不胜感激。我发现其他用户也有类似的问题,但到目前为止,没人知道答案。让我感到困惑的一件事是,为什么我们根本需要使用“唤醒”常量?private static final String KWS_SEARCH = "wakeup";private static final String KEYPHRASE = "oh mighty computer";...recognizer.addKeyphraseSearch(KWS_SEARCH, KEYPHRASE);有wakeup什么关系吗?我已经取得了一些进步(?):使用,addGrammarSearch我可以使用.gram文件列出我的单词,例如up,down,left,right,forwards,backwards,如果我只说那些特定的单词,这似乎很好用。但是,任何其他字词都将导致系统将所陈述的内容与“最近”字词进行匹配。理想情况下,如果.gram文件中没有说出的单词,我不希望被识别...
查看完整描述

3 回答

?
aluckdog

TA贡献1847条经验 获得超7个赞

您可以使用addKeywordSearch哪个用于归档密钥短语。每行一个短语,例如//中的每个短语都有阈值


up /1.0/

down /1.0/

left /1.0/

right /1.0/

forwards /1e-1/

必须选择阈值以避免错误警报。


查看完整回答
反对 回复 2019-10-12
?
萧十郎

TA贡献1815条经验 获得超13个赞

正在为PocketSphinx演示更新Antinous修订,以使其能够在Android Studio上运行。这就是我到目前为止


//Note: change MainActivity to PocketSphinxActivity for demo use...

public class MainActivity extends Activity implements RecognitionListener {

private static final String DIGITS_SEARCH = "digits";

private SpeechRecognizer recognizer;


/* Used to handle permission request */

private static final int PERMISSIONS_REQUEST_RECORD_AUDIO = 1;


@Override

public void onCreate(Bundle state) {

    super.onCreate(state);


    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_GRANTED) {

        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.RECORD_AUDIO}, PERMISSIONS_REQUEST_RECORD_AUDIO);

        return;

    }


    new AsyncTask<Void, Void, Exception>() {

        @Override

        protected Exception doInBackground(Void... params) {

            try {

                Assets assets = new Assets(MainActivity.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 {

                reset();

            }

        }

    }.execute();

    ((TextView) findViewById(R.id.caption_text)).setText("Say one, two, three, four, five, six...");

}


/**

 * 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;

    } else if (hypothesis != null) {

        if (recognizer != null) {

            //recognizer.rapidSphinxPartialResult(hypothesis.getHypstr());

            String text = hypothesis.getHypstr();

            if (text.equals(DIGITS_SEARCH)) {

                recognizer.cancel();

                performAction();

                recognizer.startListening(DIGITS_SEARCH);

            }else{

                //Toast.makeText(getApplicationContext(),"Partial result = " +text,Toast.LENGTH_SHORT).show();

            }

        }

    }

}

@Override

public void onResult(Hypothesis hypothesis) {

    ((TextView) findViewById(R.id.result_text)).setText("");

    if (hypothesis != null) {

        String text = hypothesis.getHypstr();

        makeText(getApplicationContext(), "Hypothesis" +text, Toast.LENGTH_SHORT).show();

    }else if(hypothesis == null){

        makeText(getApplicationContext(), "hypothesis = null", Toast.LENGTH_SHORT).show();

    }

}

@Override

public void onDestroy() {

    super.onDestroy();

    recognizer.cancel();

    recognizer.shutdown();

}

@Override

public void onBeginningOfSpeech() {

}

@Override

public void onEndOfSpeech() {

   reset();

}

@Override

public void onTimeout() {

}

private void setupRecognizer(File assetsDir) throws IOException {

    // The recognizer can be configured to perform multiple searches

    // of different kind and switch between them

    recognizer = defaultSetup()

            .setAcousticModel(new File(assetsDir, "en-us-ptm"))

            .setDictionary(new File(assetsDir, "cmudict-en-us.dict"))

            // .setRawLogDir(assetsDir).setKeywordThreshold(1e-20f)

            .getRecognizer();

    recognizer.addListener(this);


    File digitsGrammar = new File(assetsDir, "digits.gram");

    recognizer.addKeywordSearch(DIGITS_SEARCH, digitsGrammar);

}

private void reset(){

    recognizer.stop();

    recognizer.startListening(DIGITS_SEARCH);

}

@Override

public void onError(Exception error) {

    ((TextView) findViewById(R.id.caption_text)).setText(error.getMessage());

}


public void performAction() {

    // do here whatever you want

    makeText(getApplicationContext(), "performAction done... ", Toast.LENGTH_SHORT).show();

}

}

请注意:此工作正在进行中。过一会再来检查。建议将不胜感激。


查看完整回答
反对 回复 2019-10-12
  • 3 回答
  • 0 关注
  • 1231 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信