Example #1
0
        void VideoSource_NewFrame(object sender, AForge.Video.NewFrameEventArgs eventArgs)
        {
            if(OnCameraFrame != null)
                OnCameraFrame(Image.Clone(eventArgs.Frame));

            if (skipFrames < Global.AppSettings.SkipFrames)
            {
                skipFrames++;
                return;
            }
            else
                skipFrames = 0;

            frame = (Bitmap)eventArgs.Frame.Clone();

            if (detector == null || tracker == null || restartDetector != null)
            {
                if (restartDetector != null)
                    detector = restartDetector;
                else
                    detector = DetectorManager.LoadDetectorWithoutExceptions(DetectorManager.DetectorDirectory + "\\" + Global.AppSettings.DetectorName + ".dll");

                if (detector == null)
                    detector = new Detector1();

                detector.Initialize(Image.Clone(frame));

                tracker = new Tracker1();
                restartDetector = null;
            }

            try
            {
                detector.ProcessFrame(ref frame);
            }
            catch(Exception exc)
            {
                if (detector.GetType() != typeof(Detector1))
                    SetDetector(new Detector1());
                else
                    throw exc;

                return;
            }

            if (OnProcessedFrame != null)
                OnProcessedFrame(Image.Clone(frame));

            List<Blob> trackedBlobs = tracker.ProcessBlobs(detector.GetBlobRectangles());

            if (OnBlobsTracked != null)
                OnBlobsTracked(trackedBlobs);

            Bitmap processedCameraFrame;
            if (OnProcessedFrame != null)
            {
                processedCameraFrame = frame.Clone(new Rectangle(0, 0, frame.Size.Width, frame.Size.Height), System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                Graphics g = Graphics.FromImage(processedCameraFrame);

                Font idFont = new Font("Arial", processedCameraFrame.Width / 20);

                //Draw rectangles to original camera frame
                foreach (Blob blob in trackedBlobs)
                {
                    if (!blob.Active)
                        continue;

                    g.DrawRectangle(Pens.Yellow, blob.Rect);

                    g.FillRectangle(new SolidBrush(Color.FromArgb(150, Color.DarkRed)),
                        new Rectangle(blob.Rect.X, blob.Rect.Y, (int)g.MeasureString(blob.Id.ToString(), idFont).Width, (int)idFont.GetHeight()));
                    g.DrawString(blob.Id.ToString(), idFont, Brushes.White, new PointF(blob.Rect.X, blob.Rect.Y));
                }

                if (OnProcessedCameraFrame != null) //this is a different thread, so it might have changed in the meantime (race condition)
                    OnProcessedCameraFrame(processedCameraFrame);
            }

            if (NewFrame != null)
            {
                List<Contact> contacts = new List<Contact>();
                foreach (Blob blob in trackedBlobs)
                {
                    Point w2s = WebcamToScreen(blob);
                    //Console.WriteLine(w2s.ToString());
                    Contact c = new Contact(blob.Id, blob.ContactState, new System.Windows.Point(w2s.X, w2s.Y), blob.Rect.Width, blob.Rect.Height);
                    contacts.Add(c);
                }

                NewFrame(this, new NewFrameEventArgs(Stopwatch.GetTimestamp(), contacts, null));
                contacts.Clear();
            }

            frame.Dispose();
        }
Example #2
0
 /// <summary>Detector will be restarted on next frame.</summary>
 public void RestartDetector()
 {
     restartDetector = detector;
 }
Example #3
0
 public void SetDetector(DetectorBase detector)
 {
     restartDetector = detector;
 }
Example #4
0
        public void Initialize(bool loadAppSettings = true)
        {
            if (camera != null && camera.IsRunning)
                Stop();

            if(loadAppSettings)
                Global.AppSettings.Load();

            camera = CameraManager.GetCamera(Global.AppSettings.Camera);
            if (camera != null)
            {
                if (Global.AppSettings.CameraMode < camera.VideoCapabilities.Length)
                {
                    camera.DesiredFrameSize = camera.VideoCapabilities[Global.AppSettings.CameraMode].FrameSize;
                    camera.DesiredFrameRate = camera.VideoCapabilities[Global.AppSettings.CameraMode].MaxFrameRate;
                }
            }
            if (camera == null)
            {
                camera = CameraManager.GetCamera(0);
                Global.AppSettings.Camera = 0;
            }
            if (camera == null)
            {
                MessageBox.Show("Er is geen (standaard) camera gevonden. Driver wordt nu afgesloten.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

                Application.Exit();
            }

            detector = null;
            restartDetector = null;
            tracker = null;

            skipFrames = Global.AppSettings.SkipFrames;
        }