Android実戦テクニックその4:音声認識


Googleの音声認識は目に見えるので、Androidにも光が付いていて、使いやすいです.
プロセスは次のとおりです.
1、音声認識Activityの起動
2、ここで音声処理(googleサーバに転送)
3、結果をActivityの結果で返す(onActivity Result)
主に使われるクラスはandroidです.speech.RecognizerIntent
以下の例ではAPI Demoを参照する.
package com.linc;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class VoiceRecognitionDemoActivity extends Activity {
    private static final String TAG = "VoiceRecognition";
    private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;
    
	private TextView textView;
	private Button button;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        initWidget();
        
        // Check to see if a recognition activity is present
        PackageManager pm = getPackageManager();
        List<ResolveInfo> activities = pm.queryIntentActivities(
                new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
        if (activities.size() != 0) {
        	button.setOnClickListener(new OnClickListener() {
        		@Override
        		public void onClick(View v) {
        			startVoiceRecognitionActivity();
        		}
        	});
        } else {
        	button.setEnabled(false);
        	button.setText("Recognizer not present");
        }
    }
    
    private void initWidget()
    {
    	textView = (TextView)findViewById(R.id.tv);
    	button = (Button)findViewById(R.id.btn);
    }
    
    /**
     * Fire an intent to start the speech recognition activity.
     */
    private void startVoiceRecognitionActivity() {
        Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);

        // Display an hint to the user about what he should say.
        intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "       ");//       

        // Given an hint to the recognizer about what the user is going to say
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);

        // Specify how many results you want to receive. The results will be sorted
        // where the first result is the one with higher confidence.
        intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 5);//     ,          。

        startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        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);
            StringBuilder stringBuilder = new StringBuilder();
            int Size = matches.size(); 
            for(int i=0;i<Size;++i)
            {
            	stringBuilder.append(matches.get(i));
            	stringBuilder.append("
"); } textView.setText(stringBuilder); } super.onActivityResult(requestCode, resultCode, data); } }
結論:
wifi(自宅の、1兆帯域)状態では、認識速度が速い.
音声パックは携帯電話を買うときにプリインストールされています.
認識率が高い.中国語も英語も数字もよく認識できます.
携帯電話のgprsでは、効果があまりよくありません.何度識別しても30秒くらいの時間がかかり、体験が大変でした.