From 0c89d460eb6d1cd8a19180d71cd317c0b2ecf664 Mon Sep 17 00:00:00 2001 From: Jos Date: Wed, 27 Apr 2022 16:40:20 +0200 Subject: [PATCH] Added count for storage items --- NewWorldCompanion.Constants/EmguConstants.cs | 3 ++ NewWorldCompanion.Entities/Item.cs | 2 +- NewWorldCompanion.Entities/SettingsNWC.cs | 3 ++ NewWorldCompanion.Interfaces/IOcrHandler.cs | 1 + NewWorldCompanion.Services/OcrHandler.cs | 7 ++- NewWorldCompanion.Services/PriceManager.cs | 3 +- .../ScreenProcessHandler.cs | 39 ++------------- .../Debug/DebugScreenCountOCRViewModel.cs | 12 +++++ .../Tabs/Debug/DebugScreenOCRViewModel.cs | 49 +++++++++++++++++++ .../ViewModels/Tabs/StorageViewModel.cs | 12 ++--- .../Tabs/Debug/DebugScreenCountOCRView.xaml | 13 ++++- .../Views/Tabs/Debug/DebugScreenOCRView.xaml | 30 ++++++++++-- NewWorldCompanion/Views/Tabs/StorageView.xaml | 2 +- NewWorldCompanion/common.props | 4 +- 14 files changed, 128 insertions(+), 52 deletions(-) diff --git a/NewWorldCompanion.Constants/EmguConstants.cs b/NewWorldCompanion.Constants/EmguConstants.cs index d78dc80..f4afe74 100644 --- a/NewWorldCompanion.Constants/EmguConstants.cs +++ b/NewWorldCompanion.Constants/EmguConstants.cs @@ -14,5 +14,8 @@ public class EmguConstants // OCR public const int DefaultThresholdMin = 90; public const int DefaultThresholdMax = 255; + public const int DefaultThresholdMaxR = 200; + public const int DefaultThresholdMaxG = 235; + public const int DefaultThresholdMaxB = 255; } } \ No newline at end of file diff --git a/NewWorldCompanion.Entities/Item.cs b/NewWorldCompanion.Entities/Item.cs index 71566bf..cc9da95 100644 --- a/NewWorldCompanion.Entities/Item.cs +++ b/NewWorldCompanion.Entities/Item.cs @@ -9,7 +9,7 @@ namespace NewWorldCompanion.Entities { public class Item { - public int Count { get; set; } = 0; + public int Count { get; set; } = 1; /// Unique identifier for items. Nwdb uses this to identify items public string ItemID { get; set; } = string.Empty; public string Localisation { get; set; } = string.Empty; diff --git a/NewWorldCompanion.Entities/SettingsNWC.cs b/NewWorldCompanion.Entities/SettingsNWC.cs index 27c7b66..15a1273 100644 --- a/NewWorldCompanion.Entities/SettingsNWC.cs +++ b/NewWorldCompanion.Entities/SettingsNWC.cs @@ -23,5 +23,8 @@ public class SettingsNWC // OCR public int EmguThresholdMin { get; set; } = 90; public int EmguThresholdMax { get; set; } = 255; + public int EmguThresholdMaxR { get; set; } = 200; + public int EmguThresholdMaxG { get; set; } = 235; + public int EmguThresholdMaxB { get; set; } = 255; } } diff --git a/NewWorldCompanion.Interfaces/IOcrHandler.cs b/NewWorldCompanion.Interfaces/IOcrHandler.cs index 8face63..f3a70f4 100644 --- a/NewWorldCompanion.Interfaces/IOcrHandler.cs +++ b/NewWorldCompanion.Interfaces/IOcrHandler.cs @@ -4,5 +4,6 @@ public interface IOcrHandler { string OcrText { get; } string OcrTextCount { get; } + string OcrTextCountRaw { get; } } } diff --git a/NewWorldCompanion.Services/OcrHandler.cs b/NewWorldCompanion.Services/OcrHandler.cs index 0411eae..9781843 100644 --- a/NewWorldCompanion.Services/OcrHandler.cs +++ b/NewWorldCompanion.Services/OcrHandler.cs @@ -25,6 +25,7 @@ public class OcrHandler : IOcrHandler private string _ocrText = string.Empty; private string _ocrTextCount = string.Empty; + private string _ocrTextCountRaw = string.Empty; // Start of Constructor region @@ -53,6 +54,7 @@ public OcrHandler(IEventAggregator eventAggregator, INewWorldDataStore newWorldD public string OcrText { get => _ocrText; set => _ocrText = value; } public string OcrTextCount { get => _ocrTextCount; set => _ocrTextCount = value; } + public string OcrTextCountRaw { get => _ocrTextCountRaw; set => _ocrTextCountRaw = value; } #endregion @@ -97,7 +99,10 @@ private void HandleOcrImageCountReadyEvent() Image image = Image.FromFile(@"ocrimages\itemcount.png"); Tesseract tesseract = new Tesseract(); string ocrText = tesseract.Read(image).Trim().Replace('\n', ' '); - OcrTextCount = ocrText; + OcrTextCountRaw = ocrText; + // Remove non-numeric characters + ocrText = new string(ocrText.Where(c => char.IsDigit(c)).ToArray()); + OcrTextCount = string.IsNullOrWhiteSpace(ocrText) ? OcrTextCount : ocrText; image.Dispose(); tesseract.Dispose(); diff --git a/NewWorldCompanion.Services/PriceManager.cs b/NewWorldCompanion.Services/PriceManager.cs index 5de997d..bc054fd 100644 --- a/NewWorldCompanion.Services/PriceManager.cs +++ b/NewWorldCompanion.Services/PriceManager.cs @@ -165,9 +165,10 @@ public void UpdatePriceData(string itemName) // Always remove from queue, even with exceptions. _priceRequestQueue.RemoveAll(item => item.Equals(itemName)); _priceRequestQueueBusy = false; + Task.Delay(100).Wait(); + if (_priceRequestQueue.Any()) { - Task.Delay(100).Wait(); UpdatePriceData(_priceRequestQueue.First()); } }); diff --git a/NewWorldCompanion.Services/ScreenProcessHandler.cs b/NewWorldCompanion.Services/ScreenProcessHandler.cs index b1c576e..8d07f61 100644 --- a/NewWorldCompanion.Services/ScreenProcessHandler.cs +++ b/NewWorldCompanion.Services/ScreenProcessHandler.cs @@ -65,6 +65,9 @@ public ScreenProcessHandler(IEventAggregator eventAggregator, ISettingsManager s public int HysteresisUpper { get => _settingsManager.Settings.EmguHysteresisUpper; } public int ThresholdMin { get => _settingsManager.Settings.EmguThresholdMin; } public int ThresholdMax { get => _settingsManager.Settings.EmguThresholdMax; } + public int ThresholdOcrMaxR { get => _settingsManager.Settings.EmguThresholdMaxR; } + public int ThresholdOcrMaxG { get => _settingsManager.Settings.EmguThresholdMaxG; } + public int ThresholdOcrMaxB { get => _settingsManager.Settings.EmguThresholdMaxB; } public Bitmap? ProcessedImage { get => _processedImage; set => _processedImage = value; } public Bitmap? RoiImage { get => _roiImage; set => _roiImage = value; } //public Bitmap? OcrImage { get => _roiImage; set => _roiImage = value; } @@ -270,20 +273,7 @@ private void ProcessImageCountOCR(Mat img) { Mat imgFilter = new Mat(img.Size, DepthType.Cv8U, 3); - // Convert the image to grayscale - CvInvoke.CvtColor(img, imgFilter, ColorConversion.Bgr2Gray); - - // Apply threshold - //CvInvoke.Threshold(imgFilter, imgFilter, 0, 255, ThresholdType.Otsu); - //CvInvoke.Threshold(imgFilter, imgFilter, ThresholdMin, ThresholdMax, ThresholdType.Binary); - CvInvoke.Threshold(imgFilter, imgFilter, ThresholdMin, ThresholdMax, ThresholdType.BinaryInv); - - // Thinning and Skeletonization - //Mat element = CvInvoke.GetStructuringElement(ElementShape.Rectangle, new System.Drawing.Size(2, 2), new System.Drawing.Point(-1, -1)); - //CvInvoke.Erode(imgFilter, imgFilter, element, new System.Drawing.Point(-1, -1), 1, BorderType.Constant, new MCvScalar(255, 255, 255)); - - // Filter out the noise - //CvInvoke.GaussianBlur(imgFilter, imgFilter, new System.Drawing.Size(0, 0), 1, 0, BorderType.Default); + CvInvoke.InRange(img, new ScalarArray(new MCvScalar(0, 0, 0)), new ScalarArray(new MCvScalar(ThresholdOcrMaxR, ThresholdOcrMaxG, ThresholdOcrMaxB)), imgFilter); if (!Directory.Exists(@"ocrimages\")) { @@ -294,12 +284,6 @@ private void ProcessImageCountOCR(Mat img) OcrImageCount = imgFilter.ToBitmap(); img.Save(@"ocrimages\itemcountraw.png"); imgFilter.Save(@"ocrimages\itemcount.png"); - - if(!File.Exists(@"ocrimages\itemcountdebug.png")) - { - img.Save(@"ocrimages\itemcountdebug.png"); - } - _eventAggregator.GetEvent().Publish(); } catch (Exception) { } @@ -320,21 +304,6 @@ public void ProcessImageCountOCRDebug(int minR, int minG, int minB, int maxR, in CvInvoke.InRange(img, new ScalarArray(new MCvScalar(minR, minG, minB)), new ScalarArray(new MCvScalar(maxR, maxG, maxB)), imgFilter); - // Convert the image to grayscale - //CvInvoke.CvtColor(img, imgFilter, ColorConversion.Bgr2Gray); - - // Apply threshold - //CvInvoke.Threshold(imgFilter, imgFilter, 0, 255, ThresholdType.Otsu); - //CvInvoke.Threshold(imgFilter, imgFilter, ThresholdMin, ThresholdMax, ThresholdType.Binary); - //CvInvoke.Threshold(imgFilter, imgFilter, ThresholdMin, ThresholdMax, ThresholdType.BinaryInv); - - // Thinning and Skeletonization - //Mat element = CvInvoke.GetStructuringElement(ElementShape.Rectangle, new System.Drawing.Size(2, 2), new System.Drawing.Point(-1, -1)); - //CvInvoke.Erode(imgFilter, imgFilter, element, new System.Drawing.Point(-1, -1), 1, BorderType.Constant, new MCvScalar(255, 255, 255)); - - // Filter out the noise - //CvInvoke.GaussianBlur(imgFilter, imgFilter, new System.Drawing.Size(0, 0), 1, 0, BorderType.Default); - if (!Directory.Exists(@"ocrimages\")) { DirectoryInfo directoryInfo = Directory.CreateDirectory(@"ocrimages\"); diff --git a/NewWorldCompanion/ViewModels/Tabs/Debug/DebugScreenCountOCRViewModel.cs b/NewWorldCompanion/ViewModels/Tabs/Debug/DebugScreenCountOCRViewModel.cs index 7734f20..96163bc 100644 --- a/NewWorldCompanion/ViewModels/Tabs/Debug/DebugScreenCountOCRViewModel.cs +++ b/NewWorldCompanion/ViewModels/Tabs/Debug/DebugScreenCountOCRViewModel.cs @@ -20,6 +20,7 @@ public class DebugScreenCountOCRViewModel : BindableBase private readonly IOcrHandler _ocrHandler; private string _itemCount = string.Empty; + private string _itemCountRaw = string.Empty; private BitmapSource? _ocrImageCount = null; private BitmapSource? _ocrImageCountRaw = null; @@ -67,6 +68,16 @@ public string ItemCount } } + public string ItemCountRaw + { + get => _itemCountRaw; + set + { + _itemCountRaw = value; + RaisePropertyChanged(nameof(ItemCountRaw)); + } + } + public int ThresholdMinR { get => _thresholdMinR; @@ -169,6 +180,7 @@ private void HandleOcrTextCountReadyEvent() Application.Current?.Dispatcher?.Invoke(() => { ItemCount = _ocrHandler.OcrTextCount; + ItemCountRaw = _ocrHandler.OcrTextCountRaw; }); } diff --git a/NewWorldCompanion/ViewModels/Tabs/Debug/DebugScreenOCRViewModel.cs b/NewWorldCompanion/ViewModels/Tabs/Debug/DebugScreenOCRViewModel.cs index 72568b8..901610f 100644 --- a/NewWorldCompanion/ViewModels/Tabs/Debug/DebugScreenOCRViewModel.cs +++ b/NewWorldCompanion/ViewModels/Tabs/Debug/DebugScreenOCRViewModel.cs @@ -19,6 +19,7 @@ public class DebugScreenOCRViewModel : BindableBase private string _itemName = string.Empty; private string _itemCount = string.Empty; + private string _itemCountRaw = string.Empty; private BitmapSource? _ocrImage = null; private BitmapSource? _ocrImageCount = null; @@ -66,6 +67,7 @@ public int ThresholdMin RaisePropertyChanged(nameof(ThresholdMin)); } } + public int ThresholdMax { get => _settingsManager.Settings.EmguThresholdMax; @@ -77,6 +79,39 @@ public int ThresholdMax } } + public int ThresholdMaxR + { + get => _settingsManager.Settings.EmguThresholdMaxR; + set + { + _settingsManager.Settings.EmguThresholdMaxR = value; + _settingsManager.SaveSettings(); + RaisePropertyChanged(nameof(ThresholdMaxR)); + } + } + + public int ThresholdMaxG + { + get => _settingsManager.Settings.EmguThresholdMaxG; + set + { + _settingsManager.Settings.EmguThresholdMaxG = value; + _settingsManager.SaveSettings(); + RaisePropertyChanged(nameof(ThresholdMaxG)); + } + } + + public int ThresholdMaxB + { + get => _settingsManager.Settings.EmguThresholdMaxB; + set + { + _settingsManager.Settings.EmguThresholdMaxB = value; + _settingsManager.SaveSettings(); + RaisePropertyChanged(nameof(ThresholdMaxB)); + } + } + public string ItemName { get => _itemName; @@ -97,6 +132,16 @@ public string ItemCount } } + public string ItemCountRaw + { + get => _itemCountRaw; + set + { + _itemCountRaw = value; + RaisePropertyChanged(nameof(ItemCountRaw)); + } + } + public BitmapSource? OcrImage { get => _ocrImage; @@ -156,6 +201,7 @@ private void HandleOcrTextCountReadyEvent() Application.Current?.Dispatcher?.Invoke(() => { ItemCount = _ocrHandler.OcrTextCount; + ItemCountRaw = _ocrHandler.OcrTextCountRaw; }); } @@ -169,6 +215,9 @@ private void RestoreDefaultsExecute() { ThresholdMin = EmguConstants.DefaultThresholdMin; ThresholdMax = EmguConstants.DefaultThresholdMax; + ThresholdMaxR = EmguConstants.DefaultThresholdMaxR; + ThresholdMaxG = EmguConstants.DefaultThresholdMaxG; + ThresholdMaxB = EmguConstants.DefaultThresholdMaxB; } private void CopyItemNameExecute() diff --git a/NewWorldCompanion/ViewModels/Tabs/StorageViewModel.cs b/NewWorldCompanion/ViewModels/Tabs/StorageViewModel.cs index 1f06126..4ad2d94 100644 --- a/NewWorldCompanion/ViewModels/Tabs/StorageViewModel.cs +++ b/NewWorldCompanion/ViewModels/Tabs/StorageViewModel.cs @@ -162,9 +162,11 @@ private void HandleOcrTextReadyEvent() var item = Items.FirstOrDefault(i => i.Storage.Equals(SelectedStorage) && i.ItemID.Equals(itemId)); if (item != null) { - // TODO item count - //item.Count = int.TryParse(_ocrHandler.OcrTextCount, out int itemCount) ? itemCount : 0; - item.Count = 0; + item.Count = int.TryParse(_ocrHandler.OcrTextCount, out int itemCount) ? itemCount : 1; + Application.Current?.Dispatcher?.Invoke(() => + { + ItemsFiltered?.Refresh(); + }); } } else @@ -174,9 +176,7 @@ private void HandleOcrTextReadyEvent() // Add item Items.Add(new Item() { - // TODO item count - //Count = int.TryParse(_ocrHandler.OcrTextCount, out int itemCount) ? itemCount : 0; - Count = 0, + Count = int.TryParse(_ocrHandler.OcrTextCount, out int itemCount) ? itemCount : 1, ItemID = itemId, Name = itemDefinition.Name, Localisation = _itemName, diff --git a/NewWorldCompanion/Views/Tabs/Debug/DebugScreenCountOCRView.xaml b/NewWorldCompanion/Views/Tabs/Debug/DebugScreenCountOCRView.xaml index 1d39932..5e9fe63 100644 --- a/NewWorldCompanion/Views/Tabs/Debug/DebugScreenCountOCRView.xaml +++ b/NewWorldCompanion/Views/Tabs/Debug/DebugScreenCountOCRView.xaml @@ -48,7 +48,18 @@