Beispiel #1
0
        public void DetectFaceRectDetection()
        {
            if (this._FrontalFaceDetector == null)
            {
                Assert.True(false, "ShapePredictor is not initialized!!");
            }

            var faceDetector = this._FrontalFaceDetector;

            const string testName = nameof(DetectFaceRectDetection);
            var          path     = this.GetDataFile("Lenna.bmp");

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

            foreach (var input in tests)
            {
                var expectResult = input.ExpectResult;
                var imageObj     = DlibTest.LoadImageAsMatrixHelp(input.Type, path);
                IEnumerable <RectDetection> result = null;

                var outputImageAction = new Func <bool, IEnumerable <RectDetection> >(expect =>
                {
                    faceDetector.Operator(imageObj, out var detections);
                    result = detections;
                    return(detections);
                });

                var successAction = new Action <IEnumerable <RectDetection> >(detections =>
                {
                    // This test does NOT check whether output image and detect face area are correct
                    //Dlib.SaveJpeg(image, $"{Path.Combine(this.GetOutDir(type, testName), $"2008_001322_{input.Type}.jpg")}");
                    Console.WriteLine($"{input.Type}");
                    foreach (var d in detections)
                    {
                        var rect = d.Rect;
                        Console.WriteLine($"\tDetectionConfidence: {d.DetectionConfidence}, WeightIndex: {d.WeightIndex}, Rect: \n\t\tLeft: {rect.Left}, Top: {rect.Top}, Right: {rect.Right}, Bottom: {rect.Bottom}");
                    }
                });

                var failAction = new Action(() =>
                {
                    Assert.True(false, $"{testName} should throw exception for InputType: {input.Type}.");
                });

                var finallyAction = new Action(() =>
                {
                    this.DisposeAndCheckDisposedState(imageObj);
                    if (result != null)
                    {
                        this.DisposeAndCheckDisposedStates(result);
                    }
                });

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

                DoTest(outputImageAction, expectResult, successAction, finallyAction, failAction, exceptionAction);
            }
        }
        public void DetectFaceMModRect2()
        {
            if (this._ShapePredictor == null)
            {
                Assert.True(false, "ShapePredictor is not initialized!!");
            }

            const string testName = "DetectFaceMModRect2";
            var          path     = this.GetDataFile("Lenna_mini.bmp");
            var          tests    = new[]
            {
                new { Type = MatrixElementTypes.RgbPixel, ExpectResult = true },
                new { Type = MatrixElementTypes.UInt8, ExpectResult = true },
                new { Type = MatrixElementTypes.UInt16, ExpectResult = true },
                new { Type = MatrixElementTypes.UInt32, ExpectResult = true },
                new { Type = MatrixElementTypes.Int8, ExpectResult = true },
                new { Type = MatrixElementTypes.Int16, ExpectResult = true },
                new { Type = MatrixElementTypes.Int32, ExpectResult = true },
                new { Type = MatrixElementTypes.HsiPixel, ExpectResult = true },
                new { Type = MatrixElementTypes.LabPixel, ExpectResult = true },
                new { Type = MatrixElementTypes.Float, ExpectResult = true },
                new { Type = MatrixElementTypes.Double, ExpectResult = true },
                new { Type = MatrixElementTypes.RgbAlphaPixel, ExpectResult = false }
            };

            using (var faceDetector = Dlib.GetFrontalFaceDetector())
                foreach (var input in tests)
                {
                    var        expectResult = input.ExpectResult;
                    var        imageObj     = DlibTest.LoadImageAsMatrixHelp(input.Type, path);
                    MModRect[] dets         = null;

                    var outputImageAction = new Func <bool, MatrixBase>(expect =>
                    {
                        dets = faceDetector.Operator(imageObj).Select(r => new MModRect {
                            Rect = r
                        }).ToArray();
                        return(imageObj);
                    });

                    var successAction = new Action <MatrixBase>(image =>
                    {
                        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);
                        }

                        // This test does NOT check whether output image and detect face area are correct
                        //Dlib.SaveJpeg(image, $"{Path.Combine(this.GetOutDir(type, testName), $"2008_001322_{input.Type}.jpg")}");
                    });

                    var failAction = new Action(() =>
                    {
                        Assert.True(false, $"{testName} should throw exception for InputType: {input.Type}.");
                    });

                    var finallyAction = new Action(() =>
                    {
                        this.DisposeAndCheckDisposedState(imageObj);
                    });

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

                    DoTest(outputImageAction, expectResult, successAction, finallyAction, failAction, exceptionAction);
                }
        }
Beispiel #3
0
        public void DetectFace3()
        {
            if (this._FrontalFaceDetector == null)
            {
                Assert.True(false, "ShapePredictor is not initialized!!");
            }

            var faceDetector = this._FrontalFaceDetector;

            const string testName = "DetectFace3";
            var          path     = this.GetDataFile("Lenna_mini.bmp");

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

            foreach (var input in tests)
            {
                var         expectResult = input.ExpectResult;
                var         imageObj     = DlibTest.LoadImageAsMatrixHelp(input.Type, path);
                Rectangle[] dets         = null;

                var outputImageAction = new Func <bool, Rectangle[]>(expect =>
                {
                    dets = faceDetector.Operator(imageObj);
                    return(dets);
                });

                var successAction = new Action <Rectangle[]>(image =>
                {
                    // This test does NOT check whether output image and detect face area are correct
                    //Dlib.SaveJpeg(image, $"{Path.Combine(this.GetOutDir(type, testName), $"2008_001322_{input.Type}.jpg")}");
                });

                var failAction = new Action(() =>
                {
                    Assert.True(false, $"{testName} should throw exception for InputType: {input.Type}.");
                });

                var finallyAction = new Action(() =>
                {
                    this.DisposeAndCheckDisposedState(imageObj);
                });

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

                DoTest(outputImageAction, expectResult, successAction, finallyAction, failAction, exceptionAction);
            }
        }