명령줄을 사용하여 음성을 텍스트로 변환하기

이 페이지에서는 REST 인터페이스curl 명령어를 사용하여 Speech-to-Text에 음성 인식 요청을 보내는 방법을 설명합니다.

Speech-to-Text를 사용하면 Google 음성 인식 기술을 개발자 애플리케이션에 간편하게 통합할 수 있습니다. 오디오 데이터를 Speech-to-Text API로 보내면 Speech-to-Text API가 해당 오디오 파일의 텍스트 스크립트를 반환합니다. 서비스에 대한 자세한 내용은 Cloud STT 기본사항을 참고하세요.

시작하기 전에

  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.

      IAM으로 이동
    2. 프로젝트를 선택합니다.
    3. 액세스 권한 부여를 클릭합니다.
    4. 새 주 구성원 필드에 사용자 식별자를 입력합니다. 일반적으로 직원 ID 풀의 사용자 식별자입니다. 자세한 내용은 IAM 정책에서 직원 풀 사용자 표현을 참고하거나 관리자에게 문의하세요.

    5. 역할 선택 목록에서 역할을 선택합니다.
    6. 역할을 추가로 부여하려면 다른 역할 추가를 클릭하고 각 역할을 추가합니다.
    7. 저장을 클릭합니다.
  5. Install the Google Cloud CLI.

  6. gcloud CLI에서 제휴 ID를 사용하도록 구성합니다.

    자세한 내용은 제휴 ID로 gcloud CLI에 로그인을 참고하세요.

  7. gcloud CLI를 초기화하려면, 다음 명령어를 실행합니다.

    gcloud init

    오디오 텍스트 변환 요청하기

    다음 코드 샘플을 사용하여 Speech-to-Text API에 recognize REST 요청을 보냅니다.

    1. 이 명령어를 실행하여 요청에 대한 입력으로 JSON 파일을 만듭니다. /full/path/to/audio/file.wav를 텍스트 변환을 수행할 오디오 파일의 경로로 바꿉니다.

      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. curl을 사용하여 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

      다음과 비슷한 응답이 표시됩니다.

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

    첫 번째 요청을 Speech-to-Text에 보냈습니다.

    삭제

    이 페이지에서 사용한 리소스 비용이 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

    콘솔

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

    Go to Manage resources

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

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

    Go to Manage resources

  13. In the project list, select the project that you want to delete, and then click Delete.
  14. In the dialog, type the project ID, and then click Shut down to delete the project.
  15. 다음 단계