コード例 #1
0
        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));
                    }
                }
            }
        }
コード例 #2
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 = "";
        }
コード例 #3
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);
        }
コード例 #4
0
ファイル: MainForm.cs プロジェクト: Cesar18102/AI_LABS
        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
                                          ));
        }
コード例 #5
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")));
            }
        }
コード例 #6
0
        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);
        }