auto genius invokation: optimize dice count OCR & dice recognition is sorted by character element priority #165

This commit is contained in:
huiyadanli
2024-01-13 22:49:14 +08:00
parent 179d64dc64
commit 7883cc2d29
4 changed files with 61 additions and 13 deletions

View File

@@ -2,10 +2,12 @@
using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BetterGenshinImpact.Core.Recognition;
using BetterGenshinImpact.GameTask.AutoGeniusInvokation.Model;
namespace BetterGenshinImpact.GameTask.AutoGeniusInvokation.Assets
{
@@ -151,6 +153,8 @@ namespace BetterGenshinImpact.GameTask.AutoGeniusInvokation.Assets
{ "geo", GameTaskManager.LoadAssetImage("AutoGeniusInvokation", @"dice\action_geo.png", ImreadModes.Color) },
{ "omni", GameTaskManager.LoadAssetImage("AutoGeniusInvokation", @"dice\action_omni.png", ImreadModes.Color) },
};
var msg = ActionPhaseDiceMats.Aggregate("", (current, kvp) => current + $"{kvp.Key.ToElementalType().ToChinese()}| ");
Debug.WriteLine($"默认骰子排序:{msg}");
}
}
}

View File

@@ -30,7 +30,7 @@ public partial class AutoGeniusInvokationConfig : ObservableObject
/// <summary>
/// 骰子数量文字识别区域
/// </summary>
public Rect MyDiceCountRect { get; set; } = new(58, 632, 45, 47); // 42,47
public Rect MyDiceCountRect { get; } = new(68, 642, 25, 31); // 42,47
///// <summary>
///// 角色卡牌区域向左扩展距离包含HP区域

View File

@@ -113,16 +113,16 @@ public class GeniusInvokationControl
{
return bitmap.ToMat();
}
Sleep(20);
}
throw new RetryException("尝试多次后,截图失败!");
}
else
{
return bitmap.ToMat();
}
}
public Mat CaptureGameGreyMat()
@@ -187,6 +187,26 @@ public class GeniusInvokationControl
Sleep(1500);
}
public void SortActionPhaseDiceMats(HashSet<ElementalType> elementSet)
{
_assets.ActionPhaseDiceMats = _assets.ActionPhaseDiceMats.OrderByDescending(kvp =>
{
for (var i = 0; i < elementSet.Count; i++)
{
if (kvp.Key == elementSet.ElementAt(i).ToLowerString())
{
return i;
}
}
return -1;
})
.ToDictionary(x => x.Key, x => x.Value);
// 打印排序后的顺序
var msg = _assets.ActionPhaseDiceMats.Aggregate("", (current, kvp) => current + $"{kvp.Key.ToElementalType().ToChinese()}| ");
_logger.LogDebug("当前骰子排序:{Msg}", msg);
}
/// <summary>
/// 获取我方三个角色卡牌区域
/// </summary>
@@ -574,6 +594,7 @@ public class GeniusInvokationControl
// 最后一张牌在右侧,而不是中间
ClickExtension.Move(rect.X + rect.Width / 2d + 120, rect.Y + rect.Height - 50);
}
m.LeftButtonDown();
Sleep(100);
m = ClickExtension.Move(rect.X + rect.Width - 50, rect.Y + rect.Height / 2d);
@@ -1228,21 +1249,42 @@ public class GeniusInvokationControl
{
var srcMat = CaptureGameGreyMat();
var diceCountMap = new Mat(srcMat, _config.MyDiceCountRect);
var text = OcrFactory.Paddle.Ocr(diceCountMap);
text = text.Replace(" ", "");
_logger.LogInformation("通过OCR识别当前骰子数量: {Text}", text);
var text = OcrFactory.Paddle.OcrWithoutDetector(diceCountMap);
text = text.Replace(" ", "")
.Replace("①", "1")
.Replace("②", "2")
.Replace("③", "3")
.Replace("④", "4")
.Replace("⑤", "5")
.Replace("⑥", "6")
.Replace("⑦", "7")
.Replace("⑧", "8")
.Replace("⑨", "9")
.Replace("⑩", "10")
.Replace("⑪", "11")
.Replace("⑫", "12")
.Replace("⑬", "13")
.Replace("⑭", "14")
.Replace("⑮", "15");
if (string.IsNullOrWhiteSpace(text))
{
//Cv2.ImWrite($"log\\dice_count_empty{DateTime.Now:yyyy-MM-dd HHmmssffff}.jpg", diceCountMap);
_logger.LogWarning("通过OCR识别当前骰子数量结果为空,无影响");
#if DEBUG
Cv2.ImWrite($"log\\dice_count_empty{DateTime.Now:yyyy-MM-dd HHmmssffff}.jpg", diceCountMap);
#endif
return -10;
}
else if (Regex.IsMatch(text, @"^[0-9]+$"))
{
_logger.LogInformation("通过OCR识别当前骰子数量: {Text}", text);
return int.Parse(text);
}
else
{
//Cv2.ImWrite($"log\\dice_count_error_{DateTime.Now:yyyy-MM-dd HHmmssffff}.jpg", diceCountMap);
_logger.LogWarning("通过OCR识别当前骰子结果: {Text}", text);
#if DEBUG
Cv2.ImWrite($"log\\dice_count_error_{DateTime.Now:yyyy-MM-dd HHmmssffff}.jpg", diceCountMap);
#endif
return -10;
}
}

View File

@@ -1,16 +1,15 @@
using BetterGenshinImpact.GameTask.AutoGeniusInvokation.Exception;
using BetterGenshinImpact.Core.Recognition.OpenCv;
using BetterGenshinImpact.GameTask.AutoGeniusInvokation.Exception;
using BetterGenshinImpact.View.Drawable;
using BetterGenshinImpact.ViewModel.Pages;
using GeniusInvokationAutoToy.Utils;
using Microsoft.Extensions.Logging;
using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using BetterGenshinImpact.Core.Recognition.OpenCv;
using BetterGenshinImpact.ViewModel.Pages;
namespace BetterGenshinImpact.GameTask.AutoGeniusInvokation.Model;
@@ -124,7 +123,7 @@ public class Duel
CurrentDiceCount = 8;
// 预计算本回合内的所有可能的元素
// 预计算本回合内的所有可能的元素 // 并调整识别骰子素材的顺序
var elementSet = PredictionDiceType();
// 0 投骰子
@@ -368,6 +367,9 @@ public class Duel
}
}
// 调整元素骰子识别素材顺序
GeniusInvokationControl.GetInstance().SortActionPhaseDiceMats(elementSet);
return elementSet;
}