Esempio n. 1
0
        private static void Load(string directory, out IList <Matrix <RgbPixel> > images, out IList <uint> labels)
        {
            var imageList = new List <Matrix <RgbPixel> >();
            var labelList = new List <uint>();

            foreach (var file in Directory.EnumerateFiles(directory))
            {
                var name = Path.GetFileName(file);
                var s    = name.Split('_');
                if (s.Length != 4 || !uint.TryParse(s[1], out var gender))
                {
                    continue;
                }

                using (var tmp = Dlib.LoadImageAsMatrix <RgbPixel>(file))
                {
                    var m = new Matrix <RgbPixel>(Size, Size);
                    Dlib.ResizeImage(tmp, m);
                    imageList.Add(m);
                    labelList.Add(gender);
                }
            }

            images = imageList;
            labels = labelList;
        }
        public static void SetFormImage(Array2D <RgbPixel> img)
        {
            Dlib.ResizeImage(img, 0.4);

            Bitmap image = new Bitmap(img.Columns, img.Rows, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

            for (int i = 0; i < img.Columns; i++)
            {
                for (int j = 0; j < img.Rows; j++)
                {
                    //Color c = Color.FromArgb(img[i][j].Red, img[i][j].Green, img[i][j].Blue);
                    //image.SetPixel(i, j, c);
                }
            }

            image.RotateFlip(RotateFlipType.Rotate90FlipNone);

            Form1.SetImage(image);
        }
Esempio n. 3
0
        private static void Load(string type, string directory, string meanImage, out IList <Matrix <RgbPixel> > images, out IList <uint> labels)
        {
            Matrix <RgbPixel> mean = null;

            try
            {
                if (File.Exists(meanImage))
                {
                    mean = Dlib.LoadImageAsMatrix <RgbPixel>(meanImage);
                }

                var csv       = ReadCsv(Path.Combine(directory, $"{type}.csv"));
                var imageList = new List <Matrix <RgbPixel> >();
                var labelList = new List <uint>();
                foreach (var kvp in csv)
                {
                    var path = Path.Combine(directory, type, kvp.Key);
                    if (!File.Exists(path))
                    {
                        continue;
                    }

                    using (var tmp = Dlib.LoadImageAsMatrix <RgbPixel>(path))
                    {
                        if (mean != null)
                        {
                            using (var m = new Matrix <RgbPixel>(Size, Size))
                            {
                                Dlib.ResizeImage(tmp, m);

                                // ToDo: Support subtract operator on DlibDotNet
                                // var ret = m - mean;
                                var ret = new Matrix <RgbPixel>(Size, Size);
                                for (var row = 0; row < Size; row++)
                                {
                                    for (var col = 0; col < Size; col++)
                                    {
                                        var left  = m[row, col];
                                        var right = mean[row, col];
                                        var red   = left.Red - right.Red;
                                        var green = left.Green - right.Green;
                                        var blue  = left.Blue - right.Blue;
                                        ret[row, col] = new RgbPixel((byte)red, (byte)green, (byte)blue);
                                    }
                                }
                                imageList.Add(ret);
                            }
                        }
                        else
                        {
                            var m = new Matrix <RgbPixel>(Size, Size);
                            Dlib.ResizeImage(tmp, m);
                            imageList.Add(m);
                        }

                        labelList.Add(kvp.Value);
                    }
                }

                images = imageList;
                labels = labelList;
            }
            finally
            {
                mean?.Dispose();
            }
        }