【问题标题】:How to use google speech recognition api in c#?如何在 C# 中使用谷歌语音识别 API?
【发布时间】:2014-08-05 21:10:10
【问题描述】:

我想从 c# 获取音频文件并发送到谷歌语音识别 API 以获得“语音到文本”的答案。

我的代码是这样的:

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());
}

这部分用于上传file.wav并获取google API的响应,我是从网上找到的。

但我的代码总是会捕捉到异常:

在调用 _HWR_SpeechToText.GetResponse(); 之前,您必须将内容长度字节写入请求流;但我已经写了 ContextLength。

所以我的问题是为什么我的程序失败了?是因为google链接或者HTTPWebRequest我使用不当?

这是我获得 API 密钥的正确位置吗?

【问题讨论】:

  • 您的文件有多大?你试过小一点的吗?
  • @NikolayShmyrev 我测试来自github.com/gillesdemey/google-speech-v2的文件
  • 究竟是哪个文件,audio文件夹里有3个
  • 另外,你的代码中真的有 / after rate 行吗?
  • @NikolayShmyrev good-morning-google.flac,我尝试了另外两个,结果一样。

标签: c# speech-recognition


【解决方案1】:

我自己对此进行了测试,如果您拥有有效的 API 密钥,以下是一个可行的解决方案。

    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();
        }
    }
}

【讨论】:

  • 仍然,与旧代码相同的异常。我无法获得 _HWR_SpeechToText.GetResponse()
  • 远程服务器返回错误:(403) Forbidden.
  • 你好,你能看看我得到谷歌 API 密钥的地方是正确的吗?我已经在我的问题中添加了密钥。
  • 是的,服务器应用程序的密钥是正确的,但您还必须启用语音 API,执行的选项应该在 API 的 & Auth --> APIs 下。我看到你也指定了一个 IP,而且我似乎是一个私有 IP,尝试删除任何 IP,它应该允许从任何 IP 访问
  • 我认为您还需要以 chromium 开发人员的身份订阅,我按照本网站上的说明进行操作,否则您无法在开发人员控制台中激活正确的 api,并且您的密钥将无法使用。 chromium.org/developers/how-tos/api-keys
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-06
  • 1970-01-01
  • 1970-01-01
  • 2017-06-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多