Google Cloud Speech API 呼叫注意事項及呼叫方式__.Net版2
阿新 • • 發佈:2019-01-02
在上一章已詳細說明如何讀取本地音訊檔案,呼叫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;
結果如下: