Example #1
0
        public FullObjectDetection[] FaceLandmarks(Array2DBase image, Rectangle[] rects)
        {
            this.ThrowIfDisposed();

            if (image == null)
            {
                throw new ArgumentNullException(nameof(image));
            }
            if (rects == null)
            {
                throw new ArgumentNullException(nameof(rects));
            }

            image.ThrowIfDisposed();

            var inType = image.ImageType.ToNativeArray2DType();

            using (var rectangles = new StdVector <Rectangle>(rects))
            {
                using (var dets = new StdVector <FullObjectDetection>())
                {
                    var ret = Native.face_recognition_face_landmarks(dets.NativePtr, this.NativePtr, inType, image.NativePtr, rectangles.NativePtr);
                    if (ret == Dlib.Native.ErrorType.InputElementTypeNotSupport)
                    {
                        throw new ArgumentException($"Input {inType} is not supported.");
                    }

                    return(dets.ToArray());
                }
            }
        }
Example #2
0
        public Matrix <double>[] FaceEncodings(Array2DBase image, FullObjectDetection[] landmarks, double padding = 0.2d)
        {
            this.ThrowIfDisposed();

            if (image == null)
            {
                throw new ArgumentNullException(nameof(image));
            }
            if (landmarks == null)
            {
                throw new ArgumentNullException(nameof(landmarks));
            }

            image.ThrowIfDisposed();

            var inType = image.ImageType.ToNativeArray2DType();

            using (var rectangles = new StdVector <FullObjectDetection>((IEnumerable <FullObjectDetection>)landmarks))
            {
                using (var dets = new StdVector <Matrix <double> >())
                {
                    var ret = Native.face_recognition_face_encodings2(dets.NativePtr, this.NativePtr, inType, image.NativePtr, rectangles.NativePtr, padding);
                    if (ret == Dlib.Native.ErrorType.InputElementTypeNotSupport)
                    {
                        throw new ArgumentException($"Input {inType} is not supported.");
                    }

                    return(dets.ToArray());
                }
            }
        }
Example #3
0
        public void ExtractFHogFeatures2()
        {
            var path = this.GetDataFile($"{LoadTarget}.bmp");

            var tests = new[]
            {
                new { Type = MatrixElementTypes.Float, ExpectResult = true },
                new { Type = MatrixElementTypes.Double, ExpectResult = true }
            };

            foreach (var output in tests)
            {
                Array2DBase       imageObj  = null;
                Array2DMatrixBase outputObj = null;

                try
                {
                    imageObj = DlibTest.LoadImageHelp(ImageTypes.RgbPixel, path);

                    switch (output.Type)
                    {
                    case MatrixElementTypes.Float:
                        outputObj = Dlib.ExtractFHogFeatures <float>(imageObj);
                        break;

                    case MatrixElementTypes.Double:
                        outputObj = Dlib.ExtractFHogFeatures <double>(imageObj);
                        break;

                    default:
                        throw new ArgumentOutOfRangeException();
                    }

                    MatrixBase matrix = Dlib.DrawFHog(outputObj);

                    if (this.CanGuiDebug)
                    {
                        var window = new ImageWindow(matrix);
                        window.WaitUntilClosed();
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    throw;
                }
                finally
                {
                    if (imageObj != null)
                    {
                        this.DisposeAndCheckDisposedState(imageObj);
                    }
                    if (outputObj != null)
                    {
                        this.DisposeAndCheckDisposedState(outputObj);
                    }
                }
            }
        }
Example #4
0
        public Rectangle[] FaceLocations(Array2DBase image, uint numberOfTimesToUpsample = 0, string model = "hog")
        {
            this.ThrowIfDisposed();

            if (image == null)
            {
                throw new ArgumentNullException(nameof(image));
            }

            image.ThrowIfDisposed();

            var inType = image.ImageType.ToNativeArray2DType();

            using (var dets = new StdVector <Rectangle>())
            {
                var ret = Native.face_recognition_face_locations(dets.NativePtr, this.NativePtr, inType, image.NativePtr, numberOfTimesToUpsample, Encoding.UTF8.GetBytes(model));
                if (ret == Dlib.Native.ErrorType.InputElementTypeNotSupport)
                {
                    throw new ArgumentException($"Input {inType} is not supported.");
                }

                return(dets.ToArray());
            }
        }
        public void SuppressNonMaximumEdges()
        {
            var path = this.GetDataFile($"{LoadTarget}.bmp");

            var tests = new[]
            {
                new { Type = ImageTypes.BgrPixel, ExpectResult = true },
                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.HsiPixel, ExpectResult = true },
                new { Type = ImageTypes.Float, ExpectResult = true },
                new { Type = ImageTypes.Double, ExpectResult = true }
            };

            var type = this.GetType().Name;

            foreach (var inputType in new[] { ImageTypes.Float, ImageTypes.Double })
            {
                foreach (var test in tests)
                {
                    Array2DBase inputObj  = null;
                    Array2DBase horzObj   = null;
                    Array2DBase vertObj   = null;
                    Array2DBase outputObj = null;

                    try
                    {
                        var inputImage = DlibTest.LoadImageHelp(inputType, path);
                        inputObj = inputImage;
                        var horz = Array2DTest.CreateArray2DHelp(inputType);
                        horzObj = horz;
                        var vert = Array2DTest.CreateArray2DHelp(inputType);
                        vertObj = vert;
                        var outputImage = Array2DTest.CreateArray2DHelp(test.Type);
                        outputObj = outputImage;

                        Dlib.SobelEdgeDetector(inputImage, horz, vert);

                        try
                        {
                            Dlib.SuppressNonMaximumEdges(horz, vert, outputImage);

                            if (!test.ExpectResult)
                            {
                                Assert.True(false, $"SuppressNonMaximumEdges should throw exception for InputType: {inputType}, Type: {test.Type}.");
                            }
                            else
                            {
                                Dlib.SaveBmp(outputImage, $"{Path.Combine(this.GetOutDir(type, "SuppressNonMaximumEdges"), $"{LoadTarget}_{inputType}_{test.Type}.bmp")}");
                            }
                        }
                        catch (ArgumentException)
                        {
                            if (!test.ExpectResult)
                            {
                                Console.WriteLine("OK");
                            }
                            else
                            {
                                throw;
                            }
                        }
                        catch (NotSupportedException)
                        {
                            if (!test.ExpectResult)
                            {
                                Console.WriteLine("OK");
                            }
                            else
                            {
                                throw;
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.StackTrace);
                        Console.WriteLine($"Failed to execute SuppressNonMaximumEdges to InputType: {inputType}, Type: {test.Type}.");
                        throw;
                    }
                    finally
                    {
                        if (inputObj != null)
                        {
                            this.DisposeAndCheckDisposedState(inputObj);
                        }
                        if (horzObj != null)
                        {
                            this.DisposeAndCheckDisposedState(horzObj);
                        }
                        if (vertObj != null)
                        {
                            this.DisposeAndCheckDisposedState(vertObj);
                        }
                        if (outputObj != null)
                        {
                            this.DisposeAndCheckDisposedState(outputObj);
                        }
                    }
                }
            }
        }
        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);
                    }
                }
            }
        }
        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.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;
                IEnumerable <Rectangle> rects = null;

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

#if DEBUG
                    using (var bmp = Image.FromFile(path.FullName))
                        using (var g = Graphics.FromImage(bmp))
                            using (var p = new Pen(Color.Blue, 1f))
                            {
                                foreach (var r in rects)
                                {
                                    g.DrawRectangle(p, r.Left, r.Top, r.Width, r.Height);
                                }

                                bmp.Save($"{Path.Combine(this.GetOutDir(type, "FindCandidateObjectLocations"), $"Lenna_{test.Type}.bmp")}");
                            }
#endif
                }
                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 (rects != null)
                    {
                        this.DisposeAndCheckDisposedState(rects);
                    }
                    if (inImg != null)
                    {
                        this.DisposeAndCheckDisposedState(inImg);
                    }
                }
            }
        }
Example #8
0
        public void Jet2()
        {
            var path = this.GetDataFile($"{LoadTarget}.bmp");

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

            var type = this.GetType().Name;

            foreach (var test in tests)
            {
                Array2DBase imageObj       = null;
                Array2DBase horzObj        = null;
                Array2DBase vertObj        = null;
                Array2DBase outputImageObj = null;
                MatrixOp    matrix         = null;
                DlibObject  windowObj      = null;

                try
                {
                    const ImageTypes inputType = ImageTypes.Float;

                    var image = DlibTest.LoadImage(test.Type, path);
                    imageObj = image;
                    var horz = Array2DTest.CreateArray2D(inputType);
                    horzObj = horz;
                    var vert = Array2DTest.CreateArray2D(inputType);
                    vertObj = vert;
                    var outputImage = Array2DTest.CreateArray2D(test.Type);
                    outputImageObj = outputImage;

                    Dlib.SobelEdgeDetector(image, horz, vert);
                    Dlib.SuppressNonMaximumEdges(horz, vert, outputImage);

                    try
                    {
                        matrix = Jet(test.Type, outputImage, test.Max, test.Min);

                        if (test.ExpectResult)
                        {
                            if (this.CanGuiDebug)
                            {
                                var window = new ImageWindow(matrix, $"{test.Type} - Max: {test.Max}, Min : {test.Min}");
                                windowObj = window;
                            }

                            Dlib.SaveBmp(image, $"{Path.Combine(this.GetOutDir(type, "Jet2"), $"{LoadTarget}_{test.Type}_{test.Max}_{test.Min}.bmp")}");
                        }
                        else
                        {
                            Assert.Fail($"Failed to execute Jet2 to Type: {test.Type}");
                        }
                    }
                    catch (Exception)
                    {
                        if (!test.ExpectResult)
                        {
                            Console.WriteLine("OK");
                        }
                        else
                        {
                            throw;
                        }
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.StackTrace);
                    Console.WriteLine($"Failed to execute Jet2 to Type: {test.Type}");
                    throw;
                }
                finally
                {
                    if (outputImageObj != null)
                    {
                        this.DisposeAndCheckDisposedState(outputImageObj);
                    }
                    if (vertObj != null)
                    {
                        this.DisposeAndCheckDisposedState(vertObj);
                    }
                    if (horzObj != null)
                    {
                        this.DisposeAndCheckDisposedState(horzObj);
                    }
                    if (windowObj != null)
                    {
                        this.DisposeAndCheckDisposedState(windowObj);
                    }
                    if (matrix != null)
                    {
                        this.DisposeAndCheckDisposedState(matrix);
                    }
                    if (imageObj != null)
                    {
                        this.DisposeAndCheckDisposedState(imageObj);
                    }
                }
            }
        }