示例#1
0
        public static IEnumerable <uint> SpectralCluster <TScalar, TKernel>(TKernel kernel, IEnumerable <Matrix <TScalar> > samples, uint clusters)
            where TScalar : struct
            where TKernel : KernelBase
        {
            if (kernel == null)
            {
                throw new ArgumentNullException(nameof(kernel));
            }
            if (samples == null)
            {
                throw new ArgumentNullException(nameof(samples));
            }

            if (!(clusters > 0))
            {
                throw new ArgumentOutOfRangeException();
            }

            kernel.ThrowIfDisposed();
            var samplesArray = samples.ToArray();

            samplesArray.ThrowIfDisposed();

            var templateRow    = kernel.TemplateRows;
            var templateColumn = kernel.TemplateColumns;

            foreach (var sample in samplesArray)
            {
                if (sample == null)
                {
                    throw new ArgumentException($"{nameof(samples)} contains null object", nameof(samples));
                }
                if (sample.IsDisposed)
                {
                    throw new ArgumentException($"{nameof(samples)} contains disposed object", nameof(samples));
                }
                if (sample.TemplateRows != templateRow)
                {
                    throw new ArgumentException($"{nameof(samples)} contains different {nameof(sample.TemplateRows)} of {typeof(Matrix<TScalar>).Name}", nameof(samples));
                }
                if (sample.TemplateColumns != templateColumn)
                {
                    throw new ArgumentException($"{nameof(samples)} contains different {nameof(sample.TemplateColumns)} of {typeof(Matrix<TScalar>).Name}", nameof(samples));
                }
            }

            using (var samplesVector = new StdVector <Matrix <TScalar> >(samplesArray))
            {
                var err = NativeMethods.spectral_cluster(kernel.KernelType.ToNativeKernelType(),
                                                         kernel.SampleType.ToNativeMatrixElementType(),
                                                         kernel.TemplateRows,
                                                         kernel.TemplateColumns,
                                                         kernel.NativePtr,
                                                         samplesVector.NativePtr,
                                                         clusters,
                                                         out var ret);
                using (var vector = new StdVector <uint>(ret))
                    return(vector.ToArray());
            }
        }
示例#2
0
        public static void LoadImageDataset <T>(string path, out IList <Matrix <T> > images, out IList <IList <Rectangle> > boxes)
            where T : struct
        {
            if (path == null)
            {
                throw new ArgumentNullException(nameof(path));
            }
            if (!File.Exists(path))
            {
                throw new FileNotFoundException("", path);
            }

            var str = Encoding.GetBytes(path);

            using (var matrix = new Matrix <T>())
                using (var retImages = new StdVector <Matrix <T> >())
                    using (var retBoxes = new StdVector <StdVector <Rectangle> >())
                        using (new EnumerableDisposer <StdVector <Rectangle> >(retBoxes))
                        {
                            var type = matrix.MatrixElementType.ToNativeMatrixElementType();
                            var ret  = NativeMethods.load_image_dataset_rectangle(type, retImages.NativePtr, retBoxes.NativePtr, str, str.Length);
                            if (ret == NativeMethods.ErrorType.MatrixElementTypeNotSupport)
                            {
                                throw new ArgumentException($"{type} is not supported.");
                            }

                            images = retImages.ToArray();
                            boxes  = retBoxes.ToArray().Select(box => box.ToArray()).ToArray();
                        }
        }
示例#3
0
        public static IEnumerable <SurfPoint> GetSurfPoints(Array2DBase image, long maxPoints = 10000, double detectionThreshold = 30.0)
        {
            if (image == null)
            {
                throw new ArgumentNullException(nameof(image));
            }

            image.ThrowIfDisposed();

            if (maxPoints <= 0)
            {
                throw  new ArgumentOutOfRangeException();
            }
            if (detectionThreshold < 0)
            {
                throw new ArgumentOutOfRangeException();
            }

            using (var points = new StdVector <SurfPoint>())
            {
                var array2DType = image.ImageType.ToNativeArray2DType();
                var ret         = Native.get_surf_points(array2DType, image.NativePtr, maxPoints, detectionThreshold, points.NativePtr);
                if (ret == Native.ErrorType.ArrayTypeNotSupport)
                {
                    throw new ArgumentException($"{image.ImageType} is not supported.");
                }

                return(points.ToArray());
            }
        }
示例#4
0
        public static ChipDetails[] GetFaceChipDetails(IEnumerable <FullObjectDetection> dets, uint size = 200, double padding = 0.2d)
        {
            if (dets == null)
            {
                throw new ArgumentNullException(nameof(dets));
            }
            if (size <= 0)
            {
                throw new ArgumentException();
            }
            if (padding < 0)
            {
                throw new ArgumentException();
            }
            if (dets.Any(detection => detection == null || detection.IsDisposed || detection.Parts != 68))
            {
                throw new ArgumentException($"{nameof(dets)} includes invalid item.");
            }

            using (var vector = new StdVector <FullObjectDetection>(dets))
                using (var vectorOfChips = new StdVector <ChipDetails>())
                {
                    NativeMethods.get_face_chip_details(vector.NativePtr, size, padding, vectorOfChips.NativePtr);
                    return(vectorOfChips.ToArray());
                }
        }
示例#5
0
        public static void LoadImageDataset <T>(string path, out Array <Array2D <T> > images, out IList <IList <FullObjectDetection> > boxes)
            where T : struct
        {
            if (path == null)
            {
                throw new ArgumentNullException(nameof(path));
            }
            if (!File.Exists(path))
            {
                throw new FileNotFoundException("", path);
            }

            if (!Array2D <T> .TryParse <T>(out var type))
            {
                throw new NotSupportedException();
            }

            var str = Encoding.GetBytes(path);

            images = new Array <Array2D <T> >();
            using (var retBoxes = new StdVector <StdVector <FullObjectDetection> >())
                using (new EnumerableDisposer <StdVector <FullObjectDetection> >(retBoxes))
                {
                    var ret = NativeMethods.load_image_dataset_array_full_object_detection(type.ToNativeArray2DType(),
                                                                                           images.NativePtr,
                                                                                           retBoxes.NativePtr,
                                                                                           str);
                    if (ret == NativeMethods.ErrorType.Array2DTypeTypeNotSupport)
                    {
                        throw new ArgumentException($"{type} is not supported.");
                    }

                    boxes = retBoxes.ToArray().Select(box => box.ToArray()).ToArray();
                }
        }
示例#6
0
            public override IEnumerable <Rectangle> Operator <U>(Matrix <U> image)
            {
                if (image == null)
                {
                    throw new ArgumentNullException(nameof(image));
                }

                image.ThrowIfDisposed();

                var type = image.MatrixElementType.ToNativeMatrixElementType();
                var ret  = NativeMethods.object_detector_scan_fhog_pyramid_operator(this._PyramidType,
                                                                                    this._PyramidRate,
                                                                                    this._FeatureExtractorType,
                                                                                    this.NativePtr,
                                                                                    type,
                                                                                    image.NativePtr,
                                                                                    out var rects);

                switch (ret)
                {
                case NativeMethods.ErrorType.MatrixElementTypeNotSupport:
                    throw new ArgumentException($"{type} is not supported.");
                }

                using (var tmp = new StdVector <Rectangle>(rects))
                    return(tmp.ToArray());
            }
示例#7
0
        public static void RandomizeSamples <T1, T2>(IList <T1> samples1, IList <T2> samples2)
        {
            if (samples1 == null)
            {
                throw new ArgumentNullException(nameof(samples1));
            }
            if (samples2 == null)
            {
                throw new ArgumentNullException(nameof(samples2));
            }
            if (samples1.Count != samples2.Count)
            {
                throw new ArgumentException();
            }

            var indexList = Enumerable.Range(0, samples1.Count).ToArray();

            using (var vector = new StdVector <int>(indexList))
            {
                NativeMethods.randomize_samples_value(vector.NativePtr);
                var result = vector.ToArray();
                for (var index = 0; index < indexList.Length; index++)
                {
                    var newIndex = result[index];
                    var old1     = samples1[index];
                    samples1[index]    = samples1[newIndex];
                    samples1[newIndex] = old1;

                    var old2 = samples2[index];
                    samples2[index]    = samples2[newIndex];
                    samples2[newIndex] = old2;
                }
            }
        }
        public static IEnumerable <long> MaxCostAssignment <T>(Matrix <T> cost)
            where T : struct
        {
            if (cost == null)
            {
                throw new ArgumentNullException(nameof(cost));
            }
            if (cost.Rows != cost.Columns)
            {
                throw new ArgumentException($"{cost.Rows} must equal to {cost.Columns}");
            }

            using (var vector = new StdVector <long>())
            {
                var type = cost.MatrixElementType.ToNativeMatrixElementType();
                var ret  = Native.max_cost_assignment(
                    type,
                    cost.NativePtr,
                    vector.NativePtr);
                if (ret == Native.ErrorType.MatrixElementTypeNotSupport)
                {
                    throw new ArgumentException($"{cost.MatrixElementType} is not supported.");
                }

                return(vector.ToArray());
            }
        }
        public static IEnumerable <Rectangle> FindCandidateObjectLocations(
            Array2DBase image,
            MatrixRangeExp <double> kvals,
            uint minSize = 20,
            uint maxMergingIterations = 50)
        {
            if (image == null)
            {
                throw new ArgumentNullException(nameof(image));
            }
            if (kvals == null)
            {
                throw new ArgumentNullException(nameof(kvals));
            }

            var array2DType = image.ImageType.ToNativeArray2DType();

            using (var dets = new StdVector <Rectangle>())
            {
                var ret = Native.find_candidate_object_locations(array2DType, image.NativePtr, dets.NativePtr, IntPtr.Zero, minSize, maxMergingIterations);
                if (ret == Native.ErrorType.ArrayTypeNotSupport)
                {
                    throw new ArgumentException($"{image.ImageType} is not supported.");
                }
                return(dets.ToArray());
            }
        }
        public void CreateWithCollection()
        {
            const int size   = 10;
            var       source = Enumerable.Range(0, size).Select(j => new StdVector <Rectangle>(Enumerable.Range(0, size).Select(i => new Rectangle(i, i, i, i))));
            var       vector = new StdVector <StdVector <Rectangle> >(source);

            Assert.AreEqual(vector.Size, size);
            var ret = vector.ToArray();

            for (var j = 0; j < size; j++)
            {
                var tmp = ret[j].ToArray();
                for (var i = 0; i < size; i++)
                {
                    Assert.AreEqual(tmp[i].Left, i);
                    Assert.AreEqual(tmp[i].Top, i);
                    Assert.AreEqual(tmp[i].Right, i);
                    Assert.AreEqual(tmp[i].Bottom, i);
                }
            }

            this.DisposeAndCheckDisposedState(vector);
            foreach (var s in source)
            {
                s.Dispose();
            }
        }
示例#11
0
        public void Operator(MatrixBase image, out IEnumerable <RectDetection> detections, double threshold = 0d)
        {
            this.ThrowIfDisposed();

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

            image.ThrowIfDisposed();

            using (var dets = new StdVector <RectDetection>())
            {
                var inType = image.MatrixElementType.ToNativeMatrixElementType();
                var ret    = NativeMethods.frontal_face_detector_matrix_operator2(this.NativePtr,
                                                                                  inType,
                                                                                  image.NativePtr,
                                                                                  threshold,
                                                                                  dets.NativePtr);
                switch (ret)
                {
                case NativeMethods.ErrorType.MatrixElementTypeNotSupport:
                    throw new ArgumentException($"Input {inType} is not supported.");
                }

                detections = dets.ToArray();
            }
        }
        public void CreateWithCollection()
        {
            const int size   = 10;
            var       source = Enumerable.Range(0, size).Select(j => new StdVector <MModRect>(Enumerable.Range(0, size).Select(i => new MModRect {
                Ignore = true, DetectionConfidence = i
            })));
            var vector = new StdVector <StdVector <MModRect> >(source);

            Assert.AreEqual(vector.Size, size);
            var ret = vector.ToArray();

            for (var j = 0; j < size; j++)
            {
                var tmp = ret[j].ToArray();
                for (var i = 0; i < size; i++)
                {
                    Assert.AreEqual(tmp[i].DetectionConfidence, i);
                    Assert.AreEqual(tmp[i].Ignore, true);
                }
            }
            this.DisposeAndCheckDisposedState(vector);
            foreach (var s in source)
            {
                s.Dispose();
            }
        }
示例#13
0
        public IEnumerable <OverlayRect> GetOverlayRects()
        {
            this.ThrowIfDisposed();
            var rects = NativeMethods.image_display_get_overlay_rects(this.NativePtr);

            using (var vector = new StdVector <OverlayRect>(rects))
                return(vector.ToArray());
        }
示例#14
0
        public static void UpsampleImageDataset <T>(uint pyramidRate,
                                                    IList <Matrix <T> > images,
                                                    IList <IList <MModRect> > objects,
                                                    uint maxImageSize = uint.MaxValue)
            where T : struct
        {
            if (images == null)
            {
                throw new ArgumentNullException(nameof(images));
            }
            if (objects == null)
            {
                throw new ArgumentNullException(nameof(objects));
            }

            images.ThrowIfDisposed();

            var imageCount  = images.Count();
            var objectCount = objects.Count();

            if (imageCount != objectCount)
            {
                throw new ArgumentException();
            }

            using (var vecImage = new StdVector <Matrix <T> >(images))
                using (var disposer = new EnumerableDisposer <StdVector <MModRect> >(objects.Select(r => new StdVector <MModRect>(r))))
                    using (var vecObject = new StdVector <StdVector <MModRect> >(disposer.Collection))
                        using (new EnumerableDisposer <StdVector <MModRect> >(vecObject))
                        {
                            Matrix <T> .TryParse <T>(out var matrixElementType);

                            var ret = NativeMethods.upsample_image_dataset_pyramid_down_mmod_rect(pyramidRate,
                                                                                                  matrixElementType.ToNativeMatrixElementType(),
                                                                                                  vecImage.NativePtr,
                                                                                                  vecObject.NativePtr,
                                                                                                  maxImageSize);
                            switch (ret)
                            {
                            case NativeMethods.ErrorType.PyramidNotSupportRate:
                                throw new ArgumentException($"{pyramidRate} is not supported.");

                            case NativeMethods.ErrorType.MatrixElementTypeNotSupport:
                                throw new ArgumentException($"{matrixElementType} is not supported.");
                            }

                            images.Clear();
                            foreach (var matrix in vecImage.ToArray())
                            {
                                images.Add(matrix);
                            }
                            objects.Clear();
                            foreach (var list in vecObject.ToArray())
                            {
                                objects.Add(list.ToArray());
                            }
                        }
        }
示例#15
0
        public static IEnumerable <Rectangle> EvaluateDetectors <T, U>(IEnumerable <ObjectDetector <ScanFHogPyramid <T, U> > > detectors,
                                                                       MatrixBase matrix,
                                                                       double adjustThreshold = 0)
            where T : class
            where U : class
        {
            if (detectors == null)
            {
                throw new ArgumentNullException(nameof(detectors));
            }
            if (matrix == null)
            {
                throw new ArgumentNullException(nameof(matrix));
            }
            var count = detectors.Count();

            if (count == 0)
            {
                throw new ArgumentException();
            }

            detectors.ThrowIfDisposed();

            var @params = detectors.Select(detector => detector.GetFHogPyramidParameter());

            var param = @params.First();
            var not   = @params.Any(p => p.PyramidRate != param.PyramidRate ||
                                    p.PyramidType != param.PyramidType ||
                                    p.FeatureExtractorType != param.FeatureExtractorType);

            if (not)
            {
                throw new ArgumentException();
            }

            var detectorArray = detectors.Select(det => det.NativePtr).ToArray();
            var elementType   = matrix.MatrixElementType.ToNativeMatrixElementType();
            var ret           = NativeMethods.scan_fhog_pyramid_evaluate_detectors(param.PyramidType,
                                                                                   param.PyramidRate,
                                                                                   param.FeatureExtractorType,
                                                                                   detectorArray,
                                                                                   count,
                                                                                   elementType,
                                                                                   matrix.NativePtr,
                                                                                   adjustThreshold,
                                                                                   out var vector);

            switch (ret)
            {
            case NativeMethods.ErrorType.FHogNotSupportExtractor:
            case NativeMethods.ErrorType.PyramidNotSupportRate:
            case NativeMethods.ErrorType.PyramidNotSupportType:
                throw new NotSupportedException();
            }

            using (var tmp = new StdVector <Rectangle>(vector))
                return(tmp.ToArray());
        }
示例#16
0
        public static void UpsampleImageDataset <T>(uint pyramidRate,
                                                    IList <Matrix <T> > images,
                                                    IList <IEnumerable <Rectangle> > objects)
            where T : struct
        {
            if (images == null)
            {
                throw new ArgumentNullException(nameof(images));
            }
            if (objects == null)
            {
                throw new ArgumentNullException(nameof(objects));
            }

            images.ThrowIfDisposed();

            var imageCount  = images.Count();
            var objectCount = objects.Count();

            if (imageCount != objectCount)
            {
                throw new ArgumentException();
            }

            using (var vecImage = new StdVector <Matrix <T> >(images))
            {
                var tmp = objects.Select(rectangles => new StdVector <Rectangle>(rectangles));
                using (var vecObject = new StdVector <StdVector <Rectangle> >(tmp))
                {
                    Matrix <T> .TryParse <T>(out var matrixElementType);

                    var ret = Native.upsample_image_dataset_pyramid_down(pyramidRate,
                                                                         matrixElementType.ToNativeMatrixElementType(),
                                                                         vecImage.NativePtr,
                                                                         vecObject.NativePtr);
                    switch (ret)
                    {
                    case Native.ErrorType.PyramidNotSupportRate:
                        throw new ArgumentException($"{pyramidRate} is not supported.");

                    case Native.ErrorType.MatrixElementTypeNotSupport:
                        throw new ArgumentException($"{matrixElementType} is not supported.");
                    }

                    images.Clear();
                    foreach (var matrix in vecImage.ToArray())
                    {
                        images.Add(matrix);
                    }
                    objects.Clear();
                    foreach (var list in vecObject.ToArray())
                    {
                        objects.Add(list);
                    }
                }
            }
        }
示例#17
0
        public static IEnumerable <Rectangle> CreateGridDetectionTemplate(Rectangle objectBox, uint cellsX, uint cellsY)
        {
            if (!(cellsX > 0 && cellsY > 0))
            {
                throw new ArgumentOutOfRangeException();
            }

            using (var native = objectBox.ToNative())
            {
                var ret = NativeMethods.create_grid_detection_template(native.NativePtr, cellsX, cellsY);
                using (var vector = new StdVector <Rectangle>(ret))
                    return(vector.ToArray());
            }
        }
示例#18
0
        public static ImageWindow.OverlayLine[] RenderFaceDetections(IEnumerable <FullObjectDetection> detection, RgbPixel color)
        {
            if (detection == null)
            {
                throw new ArgumentNullException(nameof(detection));
            }

            using (var vectorIn = new StdVector <FullObjectDetection>(detection))
                using (var vectorOut = new StdVector <ImageWindow.OverlayLine>())
                {
                    Native.render_face_detections(vectorIn.NativePtr, ref color, vectorOut.NativePtr);
                    return(vectorOut.ToArray());
                }
        }
        public void CreateWithCollection()
        {
            const int size   = 10;
            var       source = Enumerable.Range(0, size).Select(s => (long)s).ToArray();
            var       vector = new StdVector <long>(source);

            Assert.AreEqual(vector.Size, size);
            var ret = vector.ToArray();

            for (var i = 0; i < size; i++)
            {
                Assert.AreEqual(ret[i], i);
            }
            this.DisposeAndCheckDisposedState(vector);
        }
示例#20
0
        public static ImageWindow.OverlayLine[] RenderFaceDetections(FullObjectDetection detection, RgbPixel color)
        {
            if (detection == null)
            {
                throw new ArgumentNullException(nameof(detection));
            }

            detection.ThrowIfDisposed(nameof(detection));

            using (var vector = new StdVector <ImageWindow.OverlayLine>())
            {
                Native.render_face_detections(detection.NativePtr, ref color, vector.NativePtr);
                return(vector.ToArray());
            }
        }
        public void CreateWithCollection()
        {
            const int size   = 10;
            var       source = Enumerable.Range(0, size).Select(i => new Matrix <int>(i, i));
            var       vector = new StdVector <Matrix <int> >(source);

            Assert.AreEqual(vector.Size, size);
            var ret = vector.ToArray();

            for (var i = 0; i < size; i++)
            {
                Assert.AreEqual(ret[i].Rows, i);
                Assert.AreEqual(ret[i].Columns, i);
            }
            this.DisposeAndCheckDisposedState(vector);
        }
示例#22
0
        public static void ChineseWhispers(IEnumerable <SamplePair> edges, uint iterations, out uint clusters, out uint[] labels)
        {
            if (edges == null)
            {
                throw new ArgumentNullException(nameof(edges));
            }

            edges.ThrowIfDisposed();

            using (var e = new StdVector <SamplePair>(edges))
                using (var l = new StdVector <uint>())
                {
                    clusters = Native.clustering_chinese_whispers(e.NativePtr, l.NativePtr, iterations);
                    labels   = l.ToArray();
                }
        }
示例#23
0
        public static void RandomizeSamples <T>(IList <T> samples)
            where T : DlibObject
        {
            if (samples == null)
            {
                throw new ArgumentNullException(nameof(samples));
            }

            using (var vector = new StdVector <T>(samples))
            {
                NativeMethods.randomize_samples_pointer(vector.NativePtr);
                var result = vector.ToArray();
                for (var index = 0; index < samples.Count; index++)
                {
                    samples[index] = result[index];
                }
            }
        }
示例#24
0
        public static void AddImageLeftRightFlips <T>(IList <Matrix <T> > images, IList <IEnumerable <Rectangle> > objects)
            where T : struct
        {
            if (images == null)
            {
                throw new ArgumentNullException(nameof(images));
            }
            if (objects == null)
            {
                throw new ArgumentNullException(nameof(objects));
            }

            images.ThrowIfDisposed();

            using (var vecImage = new StdVector <Matrix <T> >(images))
            {
                var tmp = objects.Select(rectangles => new StdVector <Rectangle>(rectangles));
                using (var vecObject = new StdVector <StdVector <Rectangle> >(tmp))
                {
                    Matrix <T> .TryParse <T>(out var matrixElementType);

                    var ret = NativeMethods.add_image_left_right_flips_rectangle(matrixElementType.ToNativeMatrixElementType(),
                                                                                 vecImage.NativePtr,
                                                                                 vecObject.NativePtr);
                    switch (ret)
                    {
                    case NativeMethods.ErrorType.MatrixElementTypeNotSupport:
                        throw new ArgumentException($"{matrixElementType} is not supported.");
                    }

                    images.Clear();
                    foreach (var matrix in vecImage.ToArray())
                    {
                        images.Add(matrix);
                    }
                    objects.Clear();
                    foreach (var list in vecObject.ToArray())
                    {
                        objects.Add(list);
                    }
                }
            }
        }
示例#25
0
        public static void CreateTiledPyramid <T, U>(Matrix <T> image, uint padding, uint outerPadding, uint pyramidRate, out Matrix <T> outImage, out IEnumerable <Rectangle> rects)
            where T : struct
            where U : Pyramid
        {
            // 10, 0
            if (image == null)
            {
                throw new ArgumentNullException(nameof(image));
            }

            outImage = default(Matrix <T>);
            rects    = default(IEnumerable <Rectangle>);

            image.ThrowIfDisposed();

            if (!Pyramid.TryGetSupportPyramidType <U>(out var pyramidType))
            {
                throw new NotSupportedException();
            }

            var inType = image.MatrixElementType.ToNativeMatrixElementType();
            var ret    = Native.create_tiled_pyramid(inType,
                                                     image.NativePtr,
                                                     pyramidType,
                                                     pyramidRate,
                                                     padding,
                                                     outerPadding,
                                                     out var outImg,
                                                     out var vecRects);

            switch (ret)
            {
            case Native.ErrorType.MatrixElementTypeNotSupport:
                throw new ArgumentException($"Input {inType} is not supported.");

            case Native.ErrorType.PyramidNotSupportRate:
                throw new NotSupportedException();
            }

            outImage = new Matrix <T>(outImg);
            using (var vec = new StdVector <Rectangle>(vecRects))
                rects = vec.ToArray();
        }
示例#26
0
        public static void LoadMNISTDataset(string folderPath,
                                            out IList <Matrix <byte> > trainingImages,
                                            out IList <uint> trainingLabels,
                                            out IList <Matrix <byte> > testingImages,
                                            out IList <uint> testingLabels)
        {
            if (folderPath == null)
            {
                throw new ArgumentNullException(nameof(folderPath));
            }
            if (!Directory.Exists(folderPath))
            {
                throw new DirectoryNotFoundException();
            }

            trainingImages = null;
            trainingLabels = null;
            testingImages  = null;
            testingLabels  = null;

            var str       = Encoding.GetBytes(folderPath);
            var strLength = str.Length;

            Array.Resize(ref str, strLength + 1);
            str[strLength] = (byte)'\0';

            NativeMethods.load_mnist_dataset(str,
                                             out var retTrainingImages,
                                             out var retTrainingLabels,
                                             out var retTestingImages,
                                             out var retTestingLabels);

            using (var tmp = new StdVector <Matrix <byte> >(retTrainingImages))
                trainingImages = tmp.ToArray();
            using (var tmp = new StdVector <uint>(retTrainingLabels))
                trainingLabels = tmp.ToArray();
            using (var tmp = new StdVector <Matrix <byte> >(retTestingImages))
                testingImages = tmp.ToArray();
            using (var tmp = new StdVector <uint>(retTestingLabels))
                testingLabels = tmp.ToArray();
        }
        public static IEnumerable <Rectangle> FindCandidateObjectLocations <T>(Array2DBase image,
                                                                               Matrix <T> kvals,
                                                                               uint minSize = 20,
                                                                               uint maxMergingIterations = 50)
            where T : struct
        {
            if (image == null)
            {
                throw new ArgumentNullException(nameof(image));
            }
            if (kvals == null)
            {
                throw new ArgumentNullException(nameof(kvals));
            }

            var array2DType = image.ImageType.ToNativeArray2DType();

            using (var dets = new StdVector <Rectangle>())
            {
                var matrixType = kvals.MatrixElementType.ToNativeMatrixElementType();
                var ret        = Native.find_candidate_object_locations(array2DType,
                                                                        image.NativePtr,
                                                                        dets.NativePtr,
                                                                        matrixType,
                                                                        kvals.NativePtr,
                                                                        minSize,
                                                                        maxMergingIterations);
                switch (ret)
                {
                case Native.ErrorType.Array2DTypeTypeNotSupport:
                    throw new ArgumentException($"{image.ImageType} is not supported.");

                case Native.ErrorType.MatrixElementTypeNotSupport:
                    throw new ArgumentException($"{matrixType} is not supported.");
                }

                return(dets.ToArray());
            }
        }
示例#28
0
            public override IEnumerable <Tuple <double, Rectangle> > OperatorWithConfidence <U>(Matrix <U> image)
            {
                if (image == null)
                {
                    throw new ArgumentNullException(nameof(image));
                }

                image.ThrowIfDisposed();

                var type = image.MatrixElementType.ToNativeMatrixElementType();
                var ret  = NativeMethods.object_detector_scan_fhog_pyramid_operator_with_confidence(this._PyramidType,
                                                                                                    this._PyramidRate,
                                                                                                    this._FeatureExtractorType,
                                                                                                    this.NativePtr,
                                                                                                    type,
                                                                                                    image.NativePtr,
                                                                                                    out var rects,
                                                                                                    out var confidences);

                switch (ret)
                {
                case NativeMethods.ErrorType.MatrixElementTypeNotSupport:
                    throw new ArgumentException($"{type} is not supported.");
                }

                using (var rectsVector = new StdVector <Rectangle>(rects))
                    using (var tconfidencesVector = new StdVector <double>(confidences))
                    {
                        var rectsArray       = rectsVector.ToArray();
                        var confidencesArray = tconfidencesVector.ToArray();
                        var count            = rectsArray.Length;
                        for (var index = 0; index < count; index++)
                        {
                            yield return(new Tuple <double, Rectangle>(confidencesArray[index], rectsArray[index]));
                        }
                    };
            }
示例#29
0
        public static void LoadMNISTDataset(string folderPath,
                                            out Matrix <byte>[] trainingImages,
                                            out uint[] trainingLabels,
                                            out Matrix <byte>[] testingImages,
                                            out uint[] testingLabels)
        {
            if (folderPath == null)
            {
                throw new ArgumentNullException(nameof(folderPath));
            }
            if (!Directory.Exists(folderPath))
            {
                throw new DirectoryNotFoundException();
            }

            trainingImages = null;
            trainingLabels = null;
            testingImages  = null;
            testingLabels  = null;

            var str = Dlib.Encoding.GetBytes(folderPath);

            NativeMethods.load_mnist_dataset(str,
                                             out var retTrainingImages,
                                             out var retTrainingLabels,
                                             out var retTestingImages,
                                             out var retTestingLabels);

            using (var tmp = new StdVector <Matrix <byte> >(retTrainingImages))
                trainingImages = tmp.ToArray();
            using (var tmp = new StdVector <uint>(retTrainingLabels))
                trainingLabels = tmp.ToArray();
            using (var tmp = new StdVector <Matrix <byte> >(retTestingImages))
                testingImages = tmp.ToArray();
            using (var tmp = new StdVector <uint>(retTestingLabels))
                testingLabels = tmp.ToArray();
        }
示例#30
0
        public Rectangle[] Operator(MatrixBase image, double threshold = 0d)
        {
            this.ThrowIfDisposed();

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

            image.ThrowIfDisposed();

            using (var dets = new StdVector <Rectangle>())
            {
                var inType = image.MatrixElementType.ToNativeMatrixElementType();
                var ret    = Native.frontal_face_detector_matrix_operator(this.NativePtr, inType, image.NativePtr, threshold, dets.NativePtr);
                switch (ret)
                {
                case Dlib.Native.ErrorType.InputElementTypeNotSupport:
                    throw new ArgumentException($"Input {inType} is not supported.");
                }

                return(dets.ToArray());
            }
        }