请求 Yandex 演讲

问题描述 投票:0回答:1

我尝试设置请求以通过桌面应用程序使用 Yandex Speech 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,但出现错误:

无法将数据写入传输连接:远程主机强行断开现有连接。 (使用谷歌翻译)

byte[] 字节 - 是:

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

附注俄语文档

c# .net httpwebrequest yandex-api
1个回答
1
投票

此要点将 POST 请求设置为 Yandex Speech Cloud。

您需要制作有效的音频文件(我使用 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;
    }
© www.soinside.com 2019 - 2024. All rights reserved.