简体   繁体   中英

Google Speech API returns empty result for some, but not the others (C#)

EMPTY result looks like this:

json[0] "{\"result\":[]}"
json[1] ""

NON-EMPTY result (desired result) looks like this:

json[0] "{\"result\":[]}"
json[1] "{\"result\":[{\"alternative\":[{\"transcript\":\"good morning Google how are you feeling today\",\"confidence\":0.987629}],\"final\":true}],\"result_index\":0}"
json[2] ""

I have this function, that is supposed to take the ".flac" file and turn it into text. For some reason, only these two sample ".flac" files return a string when passed through Google Speech API, other flac files return EMPTY result. Same problem these guys are having: link

Here are all my flac files: link

my.flac and this_is_a_test.flac work perfectly, google speech API gives me a jason object with the text in it.

however, recorded.flac does NOT work with google speech API and gives me EMPTY json object.

DEBUGGING:

  1. i thought it was the microphone that was the problem, and i recorded recorded.flac many times, loud and clear, and converted it to flac using ffmpeg. But google speech API still can't recognize recorded.flac
  2. I thought i got the formatting wrong in the code, so i tried

    _HWR_SpeechToText.ContentType = "audio/116; rate=16000";

    instead of

_HWR_SpeechToText.ContentType ="audio/x-flac; rate=44100";

Then, none of them worked, not a single flac file. so i changed it back.

Here is my google speech API code that turns FLAC files into TEXT (i don't think it is necessary, but, whatever):

public void convert_to_text()
    {
        FileStream fileStream = File.OpenRead("recorded.flac");//my.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=" + ACCESS_GOOGLE_SPEECH_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();

        StreamReader SR_Response = new StreamReader(HWR_Response.GetResponseStream());
        string responseFromServer = (SR_Response.ReadToEnd());

        String[] jsons = responseFromServer.Split('\n');
        foreach (String j in jsons)
        {
            dynamic jsonObject = JsonConvert.DeserializeObject(j);
            if (jsonObject == null || jsonObject.result.Count <= 0)
            {
                continue;
            }
            text = jsonObject.result[0].alternative[0].transcript;
            jsons = null;
        }
        label1.Content = text;
    }

First check that the file is 16-bit PCM Mono and not stereo. Easy to do with http://www.audacityteam.org/

在此处输入图片说明

Then you can use this simple code to do 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 technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM