Skip to content

Commit

Permalink
Added count for storage items
Browse files Browse the repository at this point in the history
  • Loading branch information
josdemmers committed Apr 27, 2022
1 parent 7347a02 commit 0c89d46
Show file tree
Hide file tree
Showing 14 changed files with 128 additions and 52 deletions.
3 changes: 3 additions & 0 deletions NewWorldCompanion.Constants/EmguConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
2 changes: 1 addition & 1 deletion NewWorldCompanion.Entities/Item.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace NewWorldCompanion.Entities
{
public class Item
{
public int Count { get; set; } = 0;
public int Count { get; set; } = 1;
/// <value>Unique identifier for items. Nwdb uses this to identify items</value>
public string ItemID { get; set; } = string.Empty;
public string Localisation { get; set; } = string.Empty;
Expand Down
3 changes: 3 additions & 0 deletions NewWorldCompanion.Entities/SettingsNWC.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
1 change: 1 addition & 0 deletions NewWorldCompanion.Interfaces/IOcrHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ public interface IOcrHandler
{
string OcrText { get; }
string OcrTextCount { get; }
string OcrTextCountRaw { get; }
}
}
7 changes: 6 additions & 1 deletion NewWorldCompanion.Services/OcrHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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();
Expand Down
3 changes: 2 additions & 1 deletion NewWorldCompanion.Services/PriceManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
});
Expand Down
39 changes: 4 additions & 35 deletions NewWorldCompanion.Services/ScreenProcessHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down Expand Up @@ -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\"))
{
Expand All @@ -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<OcrImageCountReadyEvent>().Publish();
}
catch (Exception) { }
Expand All @@ -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\");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -67,6 +68,16 @@ public string ItemCount
}
}

public string ItemCountRaw
{
get => _itemCountRaw;
set
{
_itemCountRaw = value;
RaisePropertyChanged(nameof(ItemCountRaw));
}
}

public int ThresholdMinR
{
get => _thresholdMinR;
Expand Down Expand Up @@ -169,6 +180,7 @@ private void HandleOcrTextCountReadyEvent()
Application.Current?.Dispatcher?.Invoke(() =>
{
ItemCount = _ocrHandler.OcrTextCount;
ItemCountRaw = _ocrHandler.OcrTextCountRaw;
});
}

Expand Down
49 changes: 49 additions & 0 deletions NewWorldCompanion/ViewModels/Tabs/Debug/DebugScreenOCRViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -66,6 +67,7 @@ public int ThresholdMin
RaisePropertyChanged(nameof(ThresholdMin));
}
}

public int ThresholdMax
{
get => _settingsManager.Settings.EmguThresholdMax;
Expand All @@ -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;
Expand All @@ -97,6 +132,16 @@ public string ItemCount
}
}

public string ItemCountRaw
{
get => _itemCountRaw;
set
{
_itemCountRaw = value;
RaisePropertyChanged(nameof(ItemCountRaw));
}
}

public BitmapSource? OcrImage
{
get => _ocrImage;
Expand Down Expand Up @@ -156,6 +201,7 @@ private void HandleOcrTextCountReadyEvent()
Application.Current?.Dispatcher?.Invoke(() =>
{
ItemCount = _ocrHandler.OcrTextCount;
ItemCountRaw = _ocrHandler.OcrTextCountRaw;
});
}

Expand All @@ -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()
Expand Down
12 changes: 6 additions & 6 deletions NewWorldCompanion/ViewModels/Tabs/StorageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down
13 changes: 12 additions & 1 deletion NewWorldCompanion/Views/Tabs/Debug/DebugScreenCountOCRView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,18 @@
<StackPanel Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="5">
<Label Content="Item count: "/>
<StackPanel Orientation="Horizontal">
<Label Content="{Binding ItemCount}"/>
<Label>
<Label.Content>
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} ({1})">
<Binding Path="ItemCount" />
<Binding Path="ItemCountRaw" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</Label.Content>
</Label>
<Button BorderThickness="0" Background="Transparent" Focusable="False" Command="{Binding CopyItemCountCommand}">
<iconPacks:PackIconMaterial Kind="ContentCopy" />
</Button>
Expand Down
30 changes: 26 additions & 4 deletions NewWorldCompanion/Views/Tabs/Debug/DebugScreenOCRView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="200"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="200"/>
<ColumnDefinition Width="*"/>
Expand All @@ -26,16 +29,24 @@
<Slider Grid.Row="0" Grid.Column="1" Minimum="0" Maximum="255" Value="{Binding ThresholdMin}" IsSnapToTickEnabled="True" TickFrequency="1" AutoToolTipPlacement="TopLeft" AutoToolTipPrecision="0"/>
<Label Grid.Row="1" Grid.Column="0" Content="Max Threshold" />
<Slider Grid.Row="1" Grid.Column="1" Minimum="0" Maximum="255" Value="{Binding ThresholdMax}" IsSnapToTickEnabled="True" TickFrequency="1" AutoToolTipPlacement="TopLeft" AutoToolTipPrecision="0"/>
<Button Grid.Row="2" Grid.Column="0" Content="Restore defaults" Command="{Binding RestoreDefaultsCommand}"/>

<StackPanel Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="3" Orientation="Vertical">
<Label Grid.Row="0" Grid.Column="2" Content="Max Threshold R" />
<Slider Grid.Row="0" Grid.Column="3" Minimum="0" Maximum="255" Value="{Binding ThresholdMaxR}" IsSnapToTickEnabled="True" TickFrequency="1" AutoToolTipPlacement="TopLeft" AutoToolTipPrecision="0"/>
<Label Grid.Row="1" Grid.Column="2" Content="Max Threshold G" />
<Slider Grid.Row="1" Grid.Column="3" Minimum="0" Maximum="255" Value="{Binding ThresholdMaxG}" IsSnapToTickEnabled="True" TickFrequency="1" AutoToolTipPlacement="TopLeft" AutoToolTipPrecision="0"/>
<Label Grid.Row="2" Grid.Column="2" Content="Max Threshold B" />
<Slider Grid.Row="2" Grid.Column="3" Minimum="0" Maximum="255" Value="{Binding ThresholdMaxB}" IsSnapToTickEnabled="True" TickFrequency="1" AutoToolTipPlacement="TopLeft" AutoToolTipPrecision="0"/>

<Button Grid.Row="3" Grid.Column="0" Content="Restore defaults" Command="{Binding RestoreDefaultsCommand}"/>

<StackPanel Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="5" Orientation="Vertical">
<StackPanel Orientation="Horizontal">
<Image Height="150" HorizontalAlignment="Left" VerticalAlignment="top" Source="{Binding OcrImage}"/>
<Image Width="100" HorizontalAlignment="Left" VerticalAlignment="top" Source="{Binding OcrImageCount}"/>
</StackPanel>
</StackPanel>

<StackPanel Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="3">
<StackPanel Grid.Row="5" Grid.Column="0" Grid.ColumnSpan="5">
<Label Content="Item name: "/>
<StackPanel Orientation="Horizontal">
<Label Content="{Binding ItemName}"/>
Expand All @@ -45,7 +56,18 @@
</StackPanel>
<Label Content="Item count: "/>
<StackPanel Orientation="Horizontal">
<Label Content="{Binding ItemCount}"/>
<Label>
<Label.Content>
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} ({1})">
<Binding Path="ItemCount" />
<Binding Path="ItemCountRaw" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</Label.Content>
</Label>
<Button BorderThickness="0" Background="Transparent" Focusable="False" Command="{Binding CopyItemCountCommand}">
<iconPacks:PackIconMaterial Kind="ContentCopy" />
</Button>
Expand Down
2 changes: 1 addition & 1 deletion NewWorldCompanion/Views/Tabs/StorageView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@

<DataGrid Grid.Row="3" Grid.Column="0" ItemsSource="{Binding ItemsFiltered}" AutoGenerateColumns="False">
<DataGrid.Columns>
<!--<DataGridTextColumn Header="Count" Binding="{Binding Count}"/>-->
<DataGridTextColumn Header="Count" Binding="{Binding Count}"/>
<DataGridTextColumn Header="Item" Binding="{Binding Localisation}"/>
<DataGridTextColumn Header="Location" Binding="{Binding Storage}"/>
</DataGrid.Columns>
Expand Down
4 changes: 2 additions & 2 deletions NewWorldCompanion/common.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>
<PropertyGroup>
<FileVersion>1.0.4.3</FileVersion>
<Version>1.0.4.3</Version>
<FileVersion>1.0.4.4</FileVersion>
<Version>1.0.4.4</Version>
<Copyright>Copyright © 2022</Copyright>
</PropertyGroup>
</Project>

0 comments on commit 0c89d46

Please sign in to comment.