I'm working on a speech recognizer but every time I say a word it puts it in a loop and keeps saying:
hi my name is Jarvis how can I help you?
What should I do?
I've also tried RecognizeAsyncCancel() but it didn't work!
My code is below:
using System;
using System.IO;
using System.Speech.Recognition;
using System.Speech.Synthesis;
using System.Windows.Forms;
namespace Jarvis
{
public partial class Form1 : Form
{
SpeechRecognitionEngine RecEngine = new SpeechRecognitionEngine();
SpeechSynthesizer speech = new SpeechSynthesizer();
public Form1()
{
InitializeComponent();
Choices choices = new Choices();
string[] text = File.ReadAllLines(Environment.CurrentDirectory + "//grammer.txt");
choices.Add(text);
Grammar grammar = new Grammar(new GrammarBuilder(choices));
RecEngine.LoadGrammar(grammar);
RecEngine.SetInputToDefaultAudioDevice();
RecEngine.RecognizeAsync(RecognizeMode.Multiple);
RecEngine.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(RecEngine_SpeechRecognized);
speech.SelectVoiceByHints(VoiceGender.Male);
}
private void RecEngine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
string result = e.Result.Text;
if (result == "Hello")
{
result = "";
result = "Hi, my name is Jarvis, how can I help you";
}
if (result == "Hi")
{
result = "";
result = "It is currently " + DateTime.Now.ToLongTimeString();
}
speech.SpeakAsync(result);
lblresult.Text = result;
result = "";
MessageBox.Show("a");
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
For starters, the setup you are using, is creating your problems, due mostly from arrangement of your code. You should also be incorporating a switch case statement in your recEngine Sub. Also, you are using SpeechSynthesizer speech in your declaration and your SpeechSynthesizer should compliment that and be: speech.Speak("Hi, my name is Jarvis, how can I help you") NOT result = "Hi, my name is Jarvis, how can I help you";.
You don't need this either: result = ""; You can still use If Statements within Case Statements if you want. With that said, here is an example from one of my old projects. I am leaving you with a link to the full project of the code. It will get you on the right path: Just scroll down to the Form1.cs at my repository.
https://github.com/DeadReport77/Scarlett-Speech-Recognition-Final-Production/commit/20f4db5d3b75b03d601d39c6fe54e8261c7976f0
I am the creator of Scarlett Witch Speech Recognition surfacing the net on YouTube and other places. This is the YouTube link: https://www.youtube.com/watch?v=39S10FoCbIA
The code will solve all your problems as well (switch case example):
private void RecEngine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
switch (e.Result.Text)
{
case "scarlett mute volume":
SendMessageW(this.Handle, WM_APPCOMMAND, this.Handle,
(IntPtr)APPCOMMAND_VOLUME_MUTE);
break;
case "scarlett volume up":
int repeat = 10;
for (int i = 0; i < repeat; i++)
SendMessageW(this.Handle, WM_APPCOMMAND, this.Handle,
(IntPtr)APPCOMMAND_VOLUME_UP);
break;
case "scarlett volume down":
for (int iter = 0; iter < 10; iter++)
SendMessageW(this.Handle, WM_APPCOMMAND, this.Handle,
(IntPtr)APPCOMMAND_VOLUME_DOWN);
break;
case "show commands":
new SoundPlayer(#"C:\Users\Leeraoy.Jenkins\source\repos\Scarlett\Resources\list.wav").Play();
Process.Start(#"C:\\Users\Leeraoy.Jenkins\source\repos\Scarlett\Resources\Commands.txt");
break;
case "tell me who you are":
new SoundPlayer(#"C:\Users\Leeraoy.Jenkins\source\repos\Scarlett\Resources\info.wav").Play();
break;
case "scarlett open youtube":
new SoundPlayer(#"C:\Users\Leeraoy.Jenkins\source\repos\Scarlett\Resources\youtube.wav").Play();
Process.Start("chrome", "Http://www.youtube.com");
break;
case "scarlett movies":
new SoundPlayer(#"C:\Users\Leeraoy.Jenkins\source\repos\Scarlett\Resources\movie.wav").Play();
Process.Start("chrome", "http://www.tubitv.com");
break;
case "scarlett open github":
new SoundPlayer(#"C:\Users\Leeraoy.Jenkins\source\repos\Scarlett\Resources\github.wav").Play();
Process.Start("chrome", "Http://www.github.com");
break;
case "open wiki":
new SoundPlayer(#"C:\Users\Leeraoy.Jenkins\source\repos\Scarlett\Resources\wiki.wav").Play();
Process.Start("http://www.wikipedia.org/");
break;
case "scarlett microsoft office":
new SoundPlayer(#"C:\Users\Leeraoy.Jenkins\source\repos\Scarlett\Resources\word.wav").Play();
Process.Start("winword");
break;
case "stop listening":
recEngine.RecognizeAsyncStop();
new SoundPlayer(#"C:\Users\Leeraoy.Jenkins\source\repos\Scarlett\Resources\mic.wav").Play();
break;
case "open command":
new SoundPlayer(#"C:\Users\Leeraoy.Jenkins\source\repos\Scarlett\Resources\prompt.wav").Play();
System.Diagnostics.Process.Start("cmd");
break;
case "scarlett run system scan":
Scanning f = new Scanning();
f.Show();
break;
case "commit to memory":
new SoundPlayer(#"C:\Users\Leeraoy.Jenkins\source\repos\Scarlett\Resources\storing.wav").Play();
break;
case "scarlett close":
new SoundPlayer(#"C:\Users\Leeraoy.Jenkins\source\repos\Scarlett\Resources\shuttingdown.wav").Play();
Sleep(TimeSpan.FromSeconds(2));
Application.Exit();
break;
case "scarlett what day is it":
Scarlett.Speak(DateTime.Today.ToString("dd-MM-yyyy"));
break;
case "scarlett out of the way":
if (WindowState == FormWindowState.Normal)
{
WindowState = FormWindowState.Minimized;
new SoundPlayer(#"C:\Users\Leeraoy.Jenkins\source\repos\Scarlett\Resources\standby.wav").Play();
}
break;
case "scarlett come back":
if (WindowState == FormWindowState.Minimized)
{
WindowState = FormWindowState.Normal;
new SoundPlayer(#"C:\Users\Leeraoy.Jenkins\source\repos\Scarlett\Resources\comeback.wav").Play();
}
break;
case "disparta":
new SoundPlayer(#"C:\Users\Leeraoy.Jenkins\source\repos\Scarlett\Resources\closingapp.wav").Play();
SendKeys.Send("%{F4}");
break;
case "scarlett play rammstein":
new SoundPlayer(#"C:\Users\Leeraoy.Jenkins\source\repos\Scarlett\Resources\video.wav").Play();
Process.Start("chrome", "https://www.youtube.com/watch?v=3b4szXcRFcs?autoplay=1&mute=1");
break;
case "page up":
SendKeys.Send("{PGUP}");
break;
case "page down":
SendKeys.Send("{PGDN}");
break;
case "new tab":
SendKeys.Send("^{t}");
break;
case "switch tab":
SendKeys.Send("^{TAB}");
break;
case "magnify":
SendKeys.Send("^{+}");
break;
case "less":
SendKeys.Send("^{-}");
break;
case "scarlett notepad":
new SoundPlayer(#"C:\Users\Leeraoy.Jenkins\source\repos\Scarlett\Resources\notepad.wav").Play();
Process.Start("notepad");
break;
case "scarlett visual studios":
new SoundPlayer(#"C:\Users\Leeraoy.Jenkins\source\repos\Scarlett\Resources\visual.wav").Play();
Process.Start("devenv.exe");
break;
default:
new SoundPlayer(#"C:\Users\Leeraoy.Jenkins\source\repos\Scarlett\Resources\unknown.wav").Play();
break;
}
}
Related
Im trying to make an app that can scan documents using C# and WIA. But I have come across a problem when setting up the page size property. When I run the scanning process my app throws an error:
An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in DigiKnjiga.exe
Additional information: Exception from HRESULT: 0x80210067
I have tried this using these properties: PageSize(3097), PageWidth(3098) & PageHeight(3099), HorizontalExtent(6151) & VerticalExtent(6152). But setting any of these values throws the before mentioned exception.
Here is the event that starts the scanning process:
private void scanNew_Click(object sender, EventArgs e)
{
if (Scanner.ChosenDevice > 0)
{
Device = deviceManager.DeviceInfos[Scanner.ChosenDevice].Connect();
switch (Scanner.ColorCode)
{
case 0://color
Device.Items[1].Properties["6146"].set_Value(1);
break;
case 1://grayscale
Device.Items[1].Properties["6146"].set_Value(2);
break;
case 2://black and white
Device.Items[1].Properties["6146"].set_Value(4);
break;
}
//(DPI)
Device.Items[1].Properties["6147"].set_Value(Scanner.DPI);
Device.Items[1].Properties["6148"].set_Value(Scanner.DPI);
//brightness
Device.Items[1].Properties["Brightness"].set_Value(Scanner.Brightness);
//contrast
Device.Items[1].Properties["Contrast"].set_Value(Scanner.Contrast);
switch (Scanner.Format)
{
case 0://A3
Device.Items[1].Properties["3097"].set_Value(10);
//Device.Items[1].Properties["6151"].set_Value(11692);
//Device.Items[1].Properties["6152"].set_Value(16535);
break;
case 1://A4
Device.Items[1].Properties["3097"].set_Value(0);
//Device.Items[1].Properties["6156"].set_Value(1);
//Device.Items[1].Properties["3098"].set_Value(8267);
//Device.Items[1].Properties["3099"].set_Value(11692);
////Device.Items[1].Properties["6151"].set_Value(1165 * 2);
////Device.Items[1].Properties["6152"].set_Value(1653 * 2);
////Device.Items[1].Properties["3097"].set_Value("0");
break;
case 2://A5
Device.Items[1].Properties["3097"].set_Value(11);
//Device.Items[1].Properties["6151"].set_Value(1165);
//Device.Items[1].Properties["6152"].set_Value(1653);
break;
}
switch (Scanner.FileType)
{
case 0://JPEG
image = (WIA.ImageFile)Device.Items[1].Transfer(WIA.FormatID.wiaFormatJPEG);
break;
case 1://PNG
image = (WIA.ImageFile)Device.Items[1].Transfer(WIA.FormatID.wiaFormatPNG);
break;
case 2://BMP
image = (WIA.ImageFile)Device.Items[1].Transfer(WIA.FormatID.wiaFormatBMP);
break;
case 3://TIFF
image = (WIA.ImageFile)Device.Items[1].Transfer(WIA.FormatID.wiaFormatTIFF);
break;
}
byte[] imageBytes = (byte[])image.FileData.get_BinaryData();
MemoryStream ms = new MemoryStream(imageBytes);
Image image2 = Image.FromStream(ms);
Bitmap smaller= new Bitmap(image2 , new Size(prikazZbirke.Width, prikazZbirke.Height));
prikazStrani.Size = new System.Drawing.Size(image2 .Width, image2 .Height);
prikazStrani.Location = new Point(0, 0);
prikazStrani.Image = image2 ;
image2 .Save("test.jpg");
}
}
I wonder if anyone knows the solution to the this problem and I thank you in advance for your answers. And at the same time apologise for any spelling mistakes as I am not english.
cahnge switch case as following, it could work(but be sure this property(3097) is supported in your scaner)
switch (Scanner.Format)
{
case 0://A3
Device.Properties["3097"].set_Value(10);
break;
case 1://A4
Device.Properties["3097"].set_Value(0);
break;
case 2://A5
Device.Properties["3097"].set_Value(11);
break;
}
So I have a Unity program, where it switches screens if the user presses ENTER.
However, whenever the user presses enter, instead of just going one screen forward, it skips all the way to the last screen.
Here is my OnGUI():
if (showScreen == true)
{
//this allows the screens to toggle
switch (pageNum)
{
case 1:
rectangle = GUI.Window(1,rectangle, Page1, "Computer");
break;
case 2:
rectangle = GUI.Window(1,rectangle, Page2, "Computer");
break;
case 3:
rectangle = GUI.Window(1,rectangle, Page3, "Computer");
break;
case 4:
rectangle = GUI.Window(1,rectangle, Page4, "Computer");
break;
case 5:
rectangle = GUI.Window(1,rectangle, Page5, "Computer");
GUILayout.Window (2,rect2, popUp, "Warning!");
break;
case 6:
rectangle = GUI.Window(1,rectangle, Page6, "Computer");
break;
case 7:
rectangle = GUILayout.Window(1,rectangle, Page7, "Computer");
break;
default:
break;
}
}
And here is where the GUIs are actually set up, and made to change:
void Page1(int windowID)
{ if (Input.GetKeyDown(KeyCode.Return))
{
GUI.FocusControl(null);
pageNum = 2;
}
}
void Page2(int windowID)
{
if (Input.GetKeyDown(KeyCode.Return))
{
pageNum = 3;
GUI.FocusControl(null);
}
}
//continue like this for 5 more Page#(int windowID)
So what can I insert into these classes to make my program stop skipping? should I put a pause into these if statements too?
So it looks like the easiest way to solve this problem is by changing how the program changes the stages. All you need to do is a boolean that changes to true once the return key has been pressed.
so just declare the boolean:
public class ClassName : MonoBehaviour
{
bool wasClicked = false;
Then to change the states just do this:
if (Input.GetKey(KeyCode.Return))
{
wasClicked = true;
}
if (wasClicked)
{
if (!Input.GetKey(KeyCode.Return))
{
GUI.FocusControl(null);
pageNum = next page number;
wasClicked = false;
}
}
I have the following code in C#. A function by which I want to highlight one of the nine images at random. The code is as follows,
public void randomize(object sender, MouseButtonEventArgs e)
{
String img = "image";
int random = RandomNumber(10, 18); //Generate Random Number
score.Content = random;
img += random; //append generated number to "image"
//Call function to highlight behind image
ToGold(random);
}
I tried to make the function call ToGold(random) to be able to dynamically refer to one of the images on XAML. But I was not able to make the code work as I intended. So I took a brute force approach as below,
public void ToGold(int Img)
{
Uri gold = new Uri("/Start;component/Images/gold1.png", UriKind.Relative); //Set Uri path of gold image
ImageSource ImgSrc = new BitmapImage(gold); //Define ImageSource and assign
switch(Img)
{
case 10:
{
image10.Source = ImgSrc;
break;
}
case 11:
{
image11.Source = ImgSrc;
break;
}
case 12:
{
image12.Source = ImgSrc;
break;
}
case 13:
{
image13.Source = ImgSrc;
break;
}
case 14:
{
image14.Source = ImgSrc;
break;
}
case 15:
{
image15.Source = ImgSrc;
break;
}
case 16:
{
image16.Source = ImgSrc;
break;
}
case 17:
{
image17.Source = ImgSrc;
break;
}
case 18:
{
image18.Source = ImgSrc;
break;
}
}
}
So my question is how do I make the code efficient by making it dynamic? Can anyone please help me?
NOTE: I have just started studying WPF. So please bear with me.
You can use the FindName method on the window to find an element by its name.
Or you could create an array containing your images in order, i.e. { image0, image1, image2, image3, ... } and then access that array with a random index.
This is probably a trivial question, however, i have yet to find an answer. I'm trying to improve my winforms application by adding the possibility of key navigation (arrow keys). The problem is I have a number of buttons arranged in rows and columns and it has proven troublesome to navigate. The UP/RIGHT and DOWN/LEFT arrows only increment and decrement the index rather than move in the specified direction. So far my only idea was to map the button indexes to a 2d array and use that as reference to the buttons, however, i have been unsuccessful. Anyone have some ideas or suggestions on how i could implement this?
I'm using C# on Visual studio 2008 and .NET3.5
Control[,] MyButtons = new Control[4,3] { {b1,b2,b3},{b4,b5,b6},{b7,b8,b9},{btn_Clear,b0,btn_Cancel}};
for(int i=0;i<4;i++)
for (int j = 0; j < 3; j++)
{
switch (e.KeyValue)
{
case 13: //return
{ e.Handled = false; break; }
case 39: //Right
{
j++;
break;
}
case 38: //Up
{
i++;
break;
}
case 37: //Left
{
j--;
break;
}
case 40: //Down
{
i--;
break;
}
}
if (e.KeyValue != 13)
{
e.Handled = true; //Don't pass on the keystroke
MyButtons[i,j].Focus(); // Set the focus to the selected control
Application.DoEvents();
}
}
With this code i keep getting this error for each element of the array
Invalid rank specifier: expected ',' or ']'
I'll give you a full example and you can extrapolate to your project/code.
The form itself:
Here's that form's code:
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;
namespace WindowsFormsApplication1
{
public partial class frmButtons : Form
{
private Int32 _selectedButton = 1;
private Dictionary<Int32, Button> _buttonDict = new Dictionary<Int32, Button>();
public frmButtons()
{
InitializeComponent();
foreach (Button b in this.Controls.OfType<Button>().Where(x => x.Tag != null))
{
Int32 i;
if (Int32.TryParse(b.Tag.ToString(), out i))
_buttonDict.Add(i, b);
}
if (_buttonDict.Count > 0)
_buttonDict[_selectedButton].Focus();
}
private void frmButtons_KeyUp(Object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Up:
_selectedButton -= 3;
e.Handled = true;
break;
case Keys.Down:
_selectedButton += 3;
e.Handled = true;
break;
case Keys.Left:
_selectedButton -= 1;
e.Handled = true;
break;
case Keys.Right:
_selectedButton += 1;
e.Handled = true;
break;
case Keys.Enter:
break;
}
if (_selectedButton < 1)
_selectedButton += 12;
else if (_selectedButton > 12)
_selectedButton -= 12;
_buttonDict[_selectedButton].Focus();
}
}
}
I notice a hitch when you hit a key...there's a default behavior in the form where it attempts to handle the arrow key to move to the next in the Tab Order. Solve that and you've got it. Sorry, but I'm out of time to work on this.
I have two forms Form 1 and Form 2.
Form 2 is inheriting from Form 1. In Form 1 I have a method on_Paint for a particular panel, in which I am drawing an image. Since Form 2 is inheriting from Form 1, the panel is being shown as painted before I even run the program (on the design of Form 2).
The problem is, that now I need to paint some things over this image after the image is painted in Form 2. How can I make this possible?
This is some code to make the problem more clear.
// This is Form 1 on_Paint method
private void grid_Paint(object sender, PaintEventArgs e)
{
Image img = Image.FromFile(resourcesPath+ "grid.fw.png");
gridGraphics = grid.CreateGraphics();
gridGraphics.DrawImage(img, 0, 0, 650, 550);
}
// This is the method which I want to be invoked after the grid is painted.. This is located in Form 1:
public void paintSprites(int row, int column, int value)
{
int yLoc = 0;
int xLoc = 0;
Graphics g = grid.CreateGraphics();
switch (row)
{
case 0: yLoc = 435; break;
case 1: yLoc = 355; break;
case 2: yLoc = 275; break;
case 3: yLoc = 195; break;
case 4: yLoc = 115; break;
case 5: yLoc = 35; break;
}
switch (column)
{
case 0: xLoc = 35; break;
case 1: xLoc = 120; break;
case 2: xLoc = 205; break;
case 3: xLoc = 290; break;
case 4: xLoc = 375; break;
case 5: xLoc = 460; break;
case 6: xLoc = 545; break;
}
if (value == 1)
{
g.DrawImage(red, xLoc, yLoc, 65, 65);
}
else g.DrawImage(gold, xLoc, yLoc, 65, 65);
}
This method is located in Form 2 and calls the method above:
public void checkWhichToPaint()
{
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLUMNS; j++)
{
if (gridMap[i, j] != 0)
{
paintSprites(i, j, gridMap[i, j]);
}
}
}
}
and I am calling the method checkWhichToPaint() after creating a new instance of Form 2 and Showing it:
Tournament tournament = new Tournament();
tournament.LoadGame();
tournament.Show();
tournament.checkWhichToPaint();
where can I call this method such as it is painted after the grid_Paint is invoked?
Thanks for your help :)
This is the answer to whoever might need it!
In Form 1:
protected virtual void grid_Paint(object sender, PaintEventArgs e)
{
Image img = Image.FromFile(resourcesPath+ "grid.fw.png");
gridGraphics = grid.CreateGraphics();
gridGraphics.DrawImage(img, 0, 0, 650, 550);
}
In Form 2:
protected override void grid_Paint(object sender, PaintEventArgs e)
{
checkWhichToPaint();
base.grid_Paint(null, null);
}
Enjoy ! :)
Override the paint method in the second form and call base paint method before you run your form 2 code.