Compare commits

...
12 Commits
10 changed files with 380 additions and 32 deletions

No files matched your search

@@ -2,7 +2,7 @@
<PropertyGroup>
<AssemblyName>BetterGI</AssemblyName>
<Version>0.50.0</Version>
<Version>0.50.1-alpha.1</Version>
<IncludeSourceRevisionInInformationalVersion>false</IncludeSourceRevisionInInformationalVersion>
<OutputType>WinExe</OutputType>
<TargetFramework>net8.0-windows10.0.22621.0</TargetFramework>
@@ -41,6 +41,7 @@ public class AutoAlbumTask(AutoMusicGameParam taskParam) : ISoloTask
catch (Exception e)
{
Logger.LogError("自动音乐专辑任务异常:{Msg}", e.Message);
Logger.LogDebug(e, "自动音乐专辑任务异常详情");
Notify.Event(NotificationEvent.AlbumError).Error("自动音游专辑异常", e);
}
}
@@ -12,6 +12,7 @@ using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text.Json;
using System.Text.RegularExpressions;
using System.Threading;
@@ -245,17 +246,34 @@ public partial class AutoPickTrigger : ITaskTrigger
else
{
var textMat = new Mat(content.CaptureRectArea.SrcMat, textRect);
var boundingRect = GetWhiteTextBoundingRect(textMat);
var boundingRect = TextRectExtractor.GetTextBoundingRect(textMat, out var bin);
// 如果找到有效区域
if (boundingRect.Width > 5 && boundingRect.Height > 5)
if (boundingRect.X <20 && boundingRect.Width > 5 && boundingRect.Height > 5)
{
// 截取只包含文字的区域
var textOnlyMat = new Mat(textMat, new Rect(0, 0,
boundingRect.Right + 3 < textMat.Width ? boundingRect.Right + 3 : textMat.Width, textMat.Height));
boundingRect.Right + 5 < textMat.Width ? boundingRect.Right + 5 : textMat.Width, textMat.Height));
text = OcrFactory.Paddle.OcrWithoutDetector(textOnlyMat);
// if (RuntimeHelper.IsDebug)
// {
// // 如果不等于正确文字,则保存图片
// if (text != "烹饪")
// {
// var path = Global.Absolute("log/pick");
// Directory.CreateDirectory(path);
// var str = $"{DateTime.Now:yyyyMMddHHmmssfff}";
// // textMat.SaveImage(Path.Combine(path, $"pick_ocr_ori_{str}.png"));
// // 画上 boundingRect
// Cv2.Rectangle(textMat, boundingRect, new Scalar(0, 0, 255), 1);
// textMat.SaveImage(Path.Combine(path, $"pick_ocr_rect_{str}.png"));
// bin.SaveImage(Path.Combine(path, $"bin_{str}.png"));
// }
// }
}
else
{
Debug.WriteLine("-- 无法识别到有效文字区域,尝试直接OCR DET");
text = OcrFactory.Paddle.Ocr(textMat);
}
}
@@ -263,6 +281,9 @@ public partial class AutoPickTrigger : ITaskTrigger
speedTimer.Record("文字识别");
if (!string.IsNullOrEmpty(text))
{
// 处理OCR识别结果,清理无效字符并确保引号配对
text = ProcessOcrText(text);
// 唯一一个动态拾取项,特殊处理,不拾取
if (text.Contains("长时间"))
{
@@ -277,19 +298,12 @@ public partial class AutoPickTrigger : ITaskTrigger
}
// 单个字符不拾取
var simpleText = PunctuationAndSpacesRegex().Replace(text, "");
if (simpleText.Length <= 1)
{
return;
}
// 纯英文不拾取
if (StringUtils.IsPureEnglish(text))
if (text.Length <= 1)
{
return;
}
if (config.WhiteListEnabled && (_whiteList.Contains(text) || _whiteList.Contains(simpleText)))
if (config.WhiteListEnabled && _whiteList.Contains(text))
{
LogPick(content, text);
Simulation.SendInput.Keyboard.KeyPress(AutoPickAssets.Instance.PickVk);
@@ -304,7 +318,7 @@ public partial class AutoPickTrigger : ITaskTrigger
return;
}
if (config.BlackListEnabled && (_blackList.Contains(text) || _blackList.Contains(simpleText)))
if (config.BlackListEnabled && _blackList.Contains(text))
{
return;
}
@@ -372,8 +386,114 @@ public partial class AutoPickTrigger : ITaskTrigger
_prevClickFrameIndex = content.FrameIndex;
}
[GeneratedRegex(@"^[\p{P} ]+|[\p{P} ]+$")]
private static partial Regex PunctuationAndSpacesRegex();
/// <summary>
/// 高性能处理OCR识别的文字结果
/// 1. 替换【、[ 为「,替换】、] 为」
/// 2. 清理左边非「字符和中文的字符
/// 3. 清理右边非」字符和中文的字符
/// 4. 确保引号配对:有「必有」,有」必有「
/// </summary>
/// <param name="text">OCR识别的原始文字</param>
/// <returns>处理后的文字</returns>
private static string ProcessOcrText(string text)
{
if (string.IsNullOrEmpty(text))
return text;
// 0. 首先替换相似的括号字符并删除换行符、空格,使用Span<char>进行原地替换以获得最佳性能
Span<char> chars = stackalloc char[text.Length];
text.AsSpan().CopyTo(chars);
int writeIndex = 0;
bool hasChanges = false;
for (int i = 0; i < chars.Length; i++)
{
char c = chars[i];
// 跳过换行符、回车符、空格、制表符等空白字符
if (char.IsWhiteSpace(c))
{
hasChanges = true;
continue;
}
// 替换括号字符
if (c == '【' || c == '[')
{
chars[writeIndex++] = '「';
hasChanges = true;
}
else if (c == '】' || c == ']')
{
chars[writeIndex++] = '」';
hasChanges = true;
}
else
{
chars[writeIndex++] = c;
}
}
// 如果有变化,使用处理后的字符;否则使用原字符串的Span
ReadOnlySpan<char> span = hasChanges ? chars.Slice(0, writeIndex) : text.AsSpan();
int start = 0;
int end = span.Length - 1;
// 1. 从左边开始,删除非「字符和中文的字符
while (start <= end)
{
char c = span[start];
if (c == '「' || (c >= 0x4E00 && c <= 0x9FFF)) // 「字符或中文字符
break;
start++;
}
// 2. 从右边开始,删除非」字符和中文的字符
while (end >= start)
{
char c = span[end];
if (c == '」' || c == '!' || (c >= 0x4E00 && c <= 0x9FFF)) // 」字符或中文字符
break;
end--;
}
// 如果所有字符都被删除了
if (start > end)
return string.Empty;
// 获取清理后的文字
var cleanedSpan = span.Slice(start, end - start + 1);
// 3. 检查并补充引号配对
bool hasLeftQuote = false;
bool hasRightQuote = false;
// 快速扫描是否存在引号
for (int i = 0; i < cleanedSpan.Length; i++)
{
if (cleanedSpan[i] == '「')
hasLeftQuote = true;
else if (cleanedSpan[i] == '」')
hasRightQuote = true;
}
// 根据引号配对规则补充
if (hasLeftQuote && !hasRightQuote)
{
// 有「但没有」,在末尾补充」
Debug.WriteLine("补充缺失的右引号");
return string.Concat(cleanedSpan, "」");
}
else if (hasRightQuote && !hasLeftQuote)
{
// 有」但没有「,在开头补充「
Debug.WriteLine("补充缺失的左引号");
return string.Concat("「", cleanedSpan);
}
return cleanedSpan.ToString();
}
}
@@ -0,0 +1,79 @@
using System;
using System.Linq;
namespace BetterGenshinImpact.GameTask.AutoPick;
using OpenCvSharp;
public static class TextRectExtractor
{
/// <summary>
/// 从图片中提取文字范围(假定文字从最左边贴边开始,向右连续)
/// 结果矩形固定 x=0,y=0,h=原图高度,只计算连续文字宽度。
/// </summary>
public static Rect GetTextBoundingRect(Mat textMat, out Mat bin)
{
// 转换为灰度图
Mat gray = new Mat();
if (textMat.Channels() == 3)
{
Cv2.CvtColor(textMat, gray, ColorConversionCodes.BGR2GRAY);
}
else
{
gray = textMat.Clone();
}
// 使用阈值160进行二值化处理
bin = new Mat();
Cv2.Threshold(gray, bin, 160, 255, ThresholdTypes.Binary);
// 形态学操作:先腐蚀后膨胀,去除噪点并保持文字完整
Mat kernel = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 3));
Cv2.Erode(bin, bin, kernel, iterations: 1);
Cv2.Dilate(bin, bin, kernel, iterations: 2);
kernel.Dispose();
gray.Dispose();
return ProjectionRect(textMat, bin);
}
private static Rect ProjectionRect(Mat textMat, Mat bin)
{
// 投影:对行做 ReduceSum,得到 1 x width 的列和
using var projection = new Mat();
Cv2.Reduce(bin, projection, 0, ReduceTypes.Sum, MatType.CV_32S);
int width = projection.Cols;
projection.GetArray(out int[] colSums);
int maxGap = 30; // 允许的最大连续空列数
int gapCount = 0;
int lastNonEmpty = -1;
for (int x = 0; x < width; x++)
{
bool hasInk = colSums[x] > 0;
if (hasInk)
{
lastNonEmpty = x;
gapCount = 0;
}
else
{
gapCount++;
if (gapCount > maxGap)
{
break;
}
}
}
if (lastNonEmpty == -1)
{
// 没有检测到文字
return new Rect();
}
Rect boundingRect = new Rect(0, 0, lastNonEmpty, textMat.Height);
return boundingRect;
}
}
@@ -16,6 +16,7 @@ using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using BetterGenshinImpact.Core.Config;
using static BetterGenshinImpact.GameTask.Common.TaskControl;
namespace BetterGenshinImpact.GameTask.GetGridIcons;
@@ -51,7 +52,7 @@ public class GridIconsAccuracyTestTask : ISoloTask
public static InferenceSession LoadModel(out Dictionary<string, float[]> prototypes)
{
#region 加载model
var session = new InferenceSession(@".\Assets\Model\Item\gridIcon.onnx");
var session = new InferenceSession(Global.Absolute(@"Assets\Model\Item\gridIcon.onnx"));
var metadata = session.ModelMetadata;
@@ -62,7 +63,7 @@ public class GridIconsAccuracyTestTask : ISoloTask
List<string> prefixList = System.Text.Json.JsonSerializer.Deserialize<List<string>>(prefixListJson) ?? throw new Exception(); // 不预测前缀
#endregion
#region 加载原型向量
var allLines = File.ReadLines(@".\Assets\Model\Item\items.csv").Skip(1); // 跳过首行列名
var allLines = File.ReadLines(Global.Absolute(@"Assets\Model\Item\items.csv")).Skip(1); // 跳过首行列名
prototypes = new Dictionary<string, float[]>();
foreach (string line in allLines)
{
@@ -358,6 +358,22 @@
</Style>
</MenuItem.Style>
</MenuItem>
<MenuItem Command="{Binding OpenScriptFolderCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ContextMenu}, Path=PlacementTarget.SelectedItem}"
Header="打开所在目录">
<MenuItem.Style>
<Style TargetType="MenuItem">
<Setter Property="Visibility" Value="Visible" />
<Style.Triggers>
<DataTrigger
Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ContextMenu}, Path=PlacementTarget.SelectedItem.Type}"
Value="Shell">
<Setter Property="Visibility" Value="Collapsed" />
</DataTrigger>
</Style.Triggers>
</Style>
</MenuItem.Style>
</MenuItem>
<MenuItem Command="{Binding DeleteScriptCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ContextMenu}, Path=PlacementTarget.SelectedItem}"
Header="移除" />
@@ -69,7 +69,6 @@
<ComboBoxItem Content="CNB" />
</ComboBox>
<ui:Button
Appearance="Success"
Icon="{ui:SymbolIcon ArrowDownload24}"
Content="立即更新"
Command="{Binding UpdateFromGitHostPlatformCommand}" />
@@ -92,7 +91,7 @@
<ui:TextBlock Grid.Row="1"
Grid.Column="0"
Foreground="{ui:ThemeResource TextFillColorTertiaryBrush}"
Text="普通用户可点此直接更新"
Text="【国内】普通用户可点此直接更新"
TextWrapping="Wrap" />
</Grid>
</ui:CardControl.Header>
@@ -121,7 +120,7 @@
<ui:TextBlock Grid.Row="1"
Grid.Column="0"
Foreground="{ui:ThemeResource TextFillColorTertiaryBrush}"
Text="Mirror酱用户可以输入CDK高速更新"
Text="【国内】Mirror酱用户可以输入CDK高速更新"
TextWrapping="Wrap" />
</Grid>
</ui:CardControl.Header>
@@ -14,6 +14,7 @@ using BetterGenshinImpact.Model;
using Meziantou.Framework.Win32;
using Wpf.Ui.Controls;
using Wpf.Ui.Violeta.Controls;
using MessageBoxResult = System.Windows.MessageBoxResult;
namespace BetterGenshinImpact.View.Windows;
@@ -30,7 +31,7 @@ public partial class CheckUpdateWindow : FluentWindow
[ObservableProperty] private string selectedGitSource = "Github";
public string GitSourceDescription => SelectedGitSource == "CNB" ? "直接从 CNB 下载并更新" : "直接从 Github 下载并更新";
public string GitSourceDescription => SelectedGitSource == "CNB" ? "【国内】直接从 CNB 下载并更新" : "【国外】直接从 Github 下载并更新";
partial void OnSelectedGitSourceChanged(string value)
{
@@ -44,13 +45,13 @@ public partial class CheckUpdateWindow : FluentWindow
_option = option ?? throw new ArgumentNullException(nameof(option));
DataContext = this;
InitializeComponent();
// 存在CDK则显示修改按钮
if (string.IsNullOrEmpty(MirrorChyanHelper.GetCdk()))
{
EditCdkButton.Visibility = Visibility.Collapsed;
}
if (option.Trigger == UpdateTrigger.Manual)
{
IgnoreButton.Visibility = Visibility.Collapsed;
@@ -62,11 +63,11 @@ public partial class CheckUpdateWindow : FluentWindow
WebpagePanel.Visibility = Visibility.Collapsed;
UpdateStatusMessageGrid.Height = 0;
ShowUpdateStatus = false;
// 隐藏开源渠道和Steambird服务卡片
GitSourceCard.Visibility = Visibility.Collapsed;
SteambirdCard.Visibility = Visibility.Collapsed;
// // 删除前几行
// MyGrid.RowDefinitions.RemoveAt(0);
// MyGrid.RowDefinitions.RemoveAt(0);
@@ -91,7 +92,7 @@ public partial class CheckUpdateWindow : FluentWindow
Closing += OnClosing;
// 延迟显示气泡提示
if (option.Channel != UpdateChannel.Alpha)
{
@@ -103,7 +104,7 @@ public partial class CheckUpdateWindow : FluentWindow
{
showTimer.Stop();
ShowOtherUpdateTip = true;
// 5秒后自动消失
var hideTimer = new DispatcherTimer
{
@@ -118,7 +119,6 @@ public partial class CheckUpdateWindow : FluentWindow
};
showTimer.Start();
}
}
protected void OnClosing(object? sender, CancelEventArgs e)
@@ -165,9 +165,21 @@ public partial class CheckUpdateWindow : FluentWindow
private async Task UpdateFromGitHostPlatformAsync()
{
string source = SelectedGitSource == "CNB" ? "cnb" : "github";
if (source == "github")
{
// 提示用户这个是国外服务器,可能会很慢
var result = await MessageBox.ShowAsync("您已选择「Github」作为更新源。\n请确认:您当前网络可正常访问 Github 文件服务?\n若不确定能否访问,建议切换至其他更新渠道。\n是否继续使用 Github 渠道更新?",
"警告", System.Windows.MessageBoxButton.OKCancel, MessageBoxImage.Exclamation, System.Windows.MessageBoxResult.None);
if (result != MessageBoxResult.OK)
{
return;
}
}
await RunUpdaterAsync($"-I --source {source}");
}
[RelayCommand]
private async Task UpdateFromSteambirdAsync()
@@ -183,7 +195,7 @@ public partial class CheckUpdateWindow : FluentWindow
{
return;
}
if (_option.Channel == UpdateChannel.Stable)
{
await RunUpdaterAsync("-I --source mirrorc");
@@ -234,7 +246,7 @@ public partial class CheckUpdateWindow : FluentWindow
await UserInteraction.Invoke(this, CheckUpdateWindowButton.Cancel);
}
}
[RelayCommand]
private void EditCdk()
{
@@ -1725,6 +1725,45 @@ public partial class ScriptControlViewModel : ViewModel
);
}
[RelayCommand]
public void OnOpenScriptFolder(ScriptGroupProject? item)
{
if (item == null)
{
return;
}
try
{
string? path = null;
switch (item.Type)
{
case "Javascript":
path = Path.Combine(Global.ScriptPath(), item.FolderName);
break;
case "KeyMouse":
path = Global.Absolute(@"User\KeyMouseScript");
break;
case "Pathing":
path = Path.Combine(MapPathingViewModel.PathJsonPath, item.FolderName);
break;
}
if (path != null && Directory.Exists(path))
{
Process.Start("explorer.exe", path);
}
else
{
_snackbarService.Show("打开失败", "目录不存在", ControlAppearance.Caution, null, TimeSpan.FromSeconds(2));
}
}
catch (Exception e)
{
_logger.LogDebug(e, "打开脚本目录失败");
_snackbarService.Show("打开失败", e.Message, ControlAppearance.Danger, null, TimeSpan.FromSeconds(3));
}
}
private void ScriptGroupsCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
{
if (e.NewItems != null)
@@ -0,0 +1,81 @@
using OpenCvSharp;
namespace BetterGenshinImpact.Test.Cv;
public class ThresholdWindow
{
private Mat? _originalImage;
private Mat? _grayImage;
private int _currentThreshold = 160;
public static void Test()
{
var window = new ThresholdWindow();
window.ShowThresholdAdjuster(@"E:\HuiTask\更好的原神\自动拾取\pick_ocr_ori_20250915011455192.png");
}
/// <summary>
/// 对指定图片进行二值化阈值拉条调整
/// </summary>
/// <param name="imagePath">图片路径</param>
public void ShowThresholdAdjuster(string imagePath)
{
// 加载原始图像
_originalImage = Cv2.ImRead(imagePath);
if (_originalImage.Empty())
{
throw new ArgumentException("无法加载图像文件");
}
// 转换为灰度图像
_grayImage = new Mat();
Cv2.CvtColor(_originalImage, _grayImage, ColorConversionCodes.BGR2GRAY);
// 创建窗口
const string windowName = "Threshold Adjuster";
const string trackbarName = "Threshold";
Cv2.NamedWindow(windowName, WindowFlags.AutoSize);
// 创建拉条,范围0-255
Cv2.CreateTrackbar(trackbarName, windowName, ref _currentThreshold, 255, OnThresholdChanged);
// 初始显示
UpdateThresholdImage(windowName);
// 等待用户按键
Console.WriteLine("按任意键关闭窗口...");
Cv2.WaitKey(0);
// 清理资源
Cv2.DestroyAllWindows();
_originalImage?.Dispose();
_grayImage?.Dispose();
}
/// <summary>
/// 阈值变化回调函数
/// </summary>
/// <param name="value">阈值</param>
/// <param name="userdata">用户数据指针(未使用)</param>
private void OnThresholdChanged(int value, IntPtr userdata)
{
_currentThreshold = value;
UpdateThresholdImage("Threshold Adjuster");
}
/// <summary>
/// 更新二值化图像显示
/// </summary>
/// <param name="windowName">窗口名称</param>
private void UpdateThresholdImage(string windowName)
{
if (_grayImage == null) return;
using var thresholdImage = new Mat();
Cv2.Threshold(_grayImage, thresholdImage, _currentThreshold, 255, ThresholdTypes.Binary);
Cv2.ImShow(windowName, thresholdImage);
}
}