Created
December 20, 2018 00:38
-
-
Save alfredplpl/7e5b3fa6f248b0c2086b7b87632f852f to your computer and use it in GitHub Desktop.
Speech-To-Text on Unity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEngine.UI; | |
using UnityEngine.Windows.Speech; | |
using System.Collections.Generic; | |
using System.Linq; | |
public class STT : MonoBehaviour | |
{ | |
public UnityEngine.UI.Text resultDisplay; | |
public UnityEngine.UI.Text TTSDisplay; | |
public AudioSource sphere; | |
public AudioSource cube; | |
public AudioSource what; | |
public AudioSource hello; | |
public AudioSource goodbye; | |
public GameObject gazePoint; | |
public GameObject[] gameObjects; | |
KeywordRecognizer keywordRecognizer; | |
Dictionary<string, System.Action> keywords = new Dictionary<string, System.Action>(); | |
void Start() | |
{ | |
//キーワード認識器用にキーワードとそれに対するアクションを入力 | |
keywords.Add("これはなんですか", () => | |
{ | |
resultDisplay.text = "プレーヤー:これはなんですか"; | |
GameObject thisObject=null; | |
foreach(GameObject gameObject in gameObjects){ | |
if(isCollision(gameObject.transform.position,gazePoint.transform.position,0.5f)){ | |
thisObject=gameObject; | |
} | |
} | |
if(thisObject == null){ | |
what.Play(); | |
TTSDisplay.text="アリシア:どれのことですか?"; | |
}else if(thisObject.name == "Boll"){ | |
sphere.Play(); | |
TTSDisplay.text="アリシア:これは球です。"; | |
}else if(thisObject.name == "Box"){ | |
cube.Play(); | |
TTSDisplay.text="アリシア:これは立方体です。"; | |
} | |
}); | |
keywords.Add("こんにちは", () => | |
{ | |
resultDisplay.text = "プレーヤー:こんにちは"; | |
hello.Play(); | |
TTSDisplay.text="アリシア:こんにちは"; | |
}); | |
keywords.Add("さようなら", () => | |
{ | |
resultDisplay.text = "プレーヤー:さようなら。"; | |
goodbye.Play(); | |
TTSDisplay.text="アリシア:さようなら。またお会いしましょう。"; | |
}); | |
keywordRecognizer = new KeywordRecognizer(keywords.Keys.ToArray()); | |
keywordRecognizer.OnPhraseRecognized += KeywordRecognizer_OnPhraseRecognized; | |
keywordRecognizer.Start(); | |
Debug.Log("Setting done."); | |
} | |
private bool isCollision(Vector3 a,Vector3 b,float r){ | |
float x = a.x - b.x; | |
float y = a.y - b.y; | |
float z = a.z - b.z; | |
// on collision | |
if (x * x + y * y + z * z < r*r) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
private void KeywordRecognizer_OnPhraseRecognized(PhraseRecognizedEventArgs args) | |
{ | |
System.Action keywordAction; | |
// if the keyword recognized is in our dictionary, call that Action. | |
if (keywords.TryGetValue(args.text, out keywordAction)) | |
{ | |
keywordAction.Invoke(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment