質問するログイン新規登録

回答編集履歴

2

内容追記

2020/10/19 16:42

投稿

u824
u824

スコア112

answer CHANGED
@@ -19,4 +19,179 @@
19
19
  Qiita | UnityでMicrosoft Cognitive Speech Servicesによる音声認識をAndroidスマホで実装する
20
20
  [https://qiita.com/tomato_sugar/items/ac4b5dbe8277496add56](https://qiita.com/tomato_sugar/items/ac4b5dbe8277496add56)
21
21
 
22
- 途中でわからないことがあれば言ってください。アドバイスできると思います。
22
+ 途中でわからないことがあれば言ってください。アドバイスできると思います。
23
+
24
+ ---
25
+
26
+ 追記:Azure Cognitive Speech SDK 導入方法
27
+
28
+ 自分のアカウントのAzure上で Cognitive Services を立てている前提です。
29
+ 立て方は上記のQiita記事を参考にしてください。
30
+
31
+ 1, Microsoftさん公式のUnityのサンプルプロジェクトをgit cloneする。
32
+ [https://github.com/Azure-Samples/cognitive-services-speech-sdk/tree/master/quickstart/csharp/unity/from-microphone](https://github.com/Azure-Samples/cognitive-services-speech-sdk/tree/master/quickstart/csharp/unity/from-microphone)
33
+ ※これをzipでDLして quickstart/csharp/unity/from-microphone でもよい。
34
+ [https://github.com/Azure-Samples/cognitive-services-speech-sdk](https://github.com/Azure-Samples/cognitive-services-speech-sdk)
35
+
36
+ 2, `Microsoft.CognitiveServices.Speech.xxx.unitypackage` をDLしてインポートする[https://aka.ms/csspeech/unitypackage](https://aka.ms/csspeech/unitypackage)
37
+
38
+ 3, Assets/Scripts/`HelloWorld.cs` を下の内容に書き換える。
39
+ 変更点は、キー等の情報をスクリプトに直接記入するようになっていたので[SerializeField]でEditor上から入力できるようにした。あと日本語対応。
40
+ ```C#
41
+ //
42
+ // Copyright (c) Microsoft. All rights reserved.
43
+ // Licensed under the MIT license. See LICENSE.md file in the project root for full license information.
44
+ //
45
+ // <code>
46
+ using UnityEngine;
47
+ using UnityEngine.UI;
48
+ using Microsoft.CognitiveServices.Speech;
49
+ #if PLATFORM_ANDROID
50
+ using UnityEngine.Android;
51
+ #endif
52
+ #if PLATFORM_IOS
53
+ using UnityEngine.iOS;
54
+ using System.Collections;
55
+ #endif
56
+
57
+ public class HelloWorld : MonoBehaviour
58
+ {
59
+ // Hook up the two properties below with a Text and Button object in your UI.
60
+ public Text outputText;
61
+ public Button startRecoButton;
62
+
63
+ private object threadLocker = new object();
64
+ private bool waitingForReco;
65
+ private string message;
66
+
67
+ private bool micPermissionGranted = false;
68
+
69
+ #if PLATFORM_ANDROID || PLATFORM_IOS
70
+ // Required to manifest microphone permission, cf.
71
+ // https://docs.unity3d.com/Manual/android-manifest.html
72
+ private Microphone mic;
73
+ #endif
74
+
75
+ [SerializeField] string subscriptionKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
76
+ [SerializeField] string serviceRegion = "japaneast";
77
+
78
+ public async void ButtonClick()
79
+ {
80
+ // Creates an instance of a speech config with specified subscription key and service region.
81
+ // Replace with your own subscription key and service region (e.g., "westus").
82
+ var config = SpeechConfig.FromSubscription(subscriptionKey, serviceRegion);
83
+ var lang = SourceLanguageConfig.FromLanguage("ja-JP");
84
+
85
+ // Make sure to dispose the recognizer after use!
86
+ using (var recognizer = new SpeechRecognizer(config, lang))
87
+ {
88
+ lock (threadLocker)
89
+ {
90
+ waitingForReco = true;
91
+ }
92
+
93
+ // Starts speech recognition, and returns after a single utterance is recognized. The end of a
94
+ // single utterance is determined by listening for silence at the end or until a maximum of 15
95
+ // seconds of audio is processed. The task returns the recognition text as result.
96
+ // Note: Since RecognizeOnceAsync() returns only a single utterance, it is suitable only for single
97
+ // shot recognition like command or query.
98
+ // For long-running multi-utterance recognition, use StartContinuousRecognitionAsync() instead.
99
+ var result = await recognizer.RecognizeOnceAsync().ConfigureAwait(false);
100
+
101
+ // Checks result.
102
+ string newMessage = string.Empty;
103
+ if (result.Reason == ResultReason.RecognizedSpeech)
104
+ {
105
+ newMessage = result.Text;
106
+ }
107
+ else if (result.Reason == ResultReason.NoMatch)
108
+ {
109
+ newMessage = "NOMATCH: Speech could not be recognized.";
110
+ }
111
+ else if (result.Reason == ResultReason.Canceled)
112
+ {
113
+ var cancellation = CancellationDetails.FromResult(result);
114
+ newMessage = $"CANCELED: Reason={cancellation.Reason} ErrorDetails={cancellation.ErrorDetails}";
115
+ }
116
+
117
+ lock (threadLocker)
118
+ {
119
+ message = newMessage;
120
+ waitingForReco = false;
121
+ }
122
+ }
123
+ }
124
+
125
+ void Start()
126
+ {
127
+ if (outputText == null)
128
+ {
129
+ UnityEngine.Debug.LogError("outputText property is null! Assign a UI Text element to it.");
130
+ }
131
+ else if (startRecoButton == null)
132
+ {
133
+ message = "startRecoButton property is null! Assign a UI Button to it.";
134
+ UnityEngine.Debug.LogError(message);
135
+ }
136
+ else
137
+ {
138
+ // Continue with normal initialization, Text and Button objects are present.
139
+ #if PLATFORM_ANDROID
140
+ // Request to use the microphone, cf.
141
+ // https://docs.unity3d.com/Manual/android-RequestingPermissions.html
142
+ message = "Waiting for mic permission";
143
+ if (!Permission.HasUserAuthorizedPermission(Permission.Microphone))
144
+ {
145
+ Permission.RequestUserPermission(Permission.Microphone);
146
+ }
147
+ #elif PLATFORM_IOS
148
+ if (!Application.HasUserAuthorization(UserAuthorization.Microphone))
149
+ {
150
+ Application.RequestUserAuthorization(UserAuthorization.Microphone);
151
+ }
152
+ #else
153
+ micPermissionGranted = true;
154
+ message = "Click button to recognize speech";
155
+ #endif
156
+ startRecoButton.onClick.AddListener(ButtonClick);
157
+ }
158
+ }
159
+
160
+ void Update()
161
+ {
162
+ #if PLATFORM_ANDROID
163
+ if (!micPermissionGranted && Permission.HasUserAuthorizedPermission(Permission.Microphone))
164
+ {
165
+ micPermissionGranted = true;
166
+ message = "Click button to recognize speech";
167
+ }
168
+ #elif PLATFORM_IOS
169
+ if (!micPermissionGranted && Application.HasUserAuthorization(UserAuthorization.Microphone))
170
+ {
171
+ micPermissionGranted = true;
172
+ message = "Click button to recognize speech";
173
+ }
174
+ #endif
175
+
176
+ lock (threadLocker)
177
+ {
178
+ if (startRecoButton != null)
179
+ {
180
+ startRecoButton.interactable = !waitingForReco && micPermissionGranted;
181
+ }
182
+ if (outputText != null)
183
+ {
184
+ outputText.text = message;
185
+ }
186
+ }
187
+ }
188
+ }
189
+ // </code>
190
+
191
+ ```
192
+
193
+ 4, 作成済みのリソースから `キー1` を `subscriptionKey` に張り付ける。また `場所` が `serviceRegion` に相当するので japaneast でなければ書き換える。
194
+ ![イメージ説明](2d0868f322cff3abb9cc4176eb76a664.png)
195
+
196
+ 5, Build Settings の Target Platform を Android に変更してビルド。実機で動作確認。(Windows でも iOS でもOKです。)
197
+ ![イメージ説明](f42c63ce85e8268f8bf7115cf7062f29.jpeg)

1

書式修正

2020/10/19 16:42

投稿

u824
u824

スコア112

answer CHANGED
@@ -1,9 +1,9 @@
1
- >音声認識APIを用いて音声をテキスト化し、switch文の中で照合させて合致したcase内の処理によってアニメーションを変えるという流れなのでしょうか
1
+ > 音声認識APIを用いて音声をテキスト化し、switch文の中で照合させて合致したcase内の処理によってアニメーションを変えるという流れなのでしょうか
2
2
 
3
3
  その処理で大丈夫です。
4
4
 
5
5
 
6
- >『そもそもAPIをどの様に用いれば上記の処理が出来るのか』が分からない
6
+ > 『そもそもAPIをどの様に用いれば上記の処理が出来るのか』が分からない
7
7
 
8
8
  unityで利用できる音声認識APIは以下のようなものがあります。
9
9
  ・`UnityEngine.Windows.Speech`