Transcreva voz em texto usando a linha de comando

Confira nesta página como enviar uma solicitação de reconhecimento de fala para a Speech-to-Text usando a interface REST e o comando curl.

A Speech-to-Text permite a fácil integração das tecnologias de reconhecimento de fala do Google nos aplicativos do desenvolvedor. Você pode enviar dados de áudio para a API Speech-to-Text, que em seguida retorna uma transcrição de texto desse arquivo de áudio. Para mais informações sobre o serviço, consulte Princípios básicos da Speech-to-Text.

Antes de começar

  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. Verifique se a cobrança está ativada para o seu projeto do Google Cloud.

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

      Acessar o IAM
    2. Selecionar um projeto.
    3. Clique em CONCEDER ACESSO.
    4. No campo Novos principais, insira seu identificador de usuário. Normalmente, é o endereço de e-mail de uma Conta do Google.

    5. Na lista Selecionar um papel, escolha um.
    6. Para conceder outros papéis, clique em Adicionar outro papel e adicione cada papel adicional.
    7. Clique em Salvar.
    8. Install the Google Cloud CLI.
    9. To initialize the gcloud CLI, run the following command:

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

      Go to project selector

    11. Verifique se a cobrança está ativada para o seu projeto do Google Cloud.

    12. Enable the Speech-to-Text APIs.

      Enable the APIs

    13. 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 colunn 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.

        Acessar o IAM
      2. Selecionar um projeto.
      3. Clique em CONCEDER ACESSO.
      4. No campo Novos principais, insira seu identificador de usuário. Normalmente, é o endereço de e-mail de uma Conta do Google.

      5. Na lista Selecionar um papel, escolha um.
      6. Para conceder outros papéis, clique em Adicionar outro papel e adicione cada papel adicional.
      7. Clique em Salvar.
      8. Install the Google Cloud CLI.
      9. To initialize the gcloud CLI, run the following command:

        gcloud init

      Fazer uma solicitação de transcrição de áudio

      Use o exemplo de código a seguir para enviar uma recognize solicitação REST para a API Speech-to-Text.

      1. Execute este comando para criar um arquivo JSON como entrada para a solicitação. Substitua /full/path/to/audio/file.wav pelo caminho para o arquivo de áudio que você quer transcrever:

        echo "{
          \"config\": {
            \"auto_decoding_config\": {},
            \"language_codes\": [\"en-US\"],
            \"model\": \"long\"
          },
          \"content\": \"$(base64 -w 0 /full/path/to/audio/file.wav | sed 's/+/-/g; s/\//_/g')\"
        }" > /tmp/data.txt
      2. Use curl para fazer uma solicitação recognize:

        curl -X POST -H "Content-Type: application/json; charset=utf-8" \
            -H "Authorization: Bearer $(gcloud auth print-access-token)" \
            -d @/tmp/data.txt \
            https://speech.googleapis.com/v2/projects/PROJECT_ID/locations/global/recognizers/_:recognize

        Uma resposta semelhante a esta será exibida:

          {
            "results": [
              {
                "alternatives": [
                  {
                    "transcript": "how old is the Brooklyn Bridge",
                    "confidence": 0.98267895
                  }
                ]
              }
            ]
          }
        

      Você enviou sua primeira solicitação para a Speech-to-Text.

      Limpeza

      Para evitar cobranças na conta do Google Cloud pelos recursos usados nesta página, siga estas etapas.

      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

      Console

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

      Delete a Google Cloud project:

      gcloud projects delete PROJECT_ID

      A seguir