Exemple #1
0
        static void Main(string[] args)
        {
            Console.Write("Enter path to an image to load: ");
            string path = Console.ReadLine();

            // Load bitmap from path
            IReadOnlyList <CnnFaceDetected> result;

            using (var bitmap = new Bitmap(path))
            {
                // Detect faces
                using var detector = new CnnFaceDetector();
                result             = detector.Detect(bitmap);
            }

            // Apply rectangles to faces
            using var image    = Image.FromFile(path);
            using var graphics = Graphics.FromImage(image);
            foreach (var faceDetected in result)
            {
                graphics.DrawRectangle(new Pen(Color.Red, 2), faceDetected.Rectangle);
                graphics.DrawString((faceDetected.Confidence * 100).ToString("0"), SystemFonts.DefaultFont, Brushes.Red, faceDetected.Rectangle);
            }

            // Save new image
            string output = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N")) + ".png";

            image.Save(output, ImageFormat.Png);

            // Open image
            Process.Start(new ProcessStartInfo(output)
            {
                UseShellExecute = true
            });
        }
Exemple #2
0
        public static IActionResult Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            // Check how many requests received from the same IP
            if (!ThrottleIp(req.HttpContext.Connection.RemoteIpAddress))
            {
                return(new StatusCodeResult(429));
            }

            // Read image from request
            using var bitmap = new Bitmap(req.Body);

            CnnFaceDetector detector = null;

            try
            {
                // Get a detector from pool
                detector = FaceDetectors.Get();

                // Detect faces
                IReadOnlyList <CnnFaceDetected> result = detector.Detect(bitmap, new Size(400, 400));
                log.LogInformation("Found {faces} faces", result.Count);

                return(new OkObjectResult(result));
            }
            finally
            {
                if (detector != null)
                {
                    FaceDetectors.Return(detector);
                }
            }
        }
        public void MultipleFaces()
        {
            using var bitmap = new Bitmap("faces.jpg");

            using var detector = new CnnFaceDetector();

            var stopwatch = Stopwatch.StartNew();
            IReadOnlyList <CnnFaceDetected> result = detector.Detect(bitmap);

            stopwatch.Stop();

            _testOutputHelper.WriteLine("Elapsed {0}", stopwatch.Elapsed.ToString());

            Assert.Equal(49, result.Count);
        }
        public void ResizeFace()
        {
            using var detector = new CnnFaceDetector();

            using (var bitmap = new Bitmap("faceh.jpg"))
            {
                var result = detector.Detect(bitmap, new Size(100, 100));
                Assert.Equal(1, result.Count);
            }

            using (var bitmap = new Bitmap("facev.jpg"))
            {
                var result = detector.Detect(bitmap, new Size(100, 100));
                Assert.Equal(1, result.Count);
            }
        }
        public void Benchmark()
        {
            using var bitmap = new Bitmap("faces.jpg");

            using var detector = new CnnFaceDetector();

            var stopwatch = Stopwatch.StartNew();

            for (int x = 0; x < 10; x++)
            {
                IReadOnlyList <CnnFaceDetected> result = detector.Detect(bitmap);
            }

            stopwatch.Stop();

            _testOutputHelper.WriteLine("Elapsed {0}. {1:0.#} per second", stopwatch.Elapsed.ToString(), stopwatch.Elapsed.TotalSeconds / 100);
        }