c# - 调用 Google Cloud Speech API 未返回任何内容,10 分钟后失败

标签 c# google-app-engine google-cloud-platform speech-recognition google-speech-api

我正在尝试使用 Google.Cloud.Speech.V1(Google Cloud Speech API 的客户端库),并且我正在使用 Google 示例代码的这个稍微修改过的版本:

public async Task<string> TranscribeSpeech(string filenameAndPath, int WAVSampleRate = 8000)
    {
        Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", Utils.GetHomeFolder() + @"\Google Speech API Key.json"); //for authentication

        var language = WebConfigurationManager.AppSettings["GoogleSpeechFromLocale"];

        var speech = SpeechClient.Create();
        var response = await speech.RecognizeAsync(new RecognitionConfig()
        {
            Encoding = RecognitionConfig.Types.AudioEncoding.Linear16,
            SampleRateHertz = WAVSampleRate,
            LanguageCode = language,
        }, RecognitionAudio.FromFile(filenameAndPath));

        return response.Results.First().Alternatives.First().Transcript;
    }

.Recognize().RecognizeAsync() 方法从不返回任何内容,并在 10 分钟后抛出异常,提示 Status(StatusCode=DeadlineExceeded,Detail= “超过最后期限”)!

换句话说,当我在 Visual Studio 中逐行调试时,代码在 await speech.RecognizeAsync() 之后永远不会继续,只会一直挂起,直到 10 分钟后抛出异常。

我的代码或 API 设置有问题吗?

我的输入文件通常只有 2-3 秒长,格式如下(ffmpeg 的输出):

流 #0:0:音频:pcm_s16le ([1][0][0][0]/0x0001),8000 Hz,单声道,s16,128 kb/s

我的应用程序代码托管在 Azure 上。 Google Cloud Platform Console 显示没有 API 调用——可能意味着我的请求以某种方式没有到达 Google 服务器。

同一应用程序还调用了 Bing Speech API,并且调用成功。

如果我从 https://developers.google.com/apis-explorer/?hl=en_US#p/speech/v1beta1/speech.speech.syncrecognize 运行调用使用相同的 WAV 文件,它会成功。

最佳答案

我带你按照安装指南:https://cloud.google.com/speech/docs/reference/libraries如果你这样做了,一切都会正常。

但是,您可以使用它的次数是有上限的。

1 内容限制:

1-1 同步请求 大约 1 分钟。

1-2 异步请求 大约 80 分钟。

1-3 Streaming Requests,也是在 1 分钟左右。

2 语音上下文限制:

2-1 Phrases per request 增加到 500 的值。

2-2 每个请求的总字符数 达到 10k 个字符。

2-3 每个短语的字符数 最多为 100。

超过 1 分钟的音频必须使用 uri 字段来引用 Google Cloud Storage 中的音频文件。

对于 StreamingRecognize 请求,音频必须以接近实时的速率发送。

尝试处理超出这些内容限制的内容会产生错误。

如果您想了解 Google Speech API 的更多限制,我建议您查看:https://cloud.google.com/speech/limits因为我在另一个 google API 中也遇到了超出限制的相同错误。

关于c# - 调用 Google Cloud Speech API 未返回任何内容,10 分钟后失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43534800/

相关文章:

c# - 修改 RavenDB 中嵌套集合中的特定项

c# - LINQ 中的多级分组?

c# - 计算一个月内的周数

google-app-engine - 我可以在 Google Cloud Datastore 中使用的命名空间数量是否有限制?

java - 如何将我的 Google App Engine 数据存储中的数据写入 com.google.appengine.api.datastore.Text

ios - 如何在 Google Cloud Platform 中启动 macOS 实例?我需要 mac 实例来运行 xamarin ios 应用程序的 xcode 构建

google-cloud-storage - Google Cloud Shell无法启动;抛出使用限制超出消息

c#: tryparse 与转换

python - 编写 python 脚本以获取 GCP 中数据存储实体的内容

google-app-engine - 在标准环境中运行至少 1 个 Google-App-Engine 实例