コード例 #1
0
ファイル: SpeechHandler.cs プロジェクト: MatanRad/Matbot
        /// <summary>
        /// Sends the voice audio to Google's API and runs HandleSpeech with transcription.
        /// </summary>
        private void TranscribeSpeech(Message m)
        {
            if (m.voice == null)
            {
                throw new Exception.EmptyVoiceMessageException(m);
            }
            if (m.voice.Duration > maxDur)
            {
                MaxDurationExceeded(m);
                return;
            }

            SpeechClient speech = SpeechClient.Create();

            RecognitionConfig config = new RecognitionConfig();

            config.Encoding        = SpeechHandler.VoiceTypeToGoogleType(m.voice.type);
            config.SampleRateHertz = m.voice.sampleRate;
            config.LanguageCode    = languageCode;
            config.ProfanityFilter = false;


            RecognizeResponse resp = speech.Recognize(config, RecognitionAudio.FromStream(m.voice.AudioStream));

            foreach (var result in resp.Results)
            {
                foreach (var alternative in result.Alternatives)
                {
                    HandleSpeech(m, alternative.Transcript);
                }
            }
        }
コード例 #2
0
ファイル: GoogleSpeech.cs プロジェクト: wlmathison/1000Words
        public static void UploadAudio(IFormFile audio)
        {
            // Reference to Google Cloud Speech-to_text Credentials
            string credential_path = @"C:\Users\Billy\workspace\capstones\1000Words\1000Words\words-247918-f13fa4057b4a.json";

            System.Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", credential_path);

            using (Stream stream = audio.OpenReadStream())
            {
                RecognitionAudio recognitionAudio = RecognitionAudio.FromStream(stream);

                var speech   = SpeechClient.Create();
                var response = speech.Recognize(new RecognitionConfig()
                {
                    Encoding     = RecognitionConfig.Types.AudioEncoding.Linear16,
                    LanguageCode = "en",
                }, recognitionAudio);

                Keywords.Clear();

                foreach (var result in response.Results)
                {
                    foreach (var alternative in result.Alternatives)
                    {
                        // Add transcript to list of keywords to be returned
                        Keywords.Add(alternative.Transcript);
                    }
                }
            }
        }
コード例 #3
0
        private static RecognitionAudio LoadResourceAudio(string name)
        {
            var type = typeof(SpeechClientSnippets);

            using (var stream = type.GetTypeInfo().Assembly.GetManifestResourceStream($"{type.Namespace}.{name}"))
            {
                return(RecognitionAudio.FromStream(stream));
            }
        }
コード例 #4
0
        private void OnComplete(Stream stream)
        {
            lock (this._lock)
            {
                var wavStream = RecognitionAudio.FromStream(stream);
                var response  = this._speech.Recognize(this._config, wavStream);

                var alternatives =
                    from r in response.Results
                    from a in r.Alternatives
                    select new AlternativeBridge(a) as IAlternative;

                this.RecognizeCompleted?.Invoke(alternatives.ToList());
            }
        }
コード例 #5
0
        // Not an actual test... just examples
        public void FactoryMethods()
        {
            // Sample: FactoryMethods
            RecognitionAudio audio1 = RecognitionAudio.FromFile("Sound/SpeechSample.flac");
            RecognitionAudio audio2 = RecognitionAudio.FromUri("https://.../HostedSpeech.flac");
            RecognitionAudio audio3 = RecognitionAudio.FromStorageUri("gs://my-bucket/my-file");

            byte[]           bytes  = ReadAudioData(); // For example, from a database
            RecognitionAudio audio4 = RecognitionAudio.FromBytes(bytes);

            using (Stream stream = OpenAudioStream()) // Any regular .NET stream
            {
                RecognitionAudio audio5 = RecognitionAudio.FromStream(stream);
            }
            // End sample
        }
コード例 #6
0
        protected override void OnStop(object sender, StoppedEventArgs e)
        {
            if (this._speech is null)
            {
                return;
            }
            var wavStream    = RecognitionAudio.FromStream(e.WaveStream);
            var results      = this._speech.Recognize(this._config, wavStream).Results;
            var alternatives = results.SelectMany(r => r.Alternatives);

            Debug.WriteLine("\n========== Results ==========");
            foreach (var alternative in alternatives)
            {
                Debug.WriteLine(alternative.Transcript + $"[{alternative.Confidence}]");
            }
            Debug.WriteLine("=============================\n");

            this.Responsed?.Invoke(alternatives.ToList());
        }
コード例 #7
0
        internal static string Parse(Stream stream)
        {
            var speech   = SpeechClient.Create();
            var response = speech.Recognize(new RecognitionConfig()
            {
                Encoding        = RecognitionConfig.Types.AudioEncoding.Flac,
                SampleRateHertz = 16000,
                LanguageCode    = "en",
            }, RecognitionAudio.FromStream(stream));
            string resultText = string.Empty;

            foreach (var result in response.Results)
            {
                foreach (var alternative in result.Alternatives)
                {
                    resultText = alternative.Transcript;
                }
            }
            return(resultText);
        }
コード例 #8
0
        public async Task <List <string> > ProcessSpeech(IFormFile speechBlob)
        {
            var speech   = SpeechClient.Create();
            var response = speech.Recognize(new RecognitionConfig()
            {
                Encoding        = RecognitionConfig.Types.AudioEncoding.Linear16,
                SampleRateHertz = 16000,
                LanguageCode    = "ro",
            }, RecognitionAudio.FromStream(speechBlob.OpenReadStream()));

            var transcripts = new List <string>();

            foreach (var result in response.Results)
            {
                foreach (var alternative in result.Alternatives)
                {
                    transcripts.Add(alternative.Transcript);
                }
            }

            return(await Task.FromResult(transcripts));
        }
コード例 #9
0
        public ActionResult <IEnumerable <string> > Post(IFormFile content)
        {
            List <string> returnResult = new List <string>();

            if (content == null)
            {
                throw new Exception("Dosya Bulunamadı!");
            }

            try
            {
                Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", credential_path);
                var speech = SpeechClient.Create();
                if (content.Length > 0)
                {
                    var response = speech.Recognize(new RecognitionConfig()
                    {
                        Encoding        = RecognitionConfig.Types.AudioEncoding.Linear16,
                        SampleRateHertz = 8000,
                        LanguageCode    = "tr-TR",
                    }, RecognitionAudio.FromStream(content.OpenReadStream()));

                    foreach (var result in response.Results)
                    {
                        foreach (var alternative in result.Alternatives)
                        {
                            returnResult.Add(alternative.Transcript);
                        }
                    }
                }

                return(returnResult);
            }
            catch (Exception e)
            {
                throw;
            }
        }
コード例 #10
0
        static void Main(string[] args)
        {
            String envName  = "GOOGLE_APPLICATION_CREDENTIALS";
            String envValue = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase).Remove(0, 6) + "\\token.json";

            //Stream stream = new FileStream(envValue, FileMode.Open, FileAccess.Read, FileShare.Read);

            //var credential = GoogleCredential.FromStream(stream);

            // Determine whether the environment variable exists.
            //if (Environment.GetEnvironmentVariable(envName) == null)
            //    // If it doesn't exist, create it.
            //    Environment.SetEnvironmentVariable(envName, envValue);

            //WaveInCapabilities deviceInfo = WaveIn.GetCapabilities(0);
            Console.WriteLine("Gravando...");
            WaveInEvent waveSource = new WaveInEvent();

            //waveSource.DeviceNumber = 0;
            waveSource.WaveFormat = new WaveFormat(16000, 1);

            waveSource.DataAvailable += new EventHandler <WaveInEventArgs>(waveSource_DataAvailable);

            MemoryStream tempFile = new MemoryStream();

            waveFile = new WaveFileWriter(tempFile, waveSource.WaveFormat);
            waveSource.StartRecording();

            Console.WriteLine("Aperte Enter para parar de gravar e enviar para reconhecimento");
            Console.ReadLine();
            waveSource.StopRecording();

            tempFile.Position = 0;


            var speech = SpeechClient.Create();



            //var response = speech.StreamingRecognize();

            // Console.ReadLine();
            long controlLen = tempFile.Length;
            long position   = 0;



            DateTime dt = DateTime.Now;

            //while ((DateTime.Now - dt).Minutes < 1)
            {
                position          = tempFile.Length - controlLen;
                controlLen        = tempFile.Length;
                tempFile.Position = position;
                try
                {
                    //Console.WriteLine("Reconhecendo...");
                    //var t = speech.AsyncRecognize(new RecognitionConfig()
                    //{
                    //    Encoding = RecognitionConfig.Types.AudioEncoding.Linear16,
                    //    SampleRate = 16000,
                    //    LanguageCode = "pt-BR"
                    //}, RecognitionAudio.FromStream(tempFile));


                    //Console.WriteLine("{0}", t.PollUntilCompleted().Result);
                    Console.WriteLine("Reconhecendo...");
                    var response = speech.SyncRecognize(new RecognitionConfig()
                    {
                        Encoding     = RecognitionConfig.Types.AudioEncoding.Linear16,
                        SampleRate   = 16000,
                        LanguageCode = "pt-BR",
                    }, RecognitionAudio.FromStream(tempFile));
                    foreach (var result in response.Results)
                    {
                        foreach (var alternative in result.Alternatives)
                        {
                            Console.WriteLine(alternative.Transcript);
                        }
                    }
                }
                finally
                {
                    Console.WriteLine("Reconhecido em {0}", DateTime.Now - dt);
                    dt = DateTime.Now;
                }
            }



            waveFile.Dispose();
            // Console.WriteLine("Reconhecimento Completo. Tempo={0}", DateTime.Now - dt);
            Console.WriteLine("Terminou");
            Console.ReadKey();
        }