private void DetectorSec(int deger) { Kamera camera = kameraKutusu1.Camera; if (camera != null) { camera.Lock(); switch (deger) { case 0: md = null; camera.MDetector = md; break; case 1: //md = new TwoFramesDifferenceMotionDetector(true); TwoFramesDifferenceMotionDetector TwoFrameTH = new TwoFramesDifferenceMotionDetector(true, noise); TwoFrameTH.DifferenceThreshold = trackBar1.Value; //label4.Text = TwoFrameTH.MotionLevel.ToString(); camera.MDetector = TwoFrameTH; break; case 2: //md = new BackgroundModelingHighPrecisionMotionDetector(true); BackgroundModelingHighPrecisionMotionDetector BgHighTH = new BackgroundModelingHighPrecisionMotionDetector(true, noise); BgHighTH.DifferenceThreshold = trackBar1.Value; //label4.Text = BgHighTH.MotionLevel.ToString(); camera.MDetector = BgHighTH; break; case 3: //md = new BackgroundModelingLowPrecisionMotionDetector(true); BackgroundModelingLowPrecisionMotionDetector BgLowTH = new BackgroundModelingLowPrecisionMotionDetector(true); BgLowTH.DifferenceThreshold = trackBar1.Value; //label4.Text = BgLowTH.MotionLevel.ToString(); camera.MDetector = BgLowTH; break; case 4: //md = new CountingMotionDetector(true); CountingMotionDetector CountTH = new CountingMotionDetector(true); CountTH.DifferenceThreshold = trackBar1.Value; //CountTH.FramesPerBackgroundUpdate = 50; camera.MDetector = CountTH; //camera.IsRunning break; } //camera.MDetector = md; camera.UnLock(); } }
private void MotionDetectionOn() { if (cameraWindow1.Camera != null) { MotionDetectionButton.Text = "ON"; MotionDetectionButton.BackColor = Color.Green; GunTrackButton.Enabled = true; CameraFeedButton.Enabled = false; CountingMotionDetector cmd = new CountingMotionDetector(true); cmd.MinObjectsWidth = 20; cmd.MinObjectsHeight = 20; cmd.MaxObjectsWidth = cameraWindow1.Width - 75; cmd.MaxObjectsHeight = cameraWindow1.Height; SetMotionDetector(cmd); } else { MessageBox.Show("No Active Camera!"); } }
//Timer that tracks refresh of position tracking private void TrackingTimer_Tick(object sender, EventArgs e) //125ms refresh rate as of right now { if (cameraWindow1.Camera != null && cameraWindow1.Camera.MotionDetector != null) { CountingMotionDetector cmd = (CountingMotionDetector)cameraWindow1.Camera.MotionDetector; Rectangle[] rectmotion = cmd.ObjectRectangles; if (rectmotion.Length != 0) { if (!firingsoundplayed && soundOn) { switch (soundTracker % 4) { case 0: PlaySound(pwd + "\\Sounds\\Sentry Sounds\\Target Found\\firing.wav", IntPtr.Zero, SoundFlags.SND_FILENAME | SoundFlags.SND_ASYNC); firingsoundplayed = true; break; case 1: PlaySound(pwd + "\\Sounds\\Sentry Sounds\\Target Found\\i_see_you.wav", IntPtr.Zero, SoundFlags.SND_FILENAME | SoundFlags.SND_ASYNC); firingsoundplayed = true; break; case 2: PlaySound(pwd + "\\Sounds\\Sentry Sounds\\Target Found\\target_acquired.wav", IntPtr.Zero, SoundFlags.SND_FILENAME | SoundFlags.SND_ASYNC); firingsoundplayed = true; break; case 3: PlaySound(pwd + "\\Sounds\\Sentry Sounds\\Target Found\\there_you_are.wav", IntPtr.Zero, SoundFlags.SND_FILENAME | SoundFlags.SND_ASYNC); firingsoundplayed = true; break; } } ceasefiringsoundplayed = false; aimDot.BackColor = Color.Red; //aimDot.Visible = true; //calculate largest detected motion area Rectangle largest = rectmotion[0]; for (int i = 1; i < rectmotion.Length; i++) { if (rectmotion[i].Width * rectmotion[i].Height > largest.Width * largest.Height) { largest = rectmotion[i]; } } //get center of largest motion area int x = largest.X + largest.Width / 2 + cameraWindow1.Location.X; int y = largest.Y + largest.Height / 2 + cameraWindow1.Location.Y; //calculate difference from last center point and adjust target lead depending on speed if (lastCenterPosition.IsEmpty) { lastCenterPosition = new Point(x, y); lastLeadingPosition = new Point(lastCenterPosition.X, lastCenterPosition.Y); } else if (!lastCenterPosition.IsEmpty) { int xdiff = x - lastCenterPosition.X; int ydiff = y - lastCenterPosition.Y; lastLeadingPosition = new Point(x + multiplier*xdiff, y + ydiff); lastCenterPosition = new Point(x, y); Console.WriteLine(xdiff.ToString()); } //set aimdot location aimDot.Location = new Point(lastLeadingPosition.X - 2, lastLeadingPosition.Y - 2); //create packet, set servo position, and fire servos.SetPorportionalMathPosition(cameraWindow1.Bounds, lastLeadingPosition); Packet packet = new Packet(servos.ServosPosition); //Console.WriteLine("(" + servos.Position.X + "," + servos.Position.Y + ")"); //MessageBox.Show("(" + servos.Position.X + "," + servos.Position.Y + ")"); packet.setFireOff(); if (firingOn) { packet.setFireOn(); } sendData(packet); } else if (rectmotion.Length == 0) { if (!ceasefiringsoundplayed && soundOn) { switch (soundTracker % 4) { case 0: PlaySound(pwd + "\\Sounds\\Sentry Sounds\\Target lost\\searching.wav", IntPtr.Zero, SoundFlags.SND_FILENAME | SoundFlags.SND_ASYNC); ceasefiringsoundplayed = true; break; case 1: PlaySound(pwd + "\\Sounds\\Sentry Sounds\\Target lost\\is_anyone_there.wav", IntPtr.Zero, SoundFlags.SND_FILENAME | SoundFlags.SND_ASYNC); ceasefiringsoundplayed = true; break; case 2: PlaySound(pwd + "\\Sounds\\Sentry Sounds\\Target lost\\target_lost.wav", IntPtr.Zero, SoundFlags.SND_FILENAME | SoundFlags.SND_ASYNC); ceasefiringsoundplayed = true; break; case 3: PlaySound(pwd + "\\Sounds\\Sentry Sounds\\Target Lost\\are_you_still_there.wav", IntPtr.Zero, SoundFlags.SND_FILENAME | SoundFlags.SND_ASYNC); ceasefiringsoundplayed = true; break; } soundTracker++; } firingsoundplayed = false; //when no motion, center aimDot and return servos to center position aimDot.Location = new Point(cameraWindow1.Location.X + cameraWindow1.Width / 2 - 2, cameraWindow1.Location.Y + cameraWindow1.Height / 2 - 2); aimDot.BackColor = Color.Yellow; //aimDot.Visible = false; lastCenterPosition = new Point(); lastLeadingPosition = new Point(); Packet packet = new Packet(servos.CenterServosPosition); packet.setFireOff(); sendData(packet); } } else { MessageBox.Show("No Active Camera w/ Active Motion Detector!"); TrackingTimer.Stop(); } }