9

I've been thinking lately to start an application based on Speech recognition. Meaning on certain results to do specific tasks. I was wondering what is the best way to proceed. I'm thinking either for PC or Android also. I would consider JAVA as my strong programming language.

I've done some searching but still I don't know which is the best way to approach this.

Have an open software do the speech recognition part for me and work on the other part? Do the whole thing by myself? And if yes is it possible in JAVA?

Any info will be appreciated.

Thank you in advance.

4

3 回答 3

6

解决此问题的最佳方法是使用现有的识别工具包以及随附的语言和声学模型。您可以训练模型以满足您的需求。

CMUSphinx可能是目前最好的 FOSS 语音识别工具包。CMUSphinx 还提供了良好的 Java 集成和演示应用程序。

于 2013-08-22T18:40:11.523 回答
4

在评估了几个第三方语音识别选项后,谷歌语音识别是迄今为止最准确的。使用 Google 语音识别时有两种基本方法。最简单的方法是启动一个 Intent 并相应地处理结果:

    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);

    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);

    startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE );

然后在您的 onActivityResults() 中,您将处理服务返回的匹配项:

    /**
 * Handle the results from the recognition activity.
 */
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    //Toast.makeText(this, "voice recog result: " + resultCode, Toast.LENGTH_LONG).show();
    if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) {
        // Fill the list view with the strings the recognizer thought it could have heard
        ArrayList<String> matches = data.getStringArrayListExtra(
                RecognizerIntent.EXTRA_RESULTS);
        // handleResults
        if (matches != null) {
            handleResults(matches); 
        }                    
    }     
}

第二种方法涉及更多,但可以更好地处理识别服务运行时可能发生的错误情况。使用这种方法,您将创建自己的识别侦听器和回调方法。例如:

开始聆听:

mSpeechRecognizer.startListening(mRecognizerIntent);

其中 mRecognizerIntent:

    mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(getBaseContext());
    mSpeechRecognizer.setRecognitionListener(mRecognitionListener);
    mRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    mRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
            RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    mRecognizerIntent.putExtra("calling_package", "com.you.package");

然后,创建您的侦听器:

    private RecognitionListener mRecognitionListener = new RecognitionListener() {
            public void onBufferReceived(byte[] buffer) {
                    // TODO Auto-generated method stub
                    //Log.d(TAG, "onBufferReceived");
            }

            public void onError(int error) {
                    // TODO Auto-generated method stub
                    // here is where you handle the error...


            public void onEvent(int eventType, Bundle params) {
                    // TODO Auto-generated method stub
                    Log.d(TAG, "onEvent");
            }

            public void onPartialResults(Bundle partialResults) {
                    // TODO Auto-generated method stub
                    Log.d(TAG, "onPartialResults");
            }

            public void onReadyForSpeech(Bundle params) {
                    // TODO Auto-generated method stub
                    Log.d(TAG, "onReadyForSpeech");

            }

            public void onResults(Bundle results) {

                    Log.d(TAG, ">>> onResults");
                    //Toast.makeText(getBaseContext(), "got voice results!", Toast.LENGTH_SHORT);

                    ArrayList<String> matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
                    handleResults(matches);


            }

            public void onRmsChanged(float rmsdB) {
                    // TODO Auto-generated method stub
                    //Log.d(TAG, "onRmsChanged");
            }

            public void onBeginningOfSpeech() {
                    // TODO Auto-generated method stub
                    Log.d(TAG, "onBeginningOfSpeech");
            }

            public void onEndOfSpeech() {
                    // TODO Auto-generated method stub
                    Log.d(TAG, "onEndOfSpeech");

            }

};

你可以添加你的 handleResults() 来做任何你想做的事情。

于 2013-08-22T19:36:35.883 回答
1

您还可以使用 Google Speech API。从 Android 可以通过 SpeechRecognizer类参考访问它

这是一个 stackoverflow 问题的链接,其中还包含一些 Java 演示代码:Java 中的语音识别

于 2013-08-22T18:52:02.130 回答