Reputation: 1693
Im trying to get the dictation recognition to be a bit more accurate. I've loaded the dictation grammar and have been trying to read through all the documentation i can find online but can't seem to find a way to expand on the list of recognised words.
Currently compared to the Google speech api the default recognition is atrocious so am looking at ways of improving on the default but am at a loss as where to start.
This isn't about specific word recognition but more generic speech to text hence why I've not gone down the route of just building a custom word grammar.
Thanks
Si
Update: added basic code below which I took from an example I found:
static void Main(string[] args)
{
SpeechRecognitionEngine sre = new SpeechRecognitionEngine(new System.Globalization.CultureInfo("en-GB"));
Grammar gr = new DictationGrammar();
sre.LoadGrammar(gr);
sre.SetInputToWaveFile("C:\\AudioStuff\\file.wav");
sre.BabbleTimeout = new TimeSpan(Int32.MaxValue);
sre.InitialSilenceTimeout = new TimeSpan(Int32.MaxValue);
sre.EndSilenceTimeout = new TimeSpan(100000000);
sre.EndSilenceTimeoutAmbiguous = new TimeSpan(100000000);
StringBuilder sb = new StringBuilder();
while (true)
{
try
{
var recText = sre.Recognize();
if (recText == null)
{
break;
}
sb.Append(recText.Text);
}
catch (Exception ex)
{
//handle exception
//...
break;
}
}
Console.WriteLine(sb.ToString());
Console.WriteLine();
Console.WriteLine("Hit any key to exit");
Console.ReadLine();
}
So the following works:
SpLexicon lex = new SpLexicon();
int langid = new System.Globalization.CultureInfo("en-US").LCID;
lex.AddPronunciation("Katie", langid, SpeechPartOfSpeech.SPSNoun, "k ey t iy")
but this doesn't:
SpLexicon lex = new SpLexicon();
int langid = new System.Globalization.CultureInfo("en-GB").LCID;
lex.AddPronunciation("Katie", langid, SpeechPartOfSpeech.SPSNoun, "k ey t iy")
:-(
Upvotes: 1
Views: 1754
Reputation: 2283
I hope i got your question right. Here is what i used. Using existing things and adding to them.
public partial class MainWindow : Window
{
SpeechRecognitionEngine _recognizer;
SpeechSynthesizer sre = new SpeechSynthesizer();
int count = 1;
public MainWindow()
{
InitializeComponent();
Initialize();
}
private void Initialize()
{
try
{
var culture = new CultureInfo("en-US");
_recognizer = new SpeechRecognitionEngine(culture);
_recognizer.SetInputToDefaultAudioDevice();
_recognizer.LoadGrammar(GetGrammer());
_recognizer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(_recognizer_SpeechRecognized);
_recognizer.RecognizeAsync(RecognizeMode.Multiple);
sre.SelectVoiceByHints(VoiceGender.Male, VoiceAge.Child);
sre.Rate = -2;
}
catch (Exception ex)
{
System.Windows.MessageBox.Show(ex.InnerException.Message);
}
}
private static Grammar GetGrammer()
{
var choices = new Choices();
//add custom commands
choices.Add(File.ReadAllLines(@"Commands.txt"));
//to add the letters to the dictionary
choices.Add(Enum.GetNames(typeof(Keys)).ToArray());
var grammer = new Grammar(new GrammarBuilder(choices));
return grammer;
}
void _recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
string speech = e.Result.Text;
//to type letters in open application like notepad
if (Enum.GetNames(typeof(Keys)).Contains(speech))
{
try
{ //send the string to the application
SendKeys.SendWait("{" + speech + "}");
}
catch (ArgumentException)
{
}
}
//handle custom commands
switch (speech)
{
case "Hello":
sre.Speak("Goodmorning ");
break;
case "Notepad":
System.Diagnostics.Process.Start("Notepad");
break;
case "Maximize":
this.WindowState = System.Windows.WindowState.Maximized;
break;
case "Minimize":
this.WindowState = System.Windows.WindowState.Minimized;
break;
case "Restore":
this.WindowState = System.Windows.WindowState.Normal;
break;
case "Close":
Close();
break;
}
}
}
You would also need to create a .txt file to load the grammar with the commands each in single line like below
Notepad
Close
Minimize
Maximize
Open
Hello
I presume you are trying to enrich the existing speech recognizer. If not, please clarify my understanding. Thanks and Cheers.
Upvotes: 1