Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
288 views
in Technique[技术] by (71.8m points)

c# - Using Google Speech API

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

Much appreciated.

Code So far:

    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)
        {

        }  
    }

This does not return any value from Google.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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"

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

So, encode to speechX or flac

include a parm with your sample rate from the recording

include your key

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...