Exemple #1
0
        public static string StartTranslate(string path, string lang)
        {
            var builder = new SpeechClientBuilder();

            builder.CredentialsPath = "key.json";
            var speech = builder.Build();

            var config = new RecognitionConfig
            {
                Encoding          = RecognitionConfig.Types.AudioEncoding.Linear16,
                LanguageCode      = lang,
                AudioChannelCount = 1
            };

            var audio = RecognitionAudio.FromFile(path);


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

            foreach (var result in response.Results)
            {
                foreach (var alternative in result.Alternatives)
                {
                    fullText += alternative.Transcript;
                }
            }
            return(fullText);
        }
Exemple #2
0
        private async Task <StreamingRecognizeStream> GetGoogleStream(int SampleRate)
        {
            var sslCredentials = new SslCredentials();

            var speechBuilder = new SpeechClientBuilder();

            speechBuilder.ChannelCredentials = sslCredentials;
            //speechBuilder.Endpoint = "google.com/speech-api/";
            var speech        = speechBuilder.Build();
            var streamingCall = speech.StreamingRecognize(CallSettings.FromHeader("x-goog-api-key", ApiKey));

            // Write the initial request with the config.
            await streamingCall.WriteAsync(
                new StreamingRecognizeRequest()
            {
                StreamingConfig = new StreamingRecognitionConfig()
                {
                    Config = new RecognitionConfig()
                    {
                        Encoding                   = RecognitionConfig.Types.AudioEncoding.Linear16,
                        SampleRateHertz            = SampleRate,
                        LanguageCode               = CultureLanguage.ToString(),
                        EnableAutomaticPunctuation = false,
                        AudioChannelCount          = 1,
                        UseEnhanced                = true
                    },
                    InterimResults = false,
                }
            });

            return(streamingCall);
        }
Exemple #3
0
        // [END speech_transcribe_with_profanity_filter_sync_gcs]

        // [START speech_transcribe_with_multi_region_sync_gcs]
        static object SyncRecognizeWithMultiRegionGcs(string storageUri)
        {
            // Use the SpeechClientBuilder to initialize the SpeechClient with the new endpoint.
            var          endPoint = "eu-speech.googleapis.com";
            SpeechClient speech   = new SpeechClientBuilder
            {
                Endpoint = endPoint
            }.Build();

            var response = speech.Recognize(new RecognitionConfig
            {
                Encoding        = RecognitionConfig.Types.AudioEncoding.Linear16,
                SampleRateHertz = 16000,
                LanguageCode    = "en",
            }, RecognitionAudio.FromStorageUri(storageUri));

            foreach (var result in response.Results)
            {
                foreach (var alternative in result.Alternatives)
                {
                    Console.WriteLine(alternative.Transcript);
                }
            }
            return(0);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="GoogleSpeechService" /> class.
        /// </summary>
        /// <param name="credentialsPath">The credentials path.</param>
        public GoogleSpeechService(string credentialsPath)
        {
            var builder = new SpeechClientBuilder()
            {
                CredentialsPath = credentialsPath,
            };

            _client = builder.Build();
        }
        public SpeechRecogniser()
        {
            //apikey = AIzaSyApSUkdJXbR29cccXDs7mmVbQYPval1F3Q
            var speech = new SpeechClientBuilder();

            speech.JsonCredentials = File.ReadAllText("../../../../AudioReader/key.json");
            var speechWriter = speech.Build();
            var config       = new RecognitionConfig
            {
                Encoding          = RecognitionConfig.Types.AudioEncoding.Linear16,
                LanguageCode      = LanguageCodes.English.UnitedKingdom,
                AudioChannelCount = 1
            };

            this.Config = config;
            this.Client = speechWriter;
        }
        public static IEnumerable <ClosedCaptionSegment> Convert(
            string gsLink)
        {
            using (Stream credentialsStream = new FileStream(
                       _credentialsPath, FileMode.Open))
            {
                var googleCredential = GoogleCredential
                                       .FromStream(credentialsStream);

                //var channel = new Grpc.Core.Channel(
                //	SpeechClient.DefaultEndpoint,//.Host,
                //	googleCredential.ToChannelCredentials());

                var client = new SpeechClientBuilder
                {
                    ChannelCredentials = googleCredential.ToChannelCredentials()
                };
                var speech = client.Build();

                var recognitionAudio = RecognitionAudio
                                       .FromStorageUri(gsLink);

                var longOperation = speech.LongRunningRecognize(
                    new RecognitionConfig
                {
                    Encoding = RecognitionConfig.Types.AudioEncoding.Flac,
                    //SampleRateHertz = 44100,
                    LanguageCode               = "en",
                    EnableWordTimeOffsets      = true,
                    EnableAutomaticPunctuation = true,
                    ProfanityFilter            = false,
                }, recognitionAudio);

                longOperation = longOperation.PollUntilCompleted();
                var response = longOperation.Result;

                foreach (var result in response.Results)
                {
                    foreach (var alternative in result.Alternatives)
                    {
                        yield return(new ClosedCaptionSegment(alternative));
                    }
                }
            }
        }
Exemple #7
0
        private void openAudioBtn_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.OpenFileDialog fileDiag = new System.Windows.Forms.OpenFileDialog();
            // fileDiag.Filter = "Text files (*txt)|*.txt";
            if (fileDiag.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                selectedFileName = fileDiag.FileName;
            }
            if (selectedFileName != "")
            {
                SpeechClientBuilder builder = new SpeechClientBuilder
                {
                    CredentialsPath = @"C:\Users\vdmil\Downloads\my_key.json"
                };
                SpeechClient speech   = builder.Build();
                var          response = speech.Recognize(new RecognitionConfig()
                {
                    Encoding        = RecognitionConfig.Types.AudioEncoding.EncodingUnspecified,
                    SampleRateHertz = 16000,
                    LanguageCode    = "en",
                }, RecognitionAudio.FromFile(selectedFileName));


                textEntry.Document.Blocks.Clear();

                string tempStr = "";

                foreach (var result in response.Results)
                {
                    foreach (var alternative in result.Alternatives)
                    {
                        tempStr += alternative.Transcript;
                    }
                }

                textEntry.Document.Blocks.Add(new Paragraph(new Run(tempStr)));
            }

            selectedFileName = "";
        }
Exemple #8
0
        static object SyncRecognizeWithCredentials(string filePath, string credentialsFilePath)
        {
            SpeechClientBuilder builder = new SpeechClientBuilder
            {
                CredentialsPath = credentialsFilePath
            };
            SpeechClient speech   = builder.Build();
            var          response = speech.Recognize(new RecognitionConfig()
            {
                Encoding        = RecognitionConfig.Types.AudioEncoding.Linear16,
                SampleRateHertz = 16000,
                LanguageCode    = "en",
            }, RecognitionAudio.FromFile(filePath));

            foreach (var result in response.Results)
            {
                foreach (var alternative in result.Alternatives)
                {
                    Console.WriteLine(alternative.Transcript);
                }
            }
            return(0);
        }
Exemple #9
0
        private void Recognize()
        {
            SpeechClientBuilder builder = new SpeechClientBuilder();

            builder.CredentialsPath = GOOGLE_API_CREDS_PATH;

            SpeechClient     client  = builder.Build();
            RecognizeRequest request = new RecognizeRequest()
            {
                Audio  = RecognitionAudio.FromFile(TEMP_AUDIO_PATH),
                Config = new RecognitionConfig()
                {
                    Encoding              = RecognitionConfig.Types.AudioEncoding.EncodingUnspecified,
                    LanguageCode          = "ru-RU",
                    EnableWordTimeOffsets = false
                }
            };
            RecognizeResponse response = client.Recognize(request);

            Result.Text = string.Join("\n", response.Results.Select(
                                          result => result.Alternatives[0].Transcript
                                          ));
        }
Exemple #10
0
        private void stopRecordAudio_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            waveIn.StopRecording();
            stopRecordAudio.Visibility = Visibility.Hidden;
            recordAudio.Visibility     = Visibility.Visible;
            if (File.Exists(audioOutput))
            {
                File.Delete(audioOutput);
            }

            writer = new WaveFileWriter(audioOutput, waveIn.WaveFormat);
            byte[] buffer = new byte[bwp.BufferLength];
            int    offset = 0;
            int    count  = bwp.BufferLength;

            var read = bwp.Read(buffer, offset, count);

            if (count > 0)
            {
                writer.Write(buffer, offset, read);
            }

            waveIn.Dispose();
            waveIn = new WaveIn();
            waveIn.DataAvailable += WaveIn_DataAvailable;
            waveIn.WaveFormat     = new NAudio.Wave.WaveFormat(16000, 1);
            writer.Close();
            writer = null;


            if (File.Exists(audioOutput))
            {
                SpeechClientBuilder builder = new SpeechClientBuilder
                {
                    CredentialsPath = @"C:\Users\vdmil\Downloads\my_key.json"
                };
                SpeechClient speech   = builder.Build();
                var          response = speech.Recognize(new RecognitionConfig()
                {
                    Encoding        = RecognitionConfig.Types.AudioEncoding.Linear16,
                    SampleRateHertz = 16000,
                    LanguageCode    = "en",
                }, RecognitionAudio.FromFile(audioOutput));


                textEntry.Document.Blocks.Clear();

                string tempStr = "No data available";

                foreach (var result in response.Results)
                {
                    foreach (var alternative in result.Alternatives)
                    {
                        tempStr = alternative.Transcript;
                    }
                }

                textEntry.Document.Blocks.Add(new Paragraph(new Run(tempStr)));
            }
            else
            {
                textEntry.Document.Blocks.Add(new Paragraph(new Run("Audio File Missing")));
            }
        }
        Material transcribeFromObject(string gcsUri, string langCode)
        {
            SpeechClientBuilder scb = new SpeechClientBuilder();

            scb.CredentialsPath = fnCredential;
            var speech = scb.Build();
            //var audio = RecognitionAudio.FromFile(fnAudio);
            var audio  = RecognitionAudio.FromStorageUri(gcsUri);
            var config = new RecognitionConfig
            {
                Encoding                   = RecognitionConfig.Types.AudioEncoding.Flac,
                SampleRateHertz            = 16000,
                LanguageCode               = langCode,
                EnableAutomaticPunctuation = true,
                EnableWordTimeOffsets      = true,
                ProfanityFilter            = false,
            };
            var op = speech.LongRunningRecognize(config, audio);

            op = op.PollUntilCompleted();
            var      response = op.Result;
            Material mat      = new Material();

            foreach (var result in response.Results)
            {
                Segment segm  = new Segment();
                var     srAlt = result.Alternatives[0];
                for (int i = 0; i < srAlt.Words.Count; ++i)
                {
                    var     srWord    = srAlt.Words[i];
                    decimal startMSec = (decimal)Math.Round(srWord.StartTime.ToTimeSpan().TotalSeconds * 1000.0);
                    decimal endMSec   = (decimal)Math.Round(srWord.EndTime.ToTimeSpan().TotalSeconds * 1000.0);
                    var     word      = new Word
                    {
                        StartSec  = startMSec / 1000,
                        LengthSec = (endMSec - startMSec) / 1000,
                        Text      = srWord.Word,
                    };
                    if (char.IsPunctuation(word.Text[word.Text.Length - 1]))
                    {
                        word.Trail = word.Text.Substring(word.Text.Length - 1);
                        word.Text  = word.Text.Substring(0, word.Text.Length - 1);
                    }
                    segm.Words.Add(word);
                    if (word.Trail == "." || word.Trail == "?" || word.Trail == "!")
                    {
                        segm.StartSec  = segm.Words[0].StartSec;
                        segm.LengthSec = segm.Words[segm.Words.Count - 1].StartSec + segm.Words[segm.Words.Count - 1].LengthSec - segm.StartSec;
                        mat.Segments.Add(segm);
                        segm = new Segment();
                    }
                }
                if (segm.Words.Count > 0)
                {
                    segm.StartSec  = segm.Words[0].StartSec;
                    segm.LengthSec = segm.Words[segm.Words.Count - 1].StartSec + segm.Words[segm.Words.Count - 1].LengthSec - segm.StartSec;
                    mat.Segments.Add(segm);
                }
            }
            return(mat);
        }