Skip to content

Commit

Permalink
Added copy action for recipe names.
Browse files Browse the repository at this point in the history
Improved ocr by mapping incorrect text.
  • Loading branch information
josdemmers committed Mar 20, 2022
1 parent 8d604cc commit 802db76
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 12 deletions.
14 changes: 14 additions & 0 deletions NewWorldCompanion.Entities/OcrMapping.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NewWorldCompanion.Entities
{
public class OcrMapping
{
public string key { get; set; } = string.Empty;
public string value { get; set; } = string.Empty;
}
}
30 changes: 28 additions & 2 deletions NewWorldCompanion.Services/OcrHandler.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
using NewWorldCompanion.Events;
using NewWorldCompanion.Entities;
using NewWorldCompanion.Events;
using NewWorldCompanion.Interfaces;
using Prism.Events;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text.Json;
using TesserNet;

namespace NewWorldCompanion.Services
Expand All @@ -12,6 +17,8 @@ public class OcrHandler : IOcrHandler
private readonly IEventAggregator _eventAggregator;
private readonly IScreenProcessHandler _screenProcessHandler;

private List<OcrMapping> _ocrMappings = new List<OcrMapping>();

private bool _isBusy = false;
private string _ocrText = string.Empty;

Expand All @@ -28,6 +35,8 @@ public OcrHandler(IEventAggregator eventAggregator, IScreenProcessHandler screen
// Init services
_screenProcessHandler = screenProcessHandler;

// Init ocr mappings
UpdateOcrMappings();
}

#endregion
Expand Down Expand Up @@ -55,7 +64,9 @@ private void HandleOcrImageReadyEvent()
{
Image image = Image.FromFile(@"ocrimages\itemname.png");
Tesseract tesseract = new Tesseract();
OcrText = tesseract.Read(image).Trim().Replace('\n', ' ');
string ocrText = tesseract.Read(image).Trim().Replace('\n', ' ');
var mapping = _ocrMappings.FirstOrDefault(m => m.key.Equals(ocrText), new OcrMapping{ key = ocrText, value = ocrText });
OcrText = mapping.value;

image.Dispose();
tesseract.Dispose();
Expand All @@ -78,6 +89,21 @@ private void HandleOcrImageReadyEvent()

#region Methods

private void UpdateOcrMappings()
{
try
{
_ocrMappings.Clear();
string fileName = "Config/OcrMappings.json";
if (File.Exists(fileName))
{
using FileStream stream = File.OpenRead(fileName);
_ocrMappings = JsonSerializer.Deserialize<List<OcrMapping>>(stream) ?? new List<OcrMapping>();
}
}
catch (Exception){}
}

#endregion
}
}
1 change: 0 additions & 1 deletion NewWorldCompanion.Services/OverlayHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@ private void Timer_Tick(object? sender, EventArgs e)
private void HandleOcrTextReadyEvent()
{
_itemName = _ocrHandler.OcrText;
//TODO Cleanup name. For example Toilvium -> Tolvium
if (!_itemName.Equals(_itemNamePrevious))
{
_priceManager.UpdatePriceData(_itemName);
Expand Down
5 changes: 4 additions & 1 deletion NewWorldCompanion.Services/ScreenProcessHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ private void ProcessImage(Mat img)
(
rectangleList[0].MinAreaRect().X + rectangleList[0].MinAreaRect().Width + 5,
rectangleList[0].MinAreaRect().Y,
(int)(rectangleList[0].MinAreaRect().Width * 3.25),
(int)(rectangleList[0].MinAreaRect().Width * 3.30),
rectangleList[0].MinAreaRect().Height - 25
);

Expand All @@ -191,6 +191,9 @@ private void ProcessImage(Mat img)

try
{
// Validate width
roiRectangle.Width = (roiRectangle.X + roiRectangle.Width) <= img.Width ? roiRectangle.Width : roiRectangle.Width - (roiRectangle.X + roiRectangle.Width - img.Width);

crop = new Mat(img, roiRectangle);
RoiImage = crop.ToBitmap();
_eventAggregator.GetEvent<RoiImageReadyEvent>().Publish();
Expand Down
30 changes: 30 additions & 0 deletions NewWorldCompanion/Config/OcrMappings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[
{
"key": "Giintstrands",
"value": "Glintstrands"
},
{
"key": "Hearty Meai",
"value": "Hearty Meal"
},
{
"key": "Orichaleum",
"value": "Orichalcum"
},
{
"key": "Recipe: Meatioaf",
"value": "Recipe: Meatloaf"
},
{
"key": "Schematic: lron-bound Wall-mounted Lantern",
"value": "Schematic: Iron-bound Wall-mounted Lantern"
},
{
"key": "Schematic: Maple Smail Table",
"value": "Schematic: Maple Small Table"
},
{
"key": "Toilvium",
"value": "Tolvium"
}
]
10 changes: 10 additions & 0 deletions NewWorldCompanion/NewWorldCompanion.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,14 @@
<SplashScreen Include="Images\splash.png" />
</ItemGroup>

<ItemGroup>
<Folder Include="Config\" />
</ItemGroup>

<ItemGroup>
<None Update="Config\OcrMappings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
19 changes: 17 additions & 2 deletions NewWorldCompanion/ViewModels/Tabs/CraftingViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public CraftingViewModel(IEventAggregator eventAggregator, ICraftingRecipeManage
// Init View commands
CraftingRecipeLearnedCommand = new DelegateCommand<object>(CraftingRecipeLearnedExecute);
VisitNwdbCommand = new DelegateCommand<object>(VisitNwdbExecute);
CopyRecipeNameCommand = new DelegateCommand<object>(CopyRecipeNameExecute);

// Init filter views
CreateCraftingRecipesFilteredView();
Expand All @@ -98,6 +99,7 @@ public CraftingViewModel(IEventAggregator eventAggregator, ICraftingRecipeManage

public DelegateCommand<object> CraftingRecipeLearnedCommand { get; }
public DelegateCommand<object> VisitNwdbCommand { get; }
public DelegateCommand<object> CopyRecipeNameCommand { get; }

public ObservableCollection<CraftingRecipe> CraftingRecipes { get => _craftingRecipes; set => _craftingRecipes = value; }
public ListCollectionView? CraftingRecipesFiltered { get; private set; }
Expand Down Expand Up @@ -247,8 +249,6 @@ private void HandleOcrTextReadyEvent()
// As the view is accessed by the UI it will need to be created on the UI thread
Application.Current?.Dispatcher?.Invoke(() =>
{
//TODO Cleanup name. For example Toilvium -> Tolvium

// Only set filter for recipe items.
if (CraftingRecipes.Any(recipe => recipe.Localisation.StartsWith(_ocrHandler.OcrText)))
{
Expand All @@ -274,6 +274,21 @@ private void VisitNwdbExecute(object url)
Process.Start(new ProcessStartInfo(url as string ?? string.Empty) { UseShellExecute = true });
}

private void CopyRecipeNameExecute(object obj)
{
// Note: New World does not accept the following special characters when using copy/paste: ':', '''.
try
{
var recipe = (CraftingRecipe)obj;
string recipeName = recipe.Localisation.Contains(':') ?
recipe.Localisation.Substring(recipe.Localisation.IndexOf(':') + 1) :
recipe.Localisation;

System.Windows.Clipboard.SetText(recipeName.Trim());
}
catch (Exception) { }
}

private void CreateCraftingRecipesFilteredView()
{
// As the view is accessed by the UI it will need to be created on the UI thread
Expand Down
6 changes: 3 additions & 3 deletions NewWorldCompanion/Views/Tabs/CooldownView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@
</TextBlock.Text>
</TextBlock>
</Label>
<Button BorderThickness="0" Background="Transparent" Command="{Binding Path=DataContext.RefreshCooldownCommand, RelativeSource={RelativeSource AncestorType={x:Type local:CooldownView}}}" CommandParameter="{Binding}">
<Button BorderThickness="0" Background="Transparent" Focusable="False" Command="{Binding Path=DataContext.RefreshCooldownCommand, RelativeSource={RelativeSource AncestorType={x:Type local:CooldownView}}}" CommandParameter="{Binding}">
<iconPacks:PackIconMaterial Kind="Refresh" />
</Button>
<Button BorderThickness="0" Background="Transparent" Command="{Binding Path=DataContext.ConfigCooldownCommand, RelativeSource={RelativeSource AncestorType={x:Type local:CooldownView}}}" CommandParameter="{Binding}">
<Button BorderThickness="0" Background="Transparent" Focusable="False" Command="{Binding Path=DataContext.ConfigCooldownCommand, RelativeSource={RelativeSource AncestorType={x:Type local:CooldownView}}}" CommandParameter="{Binding}">
<iconPacks:PackIconMaterial Kind="Cog" />
</Button>
<Button BorderThickness="0" Background="Transparent" Command="{Binding Path=DataContext.DeleteCooldownCommand, RelativeSource={RelativeSource AncestorType={x:Type local:CooldownView}}}" CommandParameter="{Binding}">
<Button BorderThickness="0" Background="Transparent" Focusable="False" Command="{Binding Path=DataContext.DeleteCooldownCommand, RelativeSource={RelativeSource AncestorType={x:Type local:CooldownView}}}" CommandParameter="{Binding}">
<iconPacks:PackIconMaterial Kind="DeleteForever" />
</Button>
</StackPanel>
Expand Down
8 changes: 7 additions & 1 deletion NewWorldCompanion/Views/Tabs/CraftingView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:iconPacks="http://metro.mahapps.com/winfx/xaml/iconpacks"
xmlns:local="clr-namespace:NewWorldCompanion.Views.Tabs"
xmlns:converters="clr-namespace:NewWorldCompanion.Converters"
xmlns:mah="http://metro.mahapps.com/winfx/xaml/controls"
Expand Down Expand Up @@ -108,10 +109,15 @@
Orientation="Vertical"
Margin="5"
Visibility="{Binding SelectedCraftingRecipe, Converter={StaticResource EmptyToVisibilityConverter}}">
<CheckBox Content="{Binding SelectedCraftingRecipe.Localisation}"
<StackPanel Orientation="Horizontal">
<CheckBox Content="{Binding SelectedCraftingRecipe.Localisation}"
IsChecked="{Binding SelectedCraftingRecipe.Learned}"
Command="{Binding CraftingRecipeLearnedCommand}"
CommandParameter="{Binding IsChecked, RelativeSource={RelativeSource Self}}"/>
<Button BorderThickness="0" Background="Transparent" Focusable="False" Command="{Binding CopyRecipeNameCommand}" CommandParameter="{Binding SelectedCraftingRecipe}">
<iconPacks:PackIconMaterial Kind="ContentCopy" />
</Button>
</StackPanel>
<Label Content="{Binding SelectedCraftingRecipe.Tradeskill}" />
<Label>
<Hyperlink Command="{Binding VisitNwdbCommand}" CommandParameter="{Binding SelectedCraftingRecipe.Url}">
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.3.0</FileVersion>
<Version>1.0.3.0</Version>
<FileVersion>1.0.3.1</FileVersion>
<Version>1.0.3.1</Version>
<Copyright>Copyright © 2022</Copyright>
</PropertyGroup>
</Project>

0 comments on commit 802db76

Please sign in to comment.