Trying to use Google Speech2Text in C #

The following simple code attempts to send a wave file to the Google Speech2Text service, but always fails with either “Gateway Timeout (504)” or the general exception “Operation Timeout”. Can anyone help?

public void ProcessWaveFile(string path) { HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create( "https://www.google.com/"+ "speech-api/v1/recognize?"+ "xjerr=1&client=speech2text&lang=en-US&maxresults=10"); ServicePointManager.ServerCertificateValidationCallback += delegate { return true; }; request.Timeout = 60000; request.Method = "POST"; request.KeepAlive = true; request.ContentType = "audio/wav"; request.UserAgent = "speech2text"; FileInfo fInfo = new FileInfo(path); long numBytes = fInfo.Length; byte[] data; using (FileStream fStream = new FileStream( path, FileMode.Open, FileAccess.Read)) { data = new byte[fStream.Length]; fStream.Read(data, 0, (int)fStream.Length); fStream.Close(); } using (Stream wrStream = request.GetRequestStream()) wrStream.Write(data, 0, data.Length); try { HttpWebResponse response = (HttpWebResponse)request.GetResponse(); var resp = response.GetResponseStream(); if (resp != null) { StreamReader sr = new StreamReader(resp); MessageBox.Show(sr.ReadToEnd()); resp.Close(); resp.Dispose(); } } catch (System.Exception ee) { MessageBox.Show(ee.Message); } } 

Thank you very much.

Shujaat

+4
source share
1 answer

Your code works for me with the following change:

 Request.ContentType = "audio/x-flac; rate=8000"; 

and the file you provide must be in FLAC format.

I recorded a small sample with a Windows Sound Recorder that creates a WMA file. Then I used VLC Player to convert the WMA file to FLAC (using the Convert options, make sure you output in RAW format, one channel and 8000 kbps)

Good link is not C # , as well as some advanced api doc in comments

+2
source

Source: https://habr.com/ru/post/1389907/


All Articles