音声認識をシステムに組み込むためのサンプル

人工知能

 前回は、音声発生をシステムに組み込むためのサンプルを紹介しましたが、今回は、音声認識をシステムに組み込むためのサンプルを紹介します。プログラミング言語は、C#です。

 音声認識も、音声発生同様、インターネット接続が不要です。「SpeechRecognitionEngine」という.NET Frameworkのライブラリを使います。予め指定してある単語を聞き取ったときのみ反応し、それに応じた処理を行うことができます。

 常駐アプリとして起動させておくことで、さまざまなアプリを音声で起動させたり終了させることが可能です。また、システムに組み込むことで、音声でメニューを選択させたりすることも可能です。

 C#のサンプルコードは、次のとおりです。

using System;
using System.Linq;
using System.Speech.Recognition;
using System.Diagnostics;

namespace VoiceToShell
{
    /// <summary>
    /// 音声コマンド
    /// </summary>
    public class VoiceCommand : IDisposable
    {
        /// <summary>
        /// 聴覚認識エンジンの定義
        /// </summary>
        SpeechRecognitionEngine engine1;
        SpeechRecognitionEngine engine2;

        /// <summary>
        /// 対象となるアプリケーション名と起動コマンド
        /// </summary>
        static string[] AppNames = new string[] { "ホゲシステム", "ホゲホゲシステム" };
        static string[] ShellWord = new string[] { "起動" };

        /// <summary>
        /// デフォルト コンストラクタ
        /// </summary>  
        public VoiceCommand()
        {
            //
        }

        /// <summary>
        /// 対象となるアプリケーション名と起動コマンドのセット
        /// </summary>
        public void Open()
        {
            //対象となるアプリケーション名のセット
            Choices awake = new Choices(AppNames);
            this.engine1 = Open(new GrammarBuilder(awake));

            //対象となる起動コマンドのセット
            Choices actions = new Choices();
            actions.Add(ShellWord);

            GrammarBuilder gb = new GrammarBuilder();
            gb.Append(awake);
            gb.Append(actions);
            this.engine2 = Open(gb);
        }

        /// <summary>
        /// 聴覚認識エンジンの起動
        /// </summary>
        /// <param name="gb"></param>
        /// <returns>聴覚認識エンジンのインスタンス</returns>
        private SpeechRecognitionEngine Open(GrammarBuilder gb)
        {
            //聴覚認識エンジンのインスタンス生成
            var engine = new SpeechRecognitionEngine();

            //イベント追加
            engine.SpeechRecognized += recogEngine_SpeechRecognized;

            //聴覚認識エンジンの設定更新
            engine.LoadGrammar(new Grammar(gb));
            engine.SetInputToDefaultAudioDevice();
            engine.RecognizeAsync(RecognizeMode.Multiple);

            //聴覚認識エンジンのインスタンスを戻り値にセット
            return engine;
        }

        /// <summary>
        /// 聴覚認識エンジンの終了
        /// </summary>
        public void Close()
        {
            //起動コマンド側の聴覚認識エンジンの停止
            if (this.engine2 != null)
            {
                this.engine2.SpeechRecognized -= recogEngine_SpeechRecognized;
                this.engine2.Dispose();
                this.engine2 = null;
            }

            //アプリケーション名側の聴覚認識エンジンの停止
            if (this.engine1 != null)
            {
                this.engine1.SpeechRecognized -= recogEngine_SpeechRecognized;
                this.engine1.Dispose();
                this.engine1 = null;
            }
        }

        /// <summary>
        /// このクラスの破棄
        /// </summary>
        public void Dispose()
        {
            //聴覚認識エンジンの終了
            this.Close();
        }


        /// <summary>
        /// 聴覚認識イベント
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void recogEngine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            //認識した文字列を表示
            Console.WriteLine(e.Result.Text);

            //起動ワードを含むか
            bool isCalledName = ShellWord.Any(x => e.Result.Text.Contains(x));
            if (isCalledName)
            {
                //対象となるアプリケーションを起動
                string cmd = GetCommand(e.Result.Text);
                if (cmd != "")
                {
                    Process p = Process.Start(cmd);
                }
            }
        }

        /// <summary>
        /// 起動コマンドを返す
        /// </summary>
        /// <param name="s"></param>
        /// <returns>起動コマンド</returns>
        private string GetCommand(string s)
        {
            //対象となるアプリケーション名を取得
            int iRes = -1;
            for (int i = 0; i < AppNames.Length; ++i)
            {
                //取得したら配列のインデックスのみ確保
                if (0 <= s.IndexOf(AppNames[i]))
                {
                    iRes = i;
                    break;
                }
            }

            //配列のインデックスから実行すべきファイルのパスを返す
            switch (iRes)
            {
                case 0:
                    return @"C:\TEMP\hoge.exe";
                case 1:
                    return @"C:\TEMP\hogehoge.exe";
                default:
                    return "";
            }
        }
    }
}

コメント

タイトルとURLをコピーしました