Android PocketSphinx: как сбросить гипотезу.Hypstr()?

Я хочу, чтобы мое устройство постоянно прослушивало ключевое слово. Я использую PocketSphinx для этой цели. Я нашел демонстрационный проект на github, который заставляет PocketSphinx постоянно слушать, используя Сервис.

Вот ссылка на проект github

В первый раз он работает нормально, но после этого гипотеза.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 после запуска службы, если кому нужно:

ПРИМЕЧАНИЕ. Я хочу сбросить строку гипотезы.getHypstr(), чтобы моя программа в следующий раз работала нормально.

Я думаю, что этого достаточно, чтобы объяснить, но если я что-то упущу, сообщите мне об этом в комментарии.

Спасибо.

Возможный дубликат Как настроить пороги для определения ключевых слов из списка в pocketsphinx-android?


person Mayur Kharche    schedule 17.02.2017    source источник
comment
@NikolayShmyrev Установка порога поможет мне без путаницы получить точное ключевое слово. Но что после этого гипотеза.Hypstr() будет возвращать одну и ту же строку снова и снова. Мой вопрос в том, как я могу сбросить эту строку до нуля, чтобы программа работала нормально в следующий раз.   -  person Nikolay Shmyrev    schedule 17.02.2017
comment
Гипотеза уже сброшена в вашем коде в switchSearch. распознаватель.отмена() делает это. Ваша единственная проблема — слишком много ложных срабатываний.   -  person Mayur Kharche    schedule 17.02.2017
comment
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=hello hello 02-17 14:27:52.119 10121-18269/com.hrupin.speechrecognitionservice D/SpeechRecognizer: Начало декодирования 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 в настоящее время работает в ручном режиме .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. .hrupin.speechrecognitionservice I/VoiceService: onBeginningOfSpeech 02-17 14:27:53.020 10121-10121/com.hrupin.speechrecognitionservice I/VoiceService: switchSearch searchName = wakeup 02-17 14:27:53.604 10121-18269/com.hrupin. служба распознавания речи I/cmusphinx: ИНФОРМАЦИЯ: cmn_prior.c(131): cmn_prior_update: from ‹ 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 1042-17 1 :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:510.2014-6014 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.hrup .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. 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_p rior_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.73: -121 -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.105- 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.hrup.hrup. служба распознавания речи I/cmusphinx: -1,32 02-17 14:27:53,605 10121-18269/com.hrupin.служба распознавания речи I/cmusphinx: 1,89 02-17 14:27:53,605 10121-18269/com.hrupin.speechrecognitionservicex 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/cmusphinx: -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.610 10120 10120 -18294/com.hrupin.speechrecognitionservice I/cmusphinx: ИНФОРМАЦИЯ: pocketsphinx.c(1006): Запись необработанного аудиофайла журнала: /storage/emulated/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: from ‹ 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.speechrecognitionservice I/cmusphinx: -11.73 02-17 14:27:54.464 10121-18294/com.hrupin.xspeechrecognitionservice : -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/cmusphi nx: 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 0 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:544.464 10214.464 10214.464 10214.464 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: ИНФОРМАЦИЯ: cmn_prior.c(116): cmn_prior_update: to ‹ 02-17 14:27:54.464 10121-18294 /com.hrupin.speechrecognition служба 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.speechrecognitionservice 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 102941-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 /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.10 -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   -  person Nikolay Shmyrev    schedule 18.02.2017