不能上传声音文件到IBM沃森语音到文本

本文关键字:语音 文本 IBM 不能 声音 文件 | 更新日期: 2023-09-27 18:16:35

我想从IBM Watson服务器获得一个响应。同样的代码也适用于谷歌语音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();
谁能给我解释一下我做错了什么?我想打一个没有/会话/的电话,因为我只需要最终答案+信心。 使用此代码,我得到以下错误:
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沃森的方法是否接近?我认为这将工作,因为它适用于谷歌API和其他API…

不能上传声音文件到IBM沃森语音到文本

我在自己的代码中发现了这个问题。

 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"]);
                }