无法启动服务? (语音识别)
我想在服务中不断地使用pocketsphinx来听取hello这个词
我收到了错误。 这是完整的堆栈跟踪 。 这是它的一小部分。
Unable to create service curlybrace.ruchir.myApp.MyService: java.lang.RuntimeException: new_Decoder returned -1
这是由于:
setupRecognizer(assetDir); //SETUP
和这个:
.getRecognizer();
在我的onCreate
:
Log.v(TAG, "Voice recognition activated!"); //Register voice recog listener :) Assets assets = null; try { assets = new Assets(MyService.this); File assetDir = assets.syncAssets(); setupRecognizer(assetDir); //SETUP Log.v(TAG, "Set up listener"); } catch (IOException e) { e.printStackTrace(); }
这是我的setupRecognizer
方法:
private void setupRecognizer(File assetDir) throws IOException { recognizer = defaultSetup() .setAcousticModel(new File(assetDir, "hmm/en-us-semi")) .setDictionary(new File(assetDir, "lm/cmu07a.dic")) .setKeywordThreshold(1e-5f) .getRecognizer(); recognizer.addListener(this); // recognizer.addKeywordSearch("Hello", assetDir); //I don't know what this does... recognizer.startListening("Hello"); //Start listeneing }
这是实现的方法之一:
@Override public void onPartialResult(Hypothesis hypothesis) { String text = hypothesis.getHypstr(); if (text.equals("Hello")) { // do something Log.v(TAG, "SPEECH RECOGNIZED HELLO!"); } }
我将不胜感激任何反馈。 积极的,消极的,甚至是评论。 在这一点上,经过2天的努力,我绝望了!
你有这个:
private void setupRecognizer(File assetDir) throws IOException { recognizer = defaultSetup() .setAcousticModel(new File(assetDir, "hmm/en-us-semi")) .setDictionary(new File(assetDir, "lm/cmu07a.dic")) .setKeywordThreshold(1e-5f) .getRecognizer(); recognizer.addListener(this); // recognizer.addKeywordSearch("Hello", assetDir); //I don't know what this does... recognizer.startListening("Hello"); //Start listeneing }
尝试将其更改为:
private void setupRecognizer(File assetDir) throws IOException { recognizer = defaultSetup() .setAcousticModel(new File(assetDir, "hmm/en-us-semi")) .setDictionary(new File(assetDir, "lm/cmu07a.dic")) .setKeywordThreshold(1e-5f) .getRecognizer(); recognizer.addListener(this); //Add this: File digitsGrammar = new File(modelsDir, "grammar/digits.gram"); recognizer.addKeywordSearch(DIGITS_SEARCH, digitsGrammar); }
要开始语音侦听,请从按钮调用此方法。 当它工作时,从服务中调用它,以使事情更简单:
recognizer.startListening("Hello"); //Start listeneing
现在,创建一个名为digits.gram的新文件,并将其放在一个名为here的文件夹中: /youProjectRootFolder/grammar/digits.gram
这个文件实际上是.txt文件,但是当你完成这个文件时,将扩展名更改为.gram里面的文字:
hello /1e-1/ hi /1e-1/ bye /1e-1/ goodbye /1e-1/ ...etc. /1e-1/
在这里你会发现类似的情况: 使用PocketSphinx识别多个关键字祝你好运!
对于命令,下面的代码是我做的,它运作良好。 如果您只进行关键字定位,请查看Sphinx下载中的关键字定位示例包并修改下面的代码。
确保assets --> sync
文件夹仅包含以下文件
folder en-us-ptm assets.lst cmudict-en-us.dict cmudict-en-us.dict.md5 command.gram your_preferred_name.dict
如果允许用户设置命令,则不需要命令和your_preferred_name.dict。 您可以稍后在代码中添加它并将其保存在下面的相应目录中。 对于关键字定位,请使用Sphinx示例中的任何名称替换command.gram。
在assets --> sync
文件夹中修改列出的文件,使其具有以下内容。 您可以使用notepad ++编辑这些文件
assets.lst
cmudict-en-us.dict en-us-ptm/README en-us-ptm/feat.params en-us-ptm/mdef en-us-ptm/means en-us-ptm/noisedict en-us-ptm/sendump en-us-ptm/transition_matrices en-us-ptm/variances
command.gram
hello /1/
如果应用程序无法理解调整阈值参数,即/ 1e-8 /阈值越小,识别器拾取单词越容易,但也更容易出现误报。 对于关键字定位,请将Sphinx关键字示例替换为您的关键字。
your_prefered_name.dict
复制cmudict-en-us.dict中的整行,其中包含command.gram中的单词,在本例中,它是单词hello。 我有一个单独的字典,以便文件更小,以便dict搜索有点改进。 所以你的your_prefered_name.dict应该是这样的
hello HH AH L OW hello(2) HH EH L OW
对于关键字定位我认为你可以将这些单词串在一起(不确定你是否必须尝试查看它是否会起作用)所以例如hello world将是
hello world HH AH L OW .... (the dot is for world)
在您的应用程序开始时创建一个名为“sphinx”的目录
String createSphinxDir() { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); String sphinxDir = prefs.getString("sphinx", null); if (sphinxDir == null) { Assets assets; try { assets = new Assets(this); File sphinxDirFile = assets.syncAssets(); if (sphinxDirFile != null) { sphinxDir = sphinxDirFile.getAbsolutePath(); Editor editor = prefs.edit(); editor.putString("sphinx", sphinxDir); editor.commit(); // Also save the command.gram and your_preferred_name.dict // to the sphinx dir here. Or save the them later to this // dir if you allow user to set the command or keyword } } catch (IOException e) { } } return sphinxDir; }
然后,无论你在哪里启动语音识别器
String sphinxDir = createSphinxDir(); if (sphinxDir != null) { try { mSpeechRecognizer = defaultSetup() .setAcousticModel(new File(sphinxDir, "en-us-ptm")) .setDictionary(new File(sphinxDir, "your_preferred_name.dict")) .setBoolean("-allphone_ci", true) .getRecognizer(); mSpeechRecognizer.addListener(your listener); // check if file exists here I have a util called FileIOUtils, you should create a method to check. if ((new File(sphinxDir + File.separator + "command.gram")).isFile()) { mSpeechRecognizer.addKeywordSearch("wakeup", new File(sphinxDir + File.separator + "command.gram")); } // Or wherever appropriate startListening("wakeup"); } catch (IOException e) { } }
对于关键字定位,只需将上面的内容更改为Sphinx示例中的一个。