示例#1
0
        static void StartRecognition()
        {
            var audioDirectory  = Path.Combine(Directory.GetCurrentDirectory(), "Audio");
            var audioCollection = new Dictionary <string, FileInfo>();

            foreach (var item in Directory.GetFiles(audioDirectory))
            {
                var audioFile = new FileInfo(item);
                audioCollection.Add(audioFile.Name, audioFile);
            }

            var audioListString = string.Join(" \n", audioCollection.Keys);

            Console.WriteLine(@"Which file to use -> {0}", audioListString);
            var response = Console.ReadLine();

            if (string.IsNullOrEmpty(response))
            {
                response = "robot.wav";
            }

            _recognizer.StartRecognition(new FileStream(Path.Combine(audioDirectory, response), FileMode.Open));

            Console.WriteLine(@"Press any key to start Speech Recognition...");
            Console.ReadLine();

            var result = _recognizer.GetResult();

            _recognizer.StopRecognition();
            if (result != null)
            {
                Console.WriteLine(result.GetHypothesis());
            }
        }
        public void Transcribe_UsingGrammar_Continuous()
        {
            _configuration = new Configuration
            {
                AcousticModelPath = ModelsDirectory,
                DictionaryPath    = _dictionaryPath,
                LanguageModelPath = _languageModelPath,
                UseGrammar        = true,
                GrammarName       = "hello",
                GrammarPath       = ModelsDirectory
            };

            _speechRecognizer = new StreamSpeechRecognizer(_configuration);

            for (int i = 1; i <= 3; i++)//TODO: Reading 3 or more files in a row causes test fail (The same happens in CMU Sphinx4)
            {
                var audioFile = Path.Combine(Directory.GetCurrentDirectory(), "Audio", string.Format("Long Audio {0}.wav", i));
                var stream    = new FileStream(audioFile, FileMode.Open);
                _speechRecognizer.StartRecognition(stream);
                var result = _speechRecognizer.GetResult();
                _speechRecognizer.StopRecognition();
                Assert.IsNotNull(result);
                var hypothesis = result.GetHypothesis();
                Assert.IsTrue(hypothesis.Contains("the time is now exactly twenty five to one") || hypothesis.Contains("there's three left on the left side the one closest to us"));
            }
        }
示例#3
0
        public async Task <string> ProcessVoiceToText(Stream stream, int bitRate)
        {
            var fn = $"a-{Guid.NewGuid()}-{bitRate}.wav";

            stream.Seek(0, SeekOrigin.Begin);
            var wavStream = new RawSourceWaveStream(stream, new WaveFormat(bitRate, 2));

            // Debugging only
            // WaveFileWriter.CreateWaveFile($"{fn}-source.wav", wavStream);

            stream.Seek(0, SeekOrigin.Begin);
            var newFormat = new WaveFormat(InputRate, 1);
            WaveFormatConversionStream cs = new WaveFormatConversionStream(newFormat, wavStream);

            // Debugging only
            // WaveFileWriter.CreateWaveFile(fn, cs);
            cs.Seek(0, SeekOrigin.Begin);
            speechRecognizer.StartRecognition(cs);
            var result = speechRecognizer.GetResult();

            speechRecognizer.StopRecognition();

            cs.Close();
            return(result?.GetHypothesis());
        }
 public void StartRecognition(Stream stream)
 {
     try
     {
         CanTranscribe = false;
         _recognizer.StartRecognition(stream);
         var result = _recognizer.GetResult();
         _recognizer.StopRecognition();
         if (result != null)
         {
             MessageBox.Show(result.GetHypothesis());
         }
         stream.Close();
         CanTranscribe = true;
     }
     catch (Exception exception)
     {
         this.LogError(exception);
     }
 }
示例#5
0
文件: Recognition.cs 项目: Ouay/MVP
        private void recognize()
        {
            waveInStream.StopRecording();
            mem.Position = 0;
            _recognizer.StartRecognition(mem, new TimeFrame(mem.Length));
            SpeechResult result = _recognizer.GetResult();

            _recognizer.StopRecognition();
            Console.WriteLine("result: " + result.GetHypothesis());
            Console.ReadKey();
            Console.ReadKey();
        }
示例#6
0
        private static void Main()
        {
            Logger.LogReceived += LogReceived;
            var modelsDirectory = Path.Combine(Directory.GetCurrentDirectory(), "Models");
            var audioDirectory  = Path.Combine(Directory.GetCurrentDirectory(), "Audio");

            if (!Directory.Exists(modelsDirectory) || !Directory.Exists(audioDirectory))
            {
                Console.WriteLine("No Models or Audio directory found!! Aborting...");
                Console.ReadLine();
                return;
            }

            _speechConfiguration = new Configuration
            {
                AcousticModelPath = modelsDirectory,
                DictionaryPath    = Path.Combine(modelsDirectory, "cmudict-en-us.dict"),
                LanguageModelPath = Path.Combine(modelsDirectory, "en-us.lm.dmp"),
                UseGrammar        = true,
                GrammarPath       = modelsDirectory,
                GrammarName       = "hello"
            };

            _speechRecognizer = new StreamSpeechRecognizer(_speechConfiguration);
            for (int i = 1; i <= 3; i++)
            {
                var audioFile = Path.Combine(audioDirectory, String.Format("Long Audio {0}.wav", i));
                var stream    = new FileStream(audioFile, FileMode.Open);
                if (i == 3)
                {
                    System.Diagnostics.Trace.WriteLine("checking");
                }
                _speechRecognizer.StartRecognition(stream);
                var result = _speechRecognizer.GetResult();
                _speechRecognizer.StopRecognition();
                if (result != null)
                {
                    Console.WriteLine(string.Format("Result: {0}", i) + result.GetHypothesis());
                }
                else
                {
                    Console.WriteLine("Result: {0}", "Sorry! Coudn't Transcribe");
                }
                var instance = ConfigurationManager.GetInstance <SpeechMarker>();
                Console.WriteLine(instance.ToString());
                stream.Close();
            }
            Console.WriteLine("DONE!");
            Console.ReadLine();
        }