简体   繁体   English

Google Speech Api从音频文件中获取文本,并在C#中返回{“ result”:[]}

[英]Google Speech Api get text from audio file returning {“result”:[]} in C#

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. 我正在尝试创建一个Windows应用程序,可以在其中获取我拥有的音频文件,然后使用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. 1)我去了这里https://groups.google.com/a/chromium.org/forum/?fromgroups#!forum/chromium-dev并成为了成员。

2) I went to my Google Developers Console and generated an API key successfully. 2)我进入了Google Developers Console,并成功生成了API密钥。

3) I got some code online and ran it: 3)我在线获得一些代码并运行它:

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. 因此,我知道我收到了HttpStatusCode.OK响应,因为looks ok...行。

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 编辑:这是我获得音频文件的地方: https : //github.com/gillesdemey/google-speech-v2

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. 它采用32位立体声。 It should be 16-bit PCM Mono. 它应该是16位PCM Mono。 So convert to mono and drop to 16-bit. 因此转换为单声道并降至16位。 I used http://www.audacityteam.org/ to convert your file. 我使用http://www.audacityteam.org/来转换您的文件。 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}

在此处输入图片说明

If Google APIs return no result, there is a high probability that it cannot fulfuill the request. 如果Google API没有返回结果,则很有可能无法完成请求。 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. 我知道这一点是因为我使用过Google自定义搜索API。 If there is no result found, it will return empty. 如果没有找到结果,它将返回空。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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