Ejemplo n.º 1
0
        public void SetHistory(FaceRegion2D parent)
        {
            Id = parent.Id;
            BoundingBoxColor = parent.BoundingBoxColor;
            EyeAngle        += parent.EyeAngle;

            History = parent.History == null
                          ? new[] { new FaceRegion2DHistory(parent) }
                          : new[] { new FaceRegion2DHistory(parent) }.Union(parent.History).Take(10);
        }
Ejemplo n.º 2
0
        private void ProcessFrame()
        {
            while (_captureContinue)
            {
                while (_processQueueLength != 2)
                {
                    Thread.Sleep(SLEEP_INTERVAL);
                }

                if ((Helper2D.FilterInstances.Mode == Mode.Cpu && useGpuMenuItem.Checked)
                    || (Helper2D.FilterInstances.Mode == Mode.Gpu && useGpuMenuItem.Checked == false))
                {
                    Helper2D.FilterInstances = new FilterInstances(useGpuMenuItem.Checked ? Mode.Gpu : Mode.Cpu);
                }

                double distance;

                if (_cameras[0].FaceRegions == null
                    || _cameras[1].FaceRegions == null)
                {
                    _resetCorrelation = true;
                }

                if (_resetCorrelation == false)
                {
                    #region Find correlation with previous face regions

                    // iterate through all cameras and track faces
                    foreach (var camera in _cameras)
                    {
                        var newRawFaceRegions = new List<FaceRegion2D>();

                        // iterate through every face found previously, rotate image and find faces
                        foreach (var faceRegion in camera.FaceRegions)
                        {
                            var image = camera.Image.Rotate(faceRegion.EyeAngle,
                                new PointF(faceRegion.Face.Location.X + faceRegion.Face.Width / 2, faceRegion.Face.Location.Y + faceRegion.Face.Height / 2),
                                    INTER.CV_INTER_CUBIC, _rotateBackground, true);

                            if (_debugRotation)
                            {
                                camera.Image = image;
                            }

                            // find faces in rotated image
                            newRawFaceRegions.AddRange(Helper2D.GetFaceRegion2Ds(image, FACE_WIDTH, FACE_HEIGHT, true, false));
                        }

                        // find best corespondence between old faces and new faces
                        IEnumerable<Tuple<int, int>> corespondences = Helper.FindCorespondence
                            (camera.FaceRegions.Select(item => item.Face.Location).ToArray(),
                            newRawFaceRegions.Select(item => item.Face.Location).ToArray(),
                            out distance);

                        if (corespondences == null
                            || corespondences.Any() == false)
                        {
                            // face regions lost .. RESET both cameras
                            _resetCorrelation = true;
                            break;
                        }

                        var newFaceRegions = new FaceRegion2D[corespondences.Count()];

                        for (int i = 0; i < corespondences.Count(); i++)
                        {
                            FaceRegion2D faceRegion = newRawFaceRegions.ElementAt(corespondences.ElementAt(i).Item2);

                            faceRegion.SetHistory(camera.FaceRegions.ElementAt(corespondences.ElementAt(i).Item1));

                            newFaceRegions[i] = faceRegion;
                        }

                        camera.FaceRegions = newFaceRegions;
                    }

                    #endregion
                }

                if (_resetCorrelation)
                {
                    #region Reset Found Faces

                    foreach (var camera in _cameras)
                    {
                        camera.FaceRegions = Helper2D.GetFaceRegion2Ds(camera.Image, FACE_WIDTH, FACE_HEIGHT, true, false);
                    }

                    #endregion
                }

                if (_cameras[0].FaceRegions.Length > 0
                    && _cameras[1].FaceRegions.Length > 0)
                {
                    #region Find correlation in stereo images and add history

                    IEnumerable<Tuple<int, int>> correlations;

                    var points = _cameras.Select(camera => camera.FaceRegions.
                            Select(region => region.Face.Location)).ToArray();

                    correlations = Helper.FindCorespondence(points.ElementAt(0), points.ElementAt(1), out distance);

                    // images have incorect correlations and history
                    if (_resetCorrelation == false
                        && correlations.Any(item => _cameras[0].FaceRegions.ElementAt(item.Item1).Id != _cameras[1].FaceRegions.ElementAt(item.Item2).Id))
                    {
                        _resetCorrelation = true;
                    }

                    if (_resetCorrelation)
                    {
                        // assign faces color and Id
                        foreach (var correlation in correlations)
                        {
                            var color = new Bgr(_random.NextDouble() * 255, _random.NextDouble() * 255, _random.NextDouble() * 255);

                            var leftFaceRegion = _cameras[0].FaceRegions.ElementAt(correlation.Item1);
                            var rightFaceRegion = _cameras[1].FaceRegions.ElementAt(correlation.Item2);

                            rightFaceRegion.Id = leftFaceRegion.Id;

                            leftFaceRegion.BoundingBoxColor = color;
                            rightFaceRegion.BoundingBoxColor = color;
                        }
                    }

                    #endregion

                    #region Recognize Faces

                    _cameras.ForEach(camera =>
                    {
                        if (camera.FaceRegions != null)
                        {
                            camera.FaceRegions.ToList().ForEach(faceRegion =>
                            {
                                Helper.DrawFaceRegionCircle(camera.Image, faceRegion, faceRegion.BoundingBoxColor);

                                var label = HelperFaces.Recognize(faceRegion.FaceImage);

                                camera.Image.Draw(string.Format("{0}", label),
                                    ref _font, faceRegion.Face.Location, new Bgr(0, 0, 255));
                            });
                        }
                    });

                    #endregion

                    var facesAvailableHandler = FacesAvailable;
                    if (facesAvailableHandler != null)
                    {
                        facesAvailableHandler(this, new FaceRegionsEventArgs(_cameras[0].FaceRegions, _cameras[1].FaceRegions, null));
                    }

                    _faces = _cameras.SelectMany(camera => camera.FaceRegions).Select(item => item.FaceImage).ToArray();
                }

                _resetCorrelation = false;

                PostProcess();

                lock (this)
                {
                    _processQueueLength = 0;
                }
            }
        }
Ejemplo n.º 3
0
        public void SetHistory(FaceRegion2D parent)
        {
            Id = parent.Id;
            BoundingBoxColor = parent.BoundingBoxColor;
            EyeAngle += parent.EyeAngle;

            History = parent.History == null
                          ? new[] {new FaceRegion2DHistory(parent)}
                          : new[] {new FaceRegion2DHistory(parent)}.Union(parent.History).Take(10);
        }