From 76cbd34eae64052b0ca3d332211d8f00d6da3d63 Mon Sep 17 00:00:00 2001 From: Roy Ben-Shabat Date: Tue, 2 Apr 2019 13:54:23 +0300 Subject: Working on color capture module... --- .../Tango.MachineStudio.ColorCapture/App.config | 75 +++++++++++++- .../ColorCaptureSettings.cs | 20 ++++ .../Models/CaptureConfig.cs | 66 ++++++++++++ .../Models/CaptureItem.cs | 16 ++- .../Tango.MachineStudio.ColorCapture.csproj | 10 ++ .../ViewModels/MainViewVM.cs | 105 ++++++++++++++++--- .../Views/MainView.xaml | 111 +++++++++++++++++++-- .../packages.config | 1 + 8 files changed, 381 insertions(+), 23 deletions(-) create mode 100644 Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ColorCapture/ColorCaptureSettings.cs create mode 100644 Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ColorCapture/Models/CaptureConfig.cs (limited to 'Software/Visual_Studio/MachineStudio/Modules') diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ColorCapture/App.config b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ColorCapture/App.config index a6a2b7fa9..8b30e609b 100644 --- a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ColorCapture/App.config +++ b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ColorCapture/App.config @@ -1,2 +1,75 @@  - \ No newline at end of file + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ColorCapture/ColorCaptureSettings.cs b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ColorCapture/ColorCaptureSettings.cs new file mode 100644 index 000000000..63ed247ef --- /dev/null +++ b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ColorCapture/ColorCaptureSettings.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Tango.MachineStudio.ColorCapture.Models; +using Tango.Settings; + +namespace Tango.MachineStudio.ColorCapture +{ + public class ColorCaptureSettings : SettingsBase + { + public CaptureConfig Config { get; set; } + + public ColorCaptureSettings() + { + Config = new CaptureConfig(); + } + } +} diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ColorCapture/Models/CaptureConfig.cs b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ColorCapture/Models/CaptureConfig.cs new file mode 100644 index 000000000..c1b7d2dcb --- /dev/null +++ b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ColorCapture/Models/CaptureConfig.cs @@ -0,0 +1,66 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Tango.Core; +using Tango.Core.Helpers; + +namespace Tango.MachineStudio.ColorCapture.Models +{ + public class CaptureConfig : ExtendedObject + { + private int _columns; + public int Columns + { + get { return _columns; } + set { _columns = value; RaisePropertyChangedAuto(); } + } + + private int _rows; + public int Rows + { + get { return _rows; } + set { _rows = value; RaisePropertyChangedAuto(); } + } + + private int _targetIndex; + public int TargetIndex + { + get { return _targetIndex; } + set { _targetIndex = value; RaisePropertyChangedAuto(); } + } + + private String _samplesFolder; + public String SamplesFolder + { + get { return _samplesFolder; } + set { _samplesFolder = value; RaisePropertyChangedAuto(); } + } + + private String _benchmarksFile; + public String BenchmarksFile + { + get { return _benchmarksFile; } + set { _benchmarksFile = value; RaisePropertyChangedAuto(); } + } + + private String _templateFile; + public String TemplateFile + { + get { return _templateFile; } + set { _templateFile = value; RaisePropertyChangedAuto(); } + } + + public CaptureConfig() + { + Columns = 10; + Rows = 11; + TargetIndex = 89; + SamplesFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Twine", "Tango", "TCC Samples"); + BenchmarksFile = Path.Combine(AssemblyHelper.GetCurrentAssemblyFolder(), "TCC", "benchmarks_rgb_lab.csv"); + TemplateFile = Path.Combine(AssemblyHelper.GetCurrentAssemblyFolder(), "TCC", "template.bmp"); + } + } +} diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ColorCapture/Models/CaptureItem.cs b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ColorCapture/Models/CaptureItem.cs index f997783ca..66fe7d7af 100644 --- a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ColorCapture/Models/CaptureItem.cs +++ b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ColorCapture/Models/CaptureItem.cs @@ -1,4 +1,5 @@ -using System; +using Newtonsoft.Json; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -10,8 +11,21 @@ namespace Tango.MachineStudio.ColorCapture.Models public class CaptureItem { public DateTime Time { get; set; } + + [JsonIgnore] + public String Folder { get; set; } + + [JsonIgnore] + public String Image { get; set; } + public Color CapturedColor { get; set; } public Color ProcessedColor { get; set; } + public Color RefColor { get; set; } + + public double RefL { get; set; } + public double RefA { get; set; } + public double RefB { get; set; } + public double DeltaE { get; set; } } } diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ColorCapture/Tango.MachineStudio.ColorCapture.csproj b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ColorCapture/Tango.MachineStudio.ColorCapture.csproj index 6bfe0938f..5ec3ae0df 100644 --- a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ColorCapture/Tango.MachineStudio.ColorCapture.csproj +++ b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ColorCapture/Tango.MachineStudio.ColorCapture.csproj @@ -53,6 +53,9 @@ ..\..\..\packages\CommonServiceLocator.1.3\lib\portable-net4+sl5+netcore45+wpa81+wp8\Microsoft.Practices.ServiceLocation.dll + + ..\..\..\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll + @@ -87,11 +90,13 @@ GlobalVersionInfo.cs + + Code @@ -115,6 +120,7 @@ ResXFileCodeGenerator Resources.Designer.cs + SettingsSingleFileGenerator @@ -146,6 +152,10 @@ {58e8825f-0c96-449c-b320-1e82b0aa876b} Tango.CSV + + {bc932dbd-7cdb-488c-99e4-f02cf441f55e} + Tango.Logging + {e4927038-348d-4295-aaf4-861c58cb3943} Tango.PMR diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ColorCapture/ViewModels/MainViewVM.cs b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ColorCapture/ViewModels/MainViewVM.cs index 855d9c0c3..d0d251444 100644 --- a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ColorCapture/ViewModels/MainViewVM.cs +++ b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ColorCapture/ViewModels/MainViewVM.cs @@ -5,6 +5,7 @@ using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Diagnostics; +using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -30,6 +31,8 @@ namespace Tango.MachineStudio.ColorCapture.ViewModels private CardDetector _cardDetector; private int _sampleCounter; private bool _abort; + private BitmapSource _last_original_source; + private DetectionOutput _last_detection_output; public IVideoCaptureProvider VideoProvider { get; set; } @@ -50,7 +53,7 @@ namespace Tango.MachineStudio.ColorCapture.ViewModels public BitmapSource DetectedSource { get { return _detectedSource; } - set { _detectedSource = value; RaisePropertyChangedAuto(); } + set { _detectedSource = value; RaisePropertyChangedAuto(); InvokeUI(() => CreateSnapshotCommand.RaiseCanExecuteChanged()); } } private ObservableCollection _colors; @@ -95,6 +98,8 @@ namespace Tango.MachineStudio.ColorCapture.ViewModels set { _measureB = value; RaisePropertyChangedAuto(); } } + public double DeltaE { get; set; } + public WpfGraphController CaptureDeltaEController { get; set; } public RelayCommand ToggleCameraCommand { get; set; } @@ -108,12 +113,24 @@ namespace Tango.MachineStudio.ColorCapture.ViewModels set { _benchmarks = value; RaisePropertyChangedAuto(); } } + private CaptureConfig _config; + public CaptureConfig Config + { + get { return _config; } + set { _config = value; RaisePropertyChangedAuto(); } + } + public RelayCommand ImportBenchmarksCommand { get; set; } public RelayCommand ExportBenchmarksCommand { get; set; } + public RelayCommand CreateSnapshotCommand { get; set; } + + public RelayCommand OpenCaptureItemCommand { get; set; } + public MainViewVM() { + Config = new CaptureConfig(); CaptureItems = new SynchronizedObservableCollection(); Benchmarks = new ObservableCollection(); _cardDetector = new CardDetector(); @@ -124,6 +141,74 @@ namespace Tango.MachineStudio.ColorCapture.ViewModels ImportBenchmarksCommand = new RelayCommand(OpenBenchmarksFile); ExportBenchmarksCommand = new RelayCommand(SaveBenchmarksFile); + CreateSnapshotCommand = new RelayCommand(CreateSnapshot, () => DetectedSource != null); + OpenCaptureItemCommand = new RelayCommand(OpenCaptureItem); + } + + private void OpenCaptureItem(CaptureItem item) + { + using (_notification.PushTaskItem("Opening snapshot folder...")) + { + Process.Start("explorer.exe", string.Format("/select,\"{0}\"", item.Image)); + } + } + + private void CreateSnapshot() + { + try + { + DateTime now = DateTime.Now; + + Directory.CreateDirectory(Config.SamplesFolder); + + String sample_folder = Config.SamplesFolder + "\\" + now.ToFileName(); + Directory.CreateDirectory(sample_folder); + + String rectified_file = sample_folder + "\\rectified.bmp"; + DetectedSource.SaveBmpFile(rectified_file); + + String original_file = sample_folder + "\\source.bmp"; + _last_original_source.SaveBmpFile(original_file); + + String means_file = sample_folder + "\\means.bmp"; + var bitmap = ColorDetector.DetectionOutputToImage(new DetectionInput() + { + Columns = Config.Columns, + Rows = Config.Rows, + TargetIndex = Config.TargetIndex + + }, _last_detection_output, 300, 310); + + bitmap.Save(means_file); + + String capture_item_file = sample_folder + "\\info.txt"; + + var rgb = new Lab(MeasureL, _measureA, _measureB).ToRgb(); + Color refColor = Color.FromArgb(255, (byte)rgb.R, (byte)rgb.G, (byte)rgb.B); + + var captureItem = new CaptureItem() + { + Image = rectified_file, + CapturedColor = CapturedColor, + ProcessedColor = ProcessedColor, + RefColor = refColor, + DeltaE = DeltaE, + RefL = MeasureL, + RefA = MeasureA, + RefB = MeasureB, + Time = now, + Folder = sample_folder, + }; + + File.WriteAllText(capture_item_file, captureItem.ToJsonString()); + + CaptureItems.Insert(0, captureItem); + } + catch (Exception ex) + { + LogManager.Log(ex, "Error in color capture snapshot save method."); + _notification.ShowError($"An error occurred while trying to create the snapshot. Please try again.\n{ex.Message}"); + } } private void SaveBenchmarksFile() @@ -170,7 +255,7 @@ namespace Tango.MachineStudio.ColorCapture.ViewModels private void ImportBenchmarks(String file) { var marks = ColorDetector.LoadBenchmarks(file).ToList(); - var benchmarks = marks.Select(x => BenchmarkItem.FromDetectionBenchmark(x,marks.IndexOf(x))).ToList(); + var benchmarks = marks.Select(x => BenchmarkItem.FromDetectionBenchmark(x, marks.IndexOf(x))).ToList(); Benchmarks = new ObservableCollection(benchmarks); } @@ -218,7 +303,6 @@ namespace Tango.MachineStudio.ColorCapture.ViewModels } } - double deltaE = 0; private async void OnVideoFrameReceived(object sender, Video.DirectShow.EventArguments.FrameReceivedEventArgs args) { if (_abort) return; @@ -232,6 +316,9 @@ namespace Tango.MachineStudio.ColorCapture.ViewModels if (result.IsDetected) { + _last_original_source = result.Source; + _last_detection_output = result.ColorDetectionOutput; + DetectedSource = result.DetectedBitmap; if (Colors == null) @@ -252,20 +339,12 @@ namespace Tango.MachineStudio.ColorCapture.ViewModels //calculate delta E. Lab measureLab = new Lab(MeasureL, MeasureA, MeasureB); - deltaE = measureLab.Compare(new Rgb(ProcessedColor.R, ProcessedColor.G, ProcessedColor.B), new CieDe2000Comparison()); - - CaptureItems.Insert(0, new CaptureItem() - { - CapturedColor = CapturedColor, - ProcessedColor = ProcessedColor, - DeltaE = deltaE, - Time = DateTime.Now, - }); + DeltaE = measureLab.Compare(new Rgb(ProcessedColor.R, ProcessedColor.G, ProcessedColor.B), new CieDe2000Comparison()); }); } } - CaptureDeltaEController.PushData(_sampleCounter++, deltaE); + CaptureDeltaEController.PushData(_sampleCounter++, DeltaE); } public override void OnApplicationReady() diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ColorCapture/Views/MainView.xaml b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ColorCapture/Views/MainView.xaml index 54231dfde..5f4f0db07 100644 --- a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ColorCapture/Views/MainView.xaml +++ b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ColorCapture/Views/MainView.xaml @@ -10,6 +10,7 @@ xmlns:graphX="clr-namespace:RealTimeGraphX.WPF.Surfaces;assembly=RealTimeGraphX.WPF" xmlns:componentsX="clr-namespace:RealTimeGraphX.WPF.Components;assembly=RealTimeGraphX.WPF" xmlns:controls="clr-namespace:Tango.MachineStudio.ColorCapture.Controls" + xmlns:sharedControls="clr-namespace:Tango.SharedUI.Controls;assembly=Tango.SharedUI" xmlns:realtimeGraphX="clr-namespace:RealTimeGraphX.WPF.Surfaces;assembly=RealTimeGraphX.WPF" xmlns:global="clr-namespace:Tango.MachineStudio.ColorCapture" xmlns:local="clr-namespace:Tango.MachineStudio.ColorCapture.Views" @@ -208,11 +209,51 @@ - Measures - + + Snapshots + + + + + + @@ -221,11 +262,29 @@ + + + + + + + - + - + + + + + + + + + + + + @@ -233,10 +292,10 @@ - + - + @@ -244,9 +303,13 @@ + + + + - + @@ -303,10 +366,42 @@ - + + + + diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ColorCapture/packages.config b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ColorCapture/packages.config index 37e4dd902..d0d0f7914 100644 --- a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ColorCapture/packages.config +++ b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ColorCapture/packages.config @@ -7,4 +7,5 @@ + \ No newline at end of file -- cgit v1.3.1