Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use google speech recognition api in c#?

I want to get the audio file from c# and send to google speech recognition API for get the "speech to text" answer.

My code is like this:

try
{                
    byte[] BA_AudioFile = GetFile(filename);              
    HttpWebRequest _HWR_SpeechToText = null;
    _HWR_SpeechToText =
                (HttpWebRequest)HttpWebRequest.Create(
                    "https://www.google.com/speech-api/v2/recognize?output=json&lang=" + DEFAULT_LANGUAGE + "&key=" + key);
    _HWR_SpeechToText.Credentials = CredentialCache.DefaultCredentials;
    _HWR_SpeechToText.Method = "POST";
    _HWR_SpeechToText.ContentType = "audio/x-flac; rate=44100";
    _HWR_SpeechToText.ContentLength = BA_AudioFile.Length;
    Stream stream = _HWR_SpeechToText.GetRequestStream();
    stream.Write(BA_AudioFile, 0, BA_AudioFile.Length);
    stream.Close();

    HttpWebResponse HWR_Response = (HttpWebResponse)_HWR_SpeechToText.GetResponse();
    if (HWR_Response.StatusCode == HttpStatusCode.OK)
    {
        StreamReader SR_Response = new StreamReader(HWR_Response.GetResponseStream());
        Console.WriteLine(SR_Response.ToString());
    }

}
catch (Exception ex)
{
    Console.WriteLine(ex.ToString());
}

This part is for upload the file.wav and get the response for the google API, which I find from Internet.

But my code always catches the exceptions:

you must write content length bytes to the request stream before calling at _HWR_SpeechToText.GetResponse(); But I already wroteh the ContextLength.

So my question is why my program failed? It's because the google link or the HTTPWebRequest I used inappropriately?

Is this the right place I got the API key?

enter image description here

like image 253
cindywmiao Avatar asked Aug 05 '14 21:08

cindywmiao


People also ask

How many languages can Google Speech API recognize?

Google now recognizes 119 languages for voice-to-text dictation.

How do I get Google speech recognition API key?

To get an API key, go to Google Developer Console, create a project, then go to Library on the right, find the Speech API, and enable it.

Is Google Speech API free?

Google Speech-to-Text is a well known speech transcription API. Google gives users 60 minutes free transcription, with $300 in free credits for Google Cloud hosting. However, since Google only supports transcribing files already in a Google Cloud Bucket, the free credits won't get you very far.


1 Answers

Just tested this myself, below is a working solution if you have a valid API key.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Net;
    using System.IO;

    namespace GoogleRequest
    {
     class Program
     {
        static void Main(string[] args)
        {
            try
            {

                FileStream fileStream = File.OpenRead("good-morning-google.flac");
                MemoryStream memoryStream = new MemoryStream();
                memoryStream.SetLength(fileStream.Length);
                fileStream.Read(memoryStream.GetBuffer(), 0, (int)fileStream.Length);
                byte[] BA_AudioFile = memoryStream.GetBuffer();
                HttpWebRequest _HWR_SpeechToText = null;
                _HWR_SpeechToText =
                            (HttpWebRequest)HttpWebRequest.Create(
                                "https://www.google.com/speech-api/v2/recognize?output=json&lang=en-us&key=YOUR_API_KEY_HERE");
                _HWR_SpeechToText.Credentials = CredentialCache.DefaultCredentials;
                _HWR_SpeechToText.Method = "POST";
                _HWR_SpeechToText.ContentType = "audio/x-flac; rate=44100";
                _HWR_SpeechToText.ContentLength = BA_AudioFile.Length;
                Stream stream = _HWR_SpeechToText.GetRequestStream();
                stream.Write(BA_AudioFile, 0, BA_AudioFile.Length);
                stream.Close();

                HttpWebResponse HWR_Response = (HttpWebResponse)_HWR_SpeechToText.GetResponse();
                if (HWR_Response.StatusCode == HttpStatusCode.OK)
                {
                    StreamReader SR_Response = new StreamReader(HWR_Response.GetResponseStream());
                    Console.WriteLine(SR_Response.ReadToEnd());
                }

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

            Console.ReadLine();
        }
    }
}
like image 139
Umo Avatar answered Sep 24 '22 15:09

Umo