/// <summary> /// Parses and saves recognition result for image /// </summary> /// <param name="results">Recognition results recieved from omr core</param> /// <param name="item">The preview image that was recognized</param> private void ProcessResult(string results, ImagePreviewViewModel item) { ObservableCollection <RecognitionResult> recognitionResults = this.ParseAnswers(results); item.RecognitionResults = recognitionResults; this.OnPropertyChanged(nameof(this.RecognitionResults)); }
/// <summary> /// Cancel recognition for selected image /// </summary> /// <param name="itemToCancel">Image to cancel recognition for</param> private void CancelSelectedImageRecognition(ImagePreviewViewModel itemToCancel) { if (itemToCancel.CanCancel) { itemToCancel.IsProcessing = false; } }
/// <summary> /// Loads preview images by provided file paths /// </summary> /// <param name="files">File paths</param> private void LoadPreviewImages(string[] files) { if (files.Length == 0) { return; } foreach (string file in files) { FileInfo fileInfo = new FileInfo(file); if (!ResolutionUtility.CheckImageSize(fileInfo)) { continue; } // load image metadata to check width and height var bitmapFrame = BitmapFrame.Create(new Uri(file), BitmapCreateOptions.DelayCreation, BitmapCacheOption.None); if (bitmapFrame.PixelWidth < 1200 || bitmapFrame.PixelHeight < 1700) { DialogManager.ShowImageSizeWarning(); } var newItem = new ImagePreviewViewModel(file, Path.GetFileName(file), fileInfo.Length); newItem.ImageFileFormat = fileInfo.Extension; this.PreviewImages.Add(newItem); } this.InitialPreviewPanelVisibility = Visibility.Collapsed; this.SelectedPreviewImage = this.PreviewImages.First(); }
/// <summary> /// Loads single preview image from specified path /// </summary> /// <param name="pathToImage"></param> private void LoadPreviewImage(string pathToImage) { FileInfo fileInfo = new FileInfo(pathToImage); if (!ResolutionUtility.CheckImageSize(fileInfo)) { return; } // load image metadata to check width and height BitmapFrame bitmapFrame = BitmapFrame.Create(new Uri(pathToImage), BitmapCreateOptions.DelayCreation, BitmapCacheOption.None); if (bitmapFrame.PixelWidth < 1200 || bitmapFrame.PixelHeight < 1700) { DialogManager.ShowImageSizeWarning(fileInfo.Name); } var newItem = new ImagePreviewViewModel(pathToImage, Path.GetFileName(pathToImage), fileInfo.Length); newItem.ImageFileFormat = fileInfo.Extension; this.PreviewImages.Add(newItem); this.InitialPreviewPanelVisibility = Visibility.Collapsed; this.CurrentStage = ResultProcessingStages.GotImagesToRecognize; }
/// <summary> /// Performs recognition for provided image /// </summary> /// <param name="itemToProcess">Image to process</param> private void RecognizeImageRoutine(ImagePreviewViewModel itemToProcess) { // check if image was already processed if (itemToProcess.IsProcessed) { // ask user confirmation if (!DialogManager.ShowConfirmDialog( string.Format("Image {0} was already recognized. Are you sure you want to run recognition again?", itemToProcess.Title))) { // if no confirmation, clean up and return itemToProcess.IsProcessing = false; itemToProcess.StatusText = string.Empty; return; } } itemToProcess.CanCancel = false; itemToProcess.StatusText = "Preparing for dispatch..."; var image = new BitmapImage(new Uri("file://" + itemToProcess.PathToImage)); byte[] imageData = TemplateSerializer.CompressImage(image, itemToProcess.ImageSizeInBytes); //string imageData = TemplateConverter.CheckAndCompressImage(image, itemToProcess.ImageFileFormat, itemToProcess.ImageSizeInBytes); string additionalPars = string.Empty; itemToProcess.StatusText = "Core processing..."; try { string result = CoreApi.RecognizeImage(itemToProcess.Title, imageData, this.templateId, itemToProcess.WasUploaded, additionalPars); Application.Current.Dispatcher.Invoke(() => { this.ProcessResult(result, itemToProcess); // update item states itemToProcess.WasUploaded = true; itemToProcess.IsProcessing = false; itemToProcess.StatusText = string.Empty; itemToProcess.CanCancel = true; }); } catch (Exception e) { // clean up in case of exception itemToProcess.IsProcessing = false; itemToProcess.StatusText = string.Empty; itemToProcess.CanCancel = true; DialogManager.ShowErrorDialog(e.Message); } }
/// <summary> /// Parses and saves recognition result for image /// </summary> /// <param name="results">Recognition results recieved from omr core</param> /// <param name="item">The preview image that was recognized</param> private void ProcessResult(ImageRecognitionResult results, ImagePreviewViewModel item) { ObservableCollection <RecognitionResult> recognitionResults = this.ParseAnswers(results.RecognizedAnswers); item.RecognitionResults = recognitionResults; item.ClippedAreas = new ObservableCollection <ClippedArea>(); foreach (var clippedArea in results.ClippedAreas) { item.ClippedAreas.Add(new ClippedArea(clippedArea.Key, clippedArea.Value)); } this.OnPropertyChanged(nameof(this.RecognitionResults)); this.OnPropertyChanged(nameof(this.ClippedAreas)); }
/// <summary> /// Recognize single selected image /// </summary> /// <param name="imagePreview">Item to recognize</param> private void RecognizeSelectedImage(ImagePreviewViewModel imagePreview) { var itemToProcess = imagePreview ?? this.SelectedPreviewImage; BackgroundWorker worker = new BackgroundWorker(); worker.DoWork += (sender, args) => { this.RecognizeImageRoutine(itemToProcess); }; itemToProcess.IsProcessing = true; itemToProcess.StatusText = "Waiting..."; worker.RunWorkerAsync(); }
/// <summary> /// Performs recognition for provided image /// </summary> /// <param name="itemToProcess">Image to process</param> private void RecognizeImageRoutine(ImagePreviewViewModel itemToProcess) { // check if image was already processed if (itemToProcess.IsProcessed) { // ask user confirmation if (!DialogManager.ShowConfirmDialog( string.Format("Image \"{0}\" was already recognized. Are you sure you want to run recognition again?", itemToProcess.Title))) { // if no confirmation, clean up and return itemToProcess.IsProcessing = false; itemToProcess.StatusText = string.Empty; return; } } itemToProcess.CanCancel = false; itemToProcess.StatusText = "Preparing for dispatch..."; if (!File.Exists(itemToProcess.PathToImage)) { DialogManager.ShowErrorDialog("Could not find image " + "\"" + itemToProcess.PathToImage + "\"."); return; } byte[] imageData = this.PreprocessImage(itemToProcess.PathToImage, this.SelectedPreprocessingConfiguration); string additionalPars = string.Empty; itemToProcess.StatusText = "Recognizing..."; try { ImageRecognitionResult result = CoreApi.RecognizeImage(itemToProcess.Title, imageData, this.templateId, itemToProcess.WasUploaded, additionalPars); Application.Current.Dispatcher.Invoke(() => { this.ProcessResult(result, itemToProcess); // update item states itemToProcess.WasUploaded = true; itemToProcess.IsProcessing = false; itemToProcess.StatusText = string.Empty; itemToProcess.CanCancel = true; itemToProcess.RecognitionError = false; }); } catch (Exception e) { // clean up in case of exception itemToProcess.IsProcessing = false; itemToProcess.StatusText = string.Empty; itemToProcess.CanCancel = true; itemToProcess.RecognitionError = true; Application.Current.Dispatcher.Invoke(() => { DialogManager.ShowErrorDialog(e.Message); }); } }