/// <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);
 }
        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>());
                }
        }
示例#3
0
        private void ProcessFrame(object sender, EventArgs e)
        {
            Stopwatch SW = new Stopwatch();

            SW.Start();

            try
            {
                Mat temp = new Mat();
                _capture.Read(temp);

                var array = new byte[temp.Width * temp.Height * temp.ElementSize];
                temp.CopyTo(array);

                Array2D <RgbPixel> cimg = Dlib.LoadImageData <RgbPixel>(array, (uint)temp.Height, (uint)temp.Width, (uint)(temp.Width * temp.ElementSize));

                Rectangle[] faces = detector.Operator(cimg);

                if (faces.Any())
                {
                    FullObjectDetection        det    = poseModel.Detect(cimg, faces[0]);
                    List <FullObjectDetection> shapes = new List <FullObjectDetection>();
                    shapes.Add(det);
                    FullObjectDetection       shape = shapes[0];
                    ImageWindow.OverlayLine[] lines = Dlib.RenderFaceDetections(shapes);

                    if (chbShowLineOnly.Checked)
                    {
                        cimg = new Array2D <RgbPixel>(cimg.Rows, cimg.Columns);
                    }

                    foreach (var line in lines)
                    {
                        Dlib.DrawLine(cimg, line.Point1, line.Point2, new RgbPixel {
                            Green = 255
                        });
                    }

                    pictureBoxImage.Image?.Dispose();
                    pictureBoxImage.Image = cimg.ToBitmap();

                    foreach (var line in lines)
                    {
                        line.Dispose();
                    }

                    for (uint i = 0; i < shape.Parts; i++)
                    {
                        landmarkPoint.Insert((int)i, new Point(shape.GetPart(i).X, shape.GetPart(i).Y));
                    }

                    GetFacialBlendShape();

                    foreach (var s in shapes)
                    {
                        s.Dispose();
                    }
                }
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception.StackTrace);
            }

            SW.Stop();
            Debug.WriteLine(string.Format("FPS: {0}", 1000 / SW.ElapsedMilliseconds));
        }
示例#4
0
        private void BackgroundWorkerOnDoWork(object sender, DoWorkEventArgs doWorkEventArgs)
        {
            var path = doWorkEventArgs.Argument as string;

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

            using (var faceDetector = Dlib.GetFrontalFaceDetector())
                using (var img = Dlib.LoadImage <RgbPixel>(path))
                {
                    Dlib.PyramidUp(img);

                    var dets = faceDetector.Operator(img);

                    var shapes = new List <FullObjectDetection>();
                    foreach (var rect in dets)
                    {
                        var shape = this._ShapePredictor.Detect(img, rect);
                        if (shape.Parts <= 2)
                        {
                            continue;
                        }
                        shapes.Add(shape);
                    }

                    if (shapes.Any())
                    {
                        var lines = Dlib.RenderFaceDetections(shapes);
                        foreach (var line in lines)
                        {
                            Dlib.DrawLine(img, line.Point1, line.Point2, new RgbPixel
                            {
                                Green = 255
                            });
                        }

                        var wb = img.ToBitmap();
                        this.pictureBoxImage.Image?.Dispose();
                        this.pictureBoxImage.Image = wb;

                        foreach (var l in lines)
                        {
                            l.Dispose();
                        }

                        var chipLocations = Dlib.GetFaceChipDetails(shapes);
                        using (var faceChips = Dlib.ExtractImageChips <RgbPixel>(img, chipLocations))
                            using (var tileImage = Dlib.TileImages(faceChips))
                            {
                                // It is NOT necessary to re-convert WriteableBitmap to Matrix.
                                // This sample demonstrate converting managed image class to
                                // dlib class and vice versa.
                                using (var tile = tileImage.ToBitmap())
                                    using (var mat = tile.ToMatrix <RgbPixel>())
                                    {
                                        var tile2 = mat.ToBitmap();
                                        this.pictureBoxTileImage.Image?.Dispose();
                                        this.pictureBoxTileImage.Image = tile2;
                                    }
                            }

                        foreach (var c in chipLocations)
                        {
                            c.Dispose();
                        }
                    }

                    foreach (var s in shapes)
                    {
                        s.Dispose();
                    }
                }
        }