public DLibFaceIdentification(IImageRotationService imageRotationService)
        {
            this.imageRotationService = imageRotationService ?? throw new ArgumentNullException(nameof(imageRotationService));
            detector = Dlib.GetFrontalFaceDetector();

            // set up a 5-point landmark detector
            predictor = ShapePredictor.Deserialize("model/shape_predictor_5_face_landmarks.dat");

            // set up a neural network for face recognition
            dnn = DlibDotNet.Dnn.LossMetric.Deserialize("model/dlib_face_recognition_resnet_model_v1.dat");

            // create a color palette for plotting
            palette = new RgbPixel[]
            {
                new RgbPixel(0xe6, 0x19, 0x4b),
                new RgbPixel(0xf5, 0x82, 0x31),
                new RgbPixel(0xff, 0xe1, 0x19),
                new RgbPixel(0xbc, 0xf6, 0x0c),
                new RgbPixel(0x3c, 0xb4, 0x4b),
                new RgbPixel(0x46, 0xf0, 0xf0),
                new RgbPixel(0x43, 0x63, 0xd8),
                new RgbPixel(0x91, 0x1e, 0xb4),
                new RgbPixel(0xf0, 0x32, 0xe6),
                new RgbPixel(0x80, 0x80, 0x80)
            };
        }
示例#2
0
        /// <summary>
        /// Processor initializer
        /// <param name="facesCascadeData">String with cascade XML for face detection, must be defined</param>
        /// <param name="eyesCascadeData">String with cascade XML for eyes detection, can be null</param>
        /// <param name="shapeData">Binary data with trained shape predictor for 68-point face landmarks recognition, can be empty or null</param>
        /// </summary>
        public virtual void Initialize(string facesCascadeData, string eyesCascadeData, byte[] shapeData = null)
        {
            // face detector - the key thing here
            if (null == facesCascadeData || facesCascadeData.Length == 0)
            {
                throw new Exception("FaceProcessor.Initialize: No face detector cascade passed, with parameter is required");
            }

            FileStorage storageFaces = new FileStorage(facesCascadeData, FileStorage.Mode.Read | FileStorage.Mode.Memory);

            cascadeFaces = new CascadeClassifier();
            if (!cascadeFaces.Read(storageFaces.GetFirstTopLevelNode()))
            {
                throw new System.Exception("FaceProcessor.Initialize: Failed to load faces cascade classifier");
            }

            // eyes detector
            if (null != eyesCascadeData)
            {
                FileStorage storageEyes = new FileStorage(eyesCascadeData, FileStorage.Mode.Read | FileStorage.Mode.Memory);
                cascadeEyes = new CascadeClassifier();
                if (!cascadeEyes.Read(storageEyes.GetFirstTopLevelNode()))
                {
                    throw new System.Exception("FaceProcessor.Initialize: Failed to load eyes cascade classifier");
                }
            }

            // shape detector
            if (null != shapeData && shapeData.Length > 0)
            {
                shapeFaces = new ShapePredictor();
                shapeFaces.LoadData(shapeData);
            }
        }
示例#3
0
 public MainForm()
 {
     this.InitializeComponent();
     this._ShapePredictor           = ShapePredictor.Deserialize("shape_predictor_68_face_landmarks.dat");
     this._BackgroundWorker         = new BackgroundWorker();
     this._BackgroundWorker.DoWork += this.BackgroundWorkerOnDoWork;
 }
示例#4
0
        public void LandmarkTest()
        {
            using (var faceDetector = FrontalFaceDetector.GetFrontalFaceDetector())
                using (var shapePredictor = new ShapePredictor(Configuration.SHAP_PREDICTOR_CONFIG))
                {
                    // convert image to dlib format
                    var img = Utils.LoadImageAsBitmap("TestImage/pic01.jpg").ToArray2D <RgbPixel>();

                    // detect faces
                    var faces = faceDetector.Detect(img);

                    // detect facial landmarks
                    foreach (var rect in faces)
                    {
                        // detect facial landmarks
                        var shape = shapePredictor.Detect(img, rect);

                        //The left eye using landmark index[42, 47].
                        Landmarks landmarkLeftEye = new Landmarks(42, 47, shape);
                        Assert.AreEqual(landmarkLeftEye.LandMarkPointList.Count, 6);
                        //index range should be 0-67
                        Landmarks landmark2 = new Landmarks(42, 68, shape);
                    }
                }
        }
示例#5
0
        public void CreateShapePredictor2()
        {
            var path      = this.GetDataFile("shape_predictor_68_face_landmarks.dat");
            var predictor = ShapePredictor.Deserialize(File.ReadAllBytes(path.FullName));

            this.DisposeAndCheckDisposedState(predictor);
        }
        // The main program entry point
        static void Main(string[] args)
        {
            using (var fd = Dlib.GetFrontalFaceDetector())
                using (var sp = ShapePredictor.Deserialize("shape_predictor_68_face_landmarks.dat"))
                {
                    // Load image from file
                    var img = Dlib.LoadImage <RgbPixel>(inputFilePath);

                    // Detect all faces
                    var faces = fd.Operator(img);

                    foreach (var face in faces)
                    {
                        // Find the landmark points for this face
                        var shape = sp.Detect(img, face);

                        // Loop through detected landmarks
                        for (int i = 0; i < shape.Parts; i++)
                        {
                            var point = shape.GetPart((uint)i);
                            var rect  = new Rectangle(point);
                            Dlib.DrawRectangle(img, rect, color: new RgbPixel(255, 255, 0), thickness: 4);
                        }
                    }

                    // Save the result
                    Dlib.SaveJpeg(img, "output.jpg");
                }
        }
示例#7
0
        /// <summary>
        /// The main program entry point
        /// </summary>
        /// <param name="args">The command line arguments</param>
        static void Main(string[] args)
        {
            // set up Dlib facedetectors and shapedetectors
            using (var fd = Dlib.GetFrontalFaceDetector())
                using (var sp = ShapePredictor.Deserialize("shape_predictor_68_face_landmarks.dat"))
                {
                    // load input image
                    var img = Dlib.LoadImage <RgbPixel>(inputFilePath);

                    // find all faces in the image
                    var faces = fd.Operator(img);
                    foreach (var face in faces)
                    {
                        // find the landmark points for this face
                        var shape = sp.Detect(img, face);

                        // draw the landmark points on the image
                        for (var i = 0; i < shape.Parts; i++)
                        {
                            var point = shape.GetPart((uint)i);
                            var rect  = new Rectangle(point);
                            Dlib.DrawRectangle(img, rect, color: new RgbPixel(255, 255, 0), thickness: 4);
                        }
                    }

                    // export the modified image
                    Dlib.SaveJpeg(img, "output.jpg");
                }
        }
示例#8
0
 public ImageUtils()
 {
     detector = Dlib.GetFrontalFaceDetector();
     sp       =
         ShapePredictor.Deserialize(
             @"..\External\shape_predictor_68_face_landmarks.dat");
 }
示例#9
0
 public void GetEyePixels(Image img)
 {
     using (var fd = Dlib.GetFrontalFaceDetector())
         using (var sp = ShapePredictor.Deserialize("shape_predictor_68_face_landmarks.dat"))
         {
             //Dlib.LoadImageData(ImagePixelFormat.Bgr,  img.W, img.H, 3)
         }
 }
        /// <summary>
        /// Initializes a new instance of the <see cref="FaceRecognition"/> class with the directory path that stores model files.
        /// </summary>
        /// <param name="directory">The directory path that stores model files.</param>
        /// <exception cref="FileNotFoundException">The model file is not found.</exception>
        /// <exception cref="DirectoryNotFoundException">The specified directory path is not found.</exception>
        private FaceRecognition(string directory)
        {
            if (!Directory.Exists(directory))
            {
                throw new DirectoryNotFoundException(directory);
            }

            var predictor68PointModel = Path.Combine(directory, FaceRecognitionModels.GetPosePredictorModelLocation());

            if (!File.Exists(predictor68PointModel))
            {
                throw new FileNotFoundException(predictor68PointModel);
            }

            var predictor5PointModel = Path.Combine(directory, FaceRecognitionModels.GetPosePredictorFivePointModelLocation());

            if (!File.Exists(predictor5PointModel))
            {
                throw new FileNotFoundException(predictor5PointModel);
            }

            var cnnFaceDetectionModel = Path.Combine(directory, FaceRecognitionModels.GetCnnFaceDetectorModelLocation());

            if (!File.Exists(cnnFaceDetectionModel))
            {
                throw new FileNotFoundException(cnnFaceDetectionModel);
            }

            var faceRecognitionModel = Path.Combine(directory, FaceRecognitionModels.GetFaceRecognitionModelLocation());

            if (!File.Exists(faceRecognitionModel))
            {
                throw new FileNotFoundException(faceRecognitionModel);
            }

            this._FaceDetector?.Dispose();
            this._FaceDetector = DlibDotNet.Dlib.GetFrontalFaceDetector();

            this._PosePredictor68Point?.Dispose();
            this._PosePredictor68Point = ShapePredictor.Deserialize(predictor68PointModel);

            this._PosePredictor5Point?.Dispose();
            this._PosePredictor5Point = ShapePredictor.Deserialize(predictor5PointModel);

            this._CnnFaceDetector?.Dispose();
            this._CnnFaceDetector = LossMmod.Deserialize(cnnFaceDetectionModel);

            this._FaceEncoder?.Dispose();
            this._FaceEncoder = LossMetric.Deserialize(faceRecognitionModel);

            var predictor194PointModel = Path.Combine(directory, FaceRecognitionModels.GetPosePredictor194PointModelLocation());

            if (File.Exists(predictor194PointModel))
            {
                this._PosePredictor194Point?.Dispose();
                this._PosePredictor194Point = ShapePredictor.Deserialize(predictor194PointModel);
            }
        }
示例#11
0
        /// <summary>
        /// Initializes a new instance of the <see cref="HelenFaceLandmarkDetector"/> class with the model file path that this detector uses.
        /// </summary>
        /// <param name="modelPath">The model file path that this detector uses.</param>
        /// <exception cref="FileNotFoundException">The model file is not found.</exception>
        public HelenFaceLandmarkDetector(string modelPath)
        {
            if (!File.Exists(modelPath))
            {
                throw new FileNotFoundException(modelPath);
            }

            this._Predictor = ShapePredictor.Deserialize(modelPath);
        }
        public DLibFaceIdentificationService(IImageRotationService imageRotationService)
        {
            this.imageRotationService = imageRotationService ?? throw new ArgumentNullException(nameof(imageRotationService));

            // set up a 5-point landmark detector
            predictor = ShapePredictor.Deserialize("model/shape_predictor_5_face_landmarks.dat");

            // set up a neural network for face recognition
            dnn = DlibDotNet.Dnn.LossMetric.Deserialize("model/dlib_face_recognition_resnet_model_v1.dat");
        }
示例#13
0
 public void computeKeyPoint(string imgPath, bool isSource)
 {
     using (var detector = Dlib.GetFrontalFaceDetector())
     {
         using (var sp = ShapePredictor.Deserialize("../../../shape_predictor_68_face_landmarks.dat"))
         {
             using (var img = Dlib.LoadImage <RgbPixel>(imgPath))
             {
                 Dlib.PyramidUp(img);
                 var dets = detector.Operator(img);
                 if (dets.Length == 0)
                 {
                     MessageBox.Show("图中未检测到人脸", "Warning");
                     return;
                 }
                 else if (dets.Length > 1)
                 {
                     MessageBox.Show("图中检测到多张人脸,取其中一张进行变换", "Warning");
                     return;
                 }
                 var shape = sp.Detect(img, dets[0]);
                 if (isSource)
                 {
                     if (sourceKeyPoint != null)
                     {
                         sourceKeyPoint.Clear();
                     }
                     else
                     {
                         sourceKeyPoint = new List <PointF>(68);
                     }
                     for (uint i = 0; i < 68; ++i)
                     {
                         sourceKeyPoint.Add(new PointF((float)shape.GetPart(i).X / 2, (float)shape.GetPart(i).Y / 2));
                     }
                 }
                 else
                 {
                     if (targetKeyPoint != null)
                     {
                         targetKeyPoint.Clear();
                     }
                     else
                     {
                         targetKeyPoint = new List <PointF>(68);
                     }
                     for (uint i = 0; i < 68; ++i)
                     {
                         targetKeyPoint.Add(new PointF((float)shape.GetPart(i).X / 2, (float)shape.GetPart(i).Y / 2));
                     }
                 }
             }
         }
     }
 }
示例#14
0
        private static void Main()
        {
            try
            {
                // You can get this file from http://dlib.net/files/mmod_front_and_rear_end_vehicle_detector.dat.bz2
                // This network was produced by the dnn_mmod_train_find_cars_ex.cpp example program.
                // As you can see, the file also includes a separately trained shape_predictor.  To see
                // a generic example of how to train those refer to train_shape_predictor_ex.cpp.
                using (var deserialize = new ProxyDeserialize("mmod_front_and_rear_end_vehicle_detector.dat"))
                    using (var net = LossMmod.Deserialize(deserialize, 1))
                        using (var sp = ShapePredictor.Deserialize(deserialize))
                            using (var img = Dlib.LoadImageAsMatrix <RgbPixel>("mmod_cars_test_image2.jpg"))
                                using (var win = new ImageWindow())
                                {
                                    win.SetImage(img);

                                    // Run the detector on the image and show us the output.
                                    var dets = net.Operator(img).First();
                                    foreach (var d in dets)
                                    {
                                        // We use a shape_predictor to refine the exact shape and location of the detection
                                        // box.  This shape_predictor is trained to simply output the 4 corner points of
                                        // the box.  So all we do is make a rectangle that tightly contains those 4 points
                                        // and that rectangle is our refined detection position.
                                        var fd   = sp.Detect(img, d);
                                        var rect = Rectangle.Empty;
                                        for (var j = 0u; j < fd.Parts; ++j)
                                        {
                                            rect += fd.GetPart(j);
                                        }

                                        if (d.Label == "rear")
                                        {
                                            win.AddOverlay(rect, new RgbPixel(255, 0, 0), d.Label);
                                        }
                                        else
                                        {
                                            win.AddOverlay(rect, new RgbPixel(255, 255, 0), d.Label);
                                        }
                                    }

                                    Console.WriteLine("Hit enter to end program");
                                    Console.ReadKey();
                                }
            }
            catch (ImageLoadException ile)
            {
                Console.WriteLine(ile.Message);
                Console.WriteLine("The test image is located in the examples folder.  So you should run this program from a sub folder so that the relative path is correct.");
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
 public static void DrawPointsOfLandmarks(FileInfo image)
 {
     using (var fd = Dlib.GetFrontalFaceDetector())
         using (var sp = ShapePredictor.Deserialize(GetFile(ShapePredictorFileName).FullName))
         {
             using (var img = Dlib.LoadImage <RgbPixel>(image.FullName))
             {
                 var faces = fd.Operator(img);
                 // for each face draw over the facial landmarks
                 foreach (var face in faces)
                 {
                     var shape = sp.Detect(img, face);
                     // draw the landmark points on the image
                     for (var i = 0; i < shape.Parts; i++)
                     {
                         var point = shape.GetPart((uint)i);
                         var rect  = new Rectangle(point);
                         if (i == 0)
                         {
                             Dlib.DrawRectangle(img, rect, color: new RgbPixel(255, 255, 255), thickness: 8);
                         }
                         else if (i == 21 || i == 22 || i == 39 || i == 42 || i == 33 || i == 51 || i == 57 ||
                                  i == 48 ||
                                  i == 54)
                         {
                             Dlib.DrawRectangle(img, rect, color: new RgbPixel(255, 0, 255), thickness: 4);
                         }
                         else if (i == 18 || i == 19 || i == 20 || i == 21) // left eye
                         {
                             Dlib.DrawRectangle(img, rect, color: new RgbPixel(255, 0, 0), 6);
                         }
                         else if (i == 22 || i == 23 || i == 24 || i == 25) // right eye
                         {
                             Dlib.DrawRectangle(img, rect, color: new RgbPixel(255, 128, 0), 6);
                         }
                         else if (i == 48 || i == 49 || i == 50) // left lip
                         {
                             Dlib.DrawRectangle(img, rect, color: new RgbPixel(255, 255, 0), 2);
                         }
                         else if (i == 52 || i == 53 || i == 54) // right lip
                         {
                             Dlib.DrawRectangle(img, rect, color: new RgbPixel(255, 0, 128), 2);
                         }
                         else
                         {
                             Dlib.DrawRectangle(img, rect, color: new RgbPixel(0, 0, 0), thickness: 4);
                         }
                     }
                     Dlib.SavePng(img, "output.jpg");
                 }
             }
         }
 }
        /// <summary>
        /// Called when MainForm loads.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MainForm_Load(object sender, EventArgs e)
        {
            SetCamera();

            // initialize face detector
            faceDetector = FrontalFaceDetector.GetFrontalFaceDetector();

            // initialize shape predictor
            shapePredictor = new ShapePredictor("shape_predictor_68_face_landmarks.dat");

            // start the player
            videoPlayer.Start();
        }
示例#17
0
        public void DetectFace4()
        {
            var model = this.GetDataFile("shape_predictor_68_face_landmarks.dat");

            var predictor1 = ShapePredictor.Deserialize(model.FullName);
            var predictor2 = ShapePredictor.Deserialize(File.ReadAllBytes(model.FullName));

            if (this._ShapePredictor == null)
            {
                Assert.True(false, "ShapePredictor is not initialized!!");
            }

            string testName = $"{nameof(DetectFace4)}";
            var    path     = this.GetDataFile("Lenna_mini.bmp");
            var    tests    = new[]
            {
                new { Type = MatrixElementTypes.RgbPixel, ExpectResult = true },
            };

            using (var faceDetector = Dlib.GetFrontalFaceDetector())
                using (var matrix = Dlib.LoadImageAsMatrix <RgbPixel>(path.FullName))
                {
                    var dets    = faceDetector.Operator(matrix);
                    var shapes1 = dets.Select(r => predictor1.Detect(matrix, r)).ToList();
                    var shapes2 = dets.Select(r => predictor2.Detect(matrix, r)).ToList();
                    Assert.Equal(shapes1.Count, shapes2.Count);

                    for (var index = 0; index < shapes1.Count; index++)
                    {
                        var shape1 = shapes1[index];
                        var shape2 = shapes2[index];

                        var r1     = shape1.Rect;
                        var r2     = shape2.Rect;
                        var parts1 = shape1.Parts;
                        var parts2 = shape2.Parts;

                        for (uint i = 0; i < parts1; i++)
                        {
                            var part1 = shape1.GetPart(i);
                            var part2 = shape2.GetPart(i);

                            Assert.Equal(part1.X, part2.X);
                            Assert.Equal(part1.Y, part2.Y);
                        }
                    }
                }

            this.DisposeAndCheckDisposedState(predictor2);
            this.DisposeAndCheckDisposedState(predictor1);
        }
        /// <summary>
        /// Called when MainForm loads.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MainForm_Load(object sender, EventArgs e)
        {
            SetCamera();

            // load the beard image
            beardImage = Bitmap.FromFile(@".\beard.png") as Bitmap;

            // set up our face detector and shape predictor
            faceDetector   = FrontalFaceDetector.GetFrontalFaceDetector();
            shapePredictor = new ShapePredictor("shape_predictor_68_face_landmarks.dat");

            // start the player
            videoPlayer.Start();
        }
示例#19
0
 private List <List <Point2f> > DetectFaces(FrontalFaceDetector detector, ShapePredictor predictor, Mat mat)
 {
     using (var image = ToArray(mat))
     {
         var points = detector.Operator(image)
                      .Select(rectangle => predictor.Detect(image, rectangle))
                      .Where(shape => shape.Parts > 2)
                      .Select(shape => Enumerable.Range(0, (int)shape.Parts)
                              .Select(i => shape.GetPart((uint)i))
                              .Select((p, i) => new Point2f(p.X, p.Y))
                              .ToList())
                      .ToList();
         return(points);
     }
 }
示例#20
0
        public void Operator()
        {
            var image = this.GetDataFile("Lenna.jpg");
            var path1 = Path.Combine(this.ModelDirectory, "dlib_face_recognition_resnet_model_v1.dat");
            var path2 = Path.Combine(this.ModelDirectory, "shape_predictor_5_face_landmarks.dat");

            using (var net1 = LossMetric.Deserialize(path1))
                using (var net2 = LossMetric.Deserialize(File.ReadAllBytes(path1)))
                    using (var sp = ShapePredictor.Deserialize(path2))
                        using (var matrix = Dlib.LoadImageAsMatrix <RgbPixel>(image.FullName))
                            using (var detector = Dlib.GetFrontalFaceDetector())
                            {
                                var faces = new List <Matrix <RgbPixel> >();
                                foreach (var face in detector.Operator(matrix))
                                {
                                    var shape          = sp.Detect(matrix, face);
                                    var faceChipDetail = Dlib.GetFaceChipDetails(shape, 150, 0.25);
                                    var faceChip       = Dlib.ExtractImageChip <RgbPixel>(matrix, faceChipDetail);
                                    faces.Add(faceChip);
                                }

                                foreach (var face in faces)
                                {
                                    using (var ret1 = net1.Operator(face))
                                        using (var ret2 = net2.Operator(face))
                                        {
                                            Assert.Equal(1, ret1.Count);
                                            Assert.Equal(1, ret2.Count);

                                            var r1 = ret1[0];
                                            var r2 = ret2[0];

                                            Assert.Equal(r1.Columns, r2.Columns);
                                            Assert.Equal(r1.Rows, r2.Rows);

                                            for (var c = 0; c < r1.Columns; c++)
                                            {
                                                for (var r = 0; r < r1.Rows; r++)
                                                {
                                                    Assert.Equal(r1[r, c], r2[r, c]);
                                                }
                                            }
                                        }

                                    face.Dispose();
                                }
                            }
        }
        private Bitmap ProcessImage(Bitmap image)
        {
            // set up Dlib facedetectors and shapedetectors
            using (var fd = FrontalFaceDetector.GetFrontalFaceDetector())
                using (var sp = new ShapePredictor("shape_predictor_68_face_landmarks.dat"))
                {
                    // convert image to dlib format
                    var img = image.ToArray2D <RgbPixel>();

                    // detect faces
                    var faces = fd.Detect(img);

                    // detect facial landmarks
                    foreach (var rect in faces)
                    {
                        // detect facial landmarks
                        var shape = sp.Detect(img, rect);

                        // extract face chip
                        var chip      = Dlib.GetFaceChipDetails(shape);
                        var thumbnail = Dlib.ExtractImageChips <RgbPixel>(img, chip);

                        // add picturebox
                        var box = new PictureBox()
                        {
                            Image    = thumbnail.ToBitmap <RgbPixel>(),
                            SizeMode = PictureBoxSizeMode.Zoom,
                            Width    = 62,
                            Height   = 62
                        };
                        imagesPanel.Controls.Add(box);

                        // draw landmarks on main image
                        var lines = Dlib.RenderFaceDetections(new FullObjectDetection[] { shape });
                        foreach (var line in lines)
                        {
                            Dlib.DrawRectangle(
                                img,
                                new DlibDotNet.Rectangle(line.Point1),
                                new RgbPixel {
                                Green = 255
                            },
                                8);
                        }
                    }
                    return(img.ToBitmap <RgbPixel>());
                }
        }
        /// <summary>
        /// Called when MainForm is closed.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            videoPlayer.Stop();

            // dispose dlib resources
            if (faceDetector != null)
            {
                faceDetector.Dispose();
                faceDetector = null;
            }
            if (shapePredictor != null)
            {
                shapePredictor.Dispose();
                shapePredictor = null;
            }
        }
        /// <summary>
        /// Called when MainForm loads.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MainForm_Load(object sender, EventArgs e)
        {
            // initialize video player
            SetCamera();

            // load the car picture
            carBox.Image = Bitmap.FromFile(@"input.jpg");

            // initialize face detector
            faceDetector = FrontalFaceDetector.GetFrontalFaceDetector();

            // initialize shape predictor to detect landmarks
            shapePredictor = new ShapePredictor("shape_predictor_68_face_landmarks.dat");

            // start the players
            videoPlayer.Start();
        }
        /// <summary>
        /// Get the image with detected faces highlighted by the rectangle
        /// </summary>
        /// <param name="image"></param>
        /// <param name="numOfFaceDetected"></param>
        /// <returns></returns>
        public Bitmap FaceDetectionFromImage(Bitmap image, out int numOfFaceDetected)
        {
            numOfFaceDetected = 0;
            if (image != null)
            {
                // set up Dlib facedetectors and shapedetectors
                using (var faceDetector = FrontalFaceDetector.GetFrontalFaceDetector())
                    using (var shapePredictor = new ShapePredictor(Configuration.SHAP_PREDICTOR_CONFIG))
                    {
                        // convert image to dlib format
                        var img = image.ToArray2D <RgbPixel>();

                        // detect faces
                        var faces = faceDetector.Detect(img);

                        // detect facial landmarks
                        foreach (var rect in faces)
                        {
                            // detect facial landmarks
                            var shape = shapePredictor.Detect(img, rect);

                            //The left eye using landmark index[42, 47].
                            Landmarks landmarkLeftEye = new Landmarks(42, 47, shape);
                            //The right eye using landmark index [36, 41].
                            Landmarks landmarkRightEye = new Landmarks(36, 41, shape);
                            //draw landmark rectangle
                            var leftEyeRect      = Utils.RectangleAdjust(landmarkLeftEye.GetLandmarkRectangle(), img);
                            var rightEyeRect     = Utils.RectangleAdjust(landmarkRightEye.GetLandmarkRectangle(), img);
                            var adjustedFaceRect = Utils.RectangleAdjust(rect, img);

                            Dlib.DrawRectangle(img, adjustedFaceRect, new RgbPixel {
                                Blue = 255
                            }, 5);
                            Dlib.DrawRectangle(img, leftEyeRect, new RgbPixel {
                                Green = 255
                            }, 2);
                            Dlib.DrawRectangle(img, rightEyeRect, new RgbPixel {
                                Green = 255
                            }, 2);
                        }
                        numOfFaceDetected = faces.Length;
                        return(img.ToBitmap <RgbPixel>());
                    }
            }
            return(image);
        }
示例#25
0
        public Form1()
        {
            InitializeComponent();
            DoubleBuffered = true;

            shapes = new List <FullObjectDetection>();

            img           = new Mat();
            eyeImage      = new Bitmap(Properties.Resources.Star);
            mustacheImage = new Bitmap(Properties.Resources.Mustache);

            detector  = Dlib.GetFrontalFaceDetector();
            predictor = ShapePredictor.Deserialize("Resources\\shape_predictor_68_face_landmarks.dat");

            capture = new VideoCapture();
            capture.Open(0);
            Application.Idle += OnCameraFrame;
        }
    /// <summary>
    /// Extract features from an image and store it in <see cref="FaceData1"/>.
    /// </summary>
    /// <param name="imageFileInfo">File info of the image.</param>
    /// <param name="sp"></param>
    /// <param name="fd"></param>
    /// <param name="getLabel">>Whether to get the label or not. False if not using for prediction.</param>
    /// <returns></returns>
    /// <seealso cref="GetFaceDataPoints1"/>
    static FaceData1 GetFaceData1FromImage(FileInfo imageFileInfo, ShapePredictor sp, FrontalFaceDetector fd, bool getLabel = true)
    {
        // load input image
        using (var img = Dlib.LoadImage <RgbPixel>(imageFileInfo.FullName))
        {
            var faces = fd.Operator(img);
            foreach (var face in faces)
            {
                var shape = sp.Detect(img, face);

                return(GetFaceDataPoints1(ref shape,
                                          getLabel
                        ? GetLabel(imageFileInfo)
                        : "Not getting label, see argument this function was called with."));
            }
        }
        Debug.WriteLine($"Unable to get facial feature from {imageFileInfo.Name} as no faces were found!");
        return(null);
    }
示例#27
0
        private void GetLandmarks(OpenCvSharp.Mat frame, OpenCvSharp.Rect face, List <System.Drawing.Rectangle> rfaces)
        {
            EyePoints      rightEye  = new EyePoints(leftEye: true);
            EyePoints      leftEye   = new EyePoints(leftEye: false);
            ShapePredictor predictor = ShapePredictor.Deserialize(shapePredictorDataFile);

            //Scalar eyecolor = new Scalar(0, 0, 255);
            Array2D <byte>      gray      = ConvertMatToDlib2DArray(frame);
            FullObjectDetection landmarks = predictor.Detect(gray, ConvertToDlib(face));

            InitializeEyes(landmarks, leftEye, rightEye);
            //DrawEye(que, landmarks, leftEye);
            //DrawEye(que, landmarks, rightEye);
            Rect leftboundingBox = BoundingBoxAroundEye(leftEye, 0);

            rfaces.Add(FromOpenCvRect(leftboundingBox));
            //DrawRect(frame, leftboundingBox);
            OpenCvSharp.Point centerOfLeftEye = DetectCenterOfEye(frame, leftboundingBox);
            centerOfLeftEye.X += leftboundingBox.X;

            Rect rightboundingBox = BoundingBoxAroundEye(rightEye, 0);

            rfaces.Add(FromOpenCvRect(rightboundingBox));
            //DrawRect(frame, rightboundingBox);
            OpenCvSharp.Point centerOfRightEye = DetectCenterOfEye(frame, rightboundingBox);
            centerOfRightEye.X += rightboundingBox.X;

            EyeDirection leftEyeDirection  = leftEye.GetEyePosition(centerOfLeftEye);
            EyeDirection rightEyeDirection = rightEye.GetEyePosition(centerOfRightEye);

            //EyeDirection eyeDirection = EyeDirection.unknown;
            //if (leftEyeDirection == EyeDirection.center || rightEyeDirection == EyeDirection.center) eyeDirection = EyeDirection.center;
            //else if (leftEyeDirection == EyeDirection.left) eyeDirection = EyeDirection.left;
            //else if (rightEyeDirection == EyeDirection.right) eyeDirection = EyeDirection.right;

            //OpenCvSharp.Point position = new OpenCvSharp.Point(50, 50);
            //Cv2.PutText(img: frame, text: eyeDirection.ToDisplay(), org: position, fontFace: HersheyFonts.HersheySimplex, fontScale: 2, new Scalar(0, 0, 255));
        }
        public static string TestCustomImage(string dir)
        {
            DataViewSchema predictionPipelineSchema;
            ITransformer   predictionPipeline = mlContext.Model.Load("model.zip", out predictionPipelineSchema);
            PredictionEngine <FeatureInputData, ExpressionPrediction> predictionEngine = mlContext.Model.CreatePredictionEngine <FeatureInputData, ExpressionPrediction>(predictionPipeline);
            var img = Dlib.LoadImage <RgbPixel>(dir);

            // Set up Dlib Face Detector
            using (var fd = Dlib.GetFrontalFaceDetector())
                // ... and Dlib Shape Detector
                using (var sp = ShapePredictor.Deserialize("shape_predictor_68_face_landmarks.dat"))
                {
                    // find all faces in the image
                    var faces = fd.Operator(img);

                    // for each face draw over the facial landmarks
                    foreach (var face in faces)
                    {
                        // find the landmark points for this face
                        var shape = sp.Detect(img, face);

                        FeatureInputData inputData = new FeatureInputData
                        {
                            leftEyebrow  = CalculateLeftEyebrow(shape),
                            rightEyebrow = CalculateRightEyebrow(shape),
                            leftLip      = CalculateLeftLip(shape),
                            rightLip     = CalculateRightLip(shape),
                            lipWidth     = CalculateLipWidth(shape),
                            lipHeight    = CalculateLipHeight(shape)
                        };

                        ExpressionPrediction prediction = predictionEngine.Predict(inputData);

                        return(prediction.expression.ToString());
                    }
                }
            return("N/A");
        }
示例#29
0
 public static void Detect(Array2D <RgbPixel> image)
 {
     using (var detector = Dlib.GetFrontalFaceDetector())
         using (var sp =
                    ShapePredictor.Deserialize(
                        @"C:\Users\Felix\source\repos\BlinkDetect\External\shape_predictor_68_face_landmarks.dat"))
         {
             var dets   = detector.Operator(image);
             var shapes = new List <FullObjectDetection>();
             foreach (var rect in dets)
             {
                 var shape = sp.Detect(image, rect);
                 Console.WriteLine($"number of parts: {shape.Parts}");
                 if (shape.Parts > 2)
                 {
                     Console.WriteLine($"pixel position of first part:  {shape.GetPart(0)}");
                     Console.WriteLine($"pixel position of second part: {shape.GetPart(1)}");
                     shapes.Add(shape);
                 }
                 var chipLocations = Dlib.GetFaceChipDetails(shapes);
             }
         }
 }
示例#30
0
 public Form1()
 {
     InitializeComponent();
     this.capture = new VideoCapture(0);
     this.frame   = new Mat();
     this.fd      = Dlib.GetFrontalFaceDetector();
     this.sp      = ShapePredictor.Deserialize(@"C:\Users\trago\OneDrive\Desktop\OpenCV\shape_predictor_68_face_landmarks.dat");
     this.model   = Utility.GetFaceModel();
     this.coeffs  = new MatOfDouble(4, 1);
     this.coeffs.SetTo(0);
     this.poseModel      = new MatOfPoint3d(1, 1, new Point3d(0, 0, 1000));
     this.poseProjection = new MatOfPoint2d();
     this.checker        = new int[4] {
         100, -10, 10, 0
     };
     this.text = new string[4] {
         "1. เอาหน้าใส่กรอบ", "2. ก้มหน้าเล็กน้อย", "3. เงยหน้าเล็กน้อย", "4. หน้าตรง"
     };
     this.timeset = 3;
     this.size    = new Size(250, 300);
     SetStart();
     SetZero();
 }