えむにわリソース

ITのスキマ的なあれこれを書きます。

Google Cloud Speech APIを使ったHoloLensアプリ(Unity2017対応など)

先週のできごと

先日、Google Cloud Speech APIを使ったアプリを制作し、リクルートATL様に寄稿させていただきました。

f:id:m2wasabi:20170718072611j:image
HoloLensで言霊を撃ってみよう | ATL SHOWCASE

それにまつわる内容で、 Tokyo HoloLens ミートアップ vol.4でLTの機会を頂きました。

www.slideshare.net

HoloLensでの音声認識は手垢の付いたネタにも関わらず良い反響を頂きました。ありがとうございます。

Unity・アセットをアップデート

アプリ自体の紹介は上記リンクにお任せするとして、 寄稿・紹介後に、Unity2017への対応・Google Cloud Speech RecognitionのV3.0へのアップグレードを行ったので、 それについて書きます。

アプリのソースコードは公開しています。

github.com

Unity2017への更新

元々Unity5.6.1 で作成したアプリですが、寄稿が終わったのでUnity 2017.1.0f3 に更新しました。 本アプリ自体、特に難しいことはしてませんので、各アセットのバージョンアップ対応が主になります。

HoloToolkit for Unity

Unity2017対応は、アーカイブはまだですがmasterブランチでは行われています。 /Assets/HoloToolkit/ディレクトリをプロジェクトから削除し、 ダウンロードしたコードから /Assets/HoloToolkit/ をコピーすることで無事動作しました。 プロジェクト内のprefabやオブジェクトは適宜置き換えましょう。

github.com

Google Cloud Speech Recognition

Google Cloud Speech Recognition

Unity2017リリースの少し前に バージョン3.0に更新されました。

1. ThreadManager.csの更新

バージョン2.3まではUWPアプリケーションでも問題なく動作していたのですが、 3.0はUWPでビルドしようとするとエラーで蹴られます。(Unity Editorでは動く) f:id:m2wasabi:20170718085727j:plain

そこで、以下のファイルを書き換えます。 \Assets\FrostweepGames\GCSpeechRecognition\Scripts\Core\Managers\ThreadManager.cs

gist.github.com

2. 名前空間の変更

また、名前空間やクラス構成がごっそりと変わっているので、対応します。

using FrostweepGames.SpeechRecognition.Utilites;
using FrostweepGames.SpeechRecognition.Google.Cloud;

using FrostweepGames.Plugins.GoogleCloud.SpeechRecognition;
3. メソッドの置き換え

使い方もがらっと変わっているので、これも更新します。

3.1. クラスの変更
private ILowLevelSpeechRecognition _speechRecognition;

private GCSpeechRecognition _speechRecognition;
3.2. 初期化処理

初期化処理も大きく変更が必要です。

_speechRecognition = SpeechRecognitionModule.Instance;
_speechRecognition.SpeechRecognizedSuccessEvent += SpeechRecognizedSuccessEventHandler;
_speechRecognition.SpeechRecognizedFailedEvent += SpeechRecognizedFailedEventHandler;

_speechRecognition = GCSpeechRecognition.Instance;
_speechRecognition.SetLanguage(Enumerators.LanguageCode.JA);
_speechRecognition.RecognitionSuccessEvent += SpeechRecognizedSuccessEventHandler;
_speechRecognition.RecognitionFailedEvent += SpeechRecognizedFailedEventHandler;
3.3. イベントハンドラ

イベントハンドラも引数が増えたので対応します。

失敗時

private void SpeechRecognizedFailedEventHandler(string obj)

private void SpeechRecognizedFailedEventHandler(string obj, long l)

成功時

private void SpeechRecognizedSuccessEventHandler(RecognitionResponse obj)

private void SpeechRecognizedSuccessEventHandler(RecognitionResponse obj, long l)
3.4. Start/Stop

録音開始メソッドは、音声の個人認識をするオプションがつきました。

開始

_speechRecognition.StartRecord();

bool VoiceDetection = false;
_speechRecognition.StartRecord(VoiceDetection);

終了

_speechRecognition.StopRuntimeRecord();

_speechRecognition.StopRecord();
変更のサンプル

https://github.com/m2wasabi/SpeechGoUnity/commit/6d1824a659f05baa8d7e136f65419b9fc096f7d5

以上の変更で無事動くようになります。

おわりに

今回、Unity2017への更新で、C#6対応などビルド環境が一変したので心配しましたが、 思ったより不具合もなく移行できました。

アップデート対応は手間だけどやっておいて損はありません。