Mentranskripsikan audio dengan beberapa saluran

Halaman ini menjelaskan cara menggunakan Cloud Speech-to-Text untuk mentranskripsikan file audio yang mencakup lebih dari satu saluran. Pengenalan multi-saluran tersedia untuk semua encoding audio yang didukung oleh Cloud STT hingga delapan saluran.

Jika menggunakan AutoDetectDecodingConfig, Anda tidak perlu menentukan jumlah saluran audio yang dimiliki file. Hal ini ditentukan secara otomatis. Anda harus menentukan jumlah saluran audio saat menggunakan ExplicitDecodingConfig.

Data audio biasanya mencakup saluran untuk setiap pembicara yang ada pada rekaman. Misalnya, audio dua orang yang berbicara melalui telepon mungkin berisi dua saluran, dan setiap saluran direkam secara terpisah.

Saat Anda mengirim permintaan dengan beberapa saluran, Cloud STT akan menampilkan hasil yang mengidentifikasi berbagai saluran yang ada dalam audio, sehingga memberi label alternatif untuk setiap hasil dengan kolom channel_tag.

Sebelum memulai

  1. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

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

  3. Enable the Speech-to-Text APIs.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the APIs

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

      Buka IAM
    2. Pilih project.
    3. Klik Grant access.
    4. Di kolom New principals, masukkan ID pengguna Anda. Biasanya, ini adalah ID untuk pengguna dalam workforce identity pool. Untuk mengetahui detailnya, lihat Merepresentasikan pengguna workforce pool dalam kebijakan IAM, atau hubungi administrator Anda.

    5. Di daftar Select a role, pilih peran.
    6. Untuk memberikan peran tambahan, klik Add another role, lalu tambahkan setiap peran tambahan.
    7. Klik Save.
  5. Install the Google Cloud CLI.

  6. Konfigurasi gcloud CLI agar menggunakan identitas gabungan Anda.

    Untuk mengetahui informasi selengkapnya, lihat Login ke gcloud CLI dengan identitas gabungan Anda.

  7. Untuk melakukan inisialisasi gcloud CLI, jalankan perintah berikut:

    gcloud init

    Library klien dapat menggunakan Kredensial Default Aplikasi untuk dengan mudah melakukan autentikasi dengan Google API dan mengirim permintaan ke API tersebut. Dengan Kredensial Default Aplikasi, Anda dapat menguji aplikasi secara lokal dan men-deploy aplikasi tanpa mengubah kode yang mendasarinya. Untuk mengetahui informasi selengkapnya, lihat Melakukan autentikasi untuk menggunakan library klien.

  8. Create local authentication credentials for your user account:

    gcloud auth application-default login

    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.

  9. Selain itu, pastikan Anda telah menginstal library klien.

    Melakukan pengenalan ucapan sinkron pada file multi-saluran

    Berikut adalah contoh cara melakukan pengenalan ucapan sinkron pada file audio multi-saluran lokal:

    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
    
    

    Pembersihan

    Agar akun Google Cloud Anda tidak dikenai biaya untuk resource yang digunakan di halaman ini, ikuti langkah-langkah berikut.

    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

    Konsol

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

    Go to Manage resources

  11. In the project list, select the project that you want to delete, and then click Delete.
  12. In the dialog, type the project ID, and then click Shut down to delete the project.
  13. gcloud

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

    Go to Manage resources

  15. In the project list, select the project that you want to delete, and then click Delete.
  16. In the dialog, type the project ID, and then click Shut down to delete the project.
  17. Langkah berikutnya