简体   繁体   English

使用 Google API 的 C# Speech To Text

[英]C# Speech To Text with Google API

I want to convert speech to text using Google Api 'speech-to-text'.我想使用 Google Api 'speech-to-text' 将语音转换为文本。 it worked when I took the example file but doesn't when I choose a file from local当我使用示例文件时它有效,但当我从本地选择文件时无效

that's my code:那是我的代码:

using Google.Cloud.Speech.V1;
using System;

namespace SpeechToTextApiDemo
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var speech = SpeechClient.Create();
            var config = new RecognitionConfig
            {
                Encoding = RecognitionConfig.Types.AudioEncoding.Flac,
                SampleRateHertz = 16000,
                LanguageCode = LanguageCodes.Hebrew.Israel,
                EnableWordTimeOffsets = true
            };
            var audio = RecognitionAudio.FromStorageUri("gs://cloud-samples-tests/speech/brooklyn.flac");

            var response = speech.Recognize(config, audio);

            foreach (var result in response.Results)
            {
                foreach (var alternative in result.Alternatives)
                {
                    Console.WriteLine($"Transcript: { alternative.Transcript}");
                    Console.WriteLine("Word details:");
                    Console.WriteLine($" Word count:{alternative.Words.Count}");
                    foreach (var item in alternative.Words)
                    {
                        Console.WriteLine($"  {item.Word}");
                        Console.WriteLine($"    WordStartTime: {item.StartTime}");
                        Console.WriteLine($"    WordEndTime: {item.EndTime}");
                    }
                }
            }
        }
    }
}

Thank you!谢谢!

You have two options:您有两个选择:

https://cloud.google.com/speech-to-text/docs/basics#speech_requests https://cloud.google.com/speech-to-text/docs/basics#speech_requests

  • You first need to upload the file to a storage bucket and pass that gs:// link and use either the Asynchronous or Synchronous API.您首先需要将文件上传到存储桶并传递 gs:// 链接并使用异步或同步 API。
  • You need to rewrite your code to use the Streaming API, that way you can stream your file and do not need to upload it first.您需要重写代码以使用 Streaming API,这样您就可以流式传输文件而无需先上传它。

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

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