Console Speech Recognition exits right after start - c#

I tried to follow this tutorial in building voice recognition C# app, the only difference is I wanted to have a Console app, not a Win Form app, so I wrote this this code:
using System;
using System.Speech.Recognition;
//using System.Speech.Synthesis;
namespace Voice_Recognation
{
class Program
{
static void Main(string[] args)
{
SpeechRecognitionEngine recEngine = new SpeechRecognitionEngine();
recEngine.SetInputToDefaultAudioDevice();
Choices commands = new Choices();
commands.Add(new string[] { "say Hi", "say Hello"});
GrammarBuilder gb = new GrammarBuilder();
gb.Append(commands);
Grammar g = new Grammar(gb);
recEngine.LoadGrammarAsync(g);
recEngine.RecognizeAsync(RecognizeMode.Multiple);
recEngine.SpeechRecognized += recEngine_SpeechRecognized;
}
// Create a simple handler for the SpeechRecognized event
static void recEngine_SpeechRecognized (object sender, SpeechRecognizedEventArgs e)
{
Console.WriteLine("Speech recognized: {0}", e.Result.Text);
switch(e.Result.Text){
case "Red":
Console.WriteLine("you said hi");
break;
default:
break;
}
}
}
}
and compiled it using mono project as below:
c:\mcs /reference:System.Speech.dll Program.cs
after adding the System.Speech.dll to the folder project, and got the Program.exe file generated.
Once I run the program at the terminal, it ends up directly, without giving me any chance to say anything!!
I've 2 questions:
What I'm missing here, and what the wrong thing I did?
and
How Is there a way to add the '.dll' file in a better way, I tried adding it to the Project.json file as below, but did not work, though I did not get any error at running dotnet restore:
"frameworks": {
"netcoreapp1.0": {
"bin": {
"assembly": "D:/2016/Speech/CORE/System.Speech.dll"
},
}
}

I solved the first part by adding Thread.Sleep so it give enough time for the thread to keep listening, another option is to make it endless loop using while(true);
I still could'not solve the second part which is how to make the VS code recognize the existing of the assembly file.
The new full code, if any is interested is below, and a more comprehensive code can be found here:
using System;
using System.Speech.Recognition;
using System.Threading;
//using System.Speech.Synthesis;
namespace Voice_Recognation
{
class Program
{
static void Main(string[] args)
{
SpeechRecognitionEngine recEngine = new SpeechRecognitionEngine();
recEngine.SetInputToDefaultAudioDevice();
Choices commands = new Choices();
commands.Add(new string[] { "say Hi", "say Hello"});
GrammarBuilder gb = new GrammarBuilder();
gb.Append(commands);
Grammar g = new Grammar(gb);
recEngine.LoadGrammarAsync(g);
recEngine.SpeechRecognized += recEngine_SpeechRecognized;
Console.WriteLine("Starting asynchronous recognition...");
recEngine.RecognizeAsync(RecognizeMode.Multiple);
// Wait 30 seconds, and then cancel asynchronous recognition.
Thread.Sleep(TimeSpan.FromSeconds(30));
// or
// while(true);
}
// Create a simple handler for the SpeechRecognized event
static void recEngine_SpeechRecognized (object sender, SpeechRecognizedEventArgs e)
{
Console.WriteLine("Speech recognized: {0}", e.Result.Text);
switch(e.Result.Text){
case "say Hello":
Console.WriteLine("you said hi");
break;
default:
break;
}
}
}
}

Related

Events not generated in this C# winform which uses azure services for converting speech to text

I have this c# winform which uses azure speech to text for converting speech into text. This winform has one checkbox for speech on/off and performs continuous speech recognition until I close the window . The problem is , when I check the checkbox only session started event is generated and nothing else happen , no other events like Recognizing, Recognized , canceled is generating . Do you know what is going wrong ?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.CognitiveServices.Speech;
namespace CsharpSTTform
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private async void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked) await SpeechContinuousRecognitionAsync();
}
public async Task SpeechContinuousRecognitionAsync()
{
// Creates an instance of a speech config with specified subscription key and service region.
// Replace with your own subscription key and service region (e.g., "westus").
var config = SpeechConfig.FromSubscription("api key", "westus");
// Creates a speech recognizer from microphone.
using (var recognizer = new SpeechRecognizer(config))
{
// Subscribes to events.
recognizer.Recognizing += (s, e) =>
{
Console.WriteLine($"RECOGNIZING: Text={e.Result.Text}");
};
recognizer.Recognized += (s, e) =>
{
var result = e.Result;
Console.WriteLine($"Reason: {result.Reason.ToString()}");
if (result.Reason == ResultReason.RecognizedSpeech)
{
Console.WriteLine($"Final result: Text: {result.Text}.");
}
};
recognizer.Canceled += (s, e) =>
{
Console.WriteLine($"\n Recognition Canceled. Reason: {e.Reason.ToString()}, CanceledReason: {e.Reason}");
};
recognizer.SessionStarted += (s, e) =>
{
Console.WriteLine("\n Session started event.");
};
recognizer.SessionStopped += (s, e) =>
{
Console.WriteLine("\n Session stopped event.");
};
// Starts continuous recognition. Uses StopContinuousRecognitionAsync() to stop recognition.
await recognizer.StartContinuousRecognitionAsync().ConfigureAwait(false);
}
}
}
}
when I check the checkbox the initial output I get is :
Session started event.
The thread 0x3274 has exited with code 0 (0x0).
After some time I got this output:
Session started event.
The thread 0x3274 has exited with code 0 (0x0).
The thread 0x5880 has exited with code 0 (0x0).
The thread 0x9e8 has exited with code 0 (0x0).
Your recognizer variable is a local variable of method SpeechContinuousRecognitionAsync, and it gets disposed right at the end of the using block, which is probably almost immediately.
If you change it to be an instance variable instead, and created without a using construct, then it will stay in memory.
You can then later Dispose() it if and when needed.
public partial class Form1 : Form
{
private SpeechRecognizer recognizer; // <-- instance variable
public async Task SpeechContinuousRecognitionAsync()
{
var config = SpeechConfig.FromSubscription("api key", "westus");
// Create a speech recognizer from microphone.
recognizer = new SpeechRecognizer(config);
recognizer.Recognizing += (s, e) =>
{
// ...
};
// ... rest of initialization
}
}

Microsoft Speech Platform speech to text

I'd like to write the speech a user says to text. Can I do this with the Microsoft Speech Platform? Perhaps I'm just misunderstanding how it's supposed to work and what its intended use case is.
I've got this console application now:
static void Main(string[] args)
{
Choices words = new Choices();
words.Add(new string[] { "test", "hello" ,"blah"});
GrammarBuilder gb = new GrammarBuilder();
gb.Append(words);
Grammar g = new Grammar(gb);
SpeechRecognitionEngine sre = new SpeechRecognitionEngine(new System.Globalization.CultureInfo("en-US"));
sre.LoadGrammar(g);
sre.SetInputToDefaultAudioDevice();
//add listeners
sre.Recognize();
Console.ReadLine();
}
And it only seems to output the words that I specify in Choices.
Would I have to add an entire dictionary of words if I wanted to match (most) of what a user will say?
Furthermore it stops right after it matches a single word. What if I wanted to capture entire sentences?
I'm looking for solutions for A) Capturing a wide array of words, and B) capturing more than one word at once.
Edit:
I found this: http://www.codeproject.com/Articles/483347/Speech-recognition-speech-to-text-text-to-speech-a#torecognizeallspeech
As seen in this page, the DictationGrammar class has a basic library of common words.
To capture more than one word at once I did
sre.RecognizeAsync(RecognizeMode.Multiple);
So my code is now this:
public static SpeechRecognitionEngine sre;
static void Main(string[] args)
{
sre = new SpeechRecognitionEngine(new System.Globalization.CultureInfo("en-US"));
sre.LoadGrammar(new Grammar(new GrammarBuilder("exit")));
sre.LoadGrammar(new DictationGrammar());
sre.SetInputToDefaultAudioDevice();
sre.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(sre_SpeechRecognized);
Console.ReadLine();
}
private static void sre_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
if (e.Result.Text == "exit")
{
sre.RecognizeAsyncStop();
}
Console.WriteLine("You said: " + e.Result.Text);
}

issue with vs2010 C# windows voice recognition

I have ran into an interesting issue with my voice recognition code for C#. I have had this code work before, but I migrated it to another project and it just wont work. I must be missing something, because there are no errors or warnings about the speech recognition and I do have the reference for speech. Here is the main function:
static void Main(string[] args)
{
Program prgm = new Program();
string[] argument = prgm.readConfigFile();
if(argument[2].ToLower().Contains("true"))
{
recognizer = new SpeechRecognitionEngine(new System.Globalization.CultureInfo("en-US"));
recognizer.LoadGrammar(new DictationGrammar());
recognizer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized);
recognizer.SetInputToDefaultAudioDevice();
recognizer.RecognizeAsync(RecognizeMode.Multiple);
}
prgm._con.updateConsole(argument, prgm._list);
}
static void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
Console.WriteLine(e.Result.Text);
}
along with the recognizer:
recognizer = new SpeechRecognitionEngine(new System.Globalization.CultureInfo("en-US"));
I did add the using System.Speech at the top of my code. When ever I start talking the event handler should start, but it never gets hit (checked with breakpoint). What am I doing wrong?

Problems with speech recognition

I am a starter who is stuck very badly on this initially my main aim is to control robots using speech. Initially I started with making grammar for my speech with this code I was even successful my code is this I made this in windows form application:
using System.Speech.Recognition;
using System.Windows.Forms;
using System.Threading;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// Create a new SpeechRecognizer instance.
sr = new SpeechRecognizer();
// Create a simple grammar that recognizes "red", "green", or "blue".
Choices colors = new Choices();
colors.Add("red");
colors.Add("green");
colors.Add("blue");
colors.Add("white");
GrammarBuilder gb = new GrammarBuilder();
gb.Append(colors);
// Create the actual Grammar instance, and then load it into the speech recognizer.
Grammar g = new Grammar(gb);
sr.LoadGrammar(g);
// Register a handler for the SpeechRecognized event.
sr.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(sr_SpeechRecognized);
}
// Simple handler for the SpeechRecognized event.
private void sr_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
MessageBox.Show(e.Result.Text);
}
private SpeechRecognizer sr;
}
Now from this code when I speak red , I get red in message box now I want to control motors therefore i need to communicate with my robots therefore i MADE ONE CONSOLE APPLICATION from help from internet FOR SENDING DATA TO MY SERVO CONTROLLER -SSC 32 THE CODE FOR ABOVE IS:
using System.IO.Ports;
using System.Threading;
namespace cConsoleAppMonitorServoCompletion
{
class Program
{
static SerialPort _serialPort;
static void Main(string[] args)
{
try
{
_serialPort = new SerialPort();
_serialPort.PortName = "COM3";
_serialPort.Open();
_serialPort.Write("#27 P1600 S750\r");
Console.WriteLine("#27 P1500 S750\r");
string output;
output = "";
//Example: "Q <cr>"
//This will return a "." if the previous move is complete, or a "+" if it is still in progress.
while (!(output == ".")) //loop until you get back a period
{
_serialPort.Write("Q \r");
output = _serialPort.ReadExisting();
Console.WriteLine(output);
Thread.Sleep(10);
}
_serialPort.Close();
}
catch (TimeoutException) { }
}
}
}
Now I want like when I speak red instead of giving a text box I want get serial command like _serialPort.Write("#27 P1600 S750\r");
Please help I have tried but I was not successful , it is my humble request please answer in more detailed manner , I am a just starter so it will be easy for me thanks in advance.
Controlling a robot using voice recognition... an ambitious project for a starter! There could be a million things going wrong here.
Just as important as the ability to write code is the ability to debug it. What can you tell us further - which parts work, which parts don't? Have you single-stepped through the code to see what happens and when, to diagnose where things start to go wrong?
You could also try some debugging output - Console.WriteLine for example - so we you can see the state of variables and flow of the code as it's running.
It looks like you need to use System.Diagnostics.Process.Start
This page has an example - how to execute console application from windows form?
// Simple handler for the SpeechRecognized event.
private void sr_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
System.Diagnostics.Process.Start( #"cmd.exe", #"/k c:\path\my.exe" );
}
An ambitious starter project indeed!
Update
private bool LaunchApp(String sAppPath, String sArg)
{
bool bSuccess = false;
try
{
//create a new process
Process myApp = new Process();
myApp.StartInfo.FileName = sAppPath;
myApp.StartInfo.Arguments = sArg;
bSuccess = myApp.Start();
}
catch (Win32Exception e)
{
MessageBox.Show("Error Details: {0}", e.Message);
}
return bSuccess;
}
if Now I want like when I speak red instead of giving a text box I want get serial command means - just to _serialPort.Write("#27 P1600 S750\r"); instead of showing messagebox (i.e. MessageBox.Show(e.Result.Text);) then task is really simple. just copy-paste that code. and add using System.IO.Ports; so that u can work with ports.
so prolly ur code will look like this:
private void sr_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
//MessageBox.Show(e.Result.Text);
try
{
_serialPort = new SerialPort();
_serialPort.PortName = "COM3";
_serialPort.Open();
_serialPort.Write("#27 P1600 S750\r");
Console.WriteLine("#27 P1500 S750\r");
string output;
output = "";
//Example: "Q <cr>"
//This will return a "." if the previous move is complete, or a "+" if it is still in progress.
while (!(output == ".")) //loop until you get back a period
{
_serialPort.Write("Q \r");
output = _serialPort.ReadExisting();
Console.WriteLine(output);
Thread.Sleep(10);
}
_serialPort.Close();
}
catch (TimeoutException) { }
}
p.s.
if you don't understand how SerialPort Class works go to MSDN

Programmatically turn off the automation features of windows speech recognition?

I'm making a program that uses the system.speech namespace (it's a simple program that will launch movies). I load all of the filenames from a folder and add them to the grammars I want to use. It's working remarkably well, however there is a hitch: I DON'T want the windows speech recognition to interact with windows at all (ie. when I say start, I don't want the start menu to open... I don't want anything to happen).
Likewise, I have a listbox for the moment that lists all of the movies found in the directory. When I say the show/movie that I want to open, the program isn't recognizing that the name was said because windows speech recognition is selecting the listboxitem from the list instead of passing that to my program.
The recognition is working otherwise, because I have words like "stop", "play", "rewind" in the grammar, and when I catch listener_SpeechRecognized, it will correctly know the word(s)/phrase that I'm saying (and currently just type it in a textbox).
Any idea how I might be able to do this?
I'd use the SpeechRecognitionEngine class rather than the SpeechRecognizer class. This creates a speech recognizer that is completely disconnected from Windows Speech Recognition.
private bool Status = false;
SpeechRecognitionEngine sre = new SpeechRecognitionEngine();
Choices dic = new Choices(new String[] {
"word1",
"word2",
});
public Form1()
{
InitializeComponent();
Grammar gmr = new Grammar(new GrammarBuilder(dic));
gmr.Name = "myGMR";
// My Dic
sre.LoadGrammar(gmr);
sre.SpeechRecognized +=
new EventHandler<SpeechRecognizedEventArgs>(sre_SpeechRecognized);
sre.SetInputToDefaultAudioDevice();
sre.RecognizeAsync(RecognizeMode.Multiple);
}
private void button1_Click(object sender, EventArgs e)
{
if (Status)
{
button1.Text = "START";
Status = false;
stslable.Text = "Stopped";
}
else {
button1.Text = "STOP";
Status = true;
stslable.Text = "Started";
}
}
public void sre_SpeechRecognized(object sender, SpeechRecognizedEventArgs ev)
{
String theText = ev.Result.Text;
MessageBox.Show(theText);
}

Categories