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);
}
Related
I can't search in google using my voice. I disabled search by default and can only be enabled by saying the word "search". I separated the words and phrases to be searched for to another file so that it wont get mixed with my recognition file and the response file. I placed the path file under the if statement of "search" so that it can only be access when the I speak "search"
public partial class Form1 : Form
{
//user and jarvis texts
string[] grammarFile = (File.ReadAllLines(#"C:\Friday AI\user.txt.txt"));
string[] responseFile = (File.ReadAllLines(#"C:\Friday AI\jarvis.txt.txt"));
//speech synthesis
SpeechSynthesizer speechSynth = new SpeechSynthesizer();
//speech recognition
Choices grammarList = new Choices();
SpeechRecognitionEngine speechRecognition = new SpeechRecognitionEngine();
public Boolean search = false;
public Form1()
{
//initialize grammarfile
grammarList.Add(grammarFile);
Grammar grammar = new Grammar(new GrammarBuilder(grammarList));
try
{
speechRecognition.RequestRecognizerUpdate();
speechRecognition.LoadGrammar(grammar);
speechRecognition.SpeechRecognized += rec_SpeechRecognized;
speechRecognition.SetInputToDefaultAudioDevice();
speechRecognition.RecognizeAsync(RecognizeMode.Multiple);
}
catch
{
return;
}
private void rec_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
string result = e.Result.Text;
int resp = Array.IndexOf(grammarFile, result);
if (search)
{
Process.Start(#"chrome.exe", "--incognito https://www.google.com/search?q=" + result);
label5.Text = "Enabled";
}
if (wake == true)
{
if (result.Contains("search"))
{
search = true;
string[] globalsearch = (File.ReadAllLines(#"C:\Friday AI\global search words.txt"));
grammarList.Add(globalsearch);
label5.Text = "Enabled";
}
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?
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;
}
}
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.
I tried implementing a tutorial for Microsoft Speech Recognition. I get no errors but still the voice is not recognized. The Code is like
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Speech.Recognition;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
//SpeechRecognizer recognizer;
SpeechRecognitionEngine sre;
public Form1()
{
InitializeComponent();
sre = new SpeechRecognitionEngine();
sre.SetInputToWaveFile(#"c:\Test\Colors.wav");
Console.WriteLine("here");
// Create a simple grammar that recognizes "red", "green", or "blue".
Choices colors = new Choices();
colors.Add(new string[] { "red", "green", "blue" });
Console.WriteLine("here");
// Create a GrammarBuilder object and append the Choices object.
GrammarBuilder gb = new GrammarBuilder();
gb.Append(colors);
// Create the Grammar instance and load it into the speech recognition engine.
Grammar g = new Grammar(gb);
sre.LoadGrammar(g);
// Register a handler for the SpeechRecognized event.
// Start recognition.
sre.SpeechRecognized +=
new EventHandler<SpeechRecognizedEventArgs>(sre_SpeechRecognized);
sre.Recognize();
Console.WriteLine("here");
}
private void Form1_Load(object sender, EventArgs e)
{
}
// Create a simple handler for the SpeechRecognized event.
void sre_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
Console.WriteLine("here");
MessageBox.Show("Speech recognized: " + e.Result.Text);
}
}
}
Kindly help me sort out !! I have no idea why it is not working and am new to C# and Visual Studio
PS: I also get messages in the output window like the following While running the program
The thread '<No Name>' (0x674) has exited with code 0 (0x0).
The thread '<No Name>' (0x1ee0) has exited with code 0 (0x0).
The thread '<No Name>' (0xf8) has exited with code 0 (0x0).
The thread '<No Name>' (0x760) has exited with code 0 (0x0).
The thread 'vshost.RunParkingWindow' (0x1184) has exited with code 0 (0x0).
Try this code works for me
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 grammer with the commands each in single line like below
Notepad
Close
Minimize
Maximize
Open
Hello