本教程介绍如何使用 Cloud Speech-to-Text 转写从电话录制的音频。
音频文件可能来自许多不同的来源。音频数据可能来自电话(如语音邮件)或视频文件所包含的音轨。
Cloud STT 可以从多种机器学习模型中的一种来转写您的音频文件,以最佳匹配音频的原始来源。为了获得更好的语音转写结果,您可以指定原始音频的来源。这样,Cloud STT 就可以在处理您的音频文件时使用针对类似数据训练过的机器学习模型。
目标
- 向 Cloud STT 发送音频转写请求,以转写从电话录制的音频(如语音邮件)。
- 为音频转写请求指定增强型语音识别模型。
费用
本教程使用 Cloud Platform 的可计费组件,包括:
- Cloud Speech-to-Text
请使用价格计算器根据您的预计使用情况来估算费用。
准备工作
本教程有几个前提条件:
- 您已经在 Google Cloud 控制台中设置了 Cloud Speech-to-Text 项目。
- 您已经在 Google Cloud 控制台中使用应用默认凭证设置了环境。
- 您已经针对所选编程语言设置开发环境。
- 您已经针对所选编程语言安装 Google Cloud 客户端库。
发送请求
为了在转写电话上录制的音频(如通话或语音邮件)时获得最佳效果,您可以将 RecognitionConfig 载荷的 model 字段设置为 phone_call。model 字段会告知 Cloud Speech-to-Text API 为转写请求使用哪种语音识别模型。
使用增强型模型可以改善电话音频转写的效果。如需使用增强型模型,您可以将 RecognitionConfig 载荷的 useEnhanced 字段设置为 true。
以下代码示例演示了如何在调用 Cloud STT 时选择特定的转写模型。
协议
如需了解完整的详细信息,请参阅 speech:recognize API 端点。
如需执行同步语音识别,请发出 POST 请求并提供相应的请求正文。以下示例展示了一个使用 curl 发出的 POST 请求。该示例使用 Google Cloud CLI 生成访问令牌。如需了解如何安装 gcloud CLI,请参阅快速入门。
curl -s -H "Content-Type: application/json" \ -H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \ https://speech.googleapis.com/v1/speech:recognize \ --data '{ "config": { "encoding": "LINEAR16", "languageCode": "en-US", "enableWordTimeOffsets": false, "enableAutomaticPunctuation": true, "model": "phone_call", "useEnhanced": true }, "audio": { "uri": "gs://cloud-samples-tests/speech/commercial_mono.wav" } }'
如需详细了解如何配置请求正文,请参阅 RecognitionConfig 参考文档。
如果请求成功,服务器将返回一个 200 OK HTTP 状态代码以及 JSON 格式的响应:
{ "results": [ { "alternatives": [ { "transcript": "Hi, I'd like to buy a Chromecast. I was wondering whether you could help me with that.", "confidence": 0.8930228 } ], "resultEndTime": "5.640s" }, { "alternatives": [ { "transcript": " Certainly, which color would you like? We are blue black and red.", "confidence": 0.9101991 } ], "resultEndTime": "10.220s" }, { "alternatives": [ { "transcript": " Let's go with the black one.", "confidence": 0.8818244 } ], "resultEndTime": "13.870s" }, { "alternatives": [ { "transcript": " Would you like the new Chromecast Ultra model or the regular Chromecast?", "confidence": 0.94733626 } ], "resultEndTime": "18.460s" }, { "alternatives": [ { "transcript": " Regular Chromecast is fine. Thank you. Okay. Sure. Would you like to ship it regular or Express?", "confidence": 0.9519095 } ], "resultEndTime": "25.930s" }, { "alternatives": [ { "transcript": " Express, please.", "confidence": 0.9101229 } ], "resultEndTime": "28.260s" }, { "alternatives": [ { "transcript": " Terrific. It's on the way. Thank you. Thank you very much. Bye.", "confidence": 0.9321616 } ], "resultEndTime": "34.150s" } ] }
Go
如需了解如何安装和使用 Cloud STT 客户端库,请参阅 Cloud STT 客户端库。如需了解详情,请参阅 Cloud STT Go API 参考文档。
如需向 Cloud STT 进行身份验证,请设置应用默认凭证。如需了解详情,请参阅为本地开发环境设置身份验证。
Java
如需了解如何安装和使用 Cloud STT 客户端库,请参阅 Cloud STT 客户端库。如需了解详情,请参阅 Cloud STT Java API 参考文档。
如需向 Cloud STT 进行身份验证,请设置应用默认凭证。如需了解详情,请参阅为本地开发环境设置身份验证。
Node.js
如需了解如何安装和使用 Cloud STT 客户端库,请参阅 Cloud STT 客户端库。如需了解详情,请参阅 Cloud STT Node.js API 参考文档。
如需向 Cloud STT 进行身份验证,请设置应用默认凭证。如需了解详情,请参阅为本地开发环境设置身份验证。
Python
如需了解如何安装和使用 Cloud STT 客户端库,请参阅 Cloud STT 客户端库。如需了解详情,请参阅 Cloud STT Python API 参考文档。
如需向 Cloud STT 进行身份验证,请设置应用默认凭证。如需了解详情,请参阅为本地开发环境设置身份验证。
其他语言
C#: 请按照客户端库页面上的 C# 设置说明操作,然后访问 .NET 版 Cloud STT 参考文档。
PHP: 请按照客户端库页面上的 PHP 设置说明操作,然后访问 PHP 版 Cloud STT 参考文档。
Ruby:请按照客户端库页面上的 Ruby 设置说明操作,然后访问 Ruby 版 Cloud STT 参考文档。
清理
为避免因本教程中使用的资源导致您的 Google Cloud 账号产生费用,请删除包含这些资源的项目,或者保留项目但删除各个资源。
删除项目
为了避免产生费用,最简单的方法是删除您为本教程创建的项目。
要删除项目,请执行以下操作:
- In the Google Cloud console, go to the Manage resources page.
- In the project list, select the project that you want to delete, and then click Delete.
- In the dialog, type the project ID, and then click Shut down to delete the project.
删除实例
要删除 Compute Engine 实例,请运行以下命令:
- In the Google Cloud console, go to the VM instances page.
- Select the checkbox for the instance that you want to delete.
- To delete the instance, click More actions, click Delete, and then follow the instructions.
删除默认网络的防火墙规则
如需删除防火墙规则,请执行以下操作:
- In the Google Cloud console, go to the Firewall page.
- Select the checkbox for the firewall rule that you want to delete.
- To delete the firewall rule, click Delete.