private void DoWork(object sender, DoWorkEventArgs e) { var currentProgress = inputProgressBar.Value; var filesProcessed = 0; var totalFilesCount = 0; var inputDirectories = Directory.GetDirectories(inputFolderPath); if (inputDirectories.Length == 0) { throw new Exception("No categories was found!"); } var labels = new string[inputDirectories.Length]; for (var i = 0; i < inputDirectories.Length; i++) { labels[i] = inputDirectories[i].Substring(inputFolderPath.Length + 1); } for (var i = 0; i < inputDirectories.Length; i++) { if (bw.CancellationPending) { e.Cancel = true; return; } var inputFilePaths = Directory.GetFiles(inputDirectories[i] + "\\", "*." + extension); if (inputFilePaths.Length == 0) { throw new Exception("No files in category was found!"); } else { totalFilesCount += inputFilePaths.Length; } } totalFilesCount *= 2; for (var k = 0; k < inputDirectories.Length; k++) { var inputFilePaths = Directory.GetFiles(inputDirectories[k] + "\\", "*." + extension); foreach (var inputFP in inputFilePaths) { if (bw.CancellationPending) { e.Cancel = true; return; } if (!ImageProcessor.PrepareImageFromFile(inputFP, width, height, extension)) { throw new Exception("Error processing the image!"); } filesProcessed += 1; var progress = 100 * filesProcessed / totalFilesCount; if (progress > currentProgress) { bw.ReportProgress(progress); } } } var setFolder = inputFolderPath.Substring(inputFolderPath.LastIndexOf('\\') + 1); for (int k = 0; k < inputDirectories.Length; k++) { var tmpPath = Path.Combine(Directory.GetCurrentDirectory(), "temp"); var label = inputDirectories[k].Substring(inputDirectories[k].LastIndexOf('\\') + 1); var inputFilePaths = Directory.GetFiles(Path.Combine(tmpPath, setFolder, label) + "\\", "*." + extension); using var fileStream = File.Open(outputFilePath, FileMode.Append, FileAccess.Write); using StreamWriter sw = new StreamWriter(fileStream); foreach (var inputFP in inputFilePaths) { if (bw.CancellationPending) { e.Cancel = true; return; } var output = ""; var image = new Bitmap(inputFP); var imageWidth = image.Width; var imageHeight = image.Height; var partWidth = imageWidth / int.Parse(Resources.PARTITION); var partHeight = imageHeight / int.Parse(Resources.PARTITION); for (var j = 0; j < int.Parse(Resources.PARTITION); j++) { for (var i = 0; i < int.Parse(Resources.PARTITION); i++) { var pixels = ImageProcessor.CountBlackPixels(image, i * partWidth, j * partHeight, (i + 1) * partWidth - 1, (j + 1) * partHeight - 1); output += pixels + ","; } } output += labels[k]; sw.WriteLine(output); filesProcessed += 1; var progress = 100 * filesProcessed / totalFilesCount; if (progress > currentProgress) { bw.ReportProgress(progress); } } } Directory.Delete(Path.Combine(Directory.GetCurrentDirectory(), "temp", setFolder), true); }
private void ConvertedDocumentView_Load(object sender, EventArgs e) { List <Rectangle> tmp = new List <Rectangle>(); if (template == DocTemplate.PersonEvidence) { initialImage = Resources.PEF; } documentWrapper.Size = inputImageSize; int width = owner.ClientSize.Width > inputImageSize.Width + 16 ? inputImageSize.Width : owner.ClientSize.Width - 28; int height = owner.ClientSize.Height > inputImageSize.Height + 67 ? inputImageSize.Height + 28 : owner.ClientSize.Height - 75; ClientSize = new Size(width, height); MaximumSize = new Size(width + 16, height + 39); inputImage = initialImage.ToImage <Bgr, byte>(); outputImage = inputImage.Convert <Gray, byte>().ThresholdBinary(new Gray(100), new Gray(255)); VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint(); Mat hierarhy = new Mat(); CvInvoke.FindContours(outputImage, contours, hierarhy, RetrType.Tree, ChainApproxMethod.ChainApproxSimple); Dictionary <int, double> contoursDict = new Dictionary <int, double>(); for (int i = 0; i < contours.Size; i++) { double area = CvInvoke.ContourArea(contours[i]); contoursDict.Add(i, area); } var items = contoursDict.OrderByDescending(i => i.Value).Skip(1).Take(predictions.Count); foreach (var item in items) { int key = int.Parse(item.Key.ToString()); tmp.Add(CvInvoke.BoundingRectangle(contours[key])); } var skipItemsCount = 0; tmp.Sort((item1, item2) => item1.Y.CompareTo(item2.Y)); partitions.ForEach(part => { var items = tmp.Skip(skipItemsCount).Take(part).ToList <Rectangle>(); items.Sort((item1, item2) => item1.X.CompareTo(item2.X)); skipItemsCount += part; foreach (var bbox in items) { int w = bbox.Width; int h = bbox.Height; int x = bbox.X + w / 10; int y = bbox.Y + h / 10; w -= 2 * w / 10; h -= 2 * h / 10; BBoxes.Add(new Rectangle(x, y, w, h)); } }); for (int i = 0; i < predictions.Count; i++) { ImageProcessor.ReplacePrediction(predictions.ElementAt(i).Value, initialImage, BBoxes.ElementAt(i)); } scaleX = (float)inputImageSize.Width / initialImage.Size.Width; scaleY = (float)inputImageSize.Height / initialImage.Size.Height; documentWrapper.Image = new Bitmap(initialImage, inputImageSize); correctArea = new Region(new Rectangle(new Point(0, 0), documentWrapper.Image.Size)); tmp = new List <Rectangle>(); BBoxes.ForEach(bbox => { var rect = new Rectangle((int)(bbox.X * scaleX), (int)(bbox.Y * scaleY), (int)(bbox.Width * scaleX), (int)(bbox.Height * scaleY)); tmp.Add(rect); correctArea.Exclude(rect); }); BBoxesScaled = tmp; }