コード例 #1
0
        public async void UpdateModelStatus()
        {
            _applicationStatusManager.ChangeCurrentAppStatus(Enums.Status.Working, "Working | loading model...");
            //get the last version of ml model with specific config
            try
            {
                Log.Information("Loading ml model.");
                Status = "Loading ml model...";
                var config = _newConfig.MlModelConfig;;
                // get local versions
                var localVersions = await MLModel.GetInstalledVersions(config);

                if (!localVersions.Contains(config.ModelVersion))
                {
                    throw new Exception($"There are no ml local model to init: {config.Image.Name}:{config.Image.Tag}");
                }

                Repository = config.Image.Name;
                Version    = $"{config.ModelVersion}";
                Type       = $"{config.Type}";
                Status     = $"Ready";
                Log.Information("Successfully init ml model.");
            }
            catch (Exception e)
            {
                Status = $"Not ready.";
                Log.Error(e, "Unable to load model.");
            }
            _applicationStatusManager.ChangeCurrentAppStatus(Enums.Status.Ready, "");
        }
コード例 #2
0
        public async Task OpenFile(string inputPath)
        {
            try
            {
                Status = "loading photos...";
                _applicationStatusManager.ChangeCurrentAppStatus(Enums.Status.Working, "");
                using (var pb = new Models.ProgressBar())
                {
                    var count       = 0;
                    var id          = 0;
                    var photoLoader = new PhotoLoader();
                    var files       = GetFilesFromDir(inputPath, false);
                    var enumerable  = files as string[] ?? files.ToArray();
                    _photos.Clear();
                    foreach (var path in enumerable)
                    {
                        try
                        {
                            await using (var stream = File.OpenRead(path))
                            {
                                if (Path.GetExtension(path).ToLower() != ".jpg" &&
                                    Path.GetExtension(path).ToLower() != ".jpeg" &&
                                    Path.GetExtension(path).ToLower() != ".png")
                                {
                                    count++;
                                    continue;
                                }
                                var annotation = new Annotation
                                {
                                    Filename = Path.GetFileName(path),
                                    Folder   = Path.GetDirectoryName(path)
                                };
                                var photo = await photoLoader.Load(path, stream, PhotoLoadType.Miniature);

                                _photos.Add(new PhotoViewModel(id, photo, annotation));
                                id++;
                                count++;
                                InputProgress     = (double)count / enumerable.Count() * 100;
                                InputTextProgress = $"{Convert.ToInt32(InputProgress)} %";
                                _applicationStatusManager.ChangeCurrentAppStatus(Enums.Status.Working, $"Working | {(int)((double) count / enumerable.Count() * 100)} %, [{count} of {enumerable.Count()}]");
                                pb.Report((double)count / enumerable.Count(), $"Processed {count} of {enumerable.Count()}");
                            }
                        }
                        catch (Exception e)
                        {
                            Log.Warning(e, $"image from {path} is skipped!");
                        }
                    }
                }
                _selectedIndex = 0;
                _applicationStatusManager.ChangeCurrentAppStatus(Enums.Status.Ready, "");
                InputTextProgress = $"loads {_photos.Count} photos.";
                Log.Information($"Loads {_photos.Count} photos.");
            }
            catch (Exception ex)
            {
                Status = "error.";
                Log.Error(ex, "Unable to load photos.");
            }
        }
コード例 #3
0
        public async void UpdateModelStatus()
        {
            _applicationStatusManager.ChangeCurrentAppStatus(Enums.Status.Working, "Working | loading model...");
            //get the last version of ml model with specific config
            try
            {
                Log.Information("Loading ml model.");
                Status = "Loading ml model...";
                var confDir    = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "lacmus");
                var configPath = Path.Join(confDir, "appConfig.json");
                _appConfig = await AppConfig.Create(configPath);

                var config = _appConfig.MlModelConfig;;
                // get local versions
                var localVersions = await MLModel.GetInstalledVersions(config);

                if (localVersions.Contains(config.ModelVersion))
                {
                    Log.Information($"Find local version: {config.Image.Name}:{config.Image.Tag}.");
                }
                else
                {
                    IsShowLoadModelButton = true;
                    throw new Exception($"There are no ml local models to init: {config.Image.Name}:{config.Image.Tag}");
                }
                Repository = config.Image.Name;
                Version    = $"{config.ModelVersion}";
                Type       = $"{config.Type}";
                using (var model = new MLModel(config))
                    await model.Download();
                Status  = $"Ready";
                IsError = false;
                Log.Information("Successfully loads ml model.");
            }
            catch (Exception e)
            {
                Status  = $"Not ready.";
                IsError = true;
                Error   = $"Error: {e.Message}";
                Log.Error(e, "Unable to load model.");
            }
            _applicationStatusManager.ChangeCurrentAppStatus(Enums.Status.Ready, "");
        }
コード例 #4
0
        public async void Init()
        {
            //get model status
            try
            {
                Status = "Loading ml model...";
                var config = _newConfig.MlModelConfig;
                // get local versions
                var localVersions = await MLModel.GetInstalledVersions(config);

                if (!localVersions.Contains(config.ModelVersion))
                {
                    throw new Exception($"There are no ml local model to init: {config.Image.Name}:{config.Image.Tag}");
                }
                if (config.ApiVersion != API_VERSION)
                {
                    throw new Exception($"Unsupported api {config.ApiVersion}. Only api v {API_VERSION} is supported.");
                }

                Dispatcher.UIThread.Post(() =>
                {
                    Repository = config.Image.Name;
                    Version    = $"{config.ModelVersion}";
                    Type       = $"{config.Type}";
                    Status     = $"Ready";
                });
            }
            catch (Exception e)
            {
                Status = $"Not ready.";
                Log.Error(e, "Unable to init model.");
            }
            // get installed models
            try
            {
                var models = await MLModel.GetInstalledModels();

                Dispatcher.UIThread.Post(() =>
                {
                    foreach (var model in models)
                    {
                        _installedModels.Add(new MlModelData(model.Image.Name,
                                                             model.Type,
                                                             model.ModelVersion,
                                                             model.ApiVersion));
                    }
                });
            }
            catch (Exception e)
            {
                Log.Error(e, "Unable to get installed models.");
            }
            //get available models
            try
            {
                foreach (var repository in _newConfig.Repositories)
                {
                    try
                    {
                        var models = await MLModel.GetAvailableModelsFromRegistry(repository);

                        Dispatcher.UIThread.Post(() =>
                        {
                            _repositories.Add(repository);
                            foreach (var model in models)
                            {
                                _avalableModels.Add(new MlModelData(model.Image.Name,
                                                                    model.Type,
                                                                    model.ModelVersion,
                                                                    model.ApiVersion));
                            }
                        });
                    }
                    catch (Exception e)
                    {
                        Log.Warning(e, $"Unable to parse models from {repository}. Skipped.");
                    }
                }
            }
            catch (Exception e)
            {
                Log.Error(e, "Unable get available models.");
            }

            Dispatcher.UIThread.Post(() =>
            {
                _applicationStatusManager.ChangeCurrentAppStatus(Enums.Status.Ready, "");
            });
        }
コード例 #5
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, "");
            }
        }