使用Google Speech API

用户名

在基于C#的应用程序中实现Google Speech API的代码是什么?我发现可以创建一个音频文件并将其发送到http://slides.html5rocks.com/#speech-input并以文本形式接收。如果您以前尝试过此方法,可以请您说明如何执行此操作或向我提供代码吗?现在被困在这里了一段时间

非常感激。

到目前为止的代码:

    SpeechRecognitionEngine rec = new SpeechRecognitionEngine();
    SpeechSynthesizer dummy = new SpeechSynthesizer();


    public Form1()
    {
        InitializeComponent();


        Choices searching = new Choices("Porsche");
        GrammarBuilder searchService = new GrammarBuilder("Search");

        searchService.Append(searching);


        // Create a Grammar object from the GrammarBuilder and load it to the  recognizer.
        Grammar googleGrammar = new Grammar(searchService); ;
        rec.RequestRecognizerUpdate();
        rec.LoadGrammar(googleGrammar);

        // Add a handler for the speech recognized event.
        rec.SpeechRecognized +=
          new EventHandler<SpeechRecognizedEventArgs>(_recognizer_SpeechRecognized);

        // Configure the input to the speech recognizer.
        rec.SetInputToDefaultAudioDevice();

        // Start asynchronous, continuous speech recognition.
        rec.RecognizeAsync(RecognizeMode.Multiple);
    }




    private void _recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
    {

        try
        {
            FileStream FS_Audiofile = new FileStream("temp.flac", FileMode.Open, FileAccess.Read);
            BinaryReader BR_Audiofile = new BinaryReader(FS_Audiofile);
            byte[] BA_AudioFile = BR_Audiofile.ReadBytes((Int32)FS_Audiofile.Length);
            FS_Audiofile.Close();
            BR_Audiofile.Close();

            HttpWebRequest _HWR_SpeechToText = null;

            _HWR_SpeechToText = (HttpWebRequest)WebRequest.Create("http://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=de-DE&maxresults=1&pfilter=0");

            _HWR_SpeechToText.Method = "POST";
            _HWR_SpeechToText.ContentType = "audio/x-flac; rate=44100";
            _HWR_SpeechToText.ContentLength = BA_AudioFile.Length;
            _HWR_SpeechToText.GetRequestStream().Write(BA_AudioFile, 0, BA_AudioFile.Length);

            HttpWebResponse HWR_Response = (HttpWebResponse)_HWR_SpeechToText.GetResponse();
            if (HWR_Response.StatusCode == HttpStatusCode.OK)
            {
                StreamReader SR_Response = new StreamReader(HWR_Response.GetResponseStream());
                textBox1.Text = SR_Response.ToString();

            }

        }
        catch (Exception ex)
        {

        }  
    }

这不会从Google返回任何值。

罗伯特·罗恩特里

只要发送的文件不是太长,以下内容就会在curl中起作用……不到5秒。

curl -X POST -H“内容类型:音频/ x-flac;速率= 16000” \ -T seg_1.flac“ https://www.google.com/speech-api/v1/recognize?\ xjerr = 1&client = speech2text&maxresults = 1&lang = zh-CN&key = ... 48593“

{“状态”:0,“ id”:“”,“假设”:[{“话语”:“现在是最喜欢的消遣”,“信心”:0.95148802}]}

因此,编码为speechX或flac

在记录中包含采样率的参数

包括你的钥匙

保持文件持续时间短(在访问API之前,您必须先拆分文件)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章