Reputation: 10891
I'm trying to create a windows application where I can take an audio file I have and transcribe the voice in it to a text file with the Google Speech Recognition API. Here is what I did:
1) I went here https://groups.google.com/a/chromium.org/forum/?fromgroups#!forum/chromium-dev and became a member.
2) I went to my Google Developers Console and generated an API key successfully.
3) I got some code online and ran it:
private void btnGoogle_Click(object sender, EventArgs e)
{
string path = @"Z:\path\to\audio\file\good-morning-google.flac";
try
{
FileStream fileStream = File.OpenRead(path);
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)
{
Console.WriteLine("looks ok...");
StreamReader SR_Response = new StreamReader(HWR_Response.GetResponseStream());
Console.WriteLine(SR_Response.ReadToEnd());
Console.WriteLine(SR_Response.ReadToEnd());
Console.WriteLine("Done");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
Console.ReadLine();
}
The code above runs. It gives me the following output:
looks ok...
{"result":[]}
Thus I know I am getting a HttpStatusCode.OK
response because the looks ok...
log line executes.
However, the result is totally empty... Why is that? Am I doing something wrong?
EDIT: Here is where I got the audio file: https://github.com/gillesdemey/google-speech-v2
Upvotes: 1
Views: 2987
Reputation: 353
If Google APIs return no result, there is a high probability that it cannot fulfuill the request. So there is nothing wrong with your code, just the test audio. Have you tried other audio file? I know this because I've worked with Google Custom Search API. If there is no result found, it will return empty.
Upvotes: 0
Reputation: 1087
First of all your code is more complex then needed, I used this:
string api_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
string path = @"C:\temp\good-morning-google.flac";
byte[] bytes = System.IO.File.ReadAllBytes(path);
WebClient client = new WebClient();
client.Headers.Add("Content-Type", "audio/x-flac; rate=44100");
byte[] result = client.UploadData(string.Format(
"https://www.google.com/speech-api/v2/recognize?client=chromium&lang=en-us&key={0}", api_key), "POST", bytes);
string s = client.Encoding.GetString(result);
The second issue you have is your audio file! It's in 32-bit stereo. It should be 16-bit PCM Mono. So convert to mono and drop to 16-bit. I used http://www.audacityteam.org/ to convert your file. See screenshot.
Then I got this response:
{"result":[]}
{"result":[{"alternative":[{"transcript":"good morning Google how are you feeling today","confidence":0.987629}],"final":true}],"result_index":0}
Upvotes: 4