请求 Yandex 语音

Request to Yandex Speech

我尝试设置请求以识别 wav 文件-> 通过桌面应用程序使用 Yandex Speech API 发送文本。

文档请求示例:

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.

(使用google翻译)

byte[] 字节- 是:

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

P.S。 documentation on Russian language

此要点集 POST 对 Yandex Speech Cloud 的请求。

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

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;
    }