PocketSphinx android 即使我不说话也能显示结果
PocketSphinx android displaying results even without me speaking
我在 android 上使用 pocketsphinx 来识别关键字,但它无法识别所需关键字以外的任何其他关键字。而且它甚至不等我说话并显示logcat中的关键字。
这是我的代码:
public class SpeechService extends Service implements RecognitionListener {
private static final String TAG = "myTag";
private static final String KWS_SEARCH = "wakeup";
private static final String KEY_PHRASE = "hello";
private SpeechRecognizer recognizer;
private PowerManager.WakeLock lock;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
super.onDestroy();
recognizer.cancel();
recognizer.shutdown();
lock.release();
}
@Override
public int onStartCommand(Intent data, int flags, int startId) {
new AsyncTask<Void, Void, Exception>() {
@Override
protected Exception doInBackground(Void... params) {
try {
Assets assets = new Assets(SpeechService.this);
File assetDir = assets.syncAssets();
setupRecognizer(assetDir);
} catch (IOException e) {
return e;
}
return null;
}
@Override
protected void onPostExecute(Exception result) {
if(result == null)
recognizer.startListening(KWS_SEARCH);
}
}.execute();
return START_STICKY;
}
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-40f)
.setBoolean("-allphone_ci", true)
.getRecognizer();
recognizer.addListener(this);
// Create keyword-activation search.
recognizer.addKeyphraseSearch(KWS_SEARCH, KEY_PHRASE);
}
@Override
public void onBeginningOfSpeech() {
//Log.i(TAG, "onBeginningOfSpeech");
}
@Override
public void onEndOfSpeech() {
//Log.i(TAG, "onEndOfSpeech");
if(!recognizer.getSearchName().equals(KWS_SEARCH)) {
recognizer.cancel();
recognizer.startListening(KWS_SEARCH);
}
}
@Override
public void onPartialResult(Hypothesis hypothesis) {
//Log.i(TAG, "onPartialResult");
if(hypothesis == null)
return;
String result = hypothesis.getHypstr();
Log.i(TAG, "result = " + result);
recognizer.cancel();
recognizer.startListening(KWS_SEARCH);
}
@Override
public void onResult(Hypothesis hypothesis) {
//Log.i(TAG, "onResult");
}
@Override
public void onError(Exception e) {
Log.i(TAG, "onError");
recognizer.startListening(KWS_SEARCH);
}
@Override
public void onTimeout() {
Log.i(TAG, "onTimeout");
recognizer.startListening(KWS_SEARCH);
}
}
这是我还没说话的Logcat。
result = hello
result = hello
result = hello hello
result = hello
result = hello hello
result = hello
等等...
我做错了什么???
如果误报过多,您可以将关键字阈值从 1e-40 降低到 1e-20 甚至 1e-10,如下所示:
.setKeywordThreshold(1e-40f)
我也有同样的问题。
我觉得太容易认了"hello",所以我第一次把关键词改成了两个字
但是不好用,有时候识别不了
所以我自己做了我的语法并使用了 addGrammarSearch 方法。顺便说一句,你的语法不能只有一个词,你必须添加一些词来欺骗噪音。
最后我在onParticalResult或onResult方法中写了Judgment方法来识别关键字
如果你有更好的答案我想知道。
我在 android 上使用 pocketsphinx 来识别关键字,但它无法识别所需关键字以外的任何其他关键字。而且它甚至不等我说话并显示logcat中的关键字。
这是我的代码:
public class SpeechService extends Service implements RecognitionListener {
private static final String TAG = "myTag";
private static final String KWS_SEARCH = "wakeup";
private static final String KEY_PHRASE = "hello";
private SpeechRecognizer recognizer;
private PowerManager.WakeLock lock;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
super.onDestroy();
recognizer.cancel();
recognizer.shutdown();
lock.release();
}
@Override
public int onStartCommand(Intent data, int flags, int startId) {
new AsyncTask<Void, Void, Exception>() {
@Override
protected Exception doInBackground(Void... params) {
try {
Assets assets = new Assets(SpeechService.this);
File assetDir = assets.syncAssets();
setupRecognizer(assetDir);
} catch (IOException e) {
return e;
}
return null;
}
@Override
protected void onPostExecute(Exception result) {
if(result == null)
recognizer.startListening(KWS_SEARCH);
}
}.execute();
return START_STICKY;
}
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-40f)
.setBoolean("-allphone_ci", true)
.getRecognizer();
recognizer.addListener(this);
// Create keyword-activation search.
recognizer.addKeyphraseSearch(KWS_SEARCH, KEY_PHRASE);
}
@Override
public void onBeginningOfSpeech() {
//Log.i(TAG, "onBeginningOfSpeech");
}
@Override
public void onEndOfSpeech() {
//Log.i(TAG, "onEndOfSpeech");
if(!recognizer.getSearchName().equals(KWS_SEARCH)) {
recognizer.cancel();
recognizer.startListening(KWS_SEARCH);
}
}
@Override
public void onPartialResult(Hypothesis hypothesis) {
//Log.i(TAG, "onPartialResult");
if(hypothesis == null)
return;
String result = hypothesis.getHypstr();
Log.i(TAG, "result = " + result);
recognizer.cancel();
recognizer.startListening(KWS_SEARCH);
}
@Override
public void onResult(Hypothesis hypothesis) {
//Log.i(TAG, "onResult");
}
@Override
public void onError(Exception e) {
Log.i(TAG, "onError");
recognizer.startListening(KWS_SEARCH);
}
@Override
public void onTimeout() {
Log.i(TAG, "onTimeout");
recognizer.startListening(KWS_SEARCH);
}
}
这是我还没说话的Logcat。
result = hello
result = hello
result = hello hello
result = hello
result = hello hello
result = hello
等等...
我做错了什么???
如果误报过多,您可以将关键字阈值从 1e-40 降低到 1e-20 甚至 1e-10,如下所示:
.setKeywordThreshold(1e-40f)
我也有同样的问题。
我觉得太容易认了"hello",所以我第一次把关键词改成了两个字
但是不好用,有时候识别不了
所以我自己做了我的语法并使用了 addGrammarSearch 方法。顺便说一句,你的语法不能只有一个词,你必须添加一些词来欺骗噪音。
最后我在onParticalResult或onResult方法中写了Judgment方法来识别关键字
如果你有更好的答案我想知道。