public static IEnumerable <FaceDetectionEntry> LoadDataset(string datasetPath, int width, int height, int n = -1)
        {
            var result = new ConcurrentBag <FaceDetectionEntry>();

            var fileNames = Directory.EnumerateFiles(Path.Combine(datasetPath, "faces"), "*.pgm");

            if (n != -1)
            {
                fileNames = fileNames.Take(n);
            }

            Parallel.ForEach(fileNames, filename =>
                             // foreach (var filename in fileNames)
            {
                Console.Write(".");
                var data  = new float[width * height];
                var pgm   = PgmImage.LoadImage(filename);
                var image = PgmImage.MakeBitmap(pgm);

                BitmapTool.ExtractData(width, height, data, image);

                result.Add(new FaceDetectionEntry {
                    IsFace = true, ImageData = data, Filename = filename
                });
            }
                             );

            return(result);
        }
Exemplo n.º 2
0
        private void evaluate_Click(object sender, EventArgs e)
        {
            var bitmap = GetImage(this.textBox1.Text);
            var output = (Bitmap)bitmap.Clone();

            bitmap = BitmapTool.ResizeImage(bitmap, 300, 300);

            bitmap.Save("input.jpg");

            var boxes = new List <BoundingBox>();

            int divisor = (int)this.divisorUpDown.Value;

            var allExtracts = BitmapTool.SlideWindow(bitmap, 140, 140 / divisor)
                              .Concat(BitmapTool.SlideWindow(bitmap, 100, 100 / divisor))
                              .Concat(BitmapTool.SlideWindow(bitmap, 80, 80 / divisor))
                              .Concat(BitmapTool.SlideWindow(bitmap, 30, 30 / divisor));

            foreach (var extracts in allExtracts.Batch(20))
            {
                boxes.AddRange(BitmapTool.Evaluate(extracts.ToList(), this._yhat, IMAGE_WIDTH, IMAGE_HEIGHT, (float)this.minProbaUpDown.Value / 100.0f));
            }

            this.currentBoundingBoxes.Clear();
            var nms    = new NonMaximumSuppresion();
            var result = nms.Nms(boxes, (float)this.nmsUpDown.Value / 100.0f);

            foreach (var boundingBox in result)
            {
                BitmapTool.DrawBoundingBox(output, boundingBox, Color.FromArgb(50, 0, 0, 255), false);
                this.currentBoundingBoxes.Add(BitmapTool.Capture(bitmap, boundingBox));
            }

            this.pictureBox1.Image = output;
        }
Exemplo n.º 3
0
        private void StartAI()
        {
            BuilderInstance <float> .Volume = new VolumeBuilder(); // Needed for GPU, must be done on AI thread

            // Must be done after setting BuilderInstance<float>.Volume
            this._yhat = SerializationExtensions.Load <float>("FaceDetection", false)[0];

            while (true)
            {
                if (this._queueIn.TryDequeue(out var bitmap))
                {
                    bitmap = BitmapTool.ResizeImage(bitmap, 300, 300);
                    var boxes = new List <BoundingBox>();


                    var allExtracts =
                        BitmapTool.SlideWindow(bitmap, 100, 100 / 9);
                    // .Concat(BitmapTool.SlideWindow(bitmap, 40, 40 / 3));
                    //.Concat(BitmapTool.SlideWindow(bitmap, 50, 50/3));

                    foreach (var extracts in allExtracts.Batch(10))
                    {
                        boxes.AddRange(BitmapTool.Evaluate(extracts.ToList(), this._yhat, IMAGE_WIDTH, IMAGE_HEIGHT, 0.8f));
                    }
                    this._queueOut.Enqueue(boxes);
                }
            }
        }
Exemplo n.º 4
0
        public IEnumerable <FaceDetectionEntry> LoadDataset(string datasetPath, int width, int height, int n = -1)
        {
            var result = new ConcurrentBag <FaceDetectionEntry>();

            var fileNames = Directory.EnumerateFiles(datasetPath, "*.jpg", SearchOption.AllDirectories);

            if (n != -1)
            {
                fileNames = fileNames.Take(n);
            }

            Parallel.ForEach(fileNames, filename =>
                             //foreach (var filename in fileNames)
            {
                Console.Write(".");
                var data  = new float[width * height];
                var image = Image.FromFile(filename) as Bitmap;

                if (this._random)
                {
                    for (var i = 0; i < this._randomPerImage; i++)
                    {
                        var x1 = Random.Value.Next((int)(image.Width * 0.8));
                        var x2 = Random.Value.Next(image.Width - x1) + this.minWidthPixel + x1;
                        var y1 = Random.Value.Next((int)(image.Height * 0.8));
                        var y2 = (int)((x2 - x1) / (float)image.Width * image.Height) + y1;
                        x2     = Math.Min(image.Width, x2);
                        y2     = Math.Min(image.Height, y2);


                        var subImage = image.Clone(new Rectangle(x1, y1, x2 - x1, y2 - y1), image.PixelFormat);
                        //       subImage.Save("test.jpg");

                        BitmapTool.ExtractData(width, height, data, subImage);
                        result.Add(new FaceDetectionEntry {
                            IsFace = false, ImageData = data, Filename = filename
                        });

                        subImage.Dispose();
                    }
                }
                else
                {
                    BitmapTool.ExtractData(width, height, data, image);
                    result.Add(new FaceDetectionEntry {
                        IsFace = false, ImageData = data, Filename = filename
                    });
                }

                image.Dispose();
            }
                             );

            return(result);
        }
Exemplo n.º 5
0
        private void bitmapToolButton_Click(object sender, EventArgs e)
        {
            //Prepare
            BitmapTool bitmapTool = new BitmapTool();

            bitmapTool.FormClosed += BitmapTool_FormClosed;

            //Hide self, show bitmap tool
            Hide();
            bitmapTool.Show();
        }
Exemplo n.º 6
0
        private void NewFrame_event(object send, NewFrameEventArgs e)
        {
            try
            {
                lock (this._locker)
                {
                    var webcamFrame = e.Frame;

                    if (this._queueIn.Count == 0)
                    {
                        this._queueIn.Enqueue((Bitmap)webcamFrame.Clone());
                    }

                    if (this._queueOut.TryDequeue(out var boundingBox))
                    {
                        this._lastBoundingBoxes = boundingBox;

                        BeginInvoke(new Action(() => { this.Text = (++this._evaluationCount).ToString(); }));
                    }

                    if (this._lastBoundingBoxes != null)
                    {
                        foreach (var box in this._lastBoundingBoxes)
                        {
                            BitmapTool.DrawBoundingBox(webcamFrame, box, Color.FromArgb(50, 0, 0, 255), false);
                        }

                        var nms    = new NonMaximumSuppresion();
                        var result = nms.Nms(this._lastBoundingBoxes, 0.20f);
                        foreach (var box in result)
                        {
                            BitmapTool.DrawBoundingBox(webcamFrame, box, Color.FromArgb(50, 0, 255, 0), false);
                        }
                    }

                    this.pictureBox.Image = (Image)webcamFrame.Clone();
                }
            }
            catch
            {
            }
        }
Exemplo n.º 7
0
        public static FaceLocalizationDataset LoadDataset(string datasetPath, int width, int height, int n = -1)
        {
            Console.Write("Loading Helen dataset");

            var result = new FaceLocalizationDataset(width, height);

            // Annotations
            var annotations = new Dictionary <string, BoundingBox>();

            foreach (var file in Directory.EnumerateFiles(Path.Combine(datasetPath, "annotation")))
            {
                using (var sr = new StreamReader(file))
                {
                    var key         = sr.ReadLine();
                    var boundingBox = new BoundingBox();

                    // Compute bounding box
                    while (!sr.EndOfStream)
                    {
                        var line = sr.ReadLine().Replace(" ", String.Empty).Split(',');
                        var x    = Single.Parse(line[0]);
                        var y    = Single.Parse(line[1]);

                        if (x < boundingBox.x1)
                        {
                            boundingBox.x1 = x;
                        }

                        if (x > boundingBox.x2)
                        {
                            boundingBox.x2 = x;
                        }

                        if (y < boundingBox.y1)
                        {
                            boundingBox.y1 = y;
                        }

                        if (y > boundingBox.y2)
                        {
                            boundingBox.y2 = y;
                        }
                    }

                    annotations[key] = boundingBox;
                }
            }

            // Images
            var fileNames = Directory.EnumerateFiles(Path.Combine(datasetPath, "helen_1"))
                            .Concat(Directory.EnumerateFiles(Path.Combine(datasetPath, "helen_2")))
                            .Concat(Directory.EnumerateFiles(Path.Combine(datasetPath, "helen_3")))
                            .Concat(Directory.EnumerateFiles(Path.Combine(datasetPath, "helen_4")))
                            .Concat(Directory.EnumerateFiles(Path.Combine(datasetPath, "helen_5")));

            if (n != -1)
            {
                fileNames = fileNames.Take(n);
            }

            Parallel.ForEach(fileNames, filename =>
                             //  foreach (var filename in fileNames)
            {
                Console.Write(".");
                var data = new float[width * height];

                // Load image
                var image          = (Bitmap)Image.FromFile(filename);
                var originalWidth  = image.Width;
                var originalHeight = image.Height;

                BitmapTool.ExtractData(width, height, data, image);

                // normalize box
                var boundingBox = annotations[Path.GetFileName(filename).Replace(".jpg", String.Empty)];
                boundingBox.x1  = boundingBox.x1 / originalWidth;
                boundingBox.y1  = boundingBox.y1 / originalHeight;
                boundingBox.x2  = boundingBox.x2 / originalWidth;
                boundingBox.y2  = boundingBox.y2 / originalHeight;

                result.TrainSet.Add(new FaceLocalizationEntry {
                    ImageData = data, BoundingBox = boundingBox, Filename = filename
                });
                //}
            });

            return(result);
        }