C# .Net使用Google進行語音辨識 Google Web Speech API

參考並使用來自 以下網址Reference: https://gist.github.com/satana/75e639434b66e6c62e18

public class GoogleVoice {
public static string ACCESS_GOOGLE_SPEECH_KEY = “AIzaSyDC8nM1S0cLpXvRc8TXrDoey-tqQsoBGnM";
// public static string TEST_NEW_PATH_2014_PART = “https://www.google.com/speech-api/v2/recognize?output=json&lang=en-us&key=";
// cmn-Hant-TW
public static string TEST_NEW_PATH_2014_PART = “https://www.google.com/speech-api/v2/recognize?output=json&lang=cmn-Hant-TW&key=";

public static string NOT_MY_KEY = “AIzaSyCnl6MRydhw_5fLXIdASxkLJzcJh5iX0M4″;

public static void Main(string[] args) {
Console.WriteLine(GoogleSpeechToTextLib.GoogleVoice.parseJSON(GoogleSpeechToTextLib.GoogleVoice.GoogleSpeechRequest(“D:/Temp/hello.flac", 44100)));
}

public static String GoogleSpeechRequest(String flacName, int sampleRate) {
string PATH = TEST_NEW_PATH_2014_PART + ACCESS_GOOGLE_SPEECH_KEY;
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(PATH);
request.Method = “POST";
byte[] byteArray = File.ReadAllBytes(flacName);
sampleRate = 44100;
request.ContentType = “audio/x-flac; rate=" + sampleRate.ToString();
request.ContentLength = byteArray.Length;
Stream sendStream = request.GetRequestStream();
sendStream.Write(byteArray, 0, byteArray.Length);
sendStream.Close();
string responseFromServer;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
var reader = new StreamReader(response.GetResponseStream());
responseFromServer = reader.ReadToEnd();
reader.Close();
response.Close();
return responseFromServer;
}

public static string parseJSON(string responseFromServer) {
string[] jsons = responseFromServer.Split(‘\n’);
string text = “";
foreach (String j in jsons) {
dynamic jsonObject = JsonConvert.DeserializeObject(j);
if (jsonObject == null || jsonObject.result.Count <= 0) {
continue;
}
text = jsonObject.result[0].alternative[0].transcript;
}
return text;
}
}

廣告

About fenjj

Perfect !!??...
本篇發表於 Uncategorized。將永久鏈結加入書籤。

發表迴響

在下方填入你的資料或按右方圖示以社群網站登入:

WordPress.com Logo

您的留言將使用 WordPress.com 帳號。 登出 / 變更 )

Twitter picture

您的留言將使用 Twitter 帳號。 登出 / 變更 )

Facebook照片

您的留言將使用 Facebook 帳號。 登出 / 變更 )

Google+ photo

您的留言將使用 Google+ 帳號。 登出 / 變更 )

連結到 %s