可以使SpeechRecognizer更快吗?

时间:2016-04-29 20:48:45

标签: android performance speech-recognition voice-recognition

我正在开发一个使用android SpeechRecognizer的应用程序。我用它来做一些简单的事情。我点击一个按钮,我的SpeechRecognizer开始收听,我从我所说的内容中得到了一些结果。

容易对吗?好吧,我的问题是我需要快速制作SpeechRecognizer。我的意思是,我点击我的按钮,我说“你好”,而SpeechRecognizer需要3-4秒才能获得可能结果的数组。我的问题是:

有可能使SpeechRecognizer更快地返回结果吗? 或者花更少的时间来关闭听力意图并开始处理它听的内容? 也许另一种方式呢?哪个会比这更好?

我正在检查库,我看到了这三个参数:

  

EXTRA_SPEECH_INPUT_MINIMUM_LENGTH_MILLIS:

     

我们停止听取演讲后应考虑的时间   输入完成。

     

EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS

     

话语的最小长度。

     

EXTRA_SPEECH_INPUT_POSSIBLY_COMPLETE_SILENCE_LENGTH_MILLIS

     

我们停止听到讲话后应该花费的时间   考虑输入可能完成。

http://developer.android.com/intl/es/reference/android/speech/RecognizerIntent.html

我已经尝试了所有这些但是它不起作用,或者我可能没有使用它们。这是我的代码:

public class MainActivity extends Activity {
private static final String TIME_FORMAT = "%02d:%02d:%02d";
private final String TAG = "MainActivity";

private StartTimerButton mSpeakButton;
private CircleProgressBar mCountdownProgressBar;
private CountDownTimer mCountDownTimer;
private TextView mTimer;
private int mRunSeconds = 0;
private SpeechRecognizer mSpeechRecognizer;
private Intent mSpeechRecognizerIntent;
private boolean mIsListening = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mRunSeconds = 0;
    mTimer = (TextView) findViewById(R.id.timerText);
    mCountdownProgressBar = (CircleProgressBar) findViewById(R.id.progressBar);
    mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
    mSpeechRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
            RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
            this.getPackageName());

//          mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_MINIMUM_LENGTH_MILLIS,
//                1000);
//        mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS,
//                1000);
//        mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_POSSIBLY_COMPLETE_SILENCE_LENGTH_MILLIS,
//                1000);

    SpeechRecognitionListener listener = new SpeechRecognitionListener();
    mSpeechRecognizer.setRecognitionListener(listener);
    mSpeakButton = (StartTimerButton) findViewById(R.id.btnSpeak);
    mSpeakButton.setReadyState(false);
    mSpeakButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (mSpeakButton.isReady()) {
                if (!mIsListening)
                    mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
            } else
                mSpeakButton.setReadyState(true);
        }
    });

}     

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    return true;
}

public void onSpeechResults(ArrayList<String> matches) {
    for (String match : matches) {

        match = match.toLowerCase();
        Log.d(TAG, "Got speech: " + match);

        if (match.contains("go")) {
            //Do Something
            mSpeechRecognizer.stopListening();
        }
        if (match.contains("stop")) {
            //Do Something
            mSpeechRecognizer.stopListening();
        }
    }
}

protected class SpeechRecognitionListener implements RecognitionListener
{

    @Override
    public void onBeginningOfSpeech()
    {
        //Log.d(TAG, "onBeginingOfSpeech");
    }

    @Override
    public void onBufferReceived(byte[] buffer)
    {

    }

    @Override
    public void onEndOfSpeech()
    {
        //Log.d(TAG, "onEndOfSpeech");
    }

    @Override
    public void onError(int error)
    {
        mSpeechRecognizer.startListening(mSpeechRecognizerIntent);

        //Log.d(TAG, "error = " + error);
    }

    @Override
    public void onEvent(int eventType, Bundle params)
    {

    }

    @Override
    public void onPartialResults(Bundle partialResults)
    {
        ArrayList<String> matches = partialResults.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
        for (String match : matches) {
            match = match.toLowerCase();
            Log.d(TAG, "onPartialResults : " + match);
        }
    }

    @Override
    public void onReadyForSpeech(Bundle params)
    {
        Log.d(TAG, "onReadyForSpeech"); //$NON-NLS-1$
    }

    @Override
    public void onResults(Bundle results)
    {
        //Log.d(TAG, "onResults"); //$NON-NLS-1$
        ArrayList<String> matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
        onSpeechResults(matches);
        // matches are the return values of speech recognition engine
        // Use these values for whatever you wish to do
    }

    @Override
    public void onRmsChanged(float rmsdB)
    {
    }
}}

谢谢大家的时间!希望有人可以帮助我一点:P!

1 个答案:

答案 0 :(得分:2)

是的,可以减少关机前的延迟....

您无法改变Google在用户发言结束时认为沉默的时间。过去使用的EXTRA_SPEECH_*参数,现在它们似乎偶尔会起作用,或根本不起作用。

您可以做的是使用部分结果来检测您想要的单词或短语,然后手动关闭识别服务。

以下是如何执行此操作的示例:

public boolean isHelloDetected(@NonNull final Context ctx, @NonNull final Locale loc, @NonNull final Bundle results) {

        boolean helloDetected = false;

        if (!results.isEmpty()) {

            final String hello = ctx.getString(R.string.hello);

            final ArrayList<String> partialData = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);

                /* handles empty string bug */
            if (partialData != null && !partialData.isEmpty()) {
                partialData.removeAll(Collections.singleton(""));

                if (!partialData.isEmpty()) {
                    final ListIterator<String> itr = partialData.listIterator();

                    String vd;
                    while (itr.hasNext()) {
                        vd = itr.next().toLowerCase(loc).trim();

                        if (vd.startsWith(hello)) {
                            helloDetected = true;
                            break;
                        }
                    }
                }
            }

            if (!helloDetected) {
                final ArrayList<String> unstableData = results.getStringArrayList("android.speech.extra.UNSTABLE_TEXT");

                    /* handles empty string bug */
                if (unstableData != null && !unstableData.isEmpty()) {
                    unstableData.removeAll(Collections.singleton(""));

                    if (!unstableData.isEmpty()) {
                        final ListIterator<String> itr = unstableData.listIterator();

                        String vd;
                        while (itr.hasNext()) {
                            vd = itr.next().toLowerCase(loc).trim();

                            if (vd.startsWith(hello)) {
                                helloDetected = true;
                                break;
                            }
                        }
                    }
                }
            }
        }

        return helloDetected;
    }

每次收到onPartialResults()

时都会运行此方法

如果返回true,则您需要在主线程上调用stopListening()(可能是new Handler(Looper.getMainLooper()).post(...

请注意,一旦您关闭了识别器,您在onResults()收到的后续和最终结果可能包含&#34;您好&#34;。因为这个词可能只被归类为不稳定。

您需要编写其他逻辑以防止在检测到hello后使用detectHello()(否则您将反复调用stopListening()) - 一些简单的布尔标记可以解决此问题。

最后,使用Collections.singleton("")删除空字符串是内部错误报告details to replicate here的一部分,使用ListIterator可能对您的样本而言过度杀伤;一个简单的for循环就足够了。

祝你好运。