Reputation:
I think this is the same question as C# system.speech.recognition alternate words, however the answer doesn't work. Microsoft API is requiring a command to be used. If I don't put in a command, it displays a messaging saying it's required. If I add one command, that is the only text that comes across. I'm wanting to write something that dictates every word I'm saying. Something like MS Agent did back in the day. Anyone have some direction as I'm failing out and don't want to use Google Cloud API. I'm wanting this to run locally.
using System;
using System.Speech.Recognition;
using System.Speech.Synthesis;
using System.Globalization;
using System.Threading;
using System.Collections.Generic;
namespace S2TextDemo
{
class Program
{
static SpeechSynthesizer ss = new SpeechSynthesizer();
static SpeechRecognitionEngine sre;
static bool speechOn = true;
static private AutoResetEvent _quitEvent;
static void Main(string[] args)
{
try
{
_quitEvent = new AutoResetEvent(false);
ss.SetOutputToDefaultAudioDevice();
CultureInfo ci = new CultureInfo("en-us");
sre = new SpeechRecognitionEngine(ci);
sre.SetInputToDefaultAudioDevice();
sre.SpeechRecognized += sre_SpeechRecognized;
//sre.SpeechRecognized += SpeechRecognizedHandler;
Choices ch_StartStopCommands = new Choices();
ch_StartStopCommands.Add("quit");
GrammarBuilder gb_StartStop = new GrammarBuilder();
gb_StartStop.Append(ch_StartStopCommands);
Grammar g_StartStop = new Grammar(gb_StartStop);
sre.LoadGrammarAsync(g_StartStop);
sre.RecognizeAsync(RecognizeMode.Multiple);
Console.WriteLine("Listening...\n");
ss.SpeakAsync("I'm now listening.");
_quitEvent.WaitOne();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.ReadLine();
}
} // Main
static void SpeechRecognizedHandler(object sender, SpeechRecognizedEventArgs e)
{
if (e.Result == null) return;
string txt = e.Result.Text;
// Add event handler code here.
// The following code illustrates some of the information available
// in the recognition result.
Console.WriteLine("Grammar({0}), {1}: {2}",
e.Result.Grammar.Name, e.Result.Audio.Duration, e.Result.Text);
// Display the semantic values in the recognition result.
foreach (KeyValuePair<String, SemanticValue> child in e.Result.Semantics)
{
Console.WriteLine(" {0} key: {1}",
child.Key, child.Value.Value ?? "null");
}
Console.WriteLine();
// Display information about the words in the recognition result.
foreach (RecognizedWordUnit word in e.Result.Words)
{
RecognizedAudio audio = e.Result.GetAudioForWordRange(word, word);
Console.WriteLine(" {0,-10} {1,-10} {2,-10} {3} ({4})",
word.Text, word.LexicalForm, word.Pronunciation,
audio.Duration, word.DisplayAttributes);
}
// Display the recognition alternates for the result.
foreach (RecognizedPhrase phrase in e.Result.Alternates)
{
Console.WriteLine(" alt({0}) {1}", phrase.Confidence, phrase.Text);
}
}
static void sre_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
double minConfidence = 0.90;
string txt = e.Result.Text;
float confidence = e.Result.Confidence;
Console.WriteLine("\nRecognized: " + txt);
if (confidence < minConfidence)
{
Console.WriteLine($"Failed confidence: {minConfidence} with {confidence}" );
return;
}
if (txt.IndexOf("quit") >= 0)
{
if(speechOn)
ss.SpeakAsync("Shutting down.");
else
Console.WriteLine("Shutting down.");
Thread.Sleep(1000);
_quitEvent.Set();
}
} // sre_SpeechRecognized
} // Program
} // ns
Upvotes: 2
Views: 1088