Google Cloud Speech API 调用注意事项及调用方式__.Net版2
来源:互联网 发布:2017大数据概念股龙头 编辑:程序博客网 时间:2024/05/18 03:12
在上一章已详细说明如何读取本地音频文件,调用GoogleCloudSpeech API转换为文字。从中可以看出,对音频文件的播放长度严格地限制在60s以内。对此限制,Google采用将音频文件上传到Cloud Storage的Bucket中,然后进行文字转换,大大地增长了文件的播放长度。针对在Google Cloud PlatForm中的系列操作及上传文件到Cloud Storage Bucket中的方式,请查看GoogleCloudSpeechAPI 的调用注意事项 。
读取Cloud Storage Bucket中的音频文件进行文字转换
注:如下Demo是windows应用程序,所有方法都为static
1.创建类型为CloudSpeechAPIService的方法,目的是通过环境变量获取Google的凭证,连接在云平台建立的项目。PS:如果此方法出现异常,请查看上一章的前提条件二。
static public CloudSpeechAPIService CreateAuthorizedClient() { GoogleCredential credential =GoogleCredential.GetApplicationDefaultAsync().Result;//读取环境变量中的GOOGLE_APPLICATION_CREDENTIALS if (credential.IsCreateScopedRequired) { credential = credential.CreateScoped(new[] { CloudSpeechAPIService.Scope.CloudPlatform });//获取认证 } return new CloudSpeechAPIService(new BaseClientService.Initializer() { HttpClientInitializer = credential, ApplicationName = "DotNet Google Cloud Platform Speech Sample", }); }
2.选择Cloud Storage Bucket中的音频文件,调用该API进行文字转换。ps:音频文件格式最好为1声道PCM,播放时间小于80minutes否则转换容易出错。
private static void SpeechTotextByUri() { var service = CreateAuthorizedClient();//获取Google云认证 var request = new Google.Apis.CloudSpeechAPI.v1beta1.Data.AsyncRecognizeRequest() { Config = new Google.Apis.CloudSpeechAPI.v1beta1.Data.RecognitionConfig() { Encoding = "LINEAR16",//编码格式PCM SampleRate = 16000,//采样频率16000HZ LanguageCode = "en-US"//英文播放文件 // LanguageCode = "cmn-Hans-CN"中文播放文件 }, Audio = new Google.Apis.CloudSpeechAPI.v1beta1.Data.RecognitionAudio() { Uri= "gs://Storage中的Bucket名称/该Bucket中的音频文件"//如Google提供的 Uri= "gs://cloud-samples-tests/speech/brooklyn.flac" } }; DateTime startTime = DateTime.Now; Console.WriteLine("Sarte Time :" + startTime); StringBuilder sb = new StringBuilder(); try { var asyncResponse = service.Speech.Asyncrecognize(request).Execute(); var name = asyncResponse.Name; Google.Apis.CloudSpeechAPI.v1beta1.Data.Operation op; do { Console.WriteLine("Waiting for server processing..."); Thread.Sleep(1000); op = service.Operations.Get(name).Execute(); if (op.Error?.Message != null) { Console.WriteLine(op.Error.Message); } } while (!(op.Done.HasValue && op.Done.Value)); dynamic results = op.Response["results"]; StringBuilder sbContent = new StringBuilder(); foreach (var result in results) { foreach (var alternative in result.alternatives) { sbContent.Append(alternative.transcript); } } sb.Append(sbContent); Console.WriteLine("The result:"); Console.WriteLine(); Console.WriteLine(sbContent.ToString()); Console.WriteLine(); } catch (Exception ex) { Console.WriteLine(ex.Message); } DateTime endTime = DateTime.Now; var timeTaken = endTime - startTime; Console.WriteLine("End Time:" + endTime + "\t" + "Time-taken:" + (timeTaken)); sb.Append("\r\nEnd Time:" + endTime + "\t" + "Time-taken:" + (timeTaken)); try { //将转换的文字内容保存到本地 StreamWriter sw = new StreamWriter(Directory.GetCurrentDirectory() + $"\\{filename}_result.txt"); sw.Write(sb.ToString()); sw.Close(); } catch (Exception ex) { Console.WriteLine("Write file failed!\t" + ex.Message); } Console.WriteLine(); Console.WriteLine("Enter any key to exit the program!"); Console.ReadKey(); }
PS:使用的命名空间有:
using System;using Google.Apis.CloudSpeechAPI.v1beta1;using Google.Apis.Auth.OAuth2;using Google.Apis.Services;using System.IO;using System.Text;using System.Threading;using Google.Cloud.Storage.V1;
结果如下:
0 0
- Google Cloud Speech API 调用注意事项及调用方式__.Net版2
- Google Cloud Speech API 调用注意事项及调用方式__.Net版1
- 使用Google cloud platform SDK 调用 google translate API
- google api 调用 jquery
- 调用google map api
- 调用google地图api
- VS2005调用MS Speech API 5.1 sphelper.h出现的问题及解决方法
- Google Speech API
- VB.NET及C#调用API
- ASP.NET调用V3版本的Google Maps API
- ASP.NET调用V3版本的Google Maps API
- ASP.NET调用V3版本的Google Maps API
- x64系统调用API注意事项
- JavaScript 通过 WebView 调用 Android Text to Speech API
- .NET 调用 Win32 API
- php调用Google translate_tts api
- google api调用 实现在线翻译
- php调用google api 天气预报
- ES新特性学习
- A+B
- C++概述(二)
- HDU 1257 最少拦截系统
- [Azure]ARM模式下使用Powershell找出订阅中没有被使用的vhd
- Google Cloud Speech API 调用注意事项及调用方式__.Net版2
- 在MS SQL中去掉getdate()的时间部分,只要日期部分
- Byte[]与图片互相转换,保存到本地
- HDU_1257 最少拦截系统
- cocos2dx 多人小游戏时间同步问题(简单版)
- jquery用法--jquery盒子模型
- Zookeeper 入门
- apache-hive-1.2.1和hbase-1.2.2的整合(伪分布式)
- storm和spring的结合