前提・実現したいこと
Unity内でWatson APIを利用したいです。
Unityちゃんとwatson apiをつなげるという以下のリンクのハンズオンを行なっており、現在、Speech to textの部分で詰まっています。
リンク内容
そのためにWatson APIをUnity内でIAM方式で認証しようとしています。
以前まではusernameとurl,passwordを用いた認証でしたが、最近IAM方式に変更になったため、参考になるリンクがなく困っています。
以下のリンクのAuthenticationの部分に、コードが書いてあるのですが、それをどう直せばいいのか全くわからず、これを貼り付けるとそもそも実行ができずコンパイルエラーが発生してしまいます。
unityでのC#で、watson apiのIAM認証方式についてご存知であれば教えていただけないでしょうか?
よろしくお願いします。
以下のエラーメッセージが発生しました。
発生している問題・エラーメッセージ
ampleSpeechToText1.OnFail() Error received: URL: https://stream.watsonplatform.net/speech-to-text/api/v1/recognize?inactivity_timeout=60&keywords=ibm&keywords_threshold=0.1&max_alternatives=1&model=ja-JP_BroadbandModel&profanity_filter=True&smart_formatting=False&speaker_labels=False×tamps=false&word_confidence=false, ErrorCode: 401, Error: 401 Unauthorized, Response: {"code":401, "error": "Unauthorized"} UnityEngine.Debug:Log(Object) SampleSpeechToText1:OnFail(Error, Dictionary`2) (at Assets/UnityChan/Scripts/SampleSpeechToText1.cs:79) IBM.Watson.DeveloperCloud.Services.SpeechToText.v1.SpeechToText:OnRecognizeResponse(Request, Response) (at Assets/watson/Scripts/Services/SpeechToText/v1/SpeechToText.cs:986) IBM.Watson.DeveloperCloud.Connection.<ProcessRequestQueue>c__Iterator0:MoveNext() (at Assets/watson/Scripts/Connection/RESTConnector.cs:534) IBM.Watson.DeveloperCloud.Utilities.Routine:MoveNext() (at Assets/watson/Scripts/Utilities/Runnable.cs:128) UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)
該当のソースコード
C#
1//SampleSpeechToText1.cs 2 3using System.Collections; 4using System.Collections.Generic; 5using UnityEngine; 6using IBM.Watson.DeveloperCloud.Services.SpeechToText.v1; 7using IBM.Watson.DeveloperCloud.Services.VisualRecognition.v3; 8using IBM.Watson.DeveloperCloud.Utilities; 9using IBM.Watson.DeveloperCloud.Connection; 10using IBM.Watson.DeveloperCloud.Services.Assistant.v1; 11 12public class SampleSpeechToText1 : MonoBehaviour 13{ 14 15 [SerializeField] 16 //private AudioClip m_AudioClip = new AudioClip(); 17 public AudioClip AudioClip; 18 //private SpeechToText m_SpeechToText = new SpeechToText(); 19 private SpeechToText m_SpeechToText; 20 21 22 23 // Use this for initialization 24 25 IEnumerator Start() 26 { 27 28 string apikey = "<apikey>"; 29 string url = "<url>"; 30 string auth = "<IAM-Authentication-Token>"; 31 32 Credentials credentials = new Credentials(apikey,auth,url); 33 m_SpeechToText = new SpeechToText(credentials); 34 m_SpeechToText.Keywords = new string[] { "ibm" }; 35 m_SpeechToText.KeywordsThreshold = 0.1f; 36 37 38 // 音声をマイクから 5 秒間取得する 39 Debug.Log("Start record"); //集音開始 40 var audioSource = GetComponent<AudioSource>(); 41 audioSource.clip = Microphone.Start(null, true, 10, 44100); 42 audioSource.loop = false; 43 audioSource.spatialBlend = 0.0f; 44 yield return new WaitForSeconds(3f); 45 Microphone.End(null); //集音終了 46 Debug.Log("Finish record"); 47 48 // ためしに録音内容を再生してみる 49 audioSource.Play(); 50 51 // SpeechToText を日本語指定して、録音音声をテキストに変換 52 m_SpeechToText.RecognizeModel = "ja-JP_BroadbandModel"; 53 54 //m_SpeechToText.Recognize(HandleOnRecognize, audioSource.clip); 55 m_SpeechToText.Recognize(HandleOnRecognize,OnFail, audioSource.clip); 56 Debug.Log("result"); 57 } 58 59 60 private void HandleOnRecognize(SpeechRecognitionEvent result, Dictionary<string, object> customData) 61 { 62 63 if (result != null && result.results.Length > 0) 64 { 65 foreach (var res in result.results) 66 { 67 foreach (var alt in res.alternatives) 68 { 69 string text = alt.transcript; 70 Debug.Log(string.Format("{0} ({1}, {2:0.00})\n", text, res.final ? "Final" : "Interim", alt.confidence)); 71 } 72 } 73 } 74 } 75 private void OnFail(RESTConnector.Error error, Dictionary<string, object> customData) 76 { 77 Debug.Log("SampleSpeechToText1.OnFail() Error received: " + error.ToString()); 78 } 79 80 // Update is called once per frame 81 void Update() 82 { 83 84 } 85}
試したこと
リンク内容
このリンクにあるauthenticationの部分を貼り付けてみましたが、動きませんでした。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。