Ejemplo n.º 1
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");
                }
        }
Ejemplo n.º 2
0
        private static void Load(string directory, out IList <Matrix <RgbPixel> > images, out IList <uint> labels)
        {
            var imageList = new List <Matrix <RgbPixel> >();
            var labelList = new List <uint>();

            foreach (var file in Directory.EnumerateFiles(directory))
            {
                var name = Path.GetFileName(file);
                var s    = name.Split('_');
                if (s.Length != 4 || !uint.TryParse(s[1], out var gender))
                {
                    continue;
                }

                using (var tmp = Dlib.LoadImageAsMatrix <RgbPixel>(file))
                {
                    var m = new Matrix <RgbPixel>(Size, Size);
                    Dlib.ResizeImage(tmp, m);
                    imageList.Add(m);
                    labelList.Add(gender);
                }
            }

            images = imageList;
            labels = labelList;
        }
Ejemplo n.º 3
0
        public static void DetectFacesAsync(string inputFilePath, string subscriptionKey, string uriBase, IFaceClient client, string vocabularyPath)
        {
            // set up Dlib facedetector
            DirectoryInfo dir = new DirectoryInfo(inputFilePath);

            using (var fd = Dlib.GetFrontalFaceDetector())
            {
                foreach (FileInfo files in dir.GetFiles("*.jpg"))
                {
                    string _inputFilePath = inputFilePath + files.Name;

                    // load input image
                    Array2D <RgbPixel> img = Dlib.LoadImage <RgbPixel>(_inputFilePath);

                    // find all faces in the image
                    Rectangle[] faces = fd.Operator(img);
                    if (faces.Length != 0)
                    {
                        Console.WriteLine("Picture " + files.Name + " have faces, sending data to Azure");
                        MakeAnalysisRequestAsync(_inputFilePath, subscriptionKey, uriBase, files.Name, client, vocabularyPath).Wait();
                    }

                    foreach (var face in faces)
                    {
                        // draw a rectangle for each face
                        Dlib.DrawRectangle(img, face, color: new RgbPixel(0, 255, 255), thickness: 4);
                    }
                    // export the modified image
                    Dlib.SaveJpeg(img, "./Results/" + files.Name);
                }
            }
        private void buscarrosto(Bitmap frame)
        {
            Image <Rgb, Byte> imageCV = new Image <Rgb, byte>(frame);

            Emgu.CV.Mat mat   = imageCV.Mat;
            var         array = new byte[mat.Width * mat.Height * mat.ElementSize];

            mat.CopyTo(array);

            using (Array2D <RgbPixel> image = Dlib.LoadImageData <RgbPixel>(array, (uint)mat.Height, (uint)mat.Width, (uint)(mat.Width * mat.ElementSize)))
            {
                using (FrontalFaceDetector fd = Dlib.GetFrontalFaceDetector())

                {
                    var faces = fd.Operator(image);
                    foreach (DlibDotNet.Rectangle face in faces)
                    {
                        FullObjectDetection shape          = _ShapePredictor.Detect(image, face);
                        ChipDetails         faceChipDetail = Dlib.GetFaceChipDetails(shape, 150, 0.25);
                        Array2D <RgbPixel>  faceChip       = Dlib.ExtractImageChip <RgbPixel>(image, faceChipDetail);
                        Bitmap bitmap1 = faceChip.ToBitmap <RgbPixel>();
                        MainWindow.main.Statusa1 = bitmap1;
                        Dlib.DrawRectangle(image, face, color: new RgbPixel(0, 255, 255), thickness: 4);
                    }
                }
                frame = image.ToBitmap <RgbPixel>();
                MainWindow.main.Statusa = frame;
            }
        }
Ejemplo n.º 5
0
        private void BackgroundWorkerOnDoWork(object sender, DoWorkEventArgs doWorkEventArgs)
        {
            var path = doWorkEventArgs.Argument as string;

            if (string.IsNullOrWhiteSpace(path) || !File.Exists(path))
            {
                return;
            }

            // DlibDotNet can create Array2D from file but this sample demonstrate
            // converting managed image class to dlib class and vice versa.
            using (var faceDetector = FrontalFaceDetector.GetFrontalFaceDetector())
                using (var ms = new MemoryStream(File.ReadAllBytes(path)))
                    using (var bitmap = (Bitmap)Image.FromStream(ms))
                    {
                        using (var image = bitmap.ToArray2D <RgbPixel>())
                        {
                            var dets = faceDetector.Detect(image);
                            foreach (var r in dets)
                            {
                                Dlib.DrawRectangle(image, r, new RgbPixel {
                                    Green = 255
                                });
                            }

                            var result = image.ToBitmap();
                            this.pictureBox.Invoke(new Action(() =>
                            {
                                this.pictureBox.Image?.Dispose();
                                this.pictureBox.Image = result;
                            }));
                        }
                    }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// 人脸对比
        /// </summary>
        /// <param name="bitmap1"></param>
        /// <param name="bitmap2"></param>
        /// <returns></returns>
        public static (float, List <Rectangle[]>) GetOutputLabels(Bitmap bitmap1, Bitmap bitmap2)
        {
            var data1 = GetData(new List <Bitmap>()
            {
                bitmap1
            }, true);
            var data2 = GetData(new List <Bitmap>()
            {
                bitmap2
            }, true);


            List <Rectangle[]> rectangles = new List <Rectangle[]>();

            if (data1[0].Item1[0] == null)
            {
                return(-1, null);
            }

            if (data2[0].Item1[0] == null)
            {
                return(-2, null);
            }

            // 欧里几德距离
            var diff = data1[0].Item1[0] - data2[0].Item1[0];

            rectangles.Add(data1[0].Item2);
            rectangles.Add(data2[0].Item2);
            return(Dlib.Length(diff), rectangles);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// 人脸查找
        /// </summary>
        /// <param name="bitmap"></param>
        /// <param name="bitmaps"></param>
        /// <returns></returns>
        public static List <(float, Rectangle[])> GetOutputLabels(Bitmap bitmap, List <Bitmap> bitmaps, bool isAFace = false)
        {
            List <(float, Rectangle[])> fNums = new List <(float, Rectangle[])>();
            var data1 = GetData(new List <Bitmap>()
            {
                bitmap
            }, isAFace);
            var data2 = GetData(bitmaps, true);


            if (data1[0].Item1[0] == null)
            {
                return(null);
            }

            foreach (var item in data2)
            {
                if (item.Item1[0] == null)
                {
                    fNums.Add((99.99f, item.Item2));
                }
                else
                {
                    // 欧里几德距离
                    var diff = data1[0].Item1[0] - data2[0].Item1[0];
                    fNums.Add((Dlib.Length(diff), item.Item2));
                }
            }
            return(fNums);
        }
Ejemplo n.º 8
0
    static void Main(string[] args)
    {
        // Read image in
        var img = Dlib.LoadImage <RgbPixel>(imgFilePath);

        // Let's detect faces and draw rectangles around them
        FaceDetector faceDetector = new FaceDetector(facialLandmarksSerializedPredictor);

        Rectangle[] facesBoundingBoxes = faceDetector.DetectFacesBoundingBoxes(img);

        foreach (var bb in facesBoundingBoxes)
        {
            Dlib.DrawRectangle(img, bb, color: new RgbPixel(0, 0, 255), thickness: 3);
        }

        // Draw eyes bounding box for subject (i.e., largest) face
        if (facesBoundingBoxes.Length > 0)
        {
            // Example code if you wish to do this only on the largest face

            /*Rectangle subjectFaceBoundingBox = new Rectangle(0, 0);
             * foreach (var bb in facesBoundingBoxes)
             * {
             *  if (bb.Area > subjectFaceBoundingBox.Area)
             *  {
             *      subjectFaceBoundingBox = bb;
             *  }
             * }*/

            // Here we do it on all faces
            foreach (var subjectFaceBoundingBox in facesBoundingBoxes)
            {
                // Next, obtain facial landmarks
                var landmarks = faceDetector.DetectFacialLandmarks(img, subjectFaceBoundingBox);
                // We also draw them
                foreach (Point p in landmarks)
                {
                    Dlib.DrawRectangle(img, new Rectangle(p), color: new RgbPixel(255, 0, 0), thickness: 3);
                }

                // Now draw bounding box around the eyes
                var topLeft         = new Point(landmarks[FacialLandmarks.RIGHT_EYEBROW].X, landmarks[FacialLandmarks.RIGHT_EYEBROW].Y);
                var bottomRight     = new Point(landmarks[FacialLandmarks.LEFT_EYEBROW].X, landmarks[FacialLandmarks.UPPER_NOSE].Y);
                var eyesBoundingBox = new Rectangle(topLeft, bottomRight);
                Dlib.DrawRectangle(img, eyesBoundingBox, color: new RgbPixel(0, 255, 0), thickness: 3);
            }
        }

        // Create output file path (for later)
        string outFilePath;
        var    tmpStrArray = imgFilePath.Split('/');
        var    extension   = tmpStrArray[tmpStrArray.Length - 1].Split('.')[1];

        outFilePath = String.Join('/', tmpStrArray.SkipLast(1).ToArray()) + '/' +
                      tmpStrArray[tmpStrArray.Length - 1].Replace("." + extension, "_out." + extension);
        Console.WriteLine(outFilePath);

        // Write img
        faceDetector.WriteImageToFilePath(img, outFilePath);
    }
Ejemplo n.º 9
0
        private static List <(OutputLabels <Matrix <float> >, Rectangle[])> GetData(List <Bitmap> bitmaps, bool isAFace = false)
        {
            var datas = new List <(OutputLabels <Matrix <float> >, Rectangle[])>();

            try
            {
                foreach (var bitmap in bitmaps)
                {
                    var faces = new List <Matrix <RgbPixel> >();
                    var dets  = new Rectangle[0];
                    //在图像中寻找人脸我们需要一个人脸检测器:
                    using (var detector = Dlib.GetFrontalFaceDetector())
                    {
                        using (var img = bitmap.ToMatrix <RgbPixel>())
                        {
                            // 人脸 面积从大到小排序
                            dets = detector.Operator(img).OrderByDescending(x => x.Area).ToArray();
                            // 是否只检测面积最大的人脸
                            if (isAFace)
                            {
                                var shape          = _SP.Detect(img, dets[0]);
                                var faceChipDetail = Dlib.GetFaceChipDetails(shape, 150, 0.25);
                                var faceChip       = Dlib.ExtractImageChip <RgbPixel>(img, faceChipDetail);
                                faces.Add(faceChip);
                            }
                            else
                            {
                                foreach (var face in dets)
                                {
                                    var shape          = _SP.Detect(img, face);
                                    var faceChipDetail = Dlib.GetFaceChipDetails(shape, 150, 0.25);
                                    var faceChip       = Dlib.ExtractImageChip <RgbPixel>(img, faceChipDetail);
                                    faces.Add(faceChip);
                                }
                            }
                            if (!faces.Any())
                            {
                                datas.Add((null, null));
                            }
                            else
                            {
                                //此调用要求DNN将每个人脸图像转换为128D矢量。
                                //在这个128D的矢量空间中,来自同一个人的图像会彼此接近
                                //但是来自不同人的载体将会非常不同。所以我们可以用这些向量
                                //辨别一对图片是来自同一个人还是不同的人。
                                datas.Add((_NET.Operator(faces), dets));
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelperNLog.Error(ex);
            }
            return(datas);
        }
Ejemplo n.º 10
0
 /// <summary>
 /// 连线
 /// </summary>
 /// <param name="shapes"></param>
 /// <returns></returns>
 public static ImageWindow.OverlayLine[] Getlines(List <FullObjectDetection> shapes)
 {
     //这个是用来连线的
     ImageWindow.OverlayLine[] lines = null;
     if (shapes.Any())
     {
         //就是这个
         lines = Dlib.RenderFaceDetections(shapes);
     }
     return(lines);
 }
Ejemplo n.º 11
0
        public Rectangle[] DetectFacesBoundingBoxes(Array2D <RgbPixel> img)
        {
            Rectangle[] facesBoundingBoxes;

            // Set up and apply face detector
            using (var fd = Dlib.GetFrontalFaceDetector())
            {
                facesBoundingBoxes = fd.Operator(img);
            }

            return(facesBoundingBoxes);
        }
Ejemplo n.º 12
0
        /// <summary>
        /// 具体计算
        /// </summary>
        /// <param name="bitmap"></param>
        /// <returns></returns>
        public Rectangle[] Face(Bitmap bitmap)
        {
            var dets = new Rectangle[0];

            using (var detector = Dlib.GetFrontalFaceDetector())
                //using (var img = Dlib.LoadImage<RgbPixel>("png.png"))
                using (var img = bitmap.ToArray2D <RgbPixel>())
                {
                    dets = detector.Operator(img);
                }
            return(dets);
        }
 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");
                 }
             }
         }
 }
Ejemplo n.º 14
0
        /// <summary>
        /// 使用路径获取位置数据
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static Rectangle[] GetResult(string url)
        {
            var dets = new Rectangle[0];

            url = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, url);
            using (var detector = Dlib.GetFrontalFaceDetector())
                //using (var img = Dlib.LoadImage<RgbPixel>("png.png"))
                using (var img = Dlib.LoadImage <RgbPixel>(url))
                {
                    dets = detector.Operator(img);
                }
            return(dets);
        }
 private static void DetectFacesOnImage(Array2D <RgbPixel> image)
 {
     // set up Dlib facedetector
     using (var fd = Dlib.GetFrontalFaceDetector())
     {
         // find all faces in the image
         var faces = fd.Operator(image);
         foreach (Rectangle face in faces)
         {
             // draw a rectangle for each face
             Dlib.DrawRectangle(image, face, color: new RgbPixel(0, 255, 255), thickness: 4);
         }
     }
 }
        public static void DetectFacesOnImage(string sourceImagePath, string destImagePath)
        {
            // set up Dlib facedetector
            using (var fd = Dlib.GetFrontalFaceDetector())
            {
                // load input image
                var image = Dlib.LoadImage <RgbPixel>(sourceImagePath);

                DetectFacesOnImage(image);

                // export the modified image
                Dlib.SaveJpeg(image, destImagePath);
            }
        }
Ejemplo n.º 17
0
        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>());
                }
        }
Ejemplo n.º 18
0
        private static IEnumerable <Matrix <RgbPixel> > JitterImage(Matrix <RgbPixel> img)
        {
            // All this function does is make 100 copies of img, all slightly jittered by being
            // zoomed, rotated, and translated a little bit differently. They are also randomly
            // mirrored left to right.
            var rnd = new Rand();

            var crops = new List <Matrix <RgbPixel> >();

            for (var i = 0; i < 100; ++i)
            {
                crops.Add(Dlib.JitterImage(img, rnd));
            }

            return(crops);
        }
Ejemplo n.º 19
0
        public void FindFaces()
        {
            using (var fd = Dlib.GetFrontalFaceDetector())
            {
                var img = Dlib.LoadImage <RgbPixel>(path);

                // find all faces in the image
                var faces = fd.Operator(img);
                foreach (var face in faces)
                {
                    // draw a rectangle for each face
                    Dlib.DrawRectangle(img, face, color: new RgbPixel(0, 255, 255), thickness: 4);
                }
                Dlib.SaveJpeg(img, @"D:\output.png");
            }
        }
Ejemplo n.º 20
0
        /// <summary>
        /// 具体计算
        /// </summary>
        /// <param name="bitmaps"></param>
        /// <returns></returns>
        private static List <List <Landmark68ViewModler> > GetDate(List <Bitmap> bitmaps)
        {
            List <List <FullObjectDetection> > Detection = new List <List <FullObjectDetection> >();


            List <List <Landmark68ViewModler> > landmark68s = new List <List <Landmark68ViewModler> >();


            //人脸检测器
            using (var detector = Dlib.GetFrontalFaceDetector())
            {
                foreach (var bitmap in bitmaps)
                {
                    var shapes = new List <FullObjectDetection>();
                    // 图片转换
                    using (var img = bitmap.ToArray2D <RgbPixel>())
                    {
                        //获取位置数据
                        var dets = detector.Operator(img);

                        // 循环人脸数据
                        foreach (var rect in dets)
                        {
                            // 特征点检测
                            var shape = _SP.Detect(img, rect);
                            if (shape.Parts > 2)
                            {
                                shapes.Add(shape);
                            }
                            List <Landmark68ViewModler> landmark68 = new List <Landmark68ViewModler>();
                            for (uint i = 0; i < shape.Parts; i++)
                            {
                                var item = shape.GetPart(i);
                                landmark68.Add(new Landmark68ViewModler()
                                {
                                    X = item.X,
                                    Y = item.Y,
                                });
                            }
                            landmark68s.Add(landmark68);
                        }
                    }
                    Detection.Add(shapes);
                }
            }
            return(landmark68s);
        }
Ejemplo n.º 21
0
        public static int Number(string file)
        {
            using (var fd = Dlib.GetFrontalFaceDetector())
            {
                var img = Dlib.LoadImage <RgbPixel>(file);

                int number = 0;

                var faces = fd.Operator(img);

                foreach (var face in faces)
                {
                    number += 1;
                }
                return(number);
            }
        }
        /// <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);
        }
Ejemplo n.º 23
0
        public static void Recognize(string file)
        {
            using (var fd = Dlib.GetFrontalFaceDetector())
            {
                var img = Dlib.LoadImage <RgbPixel>(file);

                //hola
                var faces = fd.Operator(img);

                foreach (var face in faces)
                {
                    Dlib.DrawRectangle(img, face, color: new RgbPixel(0, 255, 255), thickness: 4);
                }


                Dlib.SaveJpeg(img, file);
            }
        }
        public static void SetFormImage(Array2D <RgbPixel> img)
        {
            Dlib.ResizeImage(img, 0.4);

            Bitmap image = new Bitmap(img.Columns, img.Rows, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

            for (int i = 0; i < img.Columns; i++)
            {
                for (int j = 0; j < img.Rows; j++)
                {
                    //Color c = Color.FromArgb(img[i][j].Red, img[i][j].Green, img[i][j].Blue);
                    //image.SetPixel(i, j, c);
                }
            }

            image.RotateFlip(RotateFlipType.Rotate90FlipNone);

            Form1.SetImage(image);
        }
    /// <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);
    }
Ejemplo n.º 26
0
        /// <summary>
        /// The main program entry point
        /// </summary>
        /// <param name="args">The command line arguments</param>
        static void Main(string[] args)
        {
            // set up Dlib facedetector
            using (var fd = Dlib.GetFrontalFaceDetector())
            {
                // 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)
                {
                    // draw a rectangle for each face
                    Dlib.DrawRectangle(img, face, color: new RgbPixel(0, 255, 255), thickness: 4);
                }

                // export the modified image
                Dlib.SaveJpeg(img, "output.jpg");
            }
        }
Ejemplo n.º 27
0
        /// <summary>
        /// 具体计算
        /// </summary>
        /// <param name="bitmaps"></param>
        /// <returns></returns>
        private static List <Rectangle[]> GetDate(List <Bitmap> bitmaps)
        {
            List <Rectangle[]> rectangles = new List <Rectangle[]>();

            // 检测器
            using (var detector = Dlib.GetFrontalFaceDetector())
            {
                // 循环所有的图片
                foreach (var bitmap in bitmaps)
                {
                    // 图片格式转化
                    using (var img = bitmap.ToArray2D <RgbPixel>())
                    {
                        // 获取位置数据
                        var dets = detector.Operator(img);
                        rectangles.Add(dets);
                    }
                }
            }
            return(rectangles);
        }
        public void GetImage(string imagePath)
        {
            Array2D <RgbPixel> image = Dlib.LoadImage <RgbPixel>(imagePath);

            using (FrontalFaceDetector fd = Dlib.GetFrontalFaceDetector())

            {
                var faces = fd.Operator(image);
                foreach (DlibDotNet.Rectangle face in faces)
                {
                    FullObjectDetection shape          = _ShapePredictor.Detect(image, face);
                    ChipDetails         faceChipDetail = Dlib.GetFaceChipDetails(shape, 150, 0.25);
                    Array2D <RgbPixel>  faceChip       = Dlib.ExtractImageChip <RgbPixel>(image, faceChipDetail);
                    Bitmap bitmap1 = faceChip.ToBitmap <RgbPixel>();
                    MainWindow.main.Statusa1 = bitmap1;
                    Dlib.DrawRectangle(image, face, color: new RgbPixel(0, 255, 255), thickness: 4);
                }
            }
            Bitmap frame = image.ToBitmap <RgbPixel>();

            MainWindow.main.Statusa = frame;
        }
        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");
        }
Ejemplo n.º 30
0
        /// <summary>
        /// 具体计算
        /// </summary>
        /// <param name="bitmap"></param>
        /// <param name="sp"></param>
        /// <returns></returns>
        public List <FullObjectDetection> Face(Bitmap bitmap)
        {
            // 加载模型文件
            if (sp == null)
            {
                var basePath = AppDomain.CurrentDomain.BaseDirectory;
                sp = ShapePredictor.Deserialize(basePath + "ShapeModel/shape_predictor_68_face_landmarks.dat");
            }

            //var link = new ImageWindow.OverlayLine[0];
            var shapes = new List <FullObjectDetection>();

            using (var detector = Dlib.GetFrontalFaceDetector())
            {
                using (var img = bitmap.ToArray2D <RgbPixel>())
                {
                    var dets = detector.Operator(img);

                    foreach (var rect in dets)
                    {
                        var shape = sp.Detect(img, rect);
                        if (shape.Parts > 2)
                        {
                            shapes.Add(shape);
                        }
                    }
                    //if (shapes.Any())
                    //{
                    //    //就是这个
                    //    var lines = Dlib.RenderFaceDetections(shapes);
                    //    link = lines;
                    //}
                }
            }
            return(shapes);
        }