Ejemplo n.º 1
0
        private async void SavePhotos()
        {
            try
            {
                _applicationStatusManager.ChangeCurrentAppStatus(Enums.Status.Working, "");
                if (!Directory.Exists(OutputPath))
                {
                    throw new Exception($"No such directory: {OutputPath}.");
                }

                //select filter
                var photosToSave    = Filter(_photos.Items, FilterIndex);
                var photoViewModels = photosToSave as PhotoViewModel[] ?? photosToSave.ToArray();
                if (!photoViewModels.Any())
                {
                    Log.Warning("There are no photos to save.");
                    _applicationStatusManager.ChangeCurrentAppStatus(Enums.Status.Ready, "");
                    return;
                }

                var count = 0;
                _applicationStatusManager.ChangeCurrentAppStatus(Enums.Status.Working, $"Working | {(int)((double) count / photoViewModels.Length * 100)} %, [{count} of {photoViewModels.Length}]");
                Parallel.ForEach(photoViewModels, async photoViewModel =>
                {
                    await Task.Run(async() =>
                    {
                        if (IsSource)
                        {
                            var srcPhotoPath = photoViewModel.Path;
                            var dstPhotoPath = Path.Combine(OutputPath, photoViewModel.Annotation.Filename);
                            if (srcPhotoPath == dstPhotoPath)
                            {
                                Log.Warning($"Photo {srcPhotoPath} skipped. File exists.");
                                return;
                            }
                            File.Copy(srcPhotoPath, dstPhotoPath, true);
                        }

                        if (IsXml)
                        {
                            var annotationPath = Path.Combine(OutputPath, $"{photoViewModel.Annotation.Filename}.xml");
                            var saver          = new AnnotationSaver();
                            saver.Save(photoViewModel.Annotation, annotationPath);
                        }

                        if (!IsDraw && !IsCrop)
                        {
                            return;
                        }

                        using var bitmap = SKBitmap.Decode(photoViewModel.Path);
                        {
                            if (IsCrop)
                            {
                                var image   = SKImage.FromBitmap(bitmap);
                                var cropIdx = 0;
                                foreach (var bbox in photoViewModel.Annotation.Objects)
                                {
                                    var subset      = image.Subset(new SKRectI(bbox.Box.Xmin, bbox.Box.Ymin, bbox.Box.Xmax, bbox.Box.Ymax));
                                    var encodedData = subset.Encode(SKEncodedImageFormat.Png, 100);
                                    var stream      = encodedData.AsStream();
                                    var path        = Path.Join(OutputPath,
                                                                $"{photoViewModel.Annotation.Filename}_crop{cropIdx}.png");
                                    SaveStream(stream, path);
                                    cropIdx++;
                                }
                            }
                            if (IsDraw)
                            {
                                var canvas = new SKCanvas(bitmap);
                                var paint  = new SKPaint {
                                    Style       = SKPaintStyle.Stroke,
                                    Color       = SKColors.Red,
                                    StrokeWidth = 10
                                };
                                foreach (var bbox in photoViewModel.Annotation.Objects)
                                {
                                    var x      = bbox.Box.Xmin;
                                    var y      = bbox.Box.Ymin;
                                    var width  = bbox.Box.Xmax - bbox.Box.Xmin;
                                    var height = bbox.Box.Ymax - bbox.Box.Ymin;
                                    canvas.DrawRect(SKRect.Create(x, y, width, height), paint);
                                }

                                var encodedData = SKImage.FromBitmap(bitmap).Encode(SKEncodedImageFormat.Png, 100);
                                var stream      = encodedData.AsStream();
                                var path        = Path.Join(OutputPath,
                                                            $"{photoViewModel.Annotation.Filename}_draw.png");
                                SaveStream(stream, path);
                            }
                        }
                        await Dispatcher.UIThread.InvokeAsync(() =>
                        {
                            count++;
                            _applicationStatusManager.ChangeCurrentAppStatus(Enums.Status.Working, $"Working | {(int)((double) count / photoViewModels.Length * 100)} %, [{count} of {photoViewModels.Length}]");
                            if (count >= photoViewModels.Length)
                            {
                                _applicationStatusManager.ChangeCurrentAppStatus(Enums.Status.Ready, "");
                            }
                        });
                    });
                });

                Log.Information($"Saved {photoViewModels.Length} photos.");
                _applicationStatusManager.ChangeCurrentAppStatus(Enums.Status.Ready, "");
            }
            catch (Exception e)
            {
                Log.Error("Unable to save photos.", e);
                _applicationStatusManager.ChangeCurrentAppStatus(Enums.Status.Ready, "");
            }
        }
Ejemplo n.º 2
0
 public async Task SaveAll(string outputPath)
 {
     try
     {
         if (!_photos.Items.Any())
         {
             Log.Warning("There are no photos to save.");
             OutputTextProgress = $"no photos to save.";
             Status             = "done.";
             return;
         }
         _applicationStatusManager.ChangeCurrentAppStatus(Enums.Status.Working, "");
         Status = "saving results...";
         try
         {
             var count      = 0;
             var viewModels = _photos.Items as PhotoViewModel[] ?? _photos.Items.ToArray();
             using (var pb = new ProgressBar())
             {
                 foreach (var photoViewModel in viewModels)
                 {
                     await Task.Run(() =>
                     {
                         var srcPhotoPath   = photoViewModel.Path;
                         var dstPhotoPath   = Path.Combine(outputPath, photoViewModel.Annotation.Filename);
                         var annotationPath = Path.Combine(outputPath, $"{photoViewModel.Annotation.Filename}.xml");
                         var annotation     = photoViewModel.Annotation;
                         annotation.Folder  = outputPath;
                         var saver          = new AnnotationSaver();
                         saver.Save(annotation, annotationPath);
                         if (srcPhotoPath == dstPhotoPath)
                         {
                             Log.Warning($"Photo {srcPhotoPath} skipped. File exists.");
                             count++;
                             OutputProgress     = (double)count / viewModels.Count() * 100;
                             OutputTextProgress = $"{Convert.ToInt32(OutputProgress)} %";
                             pb.Report((double)count / viewModels.Count(), $"Saving files {count} of {viewModels.Length}");
                             return;
                         }
                         File.Copy(srcPhotoPath, dstPhotoPath, true);
                         count++;
                         OutputProgress     = (double)count / viewModels.Count() * 100;
                         OutputTextProgress = $"{Convert.ToInt32(OutputProgress)} %";
                         pb.Report((double)count / viewModels.Count(), $"Saving files {count} of {viewModels.Length}");
                     });
                 }
             }
         }
         catch (Exception e)
         {
             throw new Exception("Unable to save photo!", e);
         }
         Log.Information($"Saved {_photos.Count} photos.");
     }
     catch (Exception ex)
     {
         Status = "error.";
         Log.Error(ex, "Unable to save photos.");
     }
     Status             = "done.";
     OutputTextProgress = $"saved {_photos.Count} photos.";
     _applicationStatusManager.ChangeCurrentAppStatus(Enums.Status.Ready, "");
 }