void controller_HeadMove(object sender, HeadEventArgs e)
        {
            if (controller == null || controller.Tracker == null)
            {
                return;
            }

            if (backproj)
            {
                try
                {
                    Camshift camshift = controller.Tracker as Camshift;

                    Bitmap backprojection = camshift.GetBackprojection(
                        PixelFormat.Format24bppRgb, camshift.TrackingObject.Rectangle);

                    if (parent.faceForm != null && !parent.faceForm.IsDisposed)
                    {
                        MatchingTracker matching = parent.faceForm.faceController.Tracker as MatchingTracker;

                        marker.Rectangles = new[] { matching.TrackingObject.Rectangle };
                        marker.ApplyInPlace(backprojection);
                    }


                    pictureBox.Image = backprojection;
                }
                catch
                {
                    pictureBox.Image = null;
                }
            }
        }
Ejemplo n.º 2
0
        private Bitmap FaceFind(Bitmap bmpBitmap)
        {
            HaarObjectDetector faceDetector = new HaarObjectDetector(new FaceHaarCascade(), minSize: 300, searchMode: ObjectDetectorSearchMode.Single);
            RectanglesMarker   faceMarker   = new RectanglesMarker(Color.Red)
            {
                Rectangles = faceDetector.ProcessFrame(bmpBitmap)
            };

            faceMarker.ApplyInPlace(bmpBitmap);
            return(bmpBitmap);
        }
Ejemplo n.º 3
0
        private Bitmap Detecting(Bitmap _bitmap)
        {
            UnmanagedImage im = UnmanagedImage.FromManagedImage(_bitmap);

            float xscale = im.Width / 160f;
            float yscale = im.Height / 120f;

            ResizeNearestNeighbor resize     = new ResizeNearestNeighbor(160, 120);
            UnmanagedImage        downsample = resize.Apply(im);


            Rectangle[] regions = detector.ProcessFrame(downsample);


            if (regions.Length > 0)
            {
                tracker.Reset();

                // Reduce the face size to avoid tracking background
                Rectangle window = new Rectangle(
                    (int)((regions[0].X + regions[0].Width / 2f) * xscale),
                    (int)((regions[0].Y + regions[0].Height / 2f) * yscale),
                    1, 1);

                window.Inflate(
                    (int)(0.2f * regions[0].Width * xscale),
                    (int)(0.4f * regions[0].Height * yscale));

                this.FaceTracked = window;

                // Initialize tracker
                tracker.SearchWindow = window;
                tracker.ProcessFrame(im);

                marker = new RectanglesMarker(window);
                marker.ApplyInPlace(im);

                // (Bitmap) Helpers.BitmapHelper.ByteArrayToImage(Helpers.RijndaelHelper.EncryptBytes(Helpers.BitmapHelper.ImageToByte(im.ToManagedImage()), "fzafa", "afzd"))

                this.isTracking = true;

                return(im.ToManagedImage());
            }
            else
            {
                this.isDetecting = false;
                return(_bitmap);
            }
        }
Ejemplo n.º 4
0
        private Bitmap Tracking(Bitmap _bitmap)
        {
            UnmanagedImage im = UnmanagedImage.FromManagedImage(_bitmap);

            // Track the object
            tracker.ProcessFrame(im);

            // Get the object position
            var obj = tracker.TrackingObject;

            this.FaceTracked = obj.Rectangle;

            marker = new RectanglesMarker(obj.Rectangle);

            if (marker != null)
            {
                marker.ApplyInPlace(im);
            }

            return(im.ToManagedImage());
        }
Ejemplo n.º 5
0
        // New frame received by the player
        private void videoSourcePlayer_NewFrame(object sender, ref Bitmap image)
        {
            if (!detecting && !tracking)
            {
                return;
            }

            lock (this)
            {
                if (detecting)
                {
                    detecting = false;
                    tracking  = false;

                    UnmanagedImage im = UnmanagedImage.FromManagedImage(image);

                    float xscale = image.Width / 160f;
                    float yscale = image.Height / 120f;

                    ResizeNearestNeighbor resize     = new ResizeNearestNeighbor(160, 120);
                    UnmanagedImage        downsample = resize.Apply(im);

                    Rectangle[] regions = detector.ProcessFrame(downsample);

                    if (regions.Length > 0)
                    {
                        tracker.Reset();

                        // Will track the first face found
                        Rectangle face = regions[0];

                        // Reduce the face size to avoid tracking background
                        Rectangle window = new Rectangle(
                            (int)((regions[0].X + regions[0].Width / 2f) * xscale),
                            (int)((regions[0].Y + regions[0].Height / 2f) * yscale),
                            1, 1);

                        window.Inflate(
                            (int)(0.2f * regions[0].Width * xscale),
                            (int)(0.4f * regions[0].Height * yscale));

                        // Initialize tracker
                        tracker.SearchWindow = window;
                        tracker.ProcessFrame(im);

                        marker = new RectanglesMarker(window);
                        marker.ApplyInPlace(im);

                        image = im.ToManagedImage();

                        tracking = true;
                        //detecting = true;
                    }
                    else
                    {
                        detecting = true;
                    }
                }
                else if (tracking)
                {
                    UnmanagedImage im = UnmanagedImage.FromManagedImage(image);

                    // Track the object
                    tracker.ProcessFrame(im);

                    // Get the object position
                    var obj = tracker.TrackingObject;
                    var wnd = tracker.SearchWindow;

                    if (displayBackprojectionToolStripMenuItem.Checked)
                    {
                        var backprojection = tracker.GetBackprojection(PixelFormat.Format24bppRgb);
                        im = UnmanagedImage.FromManagedImage(backprojection);
                    }

                    if (drawObjectAxisToolStripMenuItem.Checked)
                    {
                        LineSegment axis = obj.GetAxis();

                        // Draw X axis
                        Drawing.Line(im, axis.Start.Round(), axis.End.Round(), Color.Red);
                    }


                    if (drawObjectBoxToolStripMenuItem.Checked && drawTrackingWindowToolStripMenuItem.Checked)
                    {
                        marker = new RectanglesMarker(new Rectangle[] { wnd, obj.Rectangle });
                    }
                    else if (drawObjectBoxToolStripMenuItem.Checked)
                    {
                        //InteractionPoints p = new InteractionPoints();
                        //p.setHead(obj.Rectangle);

                        marker = new RectanglesMarker(obj.Rectangle);
                    }
                    else if (drawTrackingWindowToolStripMenuItem.Checked)
                    {
                        marker = new RectanglesMarker(wnd);
                    }
                    else
                    {
                        marker = null;
                    }


                    if (marker != null)
                    {
                        marker.ApplyInPlace(im);
                    }
                    image = im.ToManagedImage();
                }
                else
                {
                    if (marker != null)
                    {
                        image = marker.Apply(image);
                    }
                }
            }
        }
Ejemplo n.º 6
0
        // New frame received by the player
        private void videoSourcePlayer_NewFrame(object sender, ref Bitmap image)
        {
            lock (this)
            {
                if (tracker == null)
                {
                    return;
                }

                if (form != null && !form.IsDisposed)
                {
                    form.Image = image;
                }

                BitmapData data = image.LockBits(new Rectangle(0, 0, image.Width, image.Height),
                                                 ImageLockMode.ReadWrite, image.PixelFormat);

                UnmanagedImage img = new UnmanagedImage(data);

                tracker.ComputeOrientation = showAngle;

                tracker.ProcessFrame(img);

                Rectangle rect = tracker.TrackingObject.Rectangle;

                UnmanagedImage hand = tracker.TrackingObject.Image;


                if (hand != null && (showContour || showFingertips))
                {
                    UnmanagedImage grayhand = Grayscale.CommonAlgorithms.BT709.Apply(hand);

                    blur.ApplyInPlace(grayhand);

                    List <IntPoint> contour = bf.FindContour(grayhand);

                    for (int i = 0; i < contour.Count; i++)
                    {
                        contour[i] += new IntPoint(rect.X, rect.Y);
                    }

                    List <IntPoint> peaks = kcurv.FindPeaks(contour);

                    if (showContour)
                    {
                        cmarker.Points = contour;
                        cmarker.ApplyInPlace(img);
                    }

                    if (showFingertips)
                    {
                        pmarker.Points = peaks;
                        pmarker.ApplyInPlace(img);
                    }
                }

                if (showRectangle)
                {
                    RectanglesMarker marker = new RectanglesMarker(rect);
                    marker.ApplyInPlace(img);
                }

                if (showAngle)
                {
                    LineSegment axis = tracker.TrackingObject.GetAxis(AxisOrientation.Vertical);

                    if (axis != null)
                    {
                        // Draw X axis
                        Drawing.Line(img, axis.Start.Round(), axis.End.Round(), Color.Red);
                    }
                }

                image.UnlockBits(data);
            }
        }
        // New frame received by the player
        private void videoSourcePlayer_NewFrame(object sender, NewFrameEventArgs args)
        {
            var direccion      = "Centro";
            int direccionServo = 2;

            if (!detecting && !tracking)
            {
                return;
            }

            lock (this)
            {
                if (detecting)
                {
                    detecting = false;
                    tracking  = false;

                    UnmanagedImage im = UnmanagedImage.FromManagedImage(args.Frame);

                    float xscale = im.Width / 160f;
                    float yscale = im.Height / 120f;

                    ResizeNearestNeighbor resize     = new ResizeNearestNeighbor(160, 120);
                    UnmanagedImage        downsample = resize.Apply(im);

                    Rectangle[] regions = detector.ProcessFrame(downsample);

                    if (regions.Length > 0)
                    {
                        tracker.Reset();

                        // Will track the first face found
                        Rectangle face = regions[0];

                        // Reduce the face size to avoid tracking background
                        Rectangle window = new Rectangle(
                            (int)((regions[0].X + regions[0].Width / 2f) * xscale),
                            (int)((regions[0].Y + regions[0].Height / 2f) * yscale),
                            1, 1);
                        Console.Write("x:" + (int)((regions[0].X + regions[0].Width / 2f) * xscale));
                        Console.Write("y:" + (int)((regions[0].X + regions[0].Height / 2f) * xscale));
                        window.Inflate(
                            (int)(0.2f * regions[0].Width * xscale),
                            (int)(0.4f * regions[0].Height * yscale));

                        // Initialize tracker
                        tracker.SearchWindow = window;
                        tracker.ProcessFrame(im);

                        marker = new RectanglesMarker(window);
                        marker.ApplyInPlace(im);

                        args.Frame = im.ToManagedImage();

                        tracking = true;
                        //detecting = true;
                    }
                    else
                    {
                        detecting = true;
                    }
                }
                else if (tracking)
                {
                    UnmanagedImage im = UnmanagedImage.FromManagedImage(args.Frame);

                    // Track the object
                    tracker.ProcessFrame(im);

                    // Get the object position
                    var obj = tracker.TrackingObject;
                    var wnd = tracker.SearchWindow;

                    //if (displayBackprojectionToolStripMenuItem.Checked)
                    //{
                    //    var backprojection = tracker.GetBackprojection(PixelFormat.Format24bppRgb);
                    //    im = UnmanagedImage.FromManagedImage(backprojection);
                    //}

                    //if (drawObjectAxisToolStripMenuItem.Checked)
                    //{
                    //    LineSegment axis = obj.GetAxis();

                    //    // Draw X axis
                    //    if (axis != null)
                    //        Drawing.Line(im, axis.Start.Round(), axis.End.Round(), Color.Red);
                    //    else detecting = true;
                    //}
                    if (obj.Rectangle.Width < (args.Frame.Width / 3) * 2)
                    {
                        if (obj.Rectangle.X < args.Frame.Width / 3)
                        {
                            direccion      = "Izquierda";
                            direccionServo = 1;
                        }
                        else if (obj.Rectangle.X > (args.Frame.Width / 3) * 2)
                        {
                            direccion      = "Derecha";
                            direccionServo = 3;
                        }
                    }
                    try
                    {
                        this.Invoke((MethodInvoker) delegate
                        {
                            if (textBox1 != null)
                            {
                                textBox1.Text = obj.Rectangle.X.ToString();
                                textBox2.Text = obj.Rectangle.Y.ToString();
                                label1.Text   = direccion;
                            }
                        });
                        ComunicacionPuertoSerie.Instance.enviarEvento(direccionServo.ToString());
                    }
                    catch (Exception e) { }
                    if (/*drawObjectBoxToolStripMenuItem.Checked && drawTrackingWindowToolStripMenuItem.Checked*/ false)
                    {
                        marker = new RectanglesMarker(new Rectangle[] { wnd, obj.Rectangle });
                    }
                    else if (/*drawObjectBoxToolStripMenuItem.Checked*/ true)
                    {
                        marker = new RectanglesMarker(obj.Rectangle);
                    }
                    else if (/*drawTrackingWindowToolStripMenuItem.Checked*/ true)
                    {
                        marker = new RectanglesMarker(wnd);
                    }
                    else
                    {
                        marker = null;
                    }


                    if (marker != null)
                    {
                        marker.ApplyInPlace(im);
                    }
                    args.Frame = im.ToManagedImage();
                }
                else
                {
                    if (marker != null)
                    {
                        args.Frame = marker.Apply(args.Frame);
                    }
                }
            }
        }
Ejemplo n.º 8
0
        public void ProcessVideo()
        {
            string basePath = Path.Combine(NUnit.Framework.TestContext.CurrentContext.TestDirectory, "detector");

            #region doc_video
            // Let's test the detector using a sample video from
            // the collection of test videos in the framework:
            TestVideos ds       = new TestVideos(basePath);
            string     fileName = ds["crowd.mp4"];

            // In this example, we will be creating a cascade for a Face detector:
            var cascade = new Accord.Vision.Detection.Cascades.FaceHaarCascade();

            // Now, create a new Haar object detector with the cascade:
            var detector = new HaarObjectDetector(cascade, minSize: 25,
                                                  searchMode: ObjectDetectorSearchMode.Average,
                                                  scalingMode: ObjectDetectorScalingMode.SmallerToGreater,
                                                  scaleFactor: 1.1f)
            {
                Suppression = 5 // This should make sure we only report regions as faces if
                                // they have been detected at least 5 times within different cascade scales.
            };

            // Now, let's open the video using FFMPEG:
            var video = new VideoFileReader();
            video.Open(fileName);

            // And then check the contents of one of the frames:
            Bitmap frame = video.ReadVideoFrame(frameIndex: 0);

            // Creating bitmaps and locking them is an expensive
            // operation. Instead, let's allocate once and reuse
            BitmapData     bitmapData     = frame.LockBits(ImageLockMode.ReadWrite);
            UnmanagedImage unmanagedImage = new UnmanagedImage(bitmapData);

            // We will create a color marker to show the faces
            var objectMarker = new RectanglesMarker(Color.Red);

            // This example is going to show two different ways to save results to disk. The
            // first is to save the results frame-by-frame, saving each individual frame as
            // a separate .png file. The second is to save them as a video in .mp4 format.

            // To save results as a movie clip in mp4 format, you can use:
            VideoFileWriter writer = new VideoFileWriter();
            writer.Open(Path.Combine(basePath, "detected_faces.mp4"), frame.Width, frame.Height);

            // Now, for each frame of the video
            for (int frameIndex = 0; frameIndex < video.FrameCount; frameIndex++)
            {
                // Read the current frame into the bitmap data
                video.ReadVideoFrame(frameIndex, bitmapData);

                // Feed the frame to the tracker
                Rectangle[] faces = detector.ProcessFrame(unmanagedImage);

                // Mark the location of the tracker object in red color
                objectMarker.Rectangles = faces;
                objectMarker.ApplyInPlace(unmanagedImage); // overwrite the frame

                // Save it to disk: first saving each frame separately:
                frame.Save(Path.Combine(basePath, "frame_{0}.png".Format(frameIndex)));

                // And then, saving as a .mp4 file:
                writer.WriteVideoFrame(bitmapData);
            }

            // The generated video can be seen at https://1drv.ms/v/s!AoiTwBxoR4OAoLJhPozzixD25XcbiQ
            video.Close();
            writer.Close();
            #endregion
        }
Ejemplo n.º 9
0
        private void videoSourcePlayer1_NewFrame(object sender, ref Bitmap image)
        {
            Invert inv = new Invert();

            inv.ApplyInPlace(image);

            UnmanagedImage ui = UnmanagedImage.FromManagedImage(image);

            pictureBox1.Image = image;


            if (controller.Tracker.TrackingObject == null)
            {
                return;
            }

            if (controller.Tracker.TrackingObject.IsEmpty)
            {
                return;
            }

            var  rect = controller.Tracker.TrackingObject.Rectangle;
            Crop crop = new Crop(rect);

            UnmanagedImage head = crop.Apply(ui);

            var points = new List <IntPoint>()
            {
                new IntPoint(head.Width / 2, head.Height / 2)
            };
            var pps = head.Collect16bppPixelValues(points);

            double mean = Accord.Statistics.Tools.Mean(pps);

            double    cutoff = mean + 15;
            Threshold t      = new Threshold((int)cutoff);
            var       mask   = t.Apply(ui);



            LevelsLinear16bpp levels = new LevelsLinear16bpp();

            levels.InGray  = new IntRange((int)cutoff, 65535);
            levels.OutGray = new IntRange(0, 65535);
            levels.ApplyInPlace(ui);


            var mask8bit = AForge.Imaging.Image.Convert16bppTo8bpp(mask.ToManagedImage());



            BlobCounter bc = new BlobCounter();

            bc.ObjectsOrder = ObjectsOrder.Area;
            bc.ProcessImage(mask8bit);
            var blobs = bc.GetObjectsInformation();

            inv.ApplyInPlace(image);
            Intersect intersect = new Intersect();

            intersect.UnmanagedOverlayImage = mask;
            mask = intersect.Apply(ui);

            List <Rectangle> rects = new List <Rectangle>();

            // Extract the uppermost largest blobs.
            for (int i = 0; i < blobs.Length; i++)
            {
                double dx = (blobs[i].Rectangle.Top - controller.Tracker.TrackingObject.Center.Y);
                double d  = (dx * dx) / controller.Tracker.TrackingObject.Area;
                if (d < 2 && blobs[i].Area > 1000)
                {
                    rects.Add(blobs[i].Rectangle);
                }
            }

            rects.Sort(compare);

            if (rects.Count > 0)
            {
                captureHand(mask, rects[0], pbLeftArm, pbLeftHand);
            }
            if (rects.Count > 1)
            {
                captureHand(mask, rects[1], pbRightArm, pbRightHand);
            }

            RectanglesMarker marker = new RectanglesMarker(rects);

            marker.MarkerColor = Color.White;
            marker.ApplyInPlace(mask8bit);

            image = mask.ToManagedImage();
        }
Ejemplo n.º 10
0
        public void ProcessFrame()
        {
            string basePath = Path.Combine(NUnit.Framework.TestContext.CurrentContext.TestDirectory, "matching-tracker");

            #region doc_track
            // Let's test the tracker using a sample video from
            // the collection of test videos in the framework:
            TestVideos ds       = new TestVideos(basePath);
            string     fileName = ds["walking.mp4"];

            // Now, let's open the video using FFMPEG:
            var video = new VideoFileReader();
            video.Open(fileName);

            // And then check the contents of one of the frames:
            Bitmap frame = video.ReadVideoFrame(frameIndex: 150);
            frame.Save(Path.Combine(basePath, "walking_frame.png"));

            // Let's register a template for the bike rider in gray shirt
            Rectangle roi = new Rectangle(x: 70, y: 105, width: 28, height: 54);

            // initialization
            var tracker = new MatchingTracker()
            {
                SearchWindow          = roi,
                Threshold             = 0.0, // never reset the tracker in case it gets lost
                RegistrationThreshold = 0.95 // re-register the template if we are 95%
                                             // confident that the tracked object is indeed the object we want to follow
            };

            // Creating bitmaps and locking them is an expensive
            // operation. Instead, let's allocate once and reuse
            BitmapData     bitmapData     = frame.LockBits(ImageLockMode.ReadWrite);
            UnmanagedImage unmanagedImage = new UnmanagedImage(bitmapData);

            // We will create two color markers: one to show the location of the
            // tracked object (red) and another one to show the regions of the image
            // that the tracker is looking at (white).
            RectanglesMarker objectMarker = new RectanglesMarker(Color.Red);
            RectanglesMarker windowMarker = new RectanglesMarker(Color.White);

            // Now, for each frame of the video
            for (int frameIndex = 0; frameIndex < video.FrameCount; frameIndex++)
            {
                // Read the current frame into the bitmap data
                video.ReadVideoFrame(frameIndex, bitmapData);

                if (frameIndex > 150) // wait until the bike rider enters the scene
                {
                    // Feed the frame to the tracker
                    tracker.ProcessFrame(unmanagedImage);

                    // Mark the location of the tracker object in red color
                    objectMarker.SingleRectangle = tracker.TrackingObject.Rectangle;
                    objectMarker.ApplyInPlace(unmanagedImage); // overwrite the frame

                    windowMarker.SingleRectangle = tracker.SearchWindow;
                    windowMarker.ApplyInPlace(unmanagedImage); // overwrite the frame
                }

                // Save it to disk
                frame.Save(Path.Combine(basePath, "frame_{0}.png".Format(frameIndex)));
            }

            frame.UnlockBits(bitmapData);
            #endregion
        }