このページでは、Speech-to-Text API と非同期音声認識を使用して、長い音声ファイル(1 分を超える)をテキストに変換する方法について説明します。
非同期音声認識について
一括音声認識では、音声処理オペレーションの長時間実行が開始されます。60 秒を超える音声を文字に変換するには、非同期音声認識を使用します。短い音声の場合は、同期音声認識を使用したほうが早くて簡単です。非同期音声認識の上限は 480 分(8 時間)です。
一括音声認識では、Cloud Storage に保存されている音声のみ文字変換できます。音声文字変換の出力は、レスポンスにインラインで送信することも(単一ファイルの一括認識リクエストの場合)、Cloud Storage に書き込むこともできます。
一括認識リクエストは、リクエストの継続的な認識処理に関する情報を含む Operation
を返します。オペレーションをポーリングすると、オペレーションの完了と音声文字変換テキストのタイミングを確認できます。
始める前に
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Speech-to-Text APIs.
-
Make sure that you have the following role or roles on the project: Cloud Speech Administrator
Check for the roles
-
In the Google Cloud console, go to the IAM page.
Go to IAM - Select the project.
-
In the Principal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator.
- For all rows that specify or include you, check the Role column to see whether the list of roles includes the required roles.
Grant the roles
-
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Speech-to-Text APIs.
-
Make sure that you have the following role or roles on the project: Cloud Speech Administrator
Check for the roles
-
In the Google Cloud console, go to the IAM page.
Go to IAM - Select the project.
-
In the Principal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator.
- For all rows that specify or include you, check the Role column to see whether the list of roles includes the required roles.
Grant the roles
-
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
クライアント ライブラリは、アプリケーションのデフォルト認証情報を使用することによって、Google API で簡単に認証を行い、これらの API にリクエストを送信できます。アプリケーションのデフォルト認証情報を使用すると、ベースとなるコードを変更することなく、ローカルでアプリケーションのテストを行ったり、アプリケーションをデプロイしたりできます。詳細については、クライアント ライブラリを使用するための認証をご覧ください。
また、クライアント ライブラリがインストールされていることを確認してください。
Cloud Storage へのアクセスを有効にする
Speech-to-Text は、サービス アカウントを使用して Cloud Storage 内のファイルにアクセスします。デフォルトでは、サービス アカウントは同じプロジェクト内の Cloud Storage ファイルにアクセスできます。
サービス アカウントのメールアドレスは次のとおりです。
service-PROJECT_NUMBER@gcp-sa-speech.iam.gserviceaccount.com
別のプロジェクトの Cloud Storage ファイルを音声文字変換するには、このサービス アカウントにもう一方のプロジェクトの Speech-to-Text サービス エージェント ロールを付与します。
gcloud projects add-iam-policy-binding PROJECT_ID \
--member=serviceAccount:service-PROJECT_NUMBER@gcp-sa-speech.iam.gserviceaccount.com \
--role=roles/speech.serviceAgent
プロジェクトの IAM ポリシーの詳細については、プロジェクト、フォルダ、組織へのアクセス権の管理をご覧ください。
サービス アカウントにさらにきめ細かくアクセス権を付与するには、特定の Cloud Storage バケットへの権限を付与します。
gcloud storage buckets add-iam-policy-binding gs://BUCKET_NAME \
--member=serviceAccount:service-PROJECT_NUMBER@gcp-sa-speech.iam.gserviceaccount.com \
--role=roles/storage.admin
Cloud Storage へのアクセスの管理の詳細については、Cloud Storage ドキュメントのアクセス制御リストの作成と管理をご覧ください。
一括認識を実行してインラインで結果を読み取る
Cloud Storage の音声ファイルに対して一括音声認識を実行し、レスポンスから音声文字変換結果をインラインで読み取る例を次に示します。
Python
一括認識を実行して結果を Cloud Storage に書き込む
次に、Cloud Storage の音声ファイルに対して一括音声認識を実行し、Cloud Storage の出力ファイルから音声文字変換結果を読み取る例を示します。Cloud Storage に書き込まれるファイルは JSON 形式の BatchRecognizeResults
メッセージです。
Python
複数のファイルに対して一括認識を実行する
次に、Cloud Storage の複数の音声ファイルに対して一括音声認識を実行し、Cloud Storage の出力ファイルから音声文字変換結果を読み取る例を示します。
Python
一括認識で動的一括処理を有効にする
動的一括処理を使用すると、より高いレイテンシに対して低コストで音声文字変換を行うことができます。この機能はバッチ認識でのみ使用できます。
次に、動的一括処理を有効にして Cloud Storage の音声ファイルに対して一括認識を実行する例を示します。
Python
ファイルごとに認識機能をオーバーライドする
一括認識のデフォルトでは、バッチ認識リクエストのファイルごとに同じ認識構成が使用されます。さまざまなファイルで異なる構成や機能が必要な場合は、[BatchRecognizeFileMetadata
][batch-file-metadata-grpc] メッセージの config
フィールドを使用して、ファイルごとに構成をオーバーライドできます。認識機能をオーバーライドする例については、認識機能のドキュメントをご覧ください。
クリーンアップ
このページで使用したリソースについて、 Google Cloud アカウントに課金されないようにするには、次の操作を行います。
-
Optional: Revoke the authentication credentials that you created, and delete the local credential file.
gcloud auth application-default revoke
-
Optional: Revoke credentials from the gcloud CLI.
gcloud auth revoke
コンソール
gcloud
Delete a Google Cloud project:
gcloud projects delete PROJECT_ID
次のステップ
- 一括認識については、リファレンス ドキュメントをご覧ください。
- ストリーミング音声を文字に変換する方法を学習する。
- 短い音声ファイルの文字変換を行う。
- Chirp を使用して、音声ファイルの音声文字変換を行う。
- ベスト プラクティスのドキュメントで、最高のパフォーマンスと精度を実現するための方法やヒントを確認する。