Sara
Sara

Reputation: 35

C# Speech To Text with Google API

I want to convert speech to text using 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!

Upvotes: 0

Views: 1389

Answers (1)

frankhommers
frankhommers

Reputation: 1315

You have two options:

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.
  • 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.

Upvotes: 0

Related Questions