2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

UnityのKeywordRecognizerを使ってみる

Last updated at Posted at 2022-01-10

この記事について

Unityの標準で用意されているKeywordRecognizerを利用してみます。

環境

Windows10(Windows10でしか動かないそうです)
Unity 2021.3.24f1

やりたいこと

今回やりたいことはゲーム内で音声を受け取ってその結果を取得してゲームに反映させるということを使用と思います。
例:メニューと発言するとUIが表示される

KeywordRecognizer

KeywordRecognizerはUnityが標準で用意している音声認識ライブラリです。
このKeywordRecognizerは登録されているキーワードの中から発言内容と最も近しい内容のものを出力するというものになっています。

簡単実装

まずは簡単に実装してみましょう。

SimpleKeywordRecognizer.cs
    public class SimpleKeywordRecognizer:MonoBehaviour
    {
        [SerializeField]private string[] keywords;
        private KeywordRecognizer _recognizer;

        void Awake()
        {
            _recognizer = new KeywordRecognizer(keywords);
        }

        private void OnEnable()
        {
            _recognizer.OnPhraseRecognized += OnPhraseRecognized;
            _recognizer.Start();
        }

        private void OnDisable()
        {
            _recognizer.OnPhraseRecognized -= OnPhraseRecognized;
            _recognizer.Stop();
        }

        private void OnPhraseRecognized(PhraseRecognizedEventArgs args)
        {
            Debug.Log(args.text + $", LEVEL:{args.confidence}");
        }
    }

出力内容

音声の入力があったと判定されると「OnPhraseRecognized」が発火します。
引数として「PhraseRecognizedEventArgs」が指定されておりますが、こちらには認識された音声内容が入っています。

説明
text 認識された音声の文字列データ。(typeSystem.String)
confidence 認識精度。High/Middle/Lowのいずれかの値が入ります。(typeUnityEngine.Windows.Speech.ConfidenceLevel)
phraseDuration 音声の長さ。(typeSystem.TimeSpan)
phraseStartTime 音声開始日時。(typeSystem.DateTime)
semanticMeanings 認識されたフレーズの語義的な意味(よく分からない)

image.png

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?