public static IEnumerable <IEnumerable <MModRect> > DetectMulti(LossMmod net, IEnumerable <Image> images, int upsampleNumTimes, int batchSize = 128)
        {
            var dimgs    = new List <Matrix <RgbPixel> >();
            var allRects = new List <IEnumerable <MModRect> >();

            using (var pyr = new PyramidDown(2))
            {
                // Copy the data into dlib based objects
                foreach (var matrix in images)
                {
                    using (var image = new Matrix <RgbPixel>())
                    {
                        var type = matrix.Matrix.MatrixElementType;
                        switch (type)
                        {
                        case MatrixElementTypes.UInt8:
                        case MatrixElementTypes.RgbPixel:
                            DlibDotNet.Dlib.AssignImage(matrix.Matrix, image);
                            break;

                        default:
                            throw new NotSupportedException("Unsupported image type, must be 8bit gray or RGB image.");
                        }

                        for (var i = 0; i < upsampleNumTimes; i++)
                        {
                            DlibDotNet.Dlib.PyramidUp(image);
                        }

                        dimgs.Add(image);

                        for (var i = 1; i < dimgs.Count; i++)
                        {
                            if (dimgs[i - 1].Columns != dimgs[i].Columns || dimgs[i - 1].Rows != dimgs[i].Rows)
                            {
                                throw new ArgumentException("Images in list must all have the same dimensions.");
                            }
                        }

                        var dets = net.Operator(dimgs, (ulong)batchSize);
                        foreach (var det in dets)
                        {
                            var rects = new List <MModRect>();
                            foreach (var d in det)
                            {
                                var drect = pyr.RectDown(new DRectangle(d.Rect), (uint)upsampleNumTimes);
                                d.Rect = new Rectangle((int)drect.Left, (int)drect.Top, (int)drect.Right, (int)drect.Bottom);
                                rects.Add(d);
                            }

                            allRects.Add(rects);
                        }
                    }
                }
            }

            return(allRects);
        }
Beispiel #2
0
        public static IEnumerable <MModRect> Detect(LossMmod net, Image image, int upsampleNumTimes)
        {
            using (var pyr = new PyramidDown(2))
            {
                var rects = new List <MModRect>();

                // Copy the data into dlib based objects
                using (var matrix = new Matrix <RgbPixel>())
                {
                    var type = image.Mode;
                    switch (type)
                    {
                    case Mode.Greyscale:
                    case Mode.Rgb:
                        DlibDotNet.Dlib.AssignImage(image.Matrix, matrix);
                        break;

                    default:
                        throw new NotSupportedException("Unsupported image type, must be 8bit gray or RGB image.");
                    }

                    // Upsampling the image will allow us to detect smaller faces but will cause the
                    // program to use more RAM and run longer.
                    var levels = upsampleNumTimes;
                    while (levels > 0)
                    {
                        levels--;
                        DlibDotNet.Dlib.PyramidUp <PyramidDown>(matrix, 2);
                    }

                    var dets = net.Operator(matrix);

                    // Scale the detection locations back to the original image size
                    // if the image was upscaled.
                    foreach (var d in dets.First())
                    {
                        var drect = pyr.RectDown(new DRectangle(d.Rect), (uint)upsampleNumTimes);
                        d.Rect = new Rectangle((int)drect.Left, (int)drect.Top, (int)drect.Right, (int)drect.Bottom);
                        rects.Add(d);
                    }

                    return(rects);
                }
            }
        }
Beispiel #3
0
        public static IEnumerable <Rectangle> RunDetectorWithUpscale1(FrontalFaceDetector detector,
                                                                      Image img,
                                                                      uint upsamplingAmount,
                                                                      double adjustThreshold,
                                                                      List <double> detectionConfidences,
                                                                      List <ulong> weightIndices)
        {
            var rectangles = new List <Rectangle>();

            if (img.Mode == Mode.Greyscale)
            {
                var greyscaleMatrix = img.Matrix as Matrix <byte>;
                if (upsamplingAmount == 0)
                {
                    detector.Operator(greyscaleMatrix, out var rectDetections, adjustThreshold);

                    var dets = rectDetections.ToArray();
                    SplitRectDetections(dets, rectangles, detectionConfidences, weightIndices);

                    foreach (var rectDetection in dets)
                    {
                        rectDetection.Dispose();
                    }
                }
                else
                {
                    using (var pyr = new PyramidDown(2))
                    {
                        Matrix <byte> temp = null;

                        try
                        {
                            DlibDotNet.Dlib.PyramidUp(greyscaleMatrix, pyr, out temp);

                            var levels = upsamplingAmount - 1;
                            while (levels > 0)
                            {
                                levels--;
                                DlibDotNet.Dlib.PyramidUp(temp);
                            }

                            detector.Operator(temp, out var rectDetections, adjustThreshold);

                            var dets = rectDetections.ToArray();
                            foreach (var t in dets)
                            {
                                t.Rect = pyr.RectDown(t.Rect, upsamplingAmount);
                            }

                            SplitRectDetections(dets, rectangles, detectionConfidences, weightIndices);

                            foreach (var rectDetection in dets)
                            {
                                rectDetection.Dispose();
                            }
                        }
                        finally
                        {
                            temp?.Dispose();
                        }
                    }
                }

                return(rectangles);
            }
            else
            {
                var rgbMatrix = img.Matrix as Matrix <RgbPixel>;
                if (upsamplingAmount == 0)
                {
                    detector.Operator(rgbMatrix, out var rectDetections, adjustThreshold);

                    var dets = rectDetections.ToArray();
                    SplitRectDetections(dets, rectangles, detectionConfidences, weightIndices);

                    foreach (var rectDetection in dets)
                    {
                        rectDetection.Dispose();
                    }
                }
                else
                {
                    using (var pyr = new PyramidDown(2))
                    {
                        Matrix <RgbPixel> temp = null;

                        try
                        {
                            DlibDotNet.Dlib.PyramidUp(rgbMatrix, pyr, out temp);

                            var levels = upsamplingAmount - 1;
                            while (levels > 0)
                            {
                                levels--;
                                DlibDotNet.Dlib.PyramidUp(temp);
                            }

                            detector.Operator(temp, out var rectDetections, adjustThreshold);

                            var dets = rectDetections.ToArray();
                            foreach (var t in dets)
                            {
                                t.Rect = pyr.RectDown(t.Rect, upsamplingAmount);
                            }

                            SplitRectDetections(dets, rectangles, detectionConfidences, weightIndices);

                            foreach (var rectDetection in dets)
                            {
                                rectDetection.Dispose();
                            }
                        }
                        finally
                        {
                            temp?.Dispose();
                        }
                    }
                }

                return(rectangles);
            }
        }