コード例 #1
0
 public FaceDetectSample()
 {
     InitializeComponent();
     pictureBoxGraphics = pictureBox1.CreateGraphics();
     eyeMark            = 0.06F;
     try
     {
         faceDetector = new FaceDetector(faceDetectorData, true, 0.0F);
     }
     catch (Exception)
     {
         try
         {
             // When built in  sample directory the following might find the classifier data file
             faceDetector = new FaceDetector(Path.Combine(@"..\..\..\release", faceDetectorData), true, 0.0F);
         }
         catch (Exception)
         {
             throw new Exception("Failed to initialize faceDetector Perhaps data file " + faceDetectorData + " is unavailable");
         }
     }
     eyeDetect       = new EyeDetect();
     leftEyeRects    = new List <RectangleF>();
     rightEyeRects   = new List <RectangleF>();
     noseRects       = new List <RectangleF>();
     leftMouthRects  = new List <RectangleF>();
     rightMouthRects = new List <RectangleF>();
 }
コード例 #2
0
        private bool DoEyeDetect(Rect faceRect, out System.Windows.Point leftEye, out System.Windows.Point rightEye)
        {
            leftEye  = new System.Windows.Point();
            rightEye = new System.Windows.Point();

            if (null == _facePixs)
            {
                return(false);
            }

            if (null == _eyeDetect)
            {
                _eyeDetect = new EyeDetect();
            }


            EyeDetectResult eyeResult = _eyeDetect.Detect(_facePixs, (int)_eyeDetectFaceRect.Width, (int)_eyeDetectFaceRect.Height);

            leftEye.X  = eyeResult.LeftEye.X * faceRect.Width / _eyeDetectFaceRect.Width + faceRect.X;
            leftEye.Y  = eyeResult.LeftEye.Y * faceRect.Height / _eyeDetectFaceRect.Height + faceRect.Y;
            rightEye.X = eyeResult.RightEye.X * faceRect.Width / _eyeDetectFaceRect.Width + faceRect.X;
            rightEye.Y = eyeResult.RightEye.Y * faceRect.Height / _eyeDetectFaceRect.Height + faceRect.Y;

            return(true);
        }
コード例 #3
0
        //static void RunDetection(string filename, Rect rect, Point leftEye, Point rightEye, ref byte [] facePix, Rect faceRect)
        static void RunDetection(string filename, Rect rect, FaceDisp.FaceData faceData, ref byte [] facePix, Rect faceRect)
        {
            EyeDetect eyeDetect       = new EyeDetect();
            int       byteCountPerPix = (int)(facePix.Length / faceRect.Width / faceRect.Height);

            bool isSuccess = eyeDetect.SetAlgorithm(_algo, _algoData);

            if (true == isSuccess)
            {
                EyeDetectResult eyeResult = eyeDetect.Detect(facePix, (int)_faceDisplayWidth, (int)_faceDisplayWidth);

                _outStream.WriteLine("{0}", filename);
                _outStream.Write("{0} {1} {2} {3} ", (int)rect.X, (int)rect.Y, (int)rect.Width, (int)rect.Height);

                if (faceData.TrueLeftEye.X > 1.0)
                {
                    Point leftEye  = FaceFeatureToScaledPoint(faceData.TrueLeftEye, rect);
                    Point rightEye = FaceFeatureToScaledPoint(faceData.TrueRightEye, rect);

                    _outStream.Write("{0:F3} {1:F3} {2:F3} {3:F3} ", leftEye.X, leftEye.Y, rightEye.X, rightEye.Y);
                }
                else
                {
                    _outStream.Write("{0:F3} {1:F3} {2:F3} {3:F3} ", faceData.TrueLeftEye.X, faceData.TrueLeftEye.Y, faceData.TrueRightEye.X, faceData.TrueRightEye.Y);
                }
                _outStream.Write("{0:F3} {1:F3} {2:F3} {3:F3} ", eyeResult.LeftEye.X / _faceDisplayWidth, eyeResult.LeftEye.Y / _faceDisplayWidth,
                                 eyeResult.RightEye.X / _faceDisplayWidth, eyeResult.RightEye.Y / _faceDisplayWidth);

                FaceFeatureResult res = eyeResult as FaceFeatureResult;
                if (null != res)
                {
                    if (faceData.Nose.X > 1.0)
                    {
                        Point nose       = FaceFeatureToScaledPoint(faceData.Nose, rect);
                        Point leftMouth  = FaceFeatureToScaledPoint(faceData.LeftMouth, rect);
                        Point rightMouth = FaceFeatureToScaledPoint(faceData.RightMouth, rect);

                        _outStream.Write("{0:F3} {1:F3} ", nose.X, nose.Y);
                        _outStream.Write("{0:F3} {1:F3} {2:F3} {3:F3} ", leftMouth.X, leftMouth.Y, rightMouth.X, rightMouth.Y);
                    }
                    else
                    {
                        _outStream.Write("{0:F3} {1:F3} ", faceData.Nose.X, faceData.Nose.Y);
                        _outStream.Write("{0:F3} {1:F3} {2:F3} {3:F3} ", faceData.LeftMouth.X, faceData.LeftMouth.Y, faceData.RightMouth.X, faceData.RightMouth.Y);
                    }
                    _outStream.Write("{0:F3} {1:F3} ", res.Nose.X / _faceDisplayWidth, res.Nose.Y / _faceDisplayWidth);
                    _outStream.Write("{0:F3} {1:F3} {2:F3} {3:F3} ", res.LeftMouth.X / _faceDisplayWidth, res.LeftMouth.Y / _faceDisplayWidth,
                                     res.RightMouth.X / _faceDisplayWidth, res.RightMouth.Y / _faceDisplayWidth);
                }
                if (_maxTransformCount > 0)
                {
                    _outStream.Write("{0:F3} {1:F3} {2:F3}", _transform.Theta, _transform.X, _transform.Y);
                }
                _outStream.WriteLine();
            }
            else
            {
                _outStream.WriteLine("Detection failed on {0}", filename);
            }
        }
コード例 #4
0
 public Detect()
 {
     try
     {
         string dataPath = FindParamFile(Path.GetFullPath(faceDetectorData));
         faceDetector = new FaceDetector(dataPath, true, 0.0F);
     }
     catch (Exception)
     {
         throw new Exception("Failed to initialize faceDetector Perhaps data file " + faceDetectorData + " is unavailable");
     }
     eyeDetect     = new EyeDetect();
     leftEyeRects  = new List <RectangleF>();
     rightEyeRects = new List <RectangleF>();
 }
コード例 #5
0
        /// <summary>
        /// Initialize a photo by displaying detected faces
        /// </summary>
        /// <param name="mainCanvas">Main canvas reference</param>
        /// <param name="filename">Full path name to image file</param>
        /// <param name="faceRectList">Detected location of faces</param>
        /// <param name="borderWidth">Border width to use around faces</param>
        /// <param name="faceIdList">Id's to use for each detected face </param>
        /// <returns>Number of faces displayed</returns>
        private int InitializeInternal(BackgroundCanvas mainCanvas, string filename, List <Rect> faceRectList,
                                       int borderWidth, List <int> faceIdList)
        {
            _faceRectList = faceRectList;
            _mainCanvas   = mainCanvas;
            _pathName     = filename;
            _fileName     = System.IO.Path.GetFileName(_pathName);

            Dpu.ImageProcessing.Image[] dataPixs = CreateMainBitMap(filename);

            _border = new Border();

            RenderTransform = new ScaleTransform(1.0, 1.0);;

            _childImage             = new Image();
            _childImage.Source      = _bitmap;
            _border.Child           = _childImage;
            _border.BorderThickness = new Thickness(borderWidth);
            _border.BorderBrush     = Brushes.Black;
            _border.Background      = Brushes.White;

            Children.Add(_border);

            double    xScale            = _bitmap.PixelWidth / _originalRect.Width;
            double    yScale            = _bitmap.PixelHeight / _originalRect.Height;
            bool      doEyeDetect       = false;
            EyeDetect eyeDetect         = null;
            Rect      displayFaceRect   = new Rect(0, 0, _faceDisplayWidth, _faceDisplayWidth);
            Rect      eyeDetectFaceRect = displayFaceRect;

            if (_leftEyeList == null)
            {
                doEyeDetect = true;
                eyeDetect   = new EyeDetect();
                eyeDetect.SetAlgorithm(_mainCanvas.OptionDialog.EyeDetectAlgo, _mainCanvas.OptionDialog.EyeDetectPathName);
                if (_mainCanvas.OptionDialog.EyeDetectAlgo == EyeDetect.AlgorithmEnum.NN)
                {
                    eyeDetectFaceRect = new Rect(0, 0, 41, 41);
                }
            }

            _outlineRectList = new List <Rectangle>();
            _faceList        = new List <Face>();

            for (int iRect = 0; iRect < faceRectList.Count; ++iRect)
            {
                Rectangle outlineRect = new Rectangle();

                Rect rect = faceRectList[iRect];
                outlineRect.Width  = rect.Width * xScale;
                outlineRect.Height = rect.Height * yScale;
                SetLeft(outlineRect, rect.X * xScale + _border.BorderThickness.Left);
                SetTop(outlineRect, rect.Y * yScale + _border.BorderThickness.Top);
                outlineRect.Stroke     = Brushes.Blue;
                outlineRect.Visibility = Visibility.Visible;
                outlineRect.Opacity    = 1.0;

                Children.Add(outlineRect);

                Image faceImage   = new Image();
                Point targetLeft  = new Point(0, 0);
                Point targetRight = new Point(0, _faceDisplayWidth);

                BitmapSource newbitMap = SelectAndNormalizeBitmap(dataPixs,
                                                                  new Point(rect.X, rect.Y), new Point(rect.X + rect.Width, rect.Y),
                                                                  new Point(0, 0), new Point(_faceDisplayWidth, 0), displayFaceRect);

                int   eyeId    = FindEyes(_leftEyeList, _rightEyeList, rect);
                Point leftEye  = new Point(0, 0);
                Point rightEye = new Point(0, 0);

                if (eyeId >= 0)
                {
                    leftEye  = _leftEyeList[eyeId];
                    rightEye = _rightEyeList[eyeId];
                }
                else if (doEyeDetect == true)
                {
                    byte[] faceEyeDetect;
                    int    faceDetectPixCount = (int)(eyeDetectFaceRect.Width * eyeDetectFaceRect.Height);

                    faceEyeDetect = SelectAndNormalizePatch(dataPixs,
                                                            new Point(rect.X, rect.Y), new Point(rect.X + rect.Width, rect.Y),
                                                            new Point(0, 0), new Point(eyeDetectFaceRect.Width, 0), eyeDetectFaceRect);


                    faceEyeDetect = ConvertToGreyScale(faceEyeDetect, faceEyeDetect.Length / faceDetectPixCount);

                    EyeDetectResult eyeResult = eyeDetect.Detect(faceEyeDetect,
                                                                 (int)eyeDetectFaceRect.Width, (int)eyeDetectFaceRect.Height);
                    leftEye.X  = eyeResult.LeftEye.X * rect.Width / eyeDetectFaceRect.Width + rect.X;
                    leftEye.Y  = eyeResult.LeftEye.Y * rect.Height / eyeDetectFaceRect.Height + rect.Y;
                    rightEye.X = eyeResult.RightEye.X * rect.Width / eyeDetectFaceRect.Width + rect.X;
                    rightEye.Y = eyeResult.RightEye.Y * rect.Height / eyeDetectFaceRect.Height + rect.Y;
                }
                // else This is case where eye list is supplied an dthis face was not found - do not add this face

                BitmapSource normBitMap = SelectAndNormalizeBitmap(dataPixs,
                                                                   leftEye, rightEye,
                                                                   _mainCanvas.OptionDialog.NormalizeLeftEyeLocation, _mainCanvas.OptionDialog.NormalizeRightEyeLocation,
                                                                   displayFaceRect);


                if (null != newbitMap && null != normBitMap)
                {
                    int faceID;
                    if (null != faceIdList && iRect < faceIdList.Count)
                    {
                        faceID = faceIdList[iRect];
                    }
                    else
                    {
                        faceID = mainCanvas.CreateNewObjectID();
                    }
                    Face face = new Face(mainCanvas, filename, faceID, normBitMap, newbitMap, iRect, this);
                    if (null != normBitMap)
                    {
                        leftEye.X  = (leftEye.X - rect.X) * _faceDisplayWidth / rect.Width;
                        leftEye.Y  = (leftEye.Y - rect.Y) * _faceDisplayWidth / rect.Height;
                        rightEye.X = (rightEye.X - rect.X) * _faceDisplayWidth / rect.Width;
                        rightEye.Y = (rightEye.Y - rect.Y) * _faceDisplayWidth / rect.Height;

                        face.LeftEye  = leftEye;
                        face.RightEye = rightEye;
                    }
                    mainCanvas.AddFace(face);
                    _faceList.Add(face);
                    _outlineRectList.Add(outlineRect);
                }
                else
                {
                    //Face is is not used remove the rect
                    _faceList.Add(null);
                }
            }

            MakeInvisible();
            return(_outlineRectList.Count);
        }
コード例 #6
0
        static void Main(string[] args)
        {
            EyeDetect d = new EyeDetect();

            Console.ReadKey();
        }