예제 #1
0
            public override Matrix <double> TestObjectDetectionFunction <U>(IEnumerable <Matrix <U> > images, IEnumerable <IEnumerable <Rectangle> > objects)
            {
                using (var vecImage = new StdVector <Matrix <U> >(images))
                    using (var disposer = new EnumerableDisposer <StdVector <Rectangle> >(objects.Select(r => new StdVector <Rectangle>(r))))
                        using (var vecObject = new StdVector <StdVector <Rectangle> >(disposer.Collection))
                            using (new EnumerableDisposer <StdVector <Rectangle> >(vecObject))
                            {
                                Matrix <U> .TryParse <U>(out var matrixElementType);

                                var ret = NativeMethods.cross_validate_object_detection_trainer_scan_fhog_pyramid_test_object_detection_function_rectangle(this._PyramidType,
                                                                                                                                                           this._PyramidRate,
                                                                                                                                                           this._FeatureExtractorType,
                                                                                                                                                           this.NativePtr,
                                                                                                                                                           matrixElementType.ToNativeMatrixElementType(),
                                                                                                                                                           vecImage.NativePtr,
                                                                                                                                                           vecObject.NativePtr,
                                                                                                                                                           out var matrix);
                                switch (ret)
                                {
                                case NativeMethods.ErrorType.MatrixElementTypeNotSupport:
                                    throw new ArgumentException($"{matrixElementType} is not supported.");
                                }

                                return(new Matrix <double>(matrix, 1, 3));
                            }
            }
            public override ObjectDetector <T> Train <U>(IEnumerable <Matrix <U> > images, IEnumerable <IEnumerable <Rectangle> > objects)
            {
                using (var vecImage = new StdVector <Matrix <U> >(images))
                    using (var disposer = new EnumerableDisposer <StdVector <Rectangle> >(objects.Select(r => new StdVector <Rectangle>(r))))
                        using (var vecObject = new StdVector <StdVector <Rectangle> >(disposer.Collection))
                            using (new EnumerableDisposer <StdVector <Rectangle> >(vecObject))
                            {
                                Matrix <U> .TryParse <U>(out var matrixElementType);

                                var ret = NativeMethods.structural_object_detection_trainer_scan_fhog_pyramid_train_rectangle(this._PyramidType,
                                                                                                                              this._PyramidRate,
                                                                                                                              this._FeatureExtractorType,
                                                                                                                              this.NativePtr,
                                                                                                                              matrixElementType.ToNativeMatrixElementType(),
                                                                                                                              vecImage.NativePtr,
                                                                                                                              vecObject.NativePtr,
                                                                                                                              out var detector);
                                switch (ret)
                                {
                                case NativeMethods.ErrorType.MatrixElementTypeNotSupport:
                                    throw new ArgumentException($"{matrixElementType} is not supported.");
                                }

                                return(new ObjectDetector <T>(detector, new ImageScanner.FHogPyramidParameter(this._PyramidType, this._PyramidRate, this._FeatureExtractorType)));
                            }
            }
예제 #3
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());
                            }
                        }
        }
예제 #4
0
        public void Load(IEnumerable <string> list)
        {
            if (list == null)
            {
                throw new ArgumentNullException(nameof(list));
            }

            using (var disposer = new EnumerableDisposer <StdString>(list.Select(s => new StdString(s))))
                using (var vector = new StdVector <StdString>(disposer.Collection))
                    NativeMethods.list_box_load_stdstring(this.NativePtr, vector.NativePtr);
        }
예제 #5
0
        public static void AddImageLeftRightFlips <T>(IList <Matrix <T> > images, IList <IList <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))
                using (var disposer = new EnumerableDisposer <StdVector <Rectangle> >(objects.Select(r => new StdVector <Rectangle>(r))))
                    using (var vecObject = new StdVector <StdVector <Rectangle> >(disposer.Collection))
                        using (new EnumerableDisposer <StdVector <Rectangle> >(vecObject))
                        {
                            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.ToArray());
                            }
                        }
        }
예제 #6
0
        public ShapePredictor Train <T>(Array <Array2D <T> > images, IEnumerable <IList <FullObjectDetection> > objects)
            where T : struct
        {
            if (images == null)
            {
                throw new ArgumentNullException(nameof(images));
            }
            if (objects == null)
            {
                throw new ArgumentNullException(nameof(objects));
            }

            this.ThrowIfDisposed();

            images.ThrowIfDisposed();

            var tmpObjects = objects.ToArray();

            tmpObjects.ThrowIfDisposed();

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

            using (var disposer = new EnumerableDisposer <StdVector <FullObjectDetection> >(tmpObjects.Select(r => new StdVector <FullObjectDetection>(r))))
                using (var objectsVector = new StdVector <StdVector <FullObjectDetection> >(disposer.Collection))
                {
                    var ret = NativeMethods.shape_predictor_trainer_train(this.NativePtr,
                                                                          type.ToNativeArray2DType(),
                                                                          images.NativePtr,
                                                                          objectsVector.NativePtr,
                                                                          out var predictor);
                    switch (ret)
                    {
                    case NativeMethods.ErrorType.Array2DTypeTypeNotSupport:
                        throw new ArgumentException($"Input {type} is not supported.");
                    }

                    return(new ShapePredictor(predictor));
                }
        }
예제 #7
0
파일: Dlib.cs 프로젝트: zhuxb711/DlibDotNet
        public static double TestShapePredictor <T>(ShapePredictor predictor,
                                                    Array <Array2D <T> > images,
                                                    IEnumerable <IList <FullObjectDetection> > objects,
                                                    IEnumerable <IList <double> > scales)
            where T : struct
        {
            if (images == null)
            {
                throw new ArgumentNullException(nameof(images));
            }
            if (objects == null)
            {
                throw new ArgumentNullException(nameof(objects));
            }
            if (scales == null)
            {
                throw new ArgumentNullException(nameof(scales));
            }

            predictor.ThrowIfDisposed();
            images.ThrowIfDisposed();

            var tmpObjects = objects.ToArray();

            tmpObjects.ThrowIfDisposed();
            var tmpScales = scales.ToArray();

            if (images.Size != tmpObjects.Length)
            {
                throw new ArgumentException();
            }

            if (tmpScales.Any())
            {
                if (tmpObjects.Where((t, index) => t.Count != tmpScales[index].Count).Any())
                {
                    throw new ArgumentException();
                }
            }

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

            using (var disposer1 = new EnumerableDisposer <StdVector <FullObjectDetection> >(tmpObjects.Select(r => new StdVector <FullObjectDetection>(r))))
                using (var objectsVector = new StdVector <StdVector <FullObjectDetection> >(disposer1.Collection))
                    using (var disposer2 = new EnumerableDisposer <StdVector <double> >(tmpScales.Select(r => new StdVector <double>(r)).ToArray()))
                        using (var scaleVector = new StdVector <StdVector <double> >(disposer2.Collection))
                        {
                            var ret = NativeMethods.shape_predictor_test_shape_predictor(predictor.NativePtr,
                                                                                         type.ToNativeArray2DType(),
                                                                                         images.NativePtr,
                                                                                         objectsVector.NativePtr,
                                                                                         scaleVector.NativePtr,
                                                                                         out var value);
                            switch (ret)
                            {
                            case NativeMethods.ErrorType.Array2DTypeTypeNotSupport:
                                throw new ArgumentException($"Input {type} is not supported.");
                            }

                            return(value);
                        }
        }
예제 #8
0
        public static Matrix <double> TestObjectDetectionFunction <T>(LossMmod detector,
                                                                      IEnumerable <Matrix <T> > images,
                                                                      IEnumerable <IEnumerable <MModRect> > truthDets,
                                                                      TestBoxOverlap overlapTester       = null,
                                                                      double adjustThreshold             = 0,
                                                                      TestBoxOverlap overlapIgnoreTester = null)
            where T : struct
        {
            if (detector == null)
            {
                throw new ArgumentNullException(nameof(detector));
            }
            if (images == null)
            {
                throw new ArgumentNullException(nameof(images));
            }
            if (truthDets == null)
            {
                throw new ArgumentNullException(nameof(truthDets));
            }

            detector.ThrowIfDisposed();
            images.ThrowIfDisposed();
            truthDets.ThrowIfDisposed();

            var disposeOverlapTester       = overlapTester == null;
            var disposeOverlapIgnoreTester = overlapIgnoreTester == null;

            try
            {
                if (disposeOverlapTester)
                {
                    overlapTester = new TestBoxOverlap();
                }
                if (disposeOverlapIgnoreTester)
                {
                    overlapIgnoreTester = new TestBoxOverlap();
                }

                using (var matrixVector = new StdVector <Matrix <T> >(images))
                    using (var disposer = new EnumerableDisposer <StdVector <MModRect> >(truthDets.Select(r => new StdVector <MModRect>(r))))
                        using (var detsVector = new StdVector <StdVector <MModRect> >(disposer.Collection))
                            using (new EnumerableDisposer <StdVector <MModRect> >(detsVector))
                            {
                                var type = detector.NetworkType;
                                Matrix <T> .TryParse <T>(out var elementTypes);

                                var matrix = images.FirstOrDefault();
                                var ret    = NativeMethods.test_object_detection_function_net(type,
                                                                                              detector.NativePtr,
                                                                                              elementTypes.ToNativeMatrixElementType(),
                                                                                              matrixVector.NativePtr,
                                                                                              matrix.TemplateRows,
                                                                                              matrix.TemplateColumns,
                                                                                              detsVector.NativePtr,
                                                                                              overlapTester.NativePtr,
                                                                                              adjustThreshold,
                                                                                              overlapIgnoreTester.NativePtr,
                                                                                              out var result);
                                switch (ret)
                                {
                                case NativeMethods.ErrorType.MatrixElementTypeNotSupport:
                                    throw new ArgumentException($"{elementTypes} is not supported.");

                                case NativeMethods.ErrorType.DnnNotSupportNetworkType:
                                    throw new NotSupportNetworkTypeException(type);
                                }

                                return(new Matrix <double>(result, 1, 3));
                            }
            }
            finally
            {
                if (disposeOverlapTester)
                {
                    overlapTester?.Dispose();
                }
                if (disposeOverlapIgnoreTester)
                {
                    overlapIgnoreTester?.Dispose();
                }
            }
        }