如何使用Google语音从长音频获取文本到文本node.js

时间:2019-09-12 15:49:36

标签: node.js google-cloud-storage google-speech-api

我遵循官方文档Google Cloud Speech-to-Text API的准则,但我一直收到错误消息:

  

starting-account-.iam.gserviceaccount.com没有   拥有storage.objects.get访问权限。

  const client = new speech.SpeechClient();
  const gcsUri = bucket uri;
  const encoding = 'LINEAR16';
  const sampleRateHertz = 16000;
  const languageCode = 'en-US';
  const config = {
    encoding: encoding,
    sampleRateHertz: sampleRateHertz,
    languageCode: languageCode
  };
  const audio = {
    uri: gcsUri
  };

  const request = {
    config: config,
    audio: audio
  };

  const [operation] = await client.longRunningRecognize(request);
  // Get a Promise representation of the final result of the job
  console.log('operation', operation);
  const [response] = await operation.promise();
  const transcription = response.results
    .map(result => result.alternatives[0].transcript)
    .join('\n');
  console.log(`Transcription: ${transcription}`);

我应该怎么做才能获得访问权限?

2 个答案:

答案 0 :(得分:0)

由于您收到错误消息,因此您的服务帐户没有storage.objects.get permission

可以通过为您的服务帐户授予相关的角色 (例如roles/storage.objectViewer来解决此问题。正如Google Cloud Platform documentation所述,您可以通过以下方式授予它:

  
      
  1. 在GCP控制台中打开IAM & Admin页面。
  2.   
  3. 选择您的项目,然后单击继续
  4.   
  5. 标识要向其中添加角色的服务帐户
  6.   
  7. 如果该服务帐户尚未在成员列表中,则不会   分配有任何角色。点击添加,然后输入电子邮件地址   服务帐户。
  8.   
  9. 如果服务帐户已在成员列表中,则该帐户具有现有角色。点击角色下的下拉列表,   您要编辑的服务帐户。
  10.   
  11. 选择一个或多个角色以应用到服务帐户。
  12.   
  13. 点击添加保存,以将角色应用于服务帐户。
  14.   

或者,您可以使用具有相关权限的另一个服务帐户进行身份验证。

答案 1 :(得分:0)

对于Google Cloud语音转文本:

文档建议以16000Hz的.wav文件

16bit 16000Hz mono .wav为我工作。

我拍摄了原始录音(或录像中的片段),并通过了一些均衡器和语音限制效果(使用免费的Audacty或Adobe Audition),然后按save as格式进行了上传,然后将其上传到gc存储,然后在其上运行ml speech

您还可以使用alpha的{​​{1}}版本……还有更多的标志可以帮助AI进行转录。我的命令如下所示:

ml speech

…您看到我将输出写入gcloud alpha ml speech recognize-long-running gs://my-bucket/my-audio-16bit-16k.wav --language-code=en-US --enable-automatic-punctuation --sample-rate=16000 --max-alternatives=1 --interaction-type=dictation --original-media-type=video --recording-device-type=pc --naics-code=541613 > my-transcript.txt

以下是Alpha上的文档:https://cloud.google.com/sdk/gcloud/reference/alpha/ml/speech/recognize-long-running