複数のチャンネルを含む音声の文字変換

このページでは、Speech-to-Text を使用して複数のチャンネルを含む音声ファイルを文字変換する方法を説明します。マルチチャンネル認識は、最大 8 チャンネルの Speech-to-Text でサポートされているすべての音声エンコードで使用できます。

AutoDetectDecodingConfig を使用している場合は、ファイルに含まれる音声チャンネルの数を指定する必要はありません。自動的に決定されます。音声チャンネル数は、ExplicitDecodingConfig を使用する場合に指定する必要があります。

通常、音声データには録音中に存在している話者ごとに 1 つのチャンネルが含まれます。たとえば、2 人が電話で会話している音声では、回線ごとに別々に録音された 2 つのチャンネルが含まれます。

複数のチャネルを使用してリクエストを送信すると、Speech-to-Text は、音声内の異なるチャネルを識別する結果を、channel_tag フィールドで各結果の代替をラベル付けして返します。

始める前に

  1. 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.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. Enable the Speech-to-Text APIs.

    Enable the APIs

  5. Make sure that you have the following role or roles on the project: Cloud Speech Administrator

    Check for the roles

    1. In the Google Cloud console, go to the IAM page.

      Go to IAM
    2. Select the project.
    3. 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.

    4. 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

    1. In the Google Cloud console, go to the IAM page.

      IAM に移動
    2. プロジェクトを選択します。
    3. [ アクセスを許可] をクリックします。
    4. [新しいプリンシパル] フィールドに、ユーザー ID を入力します。 これは通常、Google アカウントのメールアドレスです。

    5. [ロールを選択] リストでロールを選択します。
    6. 追加のロールを付与するには、 [別のロールを追加] をクリックして各ロールを追加します。
    7. [保存] をクリックします。
  6. Install the Google Cloud CLI.

  7. 外部 ID プロバイダ(IdP)を使用している場合は、まずフェデレーション ID を使用して gcloud CLI にログインする必要があります。

  8. gcloud CLI を初期化するには、次のコマンドを実行します。

    gcloud init
  9. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  10. Verify that billing is enabled for your Google Cloud project.

  11. Enable the Speech-to-Text APIs.

    Enable the APIs

  12. Make sure that you have the following role or roles on the project: Cloud Speech Administrator

    Check for the roles

    1. In the Google Cloud console, go to the IAM page.

      Go to IAM
    2. Select the project.
    3. 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.

    4. 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

    1. In the Google Cloud console, go to the IAM page.

      IAM に移動
    2. プロジェクトを選択します。
    3. [ アクセスを許可] をクリックします。
    4. [新しいプリンシパル] フィールドに、ユーザー ID を入力します。 これは通常、Google アカウントのメールアドレスです。

    5. [ロールを選択] リストでロールを選択します。
    6. 追加のロールを付与するには、 [別のロールを追加] をクリックして各ロールを追加します。
    7. [保存] をクリックします。
  13. Install the Google Cloud CLI.

  14. 外部 ID プロバイダ(IdP)を使用している場合は、まずフェデレーション ID を使用して gcloud CLI にログインする必要があります。

  15. gcloud CLI を初期化するには、次のコマンドを実行します。

    gcloud init
  16. クライアント ライブラリは、アプリケーションのデフォルト認証情報を使用することによって Google API で簡単に認証を行い、これらの API にリクエストを送信できます。アプリケーションのデフォルト認証情報を使用すると、ベースとなるコードを変更することなく、ローカルでアプリケーションのテストを行ったり、アプリケーションをデプロイしたりできます。詳細については、クライアント ライブラリを使用するための認証をご覧ください。

  17. 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.

  18. また、クライアント ライブラリがインストールされていることを確認してください。

    マルチチャンネル ファイルで同期音声認識を実行する

    ローカル マルチチャンネル音声ファイルに対して、同期音声認識を行う例を次に示します。

    Python

    import os
    
    from google.cloud.speech_v2 import SpeechClient
    from google.cloud.speech_v2.types import cloud_speech
    
    PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"]
    
    
    def transcribe_multichannel_v2(
        audio_file: str,
    ) -> cloud_speech.RecognizeResponse:
        """Transcribe the given audio file synchronously with multichannel.
        Args:
            audio_file (str): Path to the local audio file to be transcribed.
                Example: "resources/two_channel_16k.wav"
        Returns:
            cloud_speech.RecognizeResponse: The full response object which includes the transcription results.
        """
        # Instantiates a client
        client = SpeechClient()
    
        # Reads a file as bytes
        with open(audio_file, "rb") as f:
            audio_content = f.read()
    
        config = cloud_speech.RecognitionConfig(
            auto_decoding_config=cloud_speech.AutoDetectDecodingConfig(),
            language_codes=["en-US"],
            model="long",
            features=cloud_speech.RecognitionFeatures(
                multi_channel_mode=cloud_speech.RecognitionFeatures.MultiChannelMode.SEPARATE_RECOGNITION_PER_CHANNEL,
            ),
        )
    
        request = cloud_speech.RecognizeRequest(
            recognizer=f"projects/{PROJECT_ID}/locations/global/recognizers/_",
            config=config,
            content=audio_content,
        )
    
        # Transcribes the audio into text
        response = client.recognize(request=request)
    
        for result in response.results:
            print(f"Transcript: {result.alternatives[0].transcript}")
            print(f"Channel tag: {result.channel_tag}")
    
        return response
    
    

    クリーンアップする

    このページで使用したリソースについて、 Google Cloud アカウントに課金されないようにするには、次の操作を行います。

    1. Optional: Revoke the authentication credentials that you created, and delete the local credential file.

      gcloud auth application-default revoke
    2. Optional: Revoke credentials from the gcloud CLI.

      gcloud auth revoke

    コンソール

  19. In the Google Cloud console, go to the Manage resources page.

    Go to Manage resources

  20. In the project list, select the project that you want to delete, and then click Delete.
  21. In the dialog, type the project ID, and then click Shut down to delete the project.
  22. gcloud

  23. In the Google Cloud console, go to the Manage resources page.

    Go to Manage resources

  24. In the project list, select the project that you want to delete, and then click Delete.
  25. In the dialog, type the project ID, and then click Shut down to delete the project.
  26. 次のステップ