Android PocketSphinx: Как сбросить гипотезу.Hypstr()?
Я хочу, чтобы мое устройство постоянно прослушивало ключевое слово. Я использую PocketSphinx для этой цели. Я нашел демонстрационный проект на github, который заставляет PocketSphinx непрерывно слушать, используя Service.
Он работает нормально в первый раз, но после этой гипотезы.Hypstr() возвращает одну и ту же строку снова и снова.
Я хочу очистить строку внутри гипотезы.Hypstr(), чтобы это приложение работало нормально для меня.
Вот мой класс обслуживания:
package com.hrupin.speechrecognitionservice;
import android.Manifest;
import android.app.Service;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.AsyncTask;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.widget.Toast;
import java.io.File;
import java.io.IOException;
import edu.cmu.pocketsphinx.Assets;
import edu.cmu.pocketsphinx.Hypothesis;
import edu.cmu.pocketsphinx.RecognitionListener;
import edu.cmu.pocketsphinx.SpeechRecognizer;
import edu.cmu.pocketsphinx.SpeechRecognizerSetup;
/**
* Created by Igor Khrupin www.hrupin.com on 6/3/16.
*/
public class VoiceService extends Service implements
RecognitionListener {
private static final String LOG_TAG = VoiceService.class.getSimpleName();
/* Named searches allow to quickly reconfigure the decoder */
private static final String KWS_SEARCH = "wakeup";
/* Keyword we are looking for to activate menu */
private static final String KEYPHRASE = "hello";
private SpeechRecognizer recognizer;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// Check if user has given permission to record audio
int permissionCheck = ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.RECORD_AUDIO);
if (permissionCheck == PackageManager.PERMISSION_GRANTED) {
runRecognizerSetup();
}
return super.onStartCommand(intent, flags, startId);
}
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(VoiceService.this);
File assetDir = assets.syncAssets();
setupRecognizer(assetDir);
} catch (IOException e) {
return e;
}
return null;
}
@Override
protected void onPostExecute(Exception result) {
if (result != null) {
Log.i(LOG_TAG, "Failed to init recognizer ");
} else {
switchSearch(KWS_SEARCH);
}
}
}.execute();
}
@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.contains(KEYPHRASE)) {
Toast.makeText(this, "onPartialResult text=" + text, Toast.LENGTH_SHORT).show();
switchSearch(KWS_SEARCH);
}
Log.i(LOG_TAG, "onPartialResult text=" +text);
}
/**
* This callback is called when we stop the recognizer.
*/
@Override
public void onResult(Hypothesis hypothesis) {
if (hypothesis != null) {
String text = hypothesis.getHypstr();
Log.i(LOG_TAG, "onResult text=" +text);
}
}
@Override
public void onBeginningOfSpeech() {
Log.i(LOG_TAG, "onBeginningOfSpeech");
}
/**
* We stop recognizer here to get a final result
*/
@Override
public void onEndOfSpeech() {
if (!recognizer.getSearchName().contains(KWS_SEARCH))
switchSearch(KWS_SEARCH);
Log.i(LOG_TAG, "onEndOfSpeech");
}
private void switchSearch(String searchName) {
Log.i(LOG_TAG, "switchSearch searchName = " + searchName);
recognizer.cancel();
recognizer.stop();
// If we are not spotting, start listening with timeout (10000 ms or 10 seconds).
recognizer.startListening(searchName, 10000);
}
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)
.setKeywordThreshold(1e-45f) // Threshold to tune for keyphrase to balance between false alarms and misses
.setBoolean("-allphone_ci", true) // Use context-independent phonetic search, context-dependent is too slow for mobile
.getRecognizer();
recognizer.addListener(this);
/** In your application you might not need to add all those searches.
* They are added here for demonstration. You can leave just one.
*/
// Create keyword-activation search.
recognizer.addKeyphraseSearch(KWS_SEARCH, KEYPHRASE);
}
@Override
public void onError(Exception error) {
Log.i(LOG_TAG, "onError " + error.getMessage());
}
@Override
public void onTimeout() {
switchSearch(KWS_SEARCH);
Log.i(LOG_TAG, "onTimeout");
}
}
и Logcat после запуска службы на случай, если кому-то понадобится:
02-17 14: 27: 51.563 10121-10121 / com.hrupin.speechrecognitionservice I / VoiceService: switchSearch searchName = wakeup 02-17 14:27:52.115 10121-10121/com.hrupin.speechrecognitionservice I/SpeechRecognizer: отменить распознавание 02- 17 14: 27: 52.115 10121-10121 / com.hrupin.speechrecognitionservice I / SpeechRecognizer: запуск распознавания "пробуждение" 02-17 14:27:52.116 10121-10121/com.hrupin.speechrecognitionservice I/VoiceService: onPartialResult text= привет привет 02-17 14:27:52.119 10121-18269/com.hrupin.speechrecognitionservice D/SpeechRecognizer: Начало декодирования 02-17 14:27:52.119 10121-18269/com.hrupin.speechrecognitionservice I/cmusphinx: INFO: pocketsphinx.c(1006): запись файла необработанного аудио журнала: /storage/emulated/0/Android/data/com.hrupin.speechrecognitionservice/files/sync/000000256.raw 02-17 14:27:52.930 10121-10139/com.hrupin.speechrecognitionservice V/ViewRootImpl: Изменение размера android.view.ViewRootImpl@3b6eb36: frame=[222,1596][857,1728] contentInsets=[0,0][0,0] visibleInsets=[0,0][0,0] reportDraw=true 02-17 14:27:52.937 10121-10135/com.hrupin.speechrecognitionservice I/libGameXtend: LUCID_1 (1487321872937) GameXtend в настоящее время работает в ручном режиме 02-17 14: 27: 52.937 10121-10135 / com.hrupin.speechrecognitionservice I / libGameXtend: LUCID_1 (1487321872937) GameXtend в настоящее время работает в состоянии no_save Save power. 02-17 14:27:52.937 10121-10135/com.hrupin.speechrecognitionservice I/libGameXtend: LUCID_1 (1487321872937) com.hrupin.speechrecognitionservice в настоящее время работает с GameXtend, с параметром PS = 1., ICE не поддерживается. Источником параметра PS является конфигурация PowerXtend 02-17 14: 27: 52.937 10121-10135 / com.hrupin.speechrecognitionservice I / libGameXtend: LUCID_1 (1487321872937) Функция касания PowerXtend не активна 02-17 14:27:53.016 10121-10121/com.hrupin.speechrecognitionservice I/VoiceService: onBeginningOfSpeech 02-17 14:27:53.020 10121-10121/com.hrupin.speechrecognitionservice I/VoiceService: switchSearch searchName = пробуждение 02-17 14:27:53.604 10121-18269/com.hrupin. речевое распознавание I/cmusphinx: INFO: cmn_prior.c(131): cmn_prior_update: от < 02-17 14:27:53.604 10121-18269/com.hrupin.speechrecognitionservice I/cmusphinx: 11,77 02-17 14:27:53.604 10121-18269/com.hrupin.speechrecognitionservice I/cmusphinx: -12.91 02-17 14:27:53.604 10121-18269/com.hrupin.speechrecognitionservice I/cmusphinx: -4.04 02-17 14:27:53.604 10121-18269/com.hrupin.speechrecognitionservice I/cmusphinx: -3.12 02-17 14:27:53.604 10121-18269/com.hrupin.speechrecognitionservice I/cmusphinx: 1.62 02-17 14:27:53.604 10121-18269/com.hrupin.speechrecognitionservice I/cmusphinx: -0.05 02-17 14:27:53.604 10121-18269/com.hrupin.speechrecognitionservice I/cmusphinx: 2.21 02-17 14:27:53.604 10121-18269/com.hrupin.speechrecognitionservice I/cmusphinx: 1.17 02-17 14:27:53.604 10121-18269/com.hrupin.speechrecognitionservice I/cmusphinx: -0.68 02-17 14:27:53.605 10121-18269/com.hrupin.speechrecognitionservice I/cmusphinx: 3.51 02-17 14:27:53.605 10121-18269/com.hrupin.speechrecognitionservice I/cmusphinx: 0.98 02-17 14:27:53.605 10121-18269/com.hrupin.speechrecognitionservice I/cmusphinx: 1.02 02-17 14:27:53.605 10121-18269/com.hrupin.speechrecognitionservice I/cmusphinx: 0.26 02-17 14:27:53.605 10121-18269/com.hrupin.speechrecognitionservice I/cmusphinx: > 02-17 14:27:53.605 10121-18269/com.hrupin.speechrecognitionservice I/cmusphinx: INFO: cmn_prior.c(149): cmn_prior_update: to < 02-17 14:27:53.605 10121-18269/com.hrupin.speechrecognitionservice I/cmusphinx: 10.72 02-17 14:27:53.605 10121-18269/com.hrupin.speechrecognitionservice I/cmusphinx: -11.73 02-17 14:27:53.605 10121-18269/com.hrupin.speechrecognitionservice I/cmusphinx: -3.15 02-17 14:27:53.605 10121-18269/com.hrupin.speechrecognitionservice I/cmusphinx: -1,51 02-17 14:27:53.605 10121-18269/com.hrupin.speechrecognitionservice I/cmusphinx: 2,40 02-17 14:27:53.605 10121-18269/com.hrupin.speechrecognitionservice I/cmusphinx: -0,52 02-17 14:27:53.605 10121-18269/com.hrupin.speechrecognitionservice I/cmusphinx: 2,35 02-17 14:27:53.605 10121-18269/com.hrupin.speechrecognitionservice I/cmusphinx: 0,37 02-17 14:27:53.605 10121-18269/com.hrupin.speechrecognitionservice I/cmusphinx: -1.32 02-17 14:27:53.605 10121-18269/com.hrupin.speechrecognitionservice I/cmusphinx: 1.89 02-17 14:27:53.605 10121-18269/com.hrupin.speechrecognitionservice I/cmusphinx: 0,47 02-17 14:27:53: 605 10121-18269 / com.hrupin.speechrecognitionservice I / cmusphinx: -0,15 02-17 14: 27: 53.605 10121-18269 / com.hrupin.speechrecognitionservice I / смфинкс: -0,24 02-17 14:27:53.605 10121-18269/com.hrupin.speechrecognitionservice I/cmusphinx: > 02-17 14:27:53.615 10121-10121/com.hrupin.speechrecognitionservice I/SpeechRecognizer: отменить распознавание 02-17 14: 27: 53.615 10121-10121 / com.hrupin.speechrecognitionservice I / SpeechRecognizer: запуск распознавания "пробуждение" 02-17 14:27:53.616 10121-10121/com.hrupin.speechrecognitionservice I/VoiceService: onPartialResult text=hello 02-17 14:27:53.620 10121-18294/com.hrupin.speechrecognitionservice D/SpeechRecognizer: запуск декодирования 02-17 14:27:53.620 10121-18294/com.hrupin.speechrecognitionservice I/cmusphinx: INFO: pocketsphinx.c(1006): запись файла необработанного аудиозаписи: / storage / эмулированный /0/Android/data/com.hrupin.speechrecognitionservice/files/sync/000000257.raw 02-17 14:27:54.464 10121-18294/com.hrupin.speechrecognitionservice I/cmusphinx: INFO: cmn_prior.c(99): cmn_prior_update: от < 02-17 14:27:54.464 10121-18294/com.hrupin.speechrecognitionservice I/cmusphinx: 10.72 02-17 14:27:54.464 10121-18294/com.hrupin.speechrecognitionser вице-я /cmusphinx: -11.73 02-17 14:27:54.464 10121-18294/com.hrupin.speechrecognitionservice I/cmusphinx: -3.15 02-17 14:27:54.464 10121-18294/com.hrupin.speechrecognitionservice I/cmusphinx: -1.51 02-17 14:27:54.464 10121-18294/com.hrupin.speechrecognitionservice I/cmusphinx: 2.40 02-17 14:27:54.464 10121-18294/com.hrupin.speechrecognitionservice I/cmusphinx: -0.52 02-17 14:27:54.464 10121-18294/com.hrupin.speechrecognitionservice I/cmusphinx: 2.35 02-17 14:27:54.464 10121-18294/com.hrupin.speechrecognitionservice I/cmusphinx: 0.37 02-17 14:27:54.464 10121-18294/com.hrupin.speechrecognitionservice I/cmusphinx: -1.32 02-17 14:27:54.464 10121-18294/com.hrupin.speechrecognitionservice I/cmusphinx: 1.89 02-17 14:27:54.464 10121-18294/com.hrupin.speechrecognitionservice I/cmusphinx: 0.47 02-17 14:27:54.464 10121-18294/com.hrupin.speechrecognitionservice I/cmusphinx: -0.15 02-17 14:27:54.464 10121-18294/com.hrupin.speechrecognitionservice I/cmusphinx: -0,24 02-17 14: 27: 54,464 10121-18294/com.hrupin.speechrecognitionservice I/cmusphinx: > 02-17 14:27:54.464 10121-18294/com.hrupin.speechrecognitionservice I/cmusphinx: INFO: cmn_prior.c(116): cmn_prior_update: до < 02-17 14:27:54.464 10121-18294/com.hrupin.speechrecognitionservice I/cmusphinx: 11.32 02-17 14:27:54.465 10121-18294/com.hrupin.speechrecognitionservice I/cmusphinx: -11.36 02-17 14:27:54.465 10121-18294/com.hrupin.speechrecognitionservice I/cmusphinx: -2.11 02-17 14:27:54.465 10121-18294/com.hrupin.speechrecognitionservice I/cmusphinx: -0.38 02-17 14:27:54.465 10121-18294/com.hrupin. речевое распознавание I/cmusphinx: 3.04 02-17 14:27:54.465 10121-18294/com.hrupin.speechrecognitionservice I/cmusphinx: -0.80 02-17 14:27:54.465 10121-18294/com.hrupin.speechrecognitionservice I/cmusphinx: 1.81 02-17 14:27:54.465 10121-18294/com.hrupin.speechrecognitionservice I/cmusphinx: 0.35 02-17 14:27:54.465 10121-18294/com.hrupin.speechrecognitionservice I/cmusphinx: -2.00 02-17 14:27:54.465 10121-18294/com.hrupin.speechrecognitionservice I/cmusphinx: 1.64 02-17 14:27:54.465 10121-18294/com.hrupin.speechrecognitionservice I/cmusphinx: 0.81 02-17 14:27:54.465 10121-18294/com.hrupin.speechrecognitionservice I/cmusphinx: 0.11 02-17 14:27:54.465 10121-18294/com.hrupin.speechrecognitionservice I/cmusphinx: -0.10 02-17 14:27:54.465 10121-18294/com.hrupin.speechrecognitionservice I/cmusphinx: > 02-17 14:27:54.496 10121-10121/com.hrupin.speechrecognitionservice I/VoiceService: onBeginningOfSpeech
ПРИМЕЧАНИЕ. Я хочу сбросить строку hypothesis.getHypstr(), чтобы моя программа работала нормально в следующий раз.
Я думаю, что этого достаточно, чтобы объяснить, но если я что-то упустил, пожалуйста, дайте мне знать в комментарии.
Спасибо.