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); }
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); }
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); }