Esempio n. 1
0
        public DetectService()
        {
            var resourcePrefix = "Demo.data.";
            // note that the prefix includes the trailing period '.' that is required
            var files    = new [] { "RFB-320.bin", "RFB-320.param" };
            var assembly = System.Reflection.IntrospectionExtensions.GetTypeInfo(typeof(DetectService)).Assembly;

            foreach (var file in files)
            {
                var path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), file);
                using (var fs = File.Create(path))
                    using (var stream = assembly.GetManifestResourceStream(resourcePrefix + file))
                    {
                        stream.Seek(0, SeekOrigin.Begin);
                        stream.CopyTo(fs);
                    }
            }

            var binPath   = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), files[0]);
            var paramPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), files[1]);

            var param = new UltraFaceParameter
            {
                BinFilePath    = binPath,
                ParamFilePath  = paramPath,
                InputWidth     = 320,
                InputLength    = 240,
                NumThread      = 1,
                ScoreThreshold = 0.7f
            };

            this._UltraFace = UltraFace.Create(param);
        }
 public Mat Detect(Mat mat)
 {
     using (var ultraFace = UltraFace.Create(_param))
     {
         using var inMat = NcnnDotNet.Mat.FromPixels(mat.Data, NcnnDotNet.PixelType.Bgr2Rgb, mat.Cols, mat.Rows);
         var faceInfos = ultraFace.Detect(inMat).ToArray();
         for (var j = 0; j < faceInfos.Length; j++)
         {
             var face = faceInfos[j];
             mat.Rectangle(new OpenCvSharp.Rect((int)face.X1, (int)face.Y1, (int)face.X2 - (int)face.X1, (int)face.Y2 - (int)face.Y1), Scalar.Red);
         }
         return(mat);
     }
 }
Esempio n. 3
0
        private static int Main(string[] args)
        {
            if (args.Length <= 2)
            {
                Console.WriteLine($"Usage: {nameof(Demo)} < ncnn bin > < ncnn param > [image files...]");
                return(1);
            }

            var binPath   = args[0];
            var paramPath = args[1];

            var param = new UltraFaceParameter
            {
                BinFilePath    = binPath,
                ParamFilePath  = paramPath,
                InputWidth     = 320,
                InputLength    = 240,
                NumThread      = 1,
                ScoreThreshold = 0.7f
            };

            using (var ultraFace = UltraFace.Create(param)) // config model input
                for (var i = 2; i < args.Length; i++)
                {
                    var imageFile = args[i];
                    Console.WriteLine($"Processing {imageFile}");

                    using var frame = Cv2.ImRead(imageFile);
                    using var inMat = NcnnDotNet.Mat.FromPixels(frame.Data, NcnnDotNet.PixelType.Bgr2Rgb, frame.Cols, frame.Rows);

                    var faceInfos = ultraFace.Detect(inMat).ToArray();
                    for (var j = 0; j < faceInfos.Length; j++)
                    {
                        var face = faceInfos[j];
                        var pt1  = new Point <float>(face.X1, face.Y1);
                        var pt2  = new Point <float>(face.X2, face.Y2);
                        Cv2.Rectangle(frame, pt1, pt2, new Scalar <double>(0, 255, 0), 2);
                    }

                    Cv2.ImShow("UltraFace", frame);
                    Cv2.WaitKey();
                    Cv2.ImWrite("result.jpg", frame);
                }

            return(0);
        }
Esempio n. 4
0
        private static int Main(string[] args)
        {
            if (args.Length <= 3)
            {
                Console.WriteLine($"Usage: {nameof(Benchmark)} < ncnn bin > < ncnn param >  < image files > < loop count >");
                return(1);
            }

            var binPath   = args[0];
            var paramPath = args[1];
            var imagePath = args[2];
            var maxLoop   = int.TryParse(args[3], out var ret) ? ret : 1000;

            var param = new UltraFaceParameter
            {
                BinFilePath    = binPath,
                ParamFilePath  = paramPath,
                InputWidth     = 320,
                InputLength    = 240,
                NumThread      = 1,
                ScoreThreshold = 0.7f
            };

            if (NcnnDotNet.Ncnn.IsSupportVulkan)
            {
                Console.WriteLine("Initializes GPU");
                NcnnDotNet.Ncnn.CreateGpuInstance();
                var index = NcnnDotNet.Ncnn.GetDefaultGpuIndex();
                Console.WriteLine($"\tGPU is {index}");
            }

            using (var ultraFace = UltraFace.Create(param))// config model input
            {
                Console.WriteLine($"Processing {imagePath}");

                using var frame = Cv2.ImRead(imagePath);

                var imageStopWatch  = new Stopwatch();
                var detectStopWatch = new Stopwatch();

                var totalImageLoad = 0d;
                var totalDetect    = 0d;

                var options = new ProgressBarOptions
                {
                    ProgressCharacter   = '─',
                    ForegroundColor     = ConsoleColor.Yellow,
                    ForegroundColorDone = ConsoleColor.DarkGreen,
                    BackgroundColor     = ConsoleColor.DarkGray,
                    ProgressBarOnBottom = true,
                };

                using (var pbar = new ProgressBar(maxLoop, "Initial message", options))
                    for (var loop = 1; loop <= maxLoop; loop++)
                    {
                        imageStopWatch.Start();
                        using var inMat = NcnnDotNet.Mat.FromPixels(frame.Data, NcnnDotNet.PixelType.Bgr2Rgb, frame.Cols, frame.Rows);
                        imageStopWatch.Stop();
                        totalImageLoad += imageStopWatch.ElapsedMilliseconds;
                        imageStopWatch.Reset();

                        detectStopWatch.Start();
                        var _ = ultraFace.Detect(inMat).ToArray();
                        detectStopWatch.Stop();
                        totalDetect += detectStopWatch.ElapsedMilliseconds;
                        detectStopWatch.Reset();

                        pbar.Tick($"Step {loop} of {maxLoop}");
                    }

                Console.WriteLine($"Total Loop {maxLoop}");
                Console.WriteLine($"\tConvert Image to Mat: Total {totalImageLoad} ms, Avg {totalImageLoad / maxLoop} ms");
                Console.WriteLine($"\t         Detect Face: Total {totalDetect} ms, Avg {totalDetect / maxLoop} ms");
                Console.WriteLine($"\t    Total Throughput: Total {totalImageLoad + totalDetect} ms, Avg {(totalImageLoad + totalDetect) / maxLoop} ms");
            }

            if (NcnnDotNet.Ncnn.IsSupportVulkan)
            {
                Console.WriteLine("Uninitializes GPU");
                NcnnDotNet.Ncnn.DestroyGpuInstance();
            }

            return(0);
        }