예제 #1
0
        private static int DetectPeleeNet(NcnnDotNet.OpenCV.Mat bgr, List <Object> objects, NcnnDotNet.Mat resized)
        {
            using (var peleenet = new Net())
            {
                if (Ncnn.IsSupportVulkan)
                {
                    peleenet.Opt.UseVulkanCompute = true;
                }

                // model is converted from https://github.com/eric612/MobileNet-YOLO
                // and can be downloaded from https://drive.google.com/open?id=1Wt6jKv13sBRMHgrGAJYlOlRF-o80pC0g
                // the ncnn model https://github.com/nihui/ncnn-assets/tree/master/models
                peleenet.LoadParam("pelee.param");
                peleenet.LoadModel("pelee.bin");

                const int targetSize = 300;

                var imgW = bgr.Cols;
                var imgH = bgr.Rows;

                using var @in = Mat.FromPixelsResize(bgr.Data, PixelType.Bgr, bgr.Cols, bgr.Rows, targetSize, targetSize);
                var meanVals = new[] { 103.9f, 116.7f, 123.6f };
                var normVals = new[] { 0.017f, 0.017f, 0.017f };
                @in.SubstractMeanNormalize(meanVals, normVals);

                using var ex = peleenet.CreateExtractor();
                //ex.SetNumThreads(4);

                ex.Input("data", @in);

                using var @out = new Mat();
                ex.Extract("detection_out", @out);

                //     printf("%d %d %d\n", out.w, out.h, out.c);
                objects.Clear();
                for (var i = 0; i < @out.H; i++)
                {
                    var values = @out.Row(i);

                    var @object = new Object();
                    @object.Label       = (int)values[0];
                    @object.Prob        = values[1];
                    @object.Rect.X      = values[2] * imgW;
                    @object.Rect.Y      = values[3] * imgH;
                    @object.Rect.Width  = values[4] * imgW - @object.Rect.X;
                    @object.Rect.Height = values[5] * imgH - @object.Rect.Y;

                    objects.Add(@object);
                }

                using var segOut = new Mat();
                ex.Extract("sigmoid", segOut);
                Ncnn.ResizeBilinear(segOut, resized, imgW, imgH);
            }

            return(0);
        }
예제 #2
0
        private int Detect(Mat image,
                           int resizedWidth,
                           int resizedHeight,
                           float scoreThreshold,
                           float nmsThreshold,
                           List <FaceInfo> faces)
        {
            if (image.IsEmpty)
            {
                Console.WriteLine("image is empty ,please check!");
                return(-1);
            }

            this._ImageH = image.H;
            this._ImageW = image.W;

            this._ScaleW = (float)this._ImageW / resizedWidth;
            this._ScaleH = (float)this._ImageH / resizedHeight;

            using (Mat @in = new Mat())
            {
                //scale
                this.DynamicScale(resizedWidth, resizedHeight);
                Ncnn.ResizeBilinear(image, @in, this._DW, this._DH);

                using (Extractor ex = this._Net.CreateExtractor())
                {
                    ex.Input("input.1", @in);

                    using (Mat heatMap = new Mat())
                    {
                        using (Mat scale = new Mat())
                        {
                            using (Mat offset = new Mat())
                            {
                                using (Mat landmarks = new Mat())
                                {
                                    ex.Extract("537", heatMap);
                                    ex.Extract("538", scale);
                                    ex.Extract("539", offset);
                                    ex.Extract("540", landmarks);

                                    this.Decode(heatMap, scale, offset, landmarks, faces, scoreThreshold, nmsThreshold);
                                    this.SquareBox(faces);
                                }
                            }
                        }
                    }
                }
            }

            return(0);
        }
예제 #3
0
        private int Detect(Mat img, ICollection <FaceInfo> faceList)
        {
            if (img.IsEmpty)
            {
                Console.WriteLine("image is empty ,please check!");
                return(-1);
            }

            this._ImageH = img.H;
            this._ImageW = img.W;

            using (var @in = new Mat())
            {
                Ncnn.ResizeBilinear(img, @in, this._InW, this._InH);

                var ncnnImg = @in;
                ncnnImg.SubstractMeanNormalize(this._MeanVals, this._NormVals);

                var boundingBoxCollection = new List <FaceInfo>();
                //var validInput = new List<FaceInfo>();

                using (var ex = this._UltraFace.CreateExtractor())
                {
                    ex.SetNumThreads(this._NumThread);
                    ex.Input("input", ncnnImg);

                    using (var scores = new Mat())
                        using (var boxes = new Mat())
                        {
                            ex.Extract("scores", scores);
                            ex.Extract("boxes", boxes);

                            GenerateBBox(boundingBoxCollection, scores, boxes, this._ScoreThreshold, this._NumAnchors);
                            NonMaximumSuppression(boundingBoxCollection, faceList);
                        }
                }
            }

            return(0);
        }