请求Yandex演讲



我尝试通过桌面应用程序使用Yandex语音API设置识别wav文件>到文本的请求。

来自文档的请求示例:

POST /asr_xml?uuid=01ae13cb744628b58fb536d496daa1e6&key=developers-simple- key&topic=maps HTTP/1.1
Content-Type: audio/x-speex
User-Agent: Dalvik/1.2.0 (Linux; U; Android 2.2.2; LG-P990 Build/FRG83G)
Host: asr.yandex.net
Transfer-Encoding: chunked
7d0
...
chunked body

因此,我在开发人员论坛上注册,获取api密钥并编写简单的代码:

public string RecognizeSpeech(byte[] bytes, String uuid, String apiKey, string topic = "queries", string lang = "ru-RU")
    {
        try
        {
            var uri = string.Format("https://asr.yandex.net/asr_xml?" +
            "uuid={0}" +
            "&key={1}" +
            "&topic={2}" +
            "&lang={3}", uuid, apiKey, topic, lang);
            var request = (HttpWebRequest)WebRequest.Create(uri);
            request.Method = "POST";
            request.ContentType = "audio/x-wav";//"audio/x-pcm;bit=16;rate=16000";
            request.ContentLength = bytes.Length;

            using (var stream = request.GetRequestStream())
            {
                stream.Write(bytes, 0, bytes.Length);// exception here
            }
            var response = request.GetResponse();
            var reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            return reader.ReadToEnd();
        }
        catch (Exception ex)
        {
          ...
        }
        return "";
    }

我尝试使用audio/x-wav或audio/x-pcm;bit=16;rate=16000,但出现错误:

Unable to write data to the transport connection: remote host forcibly ripped existing connection.

(使用谷歌翻译)

字节[] 字节- 是:

var audioBytes = File.ReadAllBytes(@"file.wav");

附言俄语文档

这个要点将POST请求设置为Yandex语音云。

您需要制作有效的音频文件(我使用 Freemake) - pcm,16 位,16000 Hz,单声道。这是解决方案:

public string PostMethod(byte[] bytes)
    {
        string postUrl = "https://asr.yandex.net/asr_xml?" +
        "uuid=01ae13cb744628b58fb536d496daa1e6&" +
        "key="+your_api_key_here+"&" +
        "topic=queries";
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(postUrl);
        request.Method = "POST";
        request.Host = "asr.yandex.net";
        request.SendChunked = true;
        request.UserAgent = "Oleg";
        request.ContentType = "audio/x-pcm;bit=16;rate=16000";
        request.ContentLength = bytes.Length;
        using (var newStream = request.GetRequestStream())
        {
            newStream.Write(bytes, 0, bytes.Length);
        }
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        string responseToString = "";
        if (response != null)
        {
            var strreader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            responseToString = strreader.ReadToEnd();
        }
        int index = responseToString.IndexOf("<variant confidence="1">");
        responseToString = responseToString.Substring(index + 24, responseToString.Length - index - 24);
        int index2 = responseToString.IndexOf("</variant>");
        responseToString = responseToString.Substring(0, index2);
        return responseToString;
    }

相关内容

  • 没有找到相关文章

最新更新