使用 Google 语音 API [英] Using Google Speech API

查看:31
本文介绍了使用 Google 语音 API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的基于 C# 的应用程序中实现 Google Speech API 的代码是什么?我发现可以创建音频文件并将其发送到 http://slides.html5rocks.com/#speech-input 并将其作为文本接收.如果您以前尝试过,能否请您解释一下如何执行此操作或向我提供代码?卡在这里好久了

What is the code for implementing the Google Speech API in my C# based application? I found out that it is possible to create an audio file and sent it to http://slides.html5rocks.com/#speech-input and receive it as text. Could you please explain how to do this or provide me with the code if you have attempted this before? Been stuck here for a while now

非常感谢.

到目前为止的代码:

    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 返回任何值.

This does not return any value from Google.

推荐答案

只要发送的文件不是太长...在 5 秒内,以下内容在 curl 中有效.

the following works in curl as long as the file sent is not too long... under 5 seconds.

curl -X POST -H "Content-Type: audio/x-flac; rate=16000" \-T seg_1.flac "https://www.google.com/speech-api/v1/recognize? \ xjerr=1&client=speech2text&maxresults=1&lang=en-US&key=...48593"

curl -X POST -H "Content-Type: audio/x-flac; rate=16000" \ -T seg_1.flac "https://www.google.com/speech-api/v1/recognize? \ xjerr=1&client=speech2text&maxresults=1&lang=en-US&key=...48593"

{"status":0,"id":"","hypotheses":[{"utterance":"现在是最喜欢的消遣","confidence":0.95148802}]}

{"status":0,"id":"","hypotheses":[{"utterance":"now it was the favorite pastime","confidence":0.95148802}]}

所以,编码为speechX或flac

So, encode to speechX or flac

在录音中包含一个包含采样率的参数

include a parm with your sample rate from the recording

包括您的密钥

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

keep the file short in duration ( you will have to split files prior to API access )

这篇关于使用 Google 语音 API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆