So I've made the application recognize what I say. But how do I make the application confirm a request when I command it to carry out a task?
As of now I have this code:
public partial class Form1 : Form
{
SpeechSynthesizer synth = new SpeechSynthesizer();
SpeechRecognitionEngine sRecognize= new SpeechRecognitionEngine();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Choices sList = new Choices();
sList.Add(new String[] { "Exit"});
Grammar gr = new Grammar(new GrammarBuilder(sList));
sRecognize.RequestRecognizerUpdate();
sRecognize.LoadGrammar(gr);
sRecognize.SpeechRecognized += sRecognize_SpeechRecognized;
sRecognize.SetInputToDefaultAudioDevice();
sRecognize.RecognizeAsync(RecognizeMode.Multiple);
sRecognize.SpeechRecognitionRejected += sRecognize_SpeechRecognitionRejected;
}
private void sRecognize_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
if (e.Result.Text == "Exit")
{
Application.Exit();
}
}
}
So my question as an example:
I say, "Exit"
Application confirms by:
Are you sure you want to exit?
And depending on my answer, the application responds.
Yes being a confirmation and No being a request cancellation. What changes do I have to make?
Something like this?
private void sRecognize_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
if (e.Result.Text == "Exit")
{
Choices sList = new Choices();
sList.Add(new String[] { "Yes"});
Grammar gr = new Grammar(new GrammarBuilder(sList));
sRecognize.RequestRecognizerUpdate();
sRecognize.LoadGrammar(gr);
sRecognize.SpeechRecognized += delegate(object sender, SpeechRecognizedEventArgs e)
{
Application.Exit();
};
sRecognize.SetInputToDefaultAudioDevice();
sRecognize.RecognizeAsync(RecognizeMode.Multiple);
}
}
Related
I'm creating a speech recognizer and i have to put the SpeechRecognizer EventArgs variable but I want what I say to accumulate in a text box and when I stop and start over, it so not be replaced
my code:
private void button2_Click_1(object sender, EventArgs e)
{
engine_load();
}
private void engine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
textBox1.Multiline = true;
textBox1.Text = e.Result.Text + Environment.NewLine;
}
private void engine_load()
{
engine = new SpeechRecognitionEngine();
engine.SetInputToDefaultAudioDevice();
Grammar g = new DictationGrammar();
engine.LoadGrammar(g);
engine.RecognizeAsync(RecognizeMode.Multiple);
engine.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(engine_SpeechRecognized);
Console.ReadLine();
}
Maybe you can declare a global variable text to hold the previously entered string.
Please refer to the following code.
string text;
private void engine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
textBox1.Multiline = true;
text += e.Result.Text + Environment.NewLine;
textBox1.Text = text;
}
I'm in making barcode scanner but "VideoCaptureDevice can't pick up any objects" ?
I'm using AForge video and ZXing in visual studio 2019 C#
I want it to be able to scan barcodes from my laptop webcam.
I tried running it with the problem but in my combobox where are supposed to be cameras available for use but they are none. I even tried activating the webcam before opening up the program.
code:
FilterInfoCollection FilterInfoCollection;
VideoCaptureDevice VideoCaptureDevice;
private void Form1_Load(object sender, EventArgs e)
{
FilterInfoCollection = new FilterInfoCollection(FilterCategory.VideoInputDevice);
foreach (FilterInfo device in FilterInfoCollection)
comboBox1.Items.Add(device.Name);
comboBox1.SelectedIndex = 0;
}
private void button1_Click(object sender, EventArgs e)
{
VideoCaptureDevice = new VideoCaptureDevice(FilterInfoCollection[comboBox1.SelectedIndex].MonikerString);
VideoCaptureDevice.NewFrame += VideoCaptureDevice_NewFrame;
VideoCaptureDevice.Start();
}
private void VideoCaptureDevice_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
Bitmap bitmap = (Bitmap)eventArgs.Frame.Clone();
BarcodeReader reader = new BarcodeReader();
var result = reader.Decode(bitmap);
if (result != null)
{
textBox1.Invoke(new MethodInvoker(delegate ()
{
textBox1.Text = result.ToString();
}));
}
pictureBox1.Image = bitmap;
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (VideoCaptureDevice != null)
{
if (VideoCaptureDevice.IsRunning)
VideoCaptureDevice.Stop();
}
}
}
}
I have been trying to experiment with the system.speech feature I have seen various online videos and web articles teaching how to properly use it, but I can't get it to work somehow. I get no errors, the program compiles as it should but when I speak nothing happens, I have tried changing my language to en-UK and back to en-US but it did nothing. I am using VS17 and the code is as follows:
SpeechRecognitionEngine recEngine = new SpeechRecognitionEngine(new CultureInfo("en-US"));
public Form1()
{
InitializeComponent();
this.TransparencyKey = (BackColor);
this.StartPosition = FormStartPosition.Manual;
this.Location = new Point(Convert.ToInt32(0.10), 300);
textBox1.Visible = false;
}
private void Form1_Load(object sender, EventArgs e)
{
Choices commands = new Choices();
commands.Add(new string[] { "hello" });
GrammarBuilder gr = new GrammarBuilder();
gr.Append(commands);
Grammar grammar = new Grammar(gr);
recEngine.LoadGrammarAsync(grammar);
recEngine.SetInputToDefaultAudioDevice();
recEngine.SpeechRecognized += RecEngine_SpeechRecognized;
}
private void RecEngine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
switch (e.Result.Text)
{
case "hello":
MessageBox.Show("Hello");
break;
}
}
private void label1_Click(object sender, EventArgs e)
{
}
private void pictureBox2_Click(object sender, EventArgs e)
{
recEngine.RecognizeAsyncStop();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
void button1_Click(object sender, EventArgs e)
{
recEngine.RecognizeAsync(RecognizeMode.Multiple);
}
}
Edit: I downloaded and tested the program with the same code below on 2 different computers and it works fine with all of them except this one. I tried to use the mic of both computers which recognized my speech. But none of them worked, so the problem lies within my PC after all, I might have to download some windows update with the speech feature or something like that. Where can I find it though?
Apparently running as an admin fixed the issue.
Somehow this thought never even reached my mind.
I created a C# project using speech recognition where I have a form that has a next and last button What I am trying to do is when I say next the button will take me to the next file or if I say back it will go to the previous file. But When debug the project it only shows me what I say instead of doing it. Does anyone know how can I Fix it?
This is the code I made:
private void Form1_Load(object sender, EventArgs e)
{
SpeechRecognizer recognizer = new SpeechRecognizer();
Choices command = new Choices();
command.Add(new string[] { "next", "last", "first" });
GrammarBuilder gb = new GrammarBuilder();
gb.Append(command);
Grammar g = new Grammar(gb);
recognizer.LoadGrammar(g);
recognizer.SpeechRecognized +=
new EventHandler<SpeechRecognizedEventArgs>(sre_SpeechRecognized);
}
void sre_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
MessageBox.Show("Speech recognized: " + e.Result.Text);
}
}
A couple of years ago I had a case study for this subject. If you compare my codes with yours you can find out something. The code below changes a light bulb's status.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Speech.Recognition;
using System.Threading;
namespace SesTanima
{
public partial class Form1 : Form
{
private SpeechRecognitionEngine recognizer = new SpeechRecognitionEngine();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
LoadGrammars();
StartRecognition();
}
private void LoadGrammars()
{
Choices choices = new Choices( new string[] {"Lights on", "Exit", "Zoom out", "Zoom in", "Reset", "Lights off" } );
GrammarBuilder grammarBuilder = new GrammarBuilder(choices);
Grammar grammar = new Grammar(grammarBuilder);
recognizer.LoadGrammar(grammar);
}
private void StartRecognition()
{
recognizer.SpeechDetected += new EventHandler<SpeechDetectedEventArgs>(recognizer_SpeechDetected);
recognizer.SpeechRecognitionRejected += new EventHandler<SpeechRecognitionRejectedEventArgs>(recognizer_SpeechRecognitionRejected);
recognizer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized);
recognizer.RecognizeCompleted += new EventHandler<RecognizeCompletedEventArgs>(recognizer_RecognizeCompleted);
Thread t1 = new Thread(delegate()
{
recognizer.SetInputToDefaultAudioDevice();
recognizer.RecognizeAsync(RecognizeMode.Single);
});
t1.Start();
}
private void recognizer_SpeechDetected(object sender, SpeechDetectedEventArgs e)
{
textBox1.Text = "Recognizing voice command...";
}
private void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
if (e.Result.Text == "Lights on")
{
pictureBox1.Image = Properties.Resources.lightsOn;
}
else if (e.Result.Text == "Lights off")
{
pictureBox1.Image = Properties.Resources.lightsOff;
}
else if ( e.Result.Text == "Exit" )
{
recognizer.Dispose();
Application.Exit();
}
else if ( e.Result.Text == "Zoom out" )
{
pictureBox1.Size = new System.Drawing.Size( 135, 107 );
}
else if ( e.Result.Text == "Zoom in" )
{
pictureBox1.Size = new System.Drawing.Size( 538, 426 );
}
else if ( e.Result.Text == "Reset" )
{
pictureBox1.Size = new System.Drawing.Size( 269, 213 );
}
textBox1.Text = e.Result.Text;
}
private void recognizer_SpeechRecognitionRejected(object sender, SpeechRecognitionRejectedEventArgs e)
{
textBox1.Text = "Failure.";
}
private void recognizer_RecognizeCompleted(object sender, RecognizeCompletedEventArgs e)
{
recognizer.RecognizeAsync();
}
}
}
I have need to write an application which uses a speech recognition engine.
How can I enter different values in multiple textboxes through voice in c#?
I can enter value in single textbox but not in second textbox. I have the following code for entering value in single textbox.
private SpeechRecognitionEngine rec;
private void voice()
{
rec = new SpeechRecognitionEngine();
rec.SetInputToDefaultAudioDevice();
Choices choice = new Choices("apple","Orange","Onion");
GrammarBuilder gr = new GrammarBuilder(choice);
Grammar grammar = new Grammar(gr);
rec.LoadGrammar(grammar);
rec.SpeechRecognized +=
new EventHandler<SpeechRecognizedEventArgs>(rec_SpeechRecogonized);
rec.RecognizeAsync(RecognizeMode.Multiple);
}
void rec_SpeechRecogonized(object sender, SpeechRecognizedEventArgs e)
{
foreach (RecognizedWordUnit word in e.Result.Words)
{
textBox1.Text = word.Text;
}
}
I will do something like this (very simple example):
private SpeechRecognitionEngine rec;
private void voice()
{
rec = new SpeechRecognitionEngine();
rec.SetInputToDefaultAudioDevice();
Choices choice = new Choices("apple","Orange","Onion", "next");
GrammarBuilder gr = new GrammarBuilder(choice);
Grammar grammar = new Grammar(gr);
rec.LoadGrammar(grammar);
rec.SpeechRecognized +=
new EventHandler<SpeechRecognizedEventArgs>(rec_SpeechRecogonized);
rec.RecognizeAsync(RecognizeMode.Multiple);
}
private TextBox currentInput;
void rec_SpeechRecogonized(object sender, SpeechRecognizedEventArgs e)
{
if (currentInput == null) currentInput = textBox1;
foreach (RecognizedWordUnit word in e.Result.Words)
{
if (word.Text = "next") { currentInput = textBox2; }
else { currentInput.Text = word.Text; }
}
}