可以使SpeechRecognizer更快吗? [英] It's possible to make SpeechRecognizer faster?

查看:73
本文介绍了可以使SpeechRecognizer更快吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个使用android SpeechRecognizer的应用程序.我将其用于简单的操作.我单击一个按钮,SpeechRecognizer开始收听,我从我的讲话中得到了一些结果.

I'm developing an application which is using the android SpeechRecognizer. I'm using it for something simple. I click in a button, my SpeechRecognizer start listening and I got some results from what I said.

容易吗?好吧,我的问题是我需要使SpeechRecognizer快速运行.我的意思是,我点击我的按钮,然后说你好".而SpeechRecognizer大约需要3-4秒才能返回具有可能结果的数组.我的问题是:

Easy right? Well, My problem is that I need to make SpeechRecognizer fast. I mean, I click in my button, I say "Hello" and SpeechRecognizer takes like 3-4 seconds in return an array with the possible results. My question is:

是否可以使SpeechRecognizer返回结果更快? 还是花费更少的时间来关闭倾听"意图并开始处理它的倾听? 也许是另一种方式呢?哪个会比这更好?

It's possible to make SpeechRecognizer return results more faster? Or take less time to close the Listening intent and start to process what it listen? Maybe another way to do it? which will have a better performance than this?

我正在检查库,并且看到了这3个参数:

I was checking the library and I saw this 3 parameters:

EXTRA_SPEECH_INPUT_MINIMUM_LENGTH_MILLIS:

在我们停止听语音以考虑输入内容之后应该花费的时间.

The amount of time that it should take after we stop hearing speech to consider the input complete.

EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS

话语的最小长度.

EXTRA_SPEECH_INPUT_POSSIBLY_COMPLETE_SILENCE_LENGTH_MILLIS

我们停止听见语音提示后应该花费的时间 认为输入可能已完成.

The amount of time that it should take after we stop hearing speech to consider the input possibly complete.

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

我已经尝试了所有这些,但是它没有用,或者也许我没有正确使用它们.这是我的代码:

I have tried all of them but it is not working, or maybe I'm not using them right. Here is my code:

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)
    {
    }
}}

推荐答案

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

Yes, it is possible to reduce the delay before shutdown....

您不能更改在用户讲话结束后Google认为保持沉默的时间. EXTRA_SPEECH_*参数曾经可以使用,但现在看起来似乎只能偶尔发挥作用,或者根本不起作用.

You cannot alter the amount of time that Google considers to be silence at the end of a user speaking. The EXTRA_SPEECH_* parameters used to work, now they appear to sporadically work at best, or not work at all.

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

What you can do, is use the partial results to detect the words or phrase you want and then manually shut down the recognition service.

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

Here's an example of how to do this:

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(...

If true is returned, you'll need to call stopListening() on the main thread (probably by new Handler(Looper.getMainLooper()).post(...

但是请注意,一旦关闭识别器,在onResults()中收到的后续结果和最终结果可能会包含"hello".由于该词可能仅被分类为不稳定词.

Be aware though, once you've shut down the recognizer, the subsequent and final results you receive in onResults() may not contain "hello". As that word may have only be classified as unstable.

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

You'll need to write additional logic to prevent using detectHello() once hello has been detected (otherwise you'll repeatedly call stopListening()) - some simple boolean markers would resolve this.

最后,使用Collections.singleton("")删除空字符串是内部错误报告的一部分,详细信息在此处复制,并且仅对您的示例使用ListIterator可能会过大;一个简单的for循环就足够了.

Finally, the use of Collections.singleton("") to remove empty strings is part of an internal bug report, details to replicate here and the use of a ListIterator may be overkill for just your sample; a simple for loop would suffice.

祝你好运.

这篇关于可以使SpeechRecognizer更快吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆