我想从IBM Watson服务器获得响应。同样的代码也适用于Google Speech API。如下代码所示,我正在向Watson API发送正确的音频。
try
{
FileStream fileStream = File.OpenRead(data_to_translate);
MemoryStream memoryStream = new MemoryStream();
memoryStream.SetLength(fileStream.Length);
fileStream.Read(memoryStream.GetBuffer(), 0, (int)fileStream.Length);
byte[] BA_AudioFile = memoryStream.GetBuffer();
string auth = string.Format("{0}:{1}", "xxxx", "xxxx");
string auth64 = Convert.ToBase64String(Encoding.ASCII.GetBytes(auth));
string credentials = string.Format("{0} {1}", "Basic", auth64);
HttpWebRequest _HWR_SpeechToText = null;
_HWR_SpeechToText =
(HttpWebRequest)HttpWebRequest.Create(
"https://stream.watsonplatform.net/speech-to-text/api/v1/recognize");
_HWR_SpeechToText.Headers[HttpRequestHeader.Authorization] = credentials;
_HWR_SpeechToText.Method = "POST";
_HWR_SpeechToText.ContentType = "audio/flac; rate=44100 channels=2";
_HWR_SpeechToText.ContentLength = BA_AudioFile.Length;
_HWR_SpeechToText.KeepAlive = false;
_HWR_SpeechToText.ProtocolVersion = HttpVersion.Version10;
_HWR_SpeechToText.ServicePoint.ConnectionLimit = 1;
Stream stream = _HWR_SpeechToText.GetRequestStream();
stream.Write(BA_AudioFile, 0, BA_AudioFile.Length);
stream.Close();
HttpWebResponse HWR_Response = (HttpWebResponse)_HWR_SpeechToText.GetResponse();
if ( HWR_Response.StatusCode == HttpStatusCode.OK )
{
StreamReader SR_Response = new StreamReader(HWR_Response.GetResponseStream());
// string text = SR_Response.ReadToEnd();
// jsonResponse json = JsonConvert.DeserializeObject<jsonResponse>(SR_Response.ReadToEnd());
//Speech API response here
var result = SR_Response.ReadToEnd();
Console.WriteLine(result);
var jsons = result.Split('\n');
foreach ( var j in jsons )
{
var jsonObject = JsonConvert.DeserializeObject<jsonResponse.SpeechResponse>(j);
if ( jsonObject == null || jsonObject.Result.Length <= 0 ) continue;
if ( jsonObject.Result[0].Alternative[0].Confidence > 0.90 )
{
Console.WriteLine("text-to-speech van google: " + jsonObject.Result[0].Alternative[0].Transcript + "\nconfidence: " + jsonObject.Result[0].Alternative[0].Confidence);
}
else if ( jsonObject.Result[0].Alternative[0].Confidence < 0.90 )
{
Console.WriteLine("Watson is te onzeker " + jsonObject.Result[0].Alternative[0].Confidence + " :( \nmaar hier is toch het antwoord: " + jsonObject.Result[0].Alternative[0].Transcript);
}
}
}
}
catch ( Exception ex )
{
Console.WriteLine(ex.ToString());
}
Console.ReadLine();
有人可以向我解释我做错了什么吗?我想在没有/ session /的情况下拨打电话,因为我只需要最终答案+信心。
通过此代码,我得到以下错误:
System.IO.IOException: Unable to write data to the transport connection: An existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host
at System.Net.Sockets.Socket.Send(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
at System.Net.Sockets.NetworkStream.Write(Byte[] buffer, Int32 offset, Int32 size)
--- End of inner exception stack trace ---
at System.Net.Sockets.NetworkStream.Write(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.Security._SslStream.StartWriting(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest)
at System.Net.Security._SslStream.ProcessWrite(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest)
at System.Net.TlsStream.Write(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.PooledStream.Write(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.ConnectStream.InternalWrite(Boolean async, Byte[] buffer, Int32 offset, Int32 size, AsyncCallback callback, Object state)
at System.Net.ConnectStream.Write(Byte[] buffer, Int32 offset, Int32 size)
at GoogleSpeech.speechRecognitionWatson.speechRecognize(String data_to_translate) in C:\Program Files (x86)\School\herkansingen jaar 4\INFAFS\ProofofConcepts\GoogleSpeech\GoogleSpeech\speechRecognitionWatson.cs:line 47
谁能告诉我应该添加,删除的内容或者我对IBM Watson的态度是否接近?我认为这会起作用,因为它适用于Google API和其他API。
最佳答案
我已经在我自己的代码中找到了问题。
FileStream fileStream = File.OpenRead(data_to_translate);
MemoryStream memoryStream = new MemoryStream();
memoryStream.SetLength(fileStream.Length);
fileStream.Read(memoryStream.GetBuffer(), 0, (int)fileStream.Length);
byte[] BA_AudioFile = memoryStream.GetBuffer();
HttpWebRequest _HWR_SpeechToText = null;
_HWR_SpeechToText =
(HttpWebRequest)HttpWebRequest.Create("https://stream.watsonplatform.net/speech-to-text/api/v1/recognize");
string auth = string.Format("{0}:{1}","Watson.uID","Watson_uPWD");
string auth64 = Convert.ToBase64String(Encoding.ASCII.GetBytes(auth));
string credentials = string.Format("{0} {1}", "Basic", auth64);
_HWR_SpeechToText.Headers[HttpRequestHeader.Authorization] = credentials;
_HWR_SpeechToText.Method = "POST";
_HWR_SpeechToText.ContentType = "audio/flac; rate=44100; channels=2;";
_HWR_SpeechToText.ContentLength = BA_AudioFile.Length;
Stream stream = _HWR_SpeechToText.GetRequestStream();
stream.Write(BA_AudioFile, 0, BA_AudioFile.Length);
stream.Close();
HttpWebResponse HWR_Response = (HttpWebResponse)_HWR_SpeechToText.GetResponse();
if (HWR_Response.StatusCode == HttpStatusCode.OK)
{
StreamReader SR_Response = new StreamReader(HWR_Response.GetResponseStream());
var result = SR_Response.ReadToEnd();
Console.WriteLine(result); var JsonObject = Newtonsoft.Json.Linq.JObject.Parse(result);
double confidence = (double)JsonObject["results"][0]["alternatives"][0]["confidence"];
if (confidence <= 0.9)
{
Console.WriteLine("Watson knows what he has heard: " + (string)JsonObject["results"][0]["alternatives"][0]["transcript"]);
Console.WriteLine("Watson's confidence!: " + (string)JsonObject["results"][0]["alternatives"][0]["confidence"]);
}
else
{
Console.WriteLine("Watson thinks: " + (string)JsonObject["results"][0]["alternatives"][0]["transcript"]);
Console.WriteLine("Watson has this confidence: " + (string)JsonObject["results"][0]["alternatives"][0]["confidence"]);
}
关于c# - 无法将声音文件上传到IBM Watson语音到文本,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32986402/