Dictation is not working on my WPF application - need suggestion - c#

Trying to use free dictated words for voice recognition. But it is not working for me. Maybe someone can have a look.
Here it starts :
public MainWindow()
{
InitializeComponent();
recognizer = new SpeechRecognitionEngine();
freeTextDictation();
}
Here is the logic:
private void freeTextDictation()
{
GrammarBuilder startStop = new GrammarBuilder();
GrammarBuilder dictation = new GrammarBuilder();
dictation.AppendDictation();
startStop.Append(new SemanticResultKey("StartDictation", new SemanticResultValue("Start Dictation", true)));
startStop.Append(new SemanticResultKey("DictationInput", dictation));
startStop.Append(new SemanticResultKey("StopDictation", new SemanticResultValue("Stop Dictation", false)));
Grammar grammar = new Grammar(startStop);
grammar.Enabled = true;
grammar.Name = " Free-Text Dictation ";
recognizer.LoadGrammar(grammar);
recognizer.SetInputToDefaultAudioDevice();
recognizer.SpeechRecognized += Recognizer_SpeechRecognized;
}
private void Recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
//MessageBox.Show(e.Result.Text.ToString());
Console.WriteLine(e.Result.Text.ToString());
}
The code is from microsoft website but I cant integrate it to get output.
Unfortunately nothing happens.
What can I do?

Related

SAPI does not implement phonetic alphabet selection. Speech Command App

In my speech command app, I am loading files from an external source, processing that data and loading them into a list of possible commands for execution
When I run the app, I get the message in the console
Main.exe Information: 0: SAPI does not implement phonetic alphabet selection.
I tried solutions such as adding
gram.Culture = New System.Globalization.CultureInfo("en-GB")
I think this is either outdated or does not work on this type of WPF application
Any tips?
Code:
{
InitializeComponent();
}
SpeechSynthesizer synth = new SpeechSynthesizer();
PromptBuilder builder = new PromptBuilder();
SpeechRecognitionEngine recog = new SpeechRecognitionEngine();
private System.Windows.Input.Key k;
private int vkey;
private Choices cmd;
private void Button_Click_1(object sender, RoutedEventArgs e)
{
b1.IsEnabled = false;
Choices list = new Choices();
cmd = Singleton.getInstance().getChoices();
list.Add(new string[] { "hello", "test", "it works", "sup", "windows", "grenade" });
Grammar gr = new Grammar(new GrammarBuilder(cmd));
try
{
recog.RequestRecognizerUpdate();
recog.LoadGrammar(gr);
recog.SpeechRecognized += Recog_SpeechRecognized;
recog.SetInputToDefaultAudioDevice();
recog.RecognizeAsync(RecognizeMode.Multiple);
}
catch
{
return;
}
}

Detecting specific phrase to start recognition for command

I am currently messing around with the System.Speech to make a Amazon Alexa type program where you will ask it a command and will speak back an answer.
Right now I have it set up to where I click a button so it starts detecting audio input from the microphone, but I want to do the Alexa style or Google style ("Alexa" or "Hey Google").
Here is what I have so far:
public partial class SpeechRecognition : Form
{
SpeechRecognitionEngine recEngine = new SpeechRecognitionEngine();
SpeechSynthesizer synth = new SpeechSynthesizer();
public SpeechRecognition()
{
InitializeComponent();
}
private void SpeechRecognition_Load(object sender, EventArgs e)
{
Choices command = new Choices();
command.Add(new string[] { "Say Hello", "Print my name", "speak selected text", "time", "current weather", "humidity"});
GrammarBuilder gBuilder = new GrammarBuilder();
gBuilder.Append(command);
Grammar grammar = new Grammar(gBuilder);
recEngine.LoadGrammarAsync(grammar);
recEngine.SetInputToDefaultAudioDevice();
recEngine.SpeechRecognized += recEngine_SpeechRecognized;
}
void recEngine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
synth.SelectVoiceByHints(VoiceGender.Neutral);
var request = new ForecastIORequest("...", 38.47382f, -76.50636f, DateTime.Now, Unit.us);
var response = request.Get();
switch (e.Result.Text)
{
case "Say Hello":
PromptBuilder builder = new PromptBuilder();
builder.StartSentence();
builder.AppendText("Hello Zack");
builder.EndSentence();
builder.AppendBreak(new TimeSpan(0, 0, 0, 0,50));
builder.StartSentence();
builder.AppendText("How are you?");
builder.EndSentence();
synth.SpeakAsync(builder);
break;
case "Print my name":
richTextBox1.Text += "\nZack";
break;
case "speak selected text":
synth.SpeakAsync(richTextBox1.SelectedText);
break;
case "time":
DateTime time = DateTime.Now;
synth.SpeakAsync(time.ToShortTimeString());
break;
case "current weather":
response = request.Get();
synth.SpeakAsync("The weather in St. Leonard is " + response.currently.summary + "with a temperature of " + Math.Round((decimal)response.currently.temperature, 0) + " degrees farenheit");
break;
case "humidity":
response = request.Get();
synth.SpeakAsync("Current humidity is " + 100 * response.currently.humidity + "percent");
break;
default:
synth.SpeakAsync("I did not recognize a command");
break;
}
}
private void enableBtn_Click(object sender, EventArgs e)
{
recEngine.RecognizeAsync(RecognizeMode.Multiple);
disableBtn.Enabled = true;
}
private void disableBtn_Click(object sender, EventArgs e)
{
recEngine.RecognizeAsyncStop();
disableBtn.Enabled = false;
}
How would I alter this so that it constantly listens for that one phrase then it will listen for a command after that? I'm guessing I need to create another instance of the recognition engine? Any help would be great.

C# Voice Command: Wait for input

I have a Windows Form app that recognizes voice commands and then performs the action accordingly. However, I can't figure out how to speak one command after the other.
Code:
if (e.Result.Text == "initiate power saving mode")
{
Taskbar taskbar = new Taskbar();
taskbar.Show();
SoundPlayer deacr = new SoundPlayer(Properties.Resources.deacr);
deacr.PlaySync();
if (e.Result.Text== "confirm")
{
SoundPlayer deacd = new SoundPlayer(Properties.Resources.deacd);
deacd.PlaySync();
Application.SetSuspendState(PowerState.Suspend, true, true);
}
else if (e.Result.Text == "cancel")
{
SoundPlayer cancelled = new SoundPlayer(Properties.Resources.cancelled);
cancelled.PlaySync();
}
}
Am I missing something, or just doing something wrong?
You need to use System.Speech.This is how i do it on my system. You can do the following:
using System.Speech.Synthesis;
using System.Speech.Recognition;
namespace Alexis
{
public partial class frmMain : Form
{
SpeechRecognitionEngine _recognizer = new SpeechRecognitionEngine();
SpeechSynthesizer Alexis = new SpeechSynthesizer();
SpeechRecognitionEngine startlistening = new SpeechRecognitionEngine();
}
// ...
}
then in the main form
private void frmMain_Load(object sender, EventArgs e)
{
_recognizer.SetInputToDefaultAudioDevice();
_recognizer.LoadGrammarAsync(new Grammar(new GrammarBuilder(new Choices(File.ReadAllLines(#"Default Commands.txt")))));
_recognizer.SpeechDetected += new EventHandler<SpeechDetectedEventArgs>(_recognizer_SpeechDetected);
_recognizer.RecognizeAsync(RecognizeMode.Multiple);
startlistening.SetInputToDefaultAudioDevice();
startlistening.LoadGrammarAsync(new Grammar(new GrammarBuilder(new Choices("alexis"))));
startlistening.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(startlistening_SpeechRecognized);
}
then do your coding. Now in order to call the commands you need to create a txt document. and put in the commands one line at a time (do not leave any open lines).
_recognizer.LoadGrammarAsync(new Grammar(new GrammarBuilder(new Choices(File.ReadAllLines(#"Default Commands.txt")))));
will trigger the commands, so that way you can call the commands. Also i would not use if (e.Result.Text == "initiate power saving mode") i would use it this way if (speech == "initiate power saving mode")
so if you wanted to continue you could do this
if (speech == "initiate power saving mode")
{
Taskbar taskbar = new Taskbar();
taskbar.Show();
SoundPlayer deacr = new SoundPlayer(Properties.Resources.deacr);
deacr.PlaySync();
}
else if (speech== "confirm")
{
SoundPlayer deacd = new SoundPlayer(Properties.Resources.deacd);
deacd.PlaySync();
Application.SetSuspendState(PowerState.Suspend, true, true);
}
else if (speech == "cancel")
{
SoundPlayer cancelled = new SoundPlayer(Properties.Resources.cancelled);
cancelled.PlaySync();
}
be sure to put the command "initiate power saving mode" and "cancel" in the commands txt document (case sensitive).
then if you wanted to keep releases down then you can create a tabbed form and add your own custom commands. Hope this helps. But remember this is an example for you to go by.

Speech Recognition windows 10

I'm trying to build a speech recognition in Windows 10 (using Cortana) in Visual C#.
This is part of my code for speech recognition using old System.Speech.Recognition and works great, but it only support english.
SpeechSynthesizer sSynth = new SpeechSynthesizer();
PromptBuilder pBuilder = new PromptBuilder();
SpeechRecognitionEngine sRecognize = new SpeechRecognitionEngine();
Choices sList = new Choices();
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
pBuilder.ClearContent();
pBuilder.AppendText(textBox2.Text);
sSynth.Speak(pBuilder);
}
private void button2_Click(object sender, EventArgs e)
{
button2.Enabled = false;
button3.Enabled = true;
sList.Add(new string[] { "who are you", "play a song" });
Grammar gr = new Grammar(new GrammarBuilder(sList));
try
{
sRecognize.RequestRecognizerUpdate();
sRecognize.LoadGrammar(gr);
sRecognize.SpeechRecognized += sRecognize_SpeechRecognized;
sRecognize.SetInputToDefaultAudioDevice();
sRecognize.RecognizeAsync(RecognizeMode.Multiple);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error");
}
}
private void sRecognize_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
textBox1.Text = textBox1.Text + " " + e.Result.Text.ToString() + "\r\n";
}
How can I do it using new speech recognition in windows 10?
Use Microsoft Speech Platform SDK v11.0 (Microsoft.Speech.Recognition).
It works like System.Speech, but you can use Italian language (separeted install) and also use SRGS Grammar. I work with both kinect (SetInputToAudioStream) and default input device (SetInputToDefaultAudioDevice) without hassle.
Also it works offline, so no need to be online as with Cortana.
With the SRGS grammar you can get a decent level of complexity for your commands
UPDATE
Here is how I initialize the recognizer
private RecognizerInfo GetRecognizer(string culture, string recognizerId)
{
try
{
foreach (var recognizer in SpeechRecognitionEngine.InstalledRecognizers())
{
if (!culture.Equals(recognizer.Culture.Name, StringComparison.OrdinalIgnoreCase)) continue;
if (!string.IsNullOrWhiteSpace(recognizerId))
{
string value;
recognizer.AdditionalInfo.TryGetValue(recognizerId, out value);
if ("true".Equals(value, StringComparison.OrdinalIgnoreCase))
return recognizer;
}
else
return recognizer;
}
}
catch (Exception e)
{
log.Error(m => m("Recognizer not found"), e);
}
return null;
}
private void InitializeSpeechRecognizer(string culture, string recognizerId, Func<Stream> audioStream)
{
log.Debug(x => x("Initializing SpeechRecognizer..."));
try
{
var recognizerInfo = GetRecognizer(culture, recognizerId);
if (recognizerInfo != null)
{
recognizer = new SpeechRecognitionEngine(recognizerInfo.Id);
//recognizer.LoadGrammar(VoiceCommands.GetCommandsGrammar(recognizerInfo.Culture));
recognizer.LoadGrammar(grammar);
recognizer.SpeechRecognized += SpeechRecognized;
recognizer.SpeechRecognitionRejected += SpeechRejected;
if (audioStream == null)
{
log.Debug(x => x("...input on DefaultAudioDevice"));
recognizer.SetInputToDefaultAudioDevice();
}
else
{
log.Debug(x => x("SpeechRecognizer input on CustomAudioStream"));
recognizer.SetInputToAudioStream(audioStream(), new SpeechAudioFormatInfo(EncodingFormat.Pcm, 16000, 16, 1, 32000, 2, null));
}
}
else
{
log.Error(x => x(Properties.Resources.SpeechRecognizerNotFound, recognizerId));
throw new Exception(string.Format(Properties.Resources.SpeechRecognizerNotFound, recognizerId));
}
log.Debug(x => x("...complete"));
}
catch (Exception e)
{
log.Error(m => m("Error while initializing SpeechEngine"), e);
throw;
}
}
Cortana API usage example is here. You can copy it and start modifying according to your needs. It creates a dialog with the user. You can not exactly reproduce your System.Speech code with Cortana API because it is designed for another purpose. If you still want to recognize just few words, you can continue using System.Speech API.
System.Speech API supports other languages, not just English. You can find more information here:
Change the language of Speech Recognition Engine library

Speech recognition not working

I am developing a WPF application which uses speech recognition. The events does not fire up when the grammar words are spoken. Secondly, I am not sure whether the engine starts up on not. How to check that? Following is the code.
namespace Summerproject_trial
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private SpeechRecognitionEngine recEngine =
new SpeechRecognitionEngine();
public MainWindow()
{
InitializeComponent();
Choices mychoices = new Choices();
mychoices.Add(new string[] {"Ok", "Test", "Hello"});
GrammarBuilder gb = new GrammarBuilder();
gb.Append(mychoices);
Grammar mygrammar = new Grammar(gb);
recEngine.LoadGrammarAsync(mygrammar);
recEngine.SpeechRecognized +=
new EventHandler<SpeechRecognizedEventArgs>
(recEngine_SpeechRecognized);
recEngine.SetInputToDefaultAudioDevice();
}
void recEngine_SpeechRecognized(object sender,
SpeechRecognizedEventArgs e)
{
MessageBox.Show("You said: " + e.Result.Text);
}
}
}
You forgot to start listening to input.
Try this in the end of your constructor.
recEngine.RecognizeAsync(RecognizeMode.Multiple);
#Anri's answer is needed, but you also need to create the SpeechRecognitionEngine with a CultureInfo. (You can create a SpeechRecognitionEngine without a CultureInfo, but then you need to set the recognizer language explicitly.)
Also: Mobile earphones (by which I assume you mean some sort of Bluetooth headset) will typically NOT work with System.Speech. The SR engine used in the desktop SR engine requires higher quality audio input than it can get from Bluetooth.
So, complete code that should work:
private SpeechRecognitionEngine recEngine =
new SpeechRecognitionEngine("en-US");
public MainWindow()
{
InitializeComponent();
Choices mychoices = new Choices();
mychoices.Add(new string[] {"Ok", "Test", "Hello"});
GrammarBuilder gb = new GrammarBuilder();
gb.Append(mychoices);
Grammar mygrammar = new Grammar(gb);
recEngine.LoadGrammarAsync(mygrammar);
recEngine.SpeechRecognized +=
new EventHandler<SpeechRecognizedEventArgs>
(recEngine_SpeechRecognized);
recEngine.SetInputToDefaultAudioDevice();
recEngine.RecognizeAsync(RecognizeMode.Multiple);
}

Categories

Resources