Skip to content

Instantly share code, notes, and snippets.

@retorillo
Created January 26, 2021 05:08
Show Gist options
  • Save retorillo/a2b553b952f74146b0aa4dab961fd339 to your computer and use it in GitHub Desktop.
Save retorillo/a2b553b952f74146b0aa4dab961fd339 to your computer and use it in GitHub Desktop.

Google Speech-to-Text の QuickStart サンプルコード を実行する

https://github.com/GoogleCloudPlatform/dotnet-docs-samples の「speech/api/QuickStart.cs」を実行するステップ

Visual Studio で必要なコンポーネント

現時点(2021年01月26日)では少なくとも以下のコンポーネントが必要。VS2019 Communityで検証。

  • .NET SDK(dotnetコマンドに必要、コンポーネントインストール後ログインしなおさないとコマンドは使えない)
  • .NET Core 2.1 ランタイム (LTS)

.NET Core 2.1をターゲットに作られているようで他のバージョンだと以下のようなエラーが出る

> dotnet run
It was not possible to find any compatible framework version
The framework 'Microsoft.NETCore.App', version '2.1.0' was not found.
  - The following frameworks were found:
      3.1.11 at [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
      5.0.2 at [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]

You can resolve the problem by installing the specified framework and/or SDK.

The specified framework can be found at:
  - https://aka.ms/dotnet-core-applaunch?framework=Microsoft.NETCore.App&framework_version=2.1.0&arch=x64&rid=win10-x64

サービス アカウントとして認証してJSONをダウンロードし環境変数GOOGLE_APPLICATION_CREDENTIALSを設定する

https://cloud.google.com/storage/docs/reference/libraries

設定していない状態で実行すると次のようなエラーが出る

> dotnet run

Unhandled Exception: System.InvalidOperationException: The Application Default Credentials are not available. They are available if running in Google Compute Engine. Otherwise, the environment variable GOOGLE_APPLICATION_CREDENTIALS must be defined pointing to a file defining the credentials. See https://developers.google.com/accounts/docs/application-default-credentials for more information.
   at Google.Apis.Auth.OAuth2.DefaultCredentialProvider.CreateDefaultCredentialAsync() in C:\Apiary\2020-03-06.14-34-34\Src\Support\Google.Apis.Auth\OAuth2\DefaultCredentialProvider.cs:line 142
   at Google.Api.Gax.Grpc.ChannelPool.CreateChannelCredentialsUncached() in T:\src\github\gax-dotnet\releasebuild\Google.Api.Gax.Grpc\ChannelPool.cs:line 59
   at Google.Api.Gax.TaskExtensions.WaitWithUnwrappedExceptions(Task task) in T:\src\github\gax-dotnet\releasebuild\Google.Api.Gax\TaskExtensions.cs:line 51
   at Google.Api.Gax.Grpc.ChannelPool.GetChannel(GrpcAdapter grpcAdapter, String endpoint, GrpcChannelOptions channelOptions) in T:\src\github\gax-dotnet\releasebuild\Google.Api.Gax.Grpc\ChannelPool.cs:line 95
   at Google.Api.Gax.Grpc.ClientBuilderBase`1.CreateCallInvoker() in T:\src\github\gax-dotnet\releasebuild\Google.Api.Gax.Grpc\ClientBuilderBase.cs:line 280
   at Google.Cloud.Speech.V1.SpeechClientBuilder.BuildImpl() in T:\src\github\google-cloud-dotnet\releasebuild\apis\Google.Cloud.Speech.V1\Google.Cloud.Speech.V1\SpeechClient.g.cs:line 151
   at GoogleCloudSamples.QuickStart.Main(String[] args) in C:\Users\retorillo\Desktop\dotnet-docs-samples\speech\api\QuickStart\QuickStart.cs:line 31

設定するにはまずGoogle Cloudの認証ライブラリをインストールする

> dotnet add package Google.Cloud.Storage.V1

Cloud Console で、サービス アカウント キーの作成 ページに移動し、新しいサービス アカウント、ロールを「オーナー」に設定しJSONをダウンロードする

その後、そのJSONファイルに対するファイルパス(フォルダ名ではなく拡張子.jsonを含む)を、環境変数GOOGLE_APPLICATION_CREDENTIALSに設定する

「Cloud Speech-to-Text API」を有効にする

PermissionDeniedと出るので、Speech-to-Text APIを有効にする必要がある。以下のURLから「ENABLE」ボタンをクリックする。ここでクレジットカードかPaypalなどの支払い方法の入力が必要になる。

https://console.developers.google.com/apis/api/speech.googleapis.com/overview

料金が気になる場合は以下のURLから確認できる。 データーロギングあり(学習用に提供)なら15秒あたり、$0.004、1分あたり$0.016、1時間で$0.96(およそ100円)。なお最初の1時間(月ごとにリセット)は無料

https://cloud.google.com/speech-to-text/pricing?hl=ja

> dotnet run

Unhandled Exception: Grpc.Core.RpcException: Status(StatusCode=PermissionDenied, Detail="Cloud Speech-to-Text API has not been used in project (PROJECTID) before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/speech.googleapis.com/overview?project=(PROJECTID) then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.")
   at Grpc.Core.Internal.AsyncCall`2.UnaryCall(TRequest msg) in T:\src\github\grpc\src\csharp\Grpc.Core\Internal\AsyncCall.cs:line 78
   at Grpc.Core.DefaultCallInvoker.BlockingUnaryCall[TRequest,TResponse](Method`2 method, String host, CallOptions options, TRequest request) in T:\src\github\grpc\src\csharp\Grpc.Core\DefaultCallInvoker.cs:line 46
   at Grpc.Core.Interceptors.InterceptingCallInvoker.<BlockingUnaryCall>b__3_0[TRequest,TResponse](TRequest req, ClientInterceptorContext`2 ctx) in T:\src\github\grpc\src\csharp\Grpc.Core.Api\Interceptors\InterceptingCallInvoker.cs:line 51
   at Grpc.Core.ClientBase.ClientBaseConfiguration.ClientBaseConfigurationInterceptor.BlockingUnaryCall[TRequest,TResponse](TRequest request, ClientInterceptorContext`2 context, BlockingUnaryCallContinuation`2 continuation) in T:\src\github\grpc\src\csharp\Grpc.Core.Api\ClientBase.cs:line 174
   at Grpc.Core.Interceptors.InterceptingCallInvoker.BlockingUnaryCall[TRequest,TResponse](Method`2 method, String host, CallOptions options, TRequest request) in T:\src\github\grpc\src\csharp\Grpc.Core.Api\Interceptors\InterceptingCallInvoker.cs:line 48
   at Google.Cloud.Speech.V1.Speech.SpeechClient.Recognize(RecognizeRequest request, CallOptions options) in T:\src\github\google-cloud-dotnet\releasebuild\apis\Google.Cloud.Speech.V1\Google.Cloud.Speech.V1\CloudSpeechGrpc.cs:line 160
   at Google.Api.Gax.Grpc.ApiCall.GrpcCallAdapter`2.CallSync(TRequest request, CallSettings callSettings) in T:\src\github\gax-dotnet\releasebuild\Google.Api.Gax.Grpc\ApiCall.cs:line 92
   at Google.Api.Gax.Grpc.ApiCallRetryExtensions.<>c__DisplayClass1_0`2.<WithRetry>b__0(TRequest request, CallSettings callSettings) in T:\src\github\gax-dotnet\releasebuild\Google.Api.Gax.Grpc\ApiCallRetryExtensions.cs:line 70
   at GoogleCloudSamples.QuickStart.Main(String[] args) in C:\Users\retorillo\Desktop\dotnet-docs-samples\speech\api\QuickStart\QuickStart.cs:line 32

動作確認

以上の設定で問題なく動作する。このサンプルコードの場合は既存の音声データー(gs://cloud-samples-data/speech/brooklyn_bridge.raw)を用いるので基本的に「how old is the Brooklyn Bridge」が出力結果となるべき

> dotnet run
how old is the Brooklyn Bridge
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment