Marcus M

Marcus M

  • 1.7k
  • 11
  • 2.3k

Face detection (Emgu cv) and Microsoft speech recognition.

Mar 7 2017 1:26 AM

Hello.
I have problem with Emgu cv and Microsoft Speech.
Speech recognition works fine until I turn on face detection (button1_Click).
Where is problem? What I'm doing wrong?

I'm thinking the problem may be in:

sre.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(sre_SpeechRecognized);

and

Application.Idle += new EventHandler(FrameGrabber);
but I don't know how solve this.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Drawing;  
  4. using System.Windows.Forms;  
  5. using Emgu.CV;  
  6. using Emgu.CV.Structure;  
  7. using Emgu.CV.CvEnum;  
  8. using System.IO;  
  9. using System.Diagnostics;  
  10. using Microsoft.Speech.Recognition;  
  11. using Microsoft.Speech.Synthesis;  
  12.   
  13. namespace MultiFaceRec  
  14. {  
  15.     public partial class FrmPrincipal : Form  
  16.     {  
  17.         //Declararation of all variables, vectors and haarcascades  
  18.         Image<Bgr, Byte> currentFrame;  
  19.         Capture grabber;  
  20.         HaarCascade face;  
  21.         HaarCascade eye;  
  22.         MCvFont font = new MCvFont(FONT.CV_FONT_HERSHEY_TRIPLEX, 0.5d, 0.5d);  
  23.         Image<Gray, byte> result, TrainedFace = null;  
  24.         Image<Gray, byte> gray = null;  
  25.         List<Image<Gray, byte>> trainingImages = new List<Image<Gray, byte>>();  
  26.         List<string> labels= new List<string>();  
  27.         List<string> NamePersons = new List<string>();  
  28.         int ContTrain, NumLabels, t;  
  29.         string name, names = null;  
  30.   
  31.   
  32.         SpeechSynthesizer synth = new SpeechSynthesizer();  
  33.   
  34.         public FrmPrincipal()  
  35.         {  
  36.             InitializeComponent();  
  37.             //Load haarcascades for face detection  
  38.             face = new HaarCascade("haarcascade_frontalface_default.xml");  
  39.   
  40.             synth.SelectVoice("Microsoft Server Speech Text to Speech Voice (pl-PL, Paulina)");  
  41.             synth.SetOutputToDefaultAudioDevice();  
  42.   
  43.             System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("pl-PL");  
  44.             SpeechRecognitionEngine sre = new SpeechRecognitionEngine(ci);  
  45.             sre.SetInputToDefaultAudioDevice();  
  46.             sre.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(sre_SpeechRecognized);  
  47.             Choices numbers = new Choices();  
  48.             numbers.Add(new string[] { "Pierwszy test aplikacji.""dwa""szczyt""jeden"});  
  49.   
  50.             GrammarBuilder gb = new GrammarBuilder();  
  51.             gb.Append(numbers);  
  52.             Grammar g = new Grammar(gb);  
  53.             sre.LoadGrammar(g);  
  54.   
  55.             sre.RecognizeAsync(RecognizeMode.Multiple);  
  56.   
  57.             try  
  58.             {  
  59.                 //Load of previus trainned faces and labels for each image  
  60.                 string Labelsinfo = File.ReadAllText(Application.StartupPath + "/TrainedFaces/TrainedLabels.txt");  
  61.                 string[] Labels = Labelsinfo.Split('%');  
  62.                 NumLabels = Convert.ToInt16(Labels[0]);  
  63.                 ContTrain = NumLabels;  
  64.                 string LoadFaces;  
  65.   
  66.                 for (int tf = 1; tf < NumLabels+1; tf++)  
  67.                 {  
  68.                     LoadFaces = "face" + tf + ".bmp";  
  69.                     trainingImages.Add(new Image<Gray, byte>(Application.StartupPath + "/TrainedFaces/" + LoadFaces));  
  70.                     labels.Add(Labels[tf]);  
  71.                 }  
  72.               
  73.             }  
  74.             catch(Exception e)  
  75.             {  
  76.                 //MessageBox.Show(e.ToString());  
  77.                 MessageBox.Show("Nothing in binary database, please add at least a face(Simply train the prototype with the Add Face Button).""Triained faces load", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);  
  78.             }  
  79.   
  80.         }  
  81.   
  82.         private void sre_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)  
  83.         {  
  84.             if (e.Result.Confidence > 0.82)  
  85.   
  86.             {  
  87.                 label6.Text = e.Result.Text;  
  88.                 synth.Speak("Powiedziales: ");  
  89.                 synth.SpeakAsync(label6.Text);  
  90.                 //label6.Text = "";  
  91.             }  
  92.             //throw new NotImplementedException();  
  93.             return;  
  94.         }  
  95.   
  96.   
  97.         private void button1_Click(object sender, EventArgs e)  
  98.         {  
  99.             //Initialize the capture device  
  100.             grabber = new Capture();  
  101.             grabber.QueryFrame();  
  102.             //Initialize the FrameGraber event  
  103.             Application.Idle += new EventHandler(FrameGrabber);  
  104.             button1.Enabled = false;  
  105.         }