Exemplo n.º 1
0
        public void DetectFaceWithConfidence()
        {
            if (this._ObjectDetector == null)
            {
                Assert.True(false, "ObjectDetector is not initialized!!");
            }

            var path  = this.GetDataFile("Lenna.jpg");
            var image = Dlib.LoadImageAsMatrix <RgbPixel>(path.FullName);

            this._ObjectDetector.Operator(image, out IEnumerable <Tuple <double, Rectangle> > tuples);
            var array = tuples.ToArray();

            Assert.Equal(array.Length, 1);

            foreach (var tuple in tuples)
            {
                Dlib.DrawRectangle(image, tuple.Item2, new RgbPixel {
                    Green = 255
                });
            }
            Assert.True(array[0].Item1 - 0.3173714 < 0.01);

            Dlib.SaveBmp(image, Path.Combine(this.GetOutDir(this.GetType().Name), "DetectFaceWithConfidence.bmp"));

            this.DisposeAndCheckDisposedState(image);
        }
        // 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");
                }
        }
Exemplo n.º 3
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 = Dlib.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.Operator(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;
                            }));
                        }
                    }
        }
Exemplo n.º 4
0
        public async Task <IEnumerable <Face> > ProcessAsync(string inputFilename)
        {
            if (!File.Exists(inputFilename))
            {
                throw new FileNotFoundException(nameof(inputFilename));
            }

            using var img = await DlibHelpers.LoadRotatedImage(imageRotationService, inputFilename);

            var faces = detector.Operator(img);

            foreach (var rect in faces)
            {
                Dlib.DrawRectangle(img, rect, new RgbPixel(0, 255, 0), 8U);
            }

            // var origFilename = new FileInfo(inputFilename).Name;
            // var outputFilename = Path.Combine(outputDirectory, $"{origFilename}_{Name}.jpg");

            // Dlib.SaveJpeg(img, outputFilename, 25);

            return(faces.Select(x => new Face
            {
                Position = new RectangleDto
                {
                    Top = x.Top,
                    Right = x.Right,
                    Left = x.Left,
                    Bottom = x.Bottom,
                }.ToRectangle(),
                Confidence = null
            }).ToList());
        }
Exemplo n.º 5
0
        public string SaveOutlinedFacesImage(string folderName, string imageName)
        {
            string folderPath = Path.Combine(PathSystem.OutputsPath, folderName);

            if (File.Exists(folderPath) == true)
            {
                File.Delete(folderPath);                                  // Check for exist file with same name and delet him
            }
            if (Directory.Exists(folderName) == false)
            {
                Directory.CreateDirectory(folderPath);                                        // Check for exist directory with same name and create her
            }
            using (var faceDetector = Dlib.GetFrontalFaceDetector())
            {
                var faces = faceDetector.Operator(Image); // Detect all faces

                foreach (var face in faces)               // Draw rectangle around every face
                {
                    Dlib.DrawRectangle(Image, face, Color, thickness: 5);
                }
            }

            string imagePath = Path.Combine(folderPath, imageName); // Create image file path

            Dlib.SaveJpeg(Image, imagePath);                        // Save image to image path

            return(imagePath);                                      // Return image path
        }
        public void DetectFace()
        {
            if (this._FrontalFaceDetector == null)
            {
                Assert.Fail("ShapePredictor is not initialized!!");
            }

            var faceDetector = this._FrontalFaceDetector;

            var path  = this.GetDataFile("2008_001322.jpg");
            var image = Dlib.LoadImage <RgbPixel>(path.FullName);

            //Interpolation.PyramidUp(image);

            var dets = faceDetector.Operator(image);

            Assert.AreEqual(dets.Length, 3);

            foreach (var r in dets)
            {
                Dlib.DrawRectangle(image, r, new RgbPixel {
                    Green = 255
                });
            }

            Dlib.SaveBmp(image, Path.Combine(this.GetOutDir(this.GetType().Name), "DetectFace.bmp"));

            this.DisposeAndCheckDisposedState(faceDetector);
            this.DisposeAndCheckDisposedState(image);
        }
Exemplo n.º 7
0
        public void DetectFace()
        {
            if (this._ShapePredictor == null)
            {
                Assert.Fail("ShapePredictor is not initialized!!");
            }

            var faceDetector = FrontalFaceDetector.GetFrontalFaceDetector();
            //Interpolation.PyramidUp(image);

            var path  = this.GetDataFile("Lenna.jpg");
            var image = Dlib.LoadImage <RgbPixel>(path.FullName);

            var dets = faceDetector.Detect(image);

            Assert.AreEqual(dets.Length, 1);

            var rects = new List <Rectangle>();

            const int offset = 1;
            var       shapes = dets.Select(r => this._ShapePredictor.Detect(image, r)).ToList();

            foreach (var shape in shapes)
            {
                var r     = shape.Rect;
                var parts = shape.Parts;
                for (uint i = 0; i < parts; i++)
                {
                    var part = shape.GetPart(i);
                    var pr   = new Rectangle(
                        part.X - offset, part.Y - offset, part.X + offset, part.Y + offset);
                    rects.Add(pr);
                }

                rects.Add(r);
            }

            foreach (var r in rects)
            {
                Dlib.DrawRectangle(image, r, new RgbPixel {
                    Green = 255
                });
            }

            Dlib.SaveBmp(image, Path.Combine(this.GetOutDir(this.GetType().Name), "DetectFace.bmp"));

            this.DisposeAndCheckDisposedState(rects);
            this.DisposeAndCheckDisposedState(dets);
            this.DisposeAndCheckDisposedState(faceDetector);
            this.DisposeAndCheckDisposedState(image);
        }
Exemplo n.º 8
0
        public byte[] GetOutlinedFacesImage()
        {
            using (var faceDetector = Dlib.GetFrontalFaceDetector())
            {
                var faces = faceDetector.Operator(Image); // Detect faces

                foreach (var face in faces)               // Draw rectangle around every face
                {
                    Dlib.DrawRectangle(Image, face, Color, thickness: 5);
                }
            }

            return(Image.ToByteArray(PathSystem)); // Convert image to bytes array and return him
        }
Exemplo n.º 9
0
        static void Main(string[] args)
        {
            using (var fd = Dlib.GetFrontalFaceDetector())
            {
                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);
                }
                Dlib.SaveJpeg(img, "1.jpg");
            }
        }
Exemplo n.º 10
0
        private async void DetectButtonClick(object sender, RoutedEventArgs e)
        {
            using (var faceDetector = Dlib.GetFrontalFaceDetector())
            {
                var dets = faceDetector.Operator(this._LoadedMatrix);
                foreach (var r in dets)
                {
                    Dlib.DrawRectangle(this._Result, r, new RgbPixel {
                        Green = 255
                    });
                }
            }

            BitmapImage image;

            using (var tmp = new Matrix <RgbPixel>(this._Result))
                image = await ToBitmapSource(tmp);

            await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
            {
                this._Image.Source = image;
            });
        }
Exemplo n.º 11
0
        private static void DrawDebugDataOnImage(
            Array2D <RgbPixel> img,
            FullObjectDetection shape,
            Rectangle face_rect,
            Rectangle left_eye_rect,
            Rectangle right_eye_rect)
        {
            if (debugData)
            {
                // For debugging, show face points
                for (uint i = 0; i < shape.Parts; ++i)
                {
                    var point = shape.GetPart(i);
                    var rect  = new Rectangle(point);
                    var color = new RgbPixel(255, 255, 0);

                    if (i >= 36 && i <= 41)
                    {
                        color = new RgbPixel(0, 0, 255);
                    }
                    else if (i >= 42 && i <= 47)
                    {
                        color = new RgbPixel(0, 255, 0);
                    }
                    if (i >= 27 && i <= 35)
                    {
                        color = new RgbPixel(255, 0, 0);
                    }

                    Dlib.DrawRectangle(img, rect, color: color, thickness: 4);
                }

                Dlib.DrawRectangle(img, face_rect, color: new RgbPixel(0, 255, 0), thickness: 4);
                Dlib.DrawRectangle(img, left_eye_rect, color: new RgbPixel(0, 255, 255), thickness: 4);
                Dlib.DrawRectangle(img, right_eye_rect, color: new RgbPixel(0, 255, 255), thickness: 4);
            }
        }
Exemplo n.º 12
0
        public void DetectFace()
        {
            if (this._ObjectDetector == null)
            {
                Assert.True(false, "ObjectDetector is not initialized!!");
            }

            var path  = this.GetDataFile("Lenna.jpg");
            var image = Dlib.LoadImageAsMatrix <RgbPixel>(path.FullName);

            this._ObjectDetector.Operator(image, out IEnumerable <Rectangle> rects);
            Assert.Equal(rects.Count(), 1);

            foreach (var r in rects)
            {
                Dlib.DrawRectangle(image, r, new RgbPixel {
                    Green = 255
                });
            }

            Dlib.SaveBmp(image, Path.Combine(this.GetOutDir(this.GetType().Name), "DetectFace.bmp"));

            this.DisposeAndCheckDisposedState(image);
        }
        public void FindCandidateObjectLocations()
        {
            var path = this.GetDataFile("Lenna.jpg");

            var tests = new[]
            {
                new { Type = ImageTypes.RgbPixel, ExpectResult = true },
                new { Type = ImageTypes.RgbAlphaPixel, ExpectResult = true },
                new { Type = ImageTypes.UInt8, ExpectResult = true },
                new { Type = ImageTypes.UInt16, ExpectResult = true },
                new { Type = ImageTypes.UInt32, ExpectResult = true },
                new { Type = ImageTypes.Int8, ExpectResult = true },
                new { Type = ImageTypes.Int16, ExpectResult = true },
                new { Type = ImageTypes.Int32, ExpectResult = true },
                new { Type = ImageTypes.HsiPixel, ExpectResult = false },
                new { Type = ImageTypes.Float, ExpectResult = false },
                new { Type = ImageTypes.Double, ExpectResult = false }
            };

            var type = this.GetType().Name;

            foreach (var test in tests)
            {
                Array2DBase inImg = null;

                try
                {
                    inImg = DlibTest.LoadImage(test.Type, path);
                    var rects = Dlib.FindCandidateObjectLocations(inImg)?.ToArray();
                    if (rects == null || !rects.Any())
                    {
                        Assert.Fail($"{nameof(FindCandidateObjectLocations)} should detect any rectangles.");
                    }

                    switch (test.Type)
                    {
                    case ImageTypes.RgbPixel:
                        foreach (var r in rects)
                        {
                            Dlib.DrawRectangle(inImg, r, new RgbPixel {
                                Blue = 255
                            });
                        }
                        break;

                    case ImageTypes.RgbAlphaPixel:
                        foreach (var r in rects)
                        {
                            Dlib.DrawRectangle(inImg, r, new RgbAlphaPixel {
                                Blue = 255, Alpha = 255
                            });
                        }
                        break;

                    case ImageTypes.UInt8:
                        foreach (var r in rects)
                        {
                            Dlib.DrawRectangle(inImg, r, (byte)0);
                        }
                        break;

                    case ImageTypes.UInt16:
                        foreach (var r in rects)
                        {
                            Dlib.DrawRectangle(inImg, r, (ushort)0);
                        }
                        break;

                    case ImageTypes.Int32:
                        foreach (var r in rects)
                        {
                            Dlib.DrawRectangle(inImg, r, 255 << 16);
                        }
                        break;

                    case ImageTypes.HsiPixel:
                        foreach (var r in rects)
                        {
                            Dlib.DrawRectangle(inImg, r, new HsiPixel {
                                H = 255
                            });
                        }
                        break;

                    case ImageTypes.Float:
                        foreach (var r in rects)
                        {
                            Dlib.DrawRectangle(inImg, r, 0f);
                        }
                        break;

                    case ImageTypes.Double:
                        foreach (var r in rects)
                        {
                            Dlib.DrawRectangle(inImg, r, 0d);
                        }
                        break;
                    }

                    Dlib.SaveBmp(inImg, $"{Path.Combine(this.GetOutDir(type, "FindCandidateObjectLocations"), $"Lenna_{test.Type}.bmp")}");
                }
                catch (Exception e)
                {
                    if (!test.ExpectResult)
                    {
                        Console.WriteLine("OK");
                    }
                    else
                    {
                        Console.WriteLine(e.StackTrace);
                        Console.WriteLine($"Failed to execute FindCandidateObjectLocations to Type: {test.Type}.");
                        throw;
                    }
                }
                finally
                {
                    if (inImg != null)
                    {
                        this.DisposeAndCheckDisposedState(inImg);
                    }
                }
            }
        }
Exemplo n.º 14
0
        private static void Main(string[] args)
        {
            if (args.Length != 1)
            {
                Console.WriteLine("You call this program like this: ");
                Console.WriteLine("./dnn_instance_segmentation_train_ex /path/to/images");
                Console.WriteLine();
                Console.WriteLine($"You will also need a trained '{InstanceSegmentationNetFilename}' file.");
                Console.WriteLine("You can either train it yourself (see example program");
                Console.WriteLine("dnn_instance_segmentation_train_ex), or download a");
                Console.WriteLine($"copy from here: http://dlib.net/files/{InstanceSegmentationNetFilename}");
                return;
            }

            try
            {
                // Read the file containing the trained network from the working directory.
                using (var deserialize = new ProxyDeserialize(InstanceSegmentationNetFilename))
                    using (var detNet = LossMmod.Deserialize(deserialize, 4))
                    {
                        var segNetsByClass = new Dictionary <string, LossMulticlassLogPerPixel>();
                        segNetsByClass.Deserialize(deserialize, 4);

                        // Show inference results in a window.
                        using (var win = new ImageWindow())
                        {
                            // Find supported image files.
                            var files = Directory.GetFiles(args[0])
                                        .Where(s => s.EndsWith(".jpeg") || s.EndsWith(".jpg") || s.EndsWith(".png")).ToArray();

                            using (var rnd = new Rand())
                            {
                                Console.WriteLine($"Found {files.Length} images, processing...");
                                foreach (var file in files.Select(s => new FileInfo(s)))
                                {
                                    // Load the input image.
                                    using (var inputImage = Dlib.LoadImageAsMatrix <RgbPixel>(file.FullName))
                                    {
                                        // Create predictions for each pixel. At this point, the type of each prediction
                                        // is an index (a value between 0 and 20). Note that the net may return an image
                                        // that is not exactly the same size as the input.
                                        using (var output = detNet.Operator(inputImage))
                                        {
                                            var instances = output.First().ToList();
                                            instances.Sort((lhs, rhs) => (int)lhs.Rect.Area - (int)rhs.Rect.Area);

                                            using (var rgbLabelImage = new Matrix <RgbPixel>())
                                            {
                                                rgbLabelImage.SetSize(inputImage.Rows, inputImage.Columns);
                                                rgbLabelImage.Assign(Enumerable.Range(0, rgbLabelImage.Size).Select(i => new RgbPixel(0, 0, 0)).ToArray());

                                                var foundSomething = false;
                                                foreach (var instance in instances)
                                                {
                                                    if (!foundSomething)
                                                    {
                                                        Console.Write("Found ");
                                                        foundSomething = true;
                                                    }
                                                    else
                                                    {
                                                        Console.Write(", ");
                                                    }

                                                    Console.Write(instance.Label);

                                                    var croppingRect = GetCroppingRect(instance.Rect);
                                                    using (var dims = new ChipDims(SegDim, SegDim))
                                                        using (var chipDetails = new ChipDetails(croppingRect, dims))
                                                            using (var inputChip = Dlib.ExtractImageChip <RgbPixel>(inputImage, chipDetails, InterpolationTypes.Bilinear))
                                                            {
                                                                if (!segNetsByClass.TryGetValue(instance.Label, out var i))
                                                                {
                                                                    // per-class segmentation net not found, so we must be using the same net for all classes
                                                                    // (see bool separate_seg_net_for_each_class in dnn_instance_segmentation_train_ex.cpp)
                                                                    if (segNetsByClass.Count == 1)
                                                                    {
                                                                        throw new ApplicationException();
                                                                    }
                                                                    if (string.IsNullOrEmpty(segNetsByClass.First().Key))
                                                                    {
                                                                        throw new ApplicationException();
                                                                    }
                                                                }

                                                                var segNet = i != null
                                                               ? i                             // use the segmentation net trained for this class
                                                               : segNetsByClass.First().Value; // use the same segmentation net for all classes

                                                                using (var mask = segNet.Operator(inputChip))
                                                                {
                                                                    var randomColor = new RgbPixel(
                                                                        rnd.GetRandom8BitNumber(),
                                                                        rnd.GetRandom8BitNumber(),
                                                                        rnd.GetRandom8BitNumber()
                                                                        );

                                                                    using (var resizedMask = new Matrix <ushort>((int)chipDetails.Rect.Height, (int)chipDetails.Rect.Width))
                                                                    {
                                                                        Dlib.ResizeImage(mask.First(), resizedMask);

                                                                        for (int r = 0, nr = resizedMask.Rows; r < nr; ++r)
                                                                        {
                                                                            for (int c = 0, nc = resizedMask.Columns; c < nc; ++c)
                                                                            {
                                                                                if (resizedMask[r, c] != 0)
                                                                                {
                                                                                    var y = (int)(chipDetails.Rect.Top + r);
                                                                                    var x = (int)(chipDetails.Rect.Left + c);
                                                                                    if (y >= 0 && y < rgbLabelImage.Rows && x >= 0 && x < rgbLabelImage.Columns)
                                                                                    {
                                                                                        rgbLabelImage[y, x] = randomColor;
                                                                                    }
                                                                                }
                                                                            }
                                                                        }
                                                                    }

                                                                    var voc2012Class = PascalVOC2012.FindVoc2012Class(instance.Label);
                                                                    Dlib.DrawRectangle(rgbLabelImage, instance.Rect, voc2012Class.RgbLabel, 1u);
                                                                }
                                                            }
                                                }

                                                instances.DisposeElement();

                                                using (var tmp = Dlib.JoinRows(inputImage, rgbLabelImage))
                                                {
                                                    // Show the input image on the left, and the predicted RGB labels on the right.
                                                    win.SetImage(tmp);

                                                    if (instances.Any())
                                                    {
                                                        Console.Write($" in {file.Name} - hit enter to process the next image");
                                                        Console.ReadKey();
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }

                        foreach (var kvp in segNetsByClass)
                        {
                            kvp.Value.Dispose();
                        }
                    }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
Exemplo n.º 15
0
        public async Task ProcessAsync(string[] inputFilenames)
        {
            var chips        = new List <Matrix <RgbPixel> >();
            var faces        = new List <Rectangle>();
            var filename     = new List <string>();
            var jsonFilename = inputFilenames.First() + ".json";

            foreach (var inputFilename in inputFilenames)
            {
                if (!File.Exists(inputFilename))
                {
                    break;
                }

                if (File.Exists(jsonFilename))
                {
                    continue;
                }

                // load the image
                using var img = await DlibHelpers.LoadRotatedImage(imageRotationService, inputFilename);

                // Dlib.SaveJpeg(img, inputFilename + "__1.jpg", 25);
                // Dlib.SaveJpeg(img, inputFilename + "__2.jpg", 25);

                // detect all faces
                foreach (var face in detector.Operator(img))
                {
                    // detect landmarks
                    var shape = predictor.Detect(img, face);

                    // extract normalized and rotated 150x150 face chip
                    var faceChipDetail = Dlib.GetFaceChipDetails(shape, 150, 0.25);
                    var faceChip       = Dlib.ExtractImageChip <RgbPixel>(img, faceChipDetail);

                    // convert the chip to a matrix and store
                    var matrix = new Matrix <RgbPixel>(faceChip);
                    chips.Add(matrix);
                    faces.Add(face);
                    filename.Add(inputFilename);
                }
            }

            if (!File.Exists(jsonFilename))
            {
                var ffd = new FoundFacesData
                {
                    // Chips = chips,
                    Faces     = faces,
                    Filenames = filename,
                };

                OutputLabels <Matrix <float> > descriptors = null;
                if (chips.Any())
                {
                    // put each fae in a 128D embedding space
                    // similar faces will be placed close together
                    // Console.WriteLine("Recognizing faces...");
                    descriptors     = dnn.Operator(chips);
                    ffd.Descriptors = descriptors.ToList();
                }
                else
                {
                    ffd.Descriptors = new List <Matrix <float> >(0);
                }

                var dto = new FoundFacesDataDto
                {
                    Faces = ffd.Faces
                            .Select(f => new RectangleDto
                    {
                        Bottom = f.Bottom,
                        Left   = f.Left,
                        Top    = f.Top,
                        Right  = f.Right,
                    })
                            .ToList(),

                    Filenames = ffd.Filenames,

                    Descriptors = ffd.Descriptors
                                  .Select(x => new MatrixFloatDto
                    {
                        Data    = x.ToArray(),
                        Row     = x.Rows,
                        Columns = x.Columns,
                    })
                                  .ToList()
                };

                var x = JsonConvert.SerializeObject(dto);
                File.WriteAllText(jsonFilename, JsonConvert.SerializeObject(dto));
            }

            FoundFacesData items;

            using (var r = new StreamReader(jsonFilename))
            {
                var json     = r.ReadToEnd();
                var itemsdto = JsonConvert.DeserializeObject <FoundFacesDataDto>(json);
                items = new FoundFacesData
                {
                    Faces       = itemsdto.Faces.Select(f => new Rectangle(f.Left, f.Top, f.Right, f.Bottom)).ToList(),
                    Filenames   = itemsdto.Filenames.ToList(),
                    Descriptors = itemsdto.Descriptors.Select(d => new Matrix <float>(d.Data, d.Row, d.Columns)).ToList(),
                };
            }

            if (items.Faces.Count <= 0)
            {
                return;
            }

            // // compare each face with all other faces
            var edges = new List <SamplePair>();

            // for (uint i = 0; i < descriptors.Count; ++i)
            // for (var j = i; j < descriptors.Count; ++j)
            //
            //     // record every pair of two similar faces
            //     // faces are similar if they are less than 0.6 apart in the 128D embedding space
            //     if (Dlib.Length(descriptors[i] - descriptors[j]) < 0.5)
            //         edges.Add(new SamplePair(i, j));
            //
            // // use the chinese whispers algorithm to find all face clusters
            // Dlib.ChineseWhispers(edges, 100, out var clusters, out var labels);
            // // Console.WriteLine($"   Found {clusters} unique person(s) in the image");
            //
            // // draw rectangles on each face using the cluster color
            // for (var i = 0; i < faces.Count; i++)
            // {
            //     var color = new RgbPixel(255, 255, 255);
            //     if (labels[i] < palette.Length)
            //         color = palette[labels[i]];
            //
            //     using var img = Dlib.LoadImage<RgbPixel>(filename[i] + "__1.jpg");
            //     Dlib.DrawRectangle(img, faces[i], color: color, thickness: 4);
            //     Dlib.SaveJpeg(img, filename[i] + "__1.jpg", 25);
            // }
            //
            // Console.WriteLine("end 1");

            // compare each face with all other faces
            edges = new List <SamplePair>();
            for (var i = 0; i < items.Descriptors.Count; ++i)
            {
                for (var j = i; j < items.Descriptors.Count; ++j)
                {
                    // record every pair of two similar faces
                    // faces are similar if they are less than 0.6 apart in the 128D embedding space
                    if (Dlib.Length(items.Descriptors[i] - items.Descriptors[j]) < 0.4)
                    {
                        edges.Add(new SamplePair((uint)i, (uint)j));
                    }
                }
            }

            // use the chinese whispers algorithm to find all face clusters
            Dlib.ChineseWhispers(edges, 100, out var clusters2, out var labels2);
            // Console.WriteLine($"   Found {clusters} unique person(s) in the image");

            // draw rectangles on each face using the cluster color
            for (var i = 0; i < items.Faces.Count; i++)
            {
                var color = palette[0];
                if (labels2[i] < palette.Length)
                {
                    color = palette[labels2[i]];
                }

                if (!File.Exists(items.Filenames[i] + $"_x{labels2[i]}.jpg"))
                {
                    using var img2 = await DlibHelpers.LoadRotatedImage(imageRotationService, items.Filenames[i]);

                    Dlib.SaveJpeg(img2, items.Filenames[i] + $"_x{labels2[i]}.jpg", 25);
                }

                using var img = Dlib.LoadImage <RgbPixel>(items.Filenames[i] + $"_x{labels2[i]}.jpg");
                Dlib.DrawRectangle(img, items.Faces[i], color: color, thickness: 4);
                Dlib.SaveJpeg(img, items.Filenames[i] + $"_x{labels2[i]}.jpg", 25);
            }

            // var origFilename = new FileInfo(inputFilename).Name;
            // var outputFilename = Path.Combine(outputDirectory, $"{origFilename}_Identification.jpg");

            // Dlib.SaveJpeg(img, inputFilename, 75);
        }
Exemplo n.º 16
0
        public void TryTrack()
        {
            const string testName = "TryTrack";

            var tests = new[]
            {
                new { Type = ImageTypes.Int32, ExpectResult = true }
            };

            var type = this.GetType().Name;

            foreach (var input in tests)
            {
                var tracker = new CorrelationTracker();
                try
                {
                    var index = 0;
                    foreach (var file in this.GetDataFiles("video_frames").Where(info => info.Name.EndsWith("jpg")))
                    {
                        var expectResult = input.ExpectResult;
                        var inputType    = input.Type;
                        var imageObj     = DlibTest.LoadImage(input.Type, file);

                        if (index == 0)
                        {
                            using (var rect = DRectangle.CenteredRect(93, 110, 38, 86))
                                tracker.StartTrack(imageObj, rect);
                        }

                        var outputImageAction = new Func <bool, Array2DBase>(expect =>
                        {
                            if (index != 0)
                            {
                                tracker.Update(imageObj);
                            }

                            return(imageObj);
                        });

                        var successAction = new Action <Array2DBase>(image =>
                        {
                            if (index != 0)
                            {
                                tracker.Update(image);
                            }
                            using (var r = tracker.GetPosition())
                            {
                                using (var img = Dlib.LoadImage <RgbPixel>(file.FullName))
                                {
                                    Dlib.DrawRectangle(img, (Rectangle)r, new RgbPixel {
                                        Red = 255
                                    }, 3);
                                    Dlib.SaveJpeg(img, Path.Combine(this.GetOutDir(type), $"{Path.GetFileNameWithoutExtension(file.FullName)}.jpg"));
                                }
                            }
                        });

                        var failAction = new Action(() =>
                        {
                            Assert.Fail($"{testName} should throw excption for InputType: {inputType}.");
                        });

                        var finallyAction = new Action(() =>
                        {
                            index++;

                            if (imageObj != null)
                            {
                                this.DisposeAndCheckDisposedState(imageObj);
                            }
                        });

                        var exceptionAction = new Action(() =>
                        {
                            Console.WriteLine($"Failed to execute {testName} to InputType: {inputType}.");
                        });

                        DoTest(outputImageAction, expectResult, successAction, finallyAction, failAction, exceptionAction);
                    }
                }
                finally
                {
                    this.DisposeAndCheckDisposedState(tracker);
                }
            }
        }
Exemplo n.º 17
0
        public JsonResult CreatePost()
        {
            try
            {
                var photo       = Request.Files[0];
                var token       = Request.Form["userToken"];
                var description = Request.Form["description"];
                var category    = (Category)int.Parse(Request.Form["category"]);

                PostViewModel postViewModel = new PostViewModel()
                {
                    Description = description, Token = token, Category = category
                };
                var user = UserService.GetUser(postViewModel.Token);
                if (user != null && photo != null)
                {
                    var fileExtension = "png";
                    if (photo.ContentType.IndexOf("jpeg") != -1)
                    {
                        fileExtension = "jpeg";
                    }
                    else if (photo.ContentType.IndexOf("jpg") != -1)
                    {
                        fileExtension = "jpg";
                    }

                    var filename = Helper.Util.GenerateFileName(user.UserID, fileExtension, "post_f_");

                    var uploads = Path.Combine(System.Configuration.ConfigurationManager.AppSettings["MediaPath"] + "Posts", filename);
                    photo.SaveAs(uploads);
                    using (var fd = Dlib.GetFrontalFaceDetector())
                    {
                        var img = Dlib.LoadImage <RgbPixel>(uploads);

                        // find all faces in the image
                        var faces = fd.Operator(img);
                        uploads  = uploads.Replace("post_f_", "post_");
                        filename = filename.Replace("post_f_", "post_");
                        //bool needSaveByDlib = true;
                        foreach (var face in faces)
                        {
                            // draw a rectangle for each face
                            Dlib.DrawRectangle(img, face, color: new RgbPixel(0, 0, 0), thickness: 10);
                            Dlib.FillRect(img, face, new RgbPixel(0, 0, 0));
                            //var image = Bitmap.FromFile(uploads);

                            /*using (var stream = new MemoryStream())
                             * {
                             *      photo.CopyTo(stream);
                             *      using (var img2 = System.Drawing.Image.FromStream(stream))
                             *      {
                             *              using (var graphic = Graphics.FromImage(img2))
                             *              {
                             *                      //var font = new Font(FontFamily.GenericSansSerif, 20, FontStyle.Bold, GraphicsUnit.Pixel);
                             *                      //var color = Color.FromArgb(128, 255, 255, 255);
                             *                      //var brush = new SolidBrush(color);
                             *                      //var point = new System.Drawing.Point(img2.Width - 120, img2.Height - 30);
                             *
                             *                      //graphic.DrawString("edi.wang", font, brush, point);
                             *                      using (var imgEmotion = System.Drawing.Image.FromFile(Path.Combine(System.Configuration.ConfigurationManager.AppSettings["MediaPath"] + "images", "emotion.png")))
                             *                              graphic.DrawImage(imgEmotion, new RectangleF(face.Left, face.Top, face.Width, face.Height));
                             *
                             *                      img2.Save(uploads, System.Drawing.Imaging.ImageFormat.Png);
                             *                      needSaveByDlib = false;
                             *              }
                             *      }
                             * }*/
                        }
                        //if (needSaveByDlib)
                        Dlib.SaveJpeg(img, uploads);
                    }
                    PostService.Insert(new Post()
                    {
                        Category     = postViewModel.Category,
                        CreationDate = DateTime.Now,
                        Description  = postViewModel.Description,
                        Filename     = filename,
                        Comments     = 0,
                        Likes        = 0,
                        Username     = user.Username,
                        UserID       = user.UserID
                    });
                    return(Json(new BaseResponse <bool>()
                    {
                        Data = true, Message = "OK", Success = true
                    }));
                }
                else
                {
                    return(Json(new BaseResponse <bool>()
                    {
                        Data = false, Message = "User not found!", Success = false
                    }));
                }
            }
            catch (Exception ex)
            {
                return(Json(new BaseResponse <bool>()
                {
                    Data = false, Message = "Operation fail, try again!<!--" + ex.Message + "-->", Success = false
                }));
            }
        }
Exemplo n.º 18
0
        public JsonResult UploadUserAvatar()
        {
            var avatarFile = Request.Files[0];
            var userToken  = Request.Form["userToken"];

            if (string.IsNullOrEmpty(userToken))
            {
                return(Json(new BaseResponse <string> {
                    Success = false, Message = "User token not sent"
                }));
            }
            if (avatarFile == null)
            {
                return(Json(new BaseResponse <string> {
                    Success = false, Message = "Avatar not sent"
                }));
            }

            var user = UserService.GetUser(userToken);

            if (user != null)
            {
                var fileExtension = "png";
                if (avatarFile.ContentType.IndexOf("jpeg") != -1)
                {
                    fileExtension = "jpeg";
                }
                else if (avatarFile.ContentType.IndexOf("jpg") != -1)
                {
                    fileExtension = "jpg";
                }

                var filename = Helper.Util.GenerateFileName(user.UserID, fileExtension, "avatar_f_");

                if (UserService.UpdateAvatar(user.UserID, filename))
                {
                    var uploads = Path.Combine(System.Configuration.ConfigurationManager.AppSettings["MediaPath"] + "avatars", filename);
                    avatarFile.SaveAs(uploads);
                    using (var fd = Dlib.GetFrontalFaceDetector())
                    {
                        var img = Dlib.LoadImage <RgbPixel>(uploads);

                        // 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);
                        }
                        uploads = uploads.Replace("avatar_f_", "avatar_");
                        Dlib.SaveJpeg(img, uploads);
                    }
                    return(Json(new BaseResponse <string> {
                        Data = filename, Success = true
                    }));
                }
                else
                {
                    return(Json(new BaseResponse <string> {
                        Success = false, Message = "Error on update avatar"
                    }));
                }
            }

            return(Json(new BaseResponse <string> {
                Success = false, Message = "Token not find a user"
            }));
        }
Exemplo n.º 19
0
        private void Timer1_Tick(object sender, EventArgs e)
        {
            capture.Read(frame);

            this.point = new Point((frame.Width - size.Width) / 2, (frame.Height - size.Height) / 2);
            this.rect  = new Rect(point, size);

            Cv2.Flip(frame, frame, FlipMode.Y);

            if (!frame.Empty() && start)
            {
                var img = ConvertToArray2D(frame);

                var faces = fd.Operator(img);

                if (faces.Any(face => IsFaceInFrame(face)))
                {
                    foreach (var face in faces)
                    {
                        if (IsFaceInFrame(face))
                        {
                            //Dlib.DrawRectangle(img, face, color: new RgbPixel(0, 255, 255), thickness: 4);
                            var shape = sp.Detect(img, face);

                            var landmarks = new MatOfPoint2d(1, 6,
                                                             (from i in new int[] { 30, 8, 36, 45, 48, 54 }
                                                              let pt = shape.GetPart((uint)i)
                                                                       select new OpenCvSharp.Point2d(pt.X, pt.Y)).ToArray());

                            var cameraMatrix = Utility.GetCameraMatrix((int)img.Rect.Width, (int)img.Rect.Height);

                            Mat rotation    = new MatOfDouble();
                            Mat translation = new MatOfDouble();
                            Cv2.SolvePnP(model, landmarks, cameraMatrix, coeffs, rotation, translation);

                            var euler = Utility.GetEulerMatrix(rotation);

                            var yaw   = 180 * euler.At <double>(0, 2) / Math.PI;
                            var pitch = 180 * euler.At <double>(0, 1) / Math.PI;
                            pitch = Math.Sign(pitch) * 180 - pitch;

                            Cv2.ProjectPoints(poseModel, rotation, translation, cameraMatrix, coeffs, poseProjection);

                            //var landmark = landmarks.At<Point2d>(0);
                            //var p = poseProjection.At<Point2d>(0);
                            //Dlib.DrawLine(
                            //    img,
                            //    new DlibDotNet.Point((int)landmark.X, (int)landmark.Y),
                            //    new DlibDotNet.Point((int)p.X, (int)p.Y),
                            //    color: new RgbPixel(0, 255, 255));

                            //foreach (var i in new int[] { 30, 8, 36, 45, 48, 54 })
                            //{
                            //    var point = shape.GetPart((uint)i);
                            //    var rect = new Rectangle(point);
                            //    Dlib.DrawRectangle(img, rect, color: new RgbPixel(255, 255, 0), thickness: 4);
                            //}
                            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(0, 255, 255), thickness: 4);
                            }

                            CheckFace(pitch, frame, face, yaw, pitch);
                            frame = img.ToBitmap().ToMat();
                        }
                    }
                }
                else if (this.step > 0)
                {
                    SetZero();
                    this.ErrorMsg.Visible = true;
                }
            }

            Cv2.Rectangle(frame, rect, Scalar.Yellow, thickness: 2);
            camera.Image = frame.ToBitmap();
        }