public override T PeekFront() { lock (root) { return(deque.PeekFront()); } }
private async void btn_startCam_Click(object sender, EventArgs e) { if (capture == null) { return; } try { stop = false; while (!stop) { Mat m = new Mat(); //Thread.Sleep(1000); capture.Read(m); if (!m.IsEmpty) { pictureBox1.SizeMode = PictureBoxSizeMode.Zoom; pictureBox2.SizeMode = PictureBoxSizeMode.Zoom; pictureBox1.Image = m.Bitmap; Image <Bgr, Byte> bitmap = new Image <Bgr, Byte>(m.Bitmap); Image <Gray, byte> gray = bitmap.Convert <Gray, Byte>(); CvInvoke.GaussianBlur(gray, gray, new Size(5, 5), 5); var element = CvInvoke.GetStructuringElement(Emgu.CV.CvEnum.ElementShape.Cross, new Size(3, 3), new Point(-1, -1)); CvInvoke.Erode(gray, gray, element, new Point(-1, -1), 1, Emgu.CV.CvEnum.BorderType.Reflect, default(MCvScalar)); CvInvoke.Flip(gray, gray, Emgu.CV.CvEnum.FlipType.Horizontal); CvInvoke.MorphologyEx(gray, gray, Emgu.CV.CvEnum.MorphOp.Open, element, new Point(-1, -1), 1, Emgu.CV.CvEnum.BorderType.Reflect, default(MCvScalar)); CascadeClassifier classfier = new CascadeClassifier("palm.xml"); CascadeClassifier classfierfist = new CascadeClassifier("fist.xml"); Rectangle[] recfist = classfierfist.DetectMultiScale(gray, 1.1, 5, new Size(125, 125)); Rectangle[] rect = classfier.DetectMultiScale(gray, 1.1, 5, new Size(125, 125)); if (recfist.Length != 0) { if (d.Count != 0) { lbl_status.Text = "Gesture Detected"; Point p1 = d.PeekBack(); Point p2 = d.PeekFront(); int x = p1.X - p2.X; int y = p1.Y - p2.Y; lbl_xy.Text = x + "," + y; if ((Math.Abs(x) > Math.Abs(y)) && Math.Abs(x) > 40) { if (x > 0) { if (IsAudioPlaying(GetDefaultRenderDevice())) { lbl_gesture.Text = "Gesture right/next Track"; sim.Keyboard.KeyPress(VirtualKeyCode.MEDIA_NEXT_TRACK); } else { lbl_gesture.Text = "Brightness increased"; ChangeBrightness("+10"); } } if (x <= 0) { if (IsAudioPlaying(GetDefaultRenderDevice())) { lbl_gesture.Text = "Gesture left/Prev TracG"; sim.Keyboard.KeyPress(VirtualKeyCode.MEDIA_PREV_TRACK); } else { lbl_gesture.Text = "Brightness decreaased"; ChangeBrightness("-10"); } } } else if ((Math.Abs(x) < Math.Abs(y)) && Math.Abs(y) > 40) { if (y < 0) { lbl_gesture.Text = "Gesture top/Volume UP"; for (int i = 1; i <= 5; i++) { sim.Keyboard.KeyPress(VirtualKeyCode.VOLUME_UP); } } if (y > 0) { lbl_gesture.Text = "Gesture down/VOlume DOWN"; for (int i = 1; i <= 5; i++) { sim.Keyboard.KeyPress(VirtualKeyCode.VOLUME_DOWN); } } } else { sim.Keyboard.KeyPress(VirtualKeyCode.MEDIA_PLAY_PAUSE); lbl_gesture.Text = "Gesture detected/Media pause/play"; } d.Clear(); rtbx.Text = ""; } } foreach (Rectangle re in rect) { Point p = new Point(re.X + (re.Width / 2), re.Y + (re.Height / 2)); //point.Text="x= "+p.X+" y ="+p.Y; rtbx.Text = "x= " + p.X + " y =" + p.Y + "\n" + rtbx.Text; d.PushBack(p); gray.Draw(re, new Gray(0.0), 3); lbl_status.Text = "Hand detected"; } pictureBox2.Image = gray.Bitmap; double fps = 30; await Task.Delay(1000 / Convert.ToInt32(fps)); } else { break; } } } catch (Exception ex) { MessageBox.Show("Error :" + ex.Message); } }