Compare commits

...
Author SHA1 Message Date
辉鸭蛋 d9aab28c7f refactor(自动拾取): 优化物品识别逻辑,移除模型存在性检查 2026-05-20 02:55:51 +08:00
辉鸭蛋 419b2a74ed refactor(自动拾取): 重构物品识别的代码逻辑
将原有的内联识别代码抽离为多个独立私有方法,拆分分类识别与OCR识别的业务逻辑,简化主流程代码;统一计时器的记录文本,修复文件末尾缺少的换行问题。
2026-05-20 02:30:34 +08:00
辉鸭蛋 c9dbb22a39 feat(自动拾取): 新增基于分类模型的拾取物品识别模式
新增拾取识别模式枚举定义,注册pick.onnx分类模型,添加自动拾取配置项与设置页面选择控件,实现完整的分类识别拾取物品逻辑,包含模型存在性检查、识别区域裁剪与置信度过滤等处理
2026-05-20 02:12:17 +08:00
6 changed files with 122 additions and 44 deletions

No files matched your search

@@ -61,6 +61,9 @@ public class BgiOnnxModel
public static readonly BgiOnnxModel BgiQClassify =
Register("BgiQClassify", @"Assets\Model\Common\q_classify_sim.onnx");
public static readonly BgiOnnxModel BgiPickClassify =
Register("BgiPickClassify", @"Assets\Model\Common\pick.onnx");
/// <summary>
/// paddleOCR V4 检测模型
/// </summary>
@@ -37,6 +37,14 @@ namespace BetterGenshinImpact.GameTask.AutoPick
[ObservableProperty]
private string _ocrEngine = PickOcrEngineEnum.Paddle.ToString();
/// <summary>
/// 拾取内容识别方式
/// - Ocr
/// - Model
/// </summary>
[ObservableProperty]
private string _itemRecognitionMode = PickItemRecognitionModeEnum.Ocr.ToString();
/// <summary>
/// 急速模式
/// 无视文字识别结果,直接拾取
@@ -1,5 +1,6 @@
using BetterGenshinImpact.Core.Config;
using BetterGenshinImpact.Core.Recognition;
using BetterGenshinImpact.Core.Recognition.ONNX;
using BetterGenshinImpact.Core.Recognition.OCR;
using BetterGenshinImpact.Core.Recognition.ONNX.SVTR;
using BetterGenshinImpact.Core.Script.Dependence.Model.TimerConfig;
@@ -8,6 +9,8 @@ using BetterGenshinImpact.GameTask.AutoPick.Assets;
using BetterGenshinImpact.Helpers;
using BetterGenshinImpact.Service;
using BetterGenshinImpact.View.Windows;
using Compunet.YoloSharp.Data;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using OpenCvSharp;
using System;
@@ -19,6 +22,7 @@ using System.Text.Json;
using System.Text.RegularExpressions;
using System.Threading;
using BetterGenshinImpact.GameTask.Model.Area;
using Compunet.YoloSharp;
namespace BetterGenshinImpact.GameTask.AutoPick;
@@ -157,6 +161,12 @@ public partial class AutoPickTrigger : ITaskTrigger
/// </summary>
private int _prevClickFrameIndex = -1;
private static readonly Lazy<BgiYoloPredictor> PickClassifierLazy = new(() =>
App.ServiceProvider.GetRequiredService<BgiOnnxFactory>().CreateYoloPredictor(BgiOnnxModel.BgiPickClassify));
private bool _loggedPickModelMissing;
//private int _fastModePickCount = 0;
public void OnCapture(CaptureContent content)
@@ -271,53 +281,16 @@ public partial class AutoPickTrigger : ITaskTrigger
return;
}
string text;
if (config.OcrEngine == nameof(PickOcrEngineEnum.Yap))
{
var textMat = new Mat(content.CaptureRectArea.CacheGreyMat, textRect);
text = TextInferenceFactory.Pick.Value.Inference(textMat);
}
else
{
using var textMat = new Mat(content.CaptureRectArea.SrcMat, textRect);
var boundingRect = TextRectExtractor.GetTextBoundingRect(textMat);
// var boundingRect = new Rect(); // 不使用自己写的文字区域提取
// 如果找到有效区域
if (boundingRect.X < 20 && boundingRect.Width > 5 && boundingRect.Height > 5)
{
// 截取只包含文字的区域
using var textOnlyMat = new Mat(textMat, new Rect(0, 0,
boundingRect.Right + 5 < textMat.Width ? boundingRect.Right + 5 : textMat.Width, textMat.Height));
text = OcrFactory.Paddle.OcrWithoutDetector(textOnlyMat);
var text = RecognizePickItem(content, foundRectArea, textRect, config);
// 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);
}
}
speedTimer.Record("文字识别");
speedTimer.Record("识别");
if (!string.IsNullOrEmpty(text))
{
// 处理OCR识别结果,清理无效字符并确保引号配对
text = ProcessOcrText(text);
if (config.ItemRecognitionMode != nameof(PickItemRecognitionModeEnum.Classify))
{
text = ProcessOcrText(text);
}
if (DoNotPick(text))
{
@@ -370,6 +343,64 @@ public partial class AutoPickTrigger : ITaskTrigger
speedTimer.DebugPrint();
}
private string RecognizePickItem(CaptureContent content, Region foundRectArea, Rect textRect, AutoPickConfig config)
{
if (config.ItemRecognitionMode == nameof(PickItemRecognitionModeEnum.Classify))
{
return RecognizePickItemByClassify(content.CaptureRectArea, foundRectArea);
}
else
{
return RecognizePickItemByOcr(content, textRect, config);
}
}
private string RecognizePickItemByOcr(CaptureContent content, Rect textRect, AutoPickConfig config)
{
if (config.OcrEngine == nameof(PickOcrEngineEnum.Yap))
{
using var greyTextMat = new Mat(content.CaptureRectArea.CacheGreyMat, textRect);
return TextInferenceFactory.Pick.Value.Inference(greyTextMat);
}
else
{
using var textMat = new Mat(content.CaptureRectArea.SrcMat, textRect);
var boundingRect = TextRectExtractor.GetTextBoundingRect(textMat);
// var boundingRect = new Rect(); // 不使用自己写的文字区域提取
// 如果找到有效区域
if (boundingRect.X < 20 && boundingRect.Width > 5 && boundingRect.Height > 5)
{
// 截取只包含文字的区域
using var textOnlyMat = new Mat(textMat, new Rect(0, 0,
boundingRect.Right + 5 < textMat.Width ? boundingRect.Right + 5 : textMat.Width, textMat.Height));
return OcrFactory.Paddle.OcrWithoutDetector(textOnlyMat);
}
Debug.WriteLine("-- 无法识别到有效文字区域,尝试直接OCR DET");
return OcrFactory.Paddle.Ocr(textMat);
}
}
private string RecognizePickItemByClassify(ImageRegion captureRectArea, Region foundRectArea)
{
using var itemRegion = captureRectArea.DeriveCrop(GetPickModelRect(foundRectArea));
var result = PickClassifierLazy.Value.Predictor.Classify(itemRegion.CacheImage);
var topClass = result.GetTopClass();
// Debug.WriteLine($"AutoPickTrigger: 拾取,{topClass.Name.Name} {topClass.Confidence:F2}");
if (topClass.Confidence <= 0.75)
{
return string.Empty;
}
// itemRegion.SrcMat.SaveImage(Global.Absolute("log\\pick.png"));
return topClass.Name.Name;
}
private static Rect GetPickModelRect(Region foundRectArea)
{
return new Rect(foundRectArea.Right + 20, foundRectArea.Y - 16, 64, 64);
}
private bool DoNotPick(string text)
{
// 唯一一个动态拾取项,特殊处理,不拾取
@@ -405,7 +436,7 @@ public partial class AutoPickTrigger : ITaskTrigger
{
return true;
}
if (text.Contains("月谕圣牌"))
{
return true;
@@ -0,0 +1,7 @@
namespace BetterGenshinImpact.GameTask.AutoPick;
public enum PickItemRecognitionModeEnum
{
Ocr,
Classify
}
@@ -80,6 +80,33 @@
Margin="0,0,8,0"
IsChecked="{Binding Config.AutoPickConfig.FastModeEnabled, Mode=TwoWay}" />
</Grid>-->
<Grid Margin="16">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<ui:TextBlock Grid.Row="0"
Grid.Column="0"
FontTypography="Body"
Text="识别方式"
TextWrapping="Wrap" />
<ui:TextBlock Grid.Row="1"
Grid.Column="0"
Foreground="{ui:ThemeResource TextFillColorTertiaryBrush}"
Text="OCR 通过文字识别拾取内容;Classify 使用物品分类模型识别"
TextWrapping="Wrap" />
<ComboBox Grid.Row="0"
Grid.RowSpan="2"
Grid.Column="1"
Width="100"
Margin="0,0,8,0"
ItemsSource="{Binding PickItemRecognitionModeNames}"
SelectedItem="{Binding Config.AutoPickConfig.ItemRecognitionMode, Mode=TwoWay}" />
</Grid>
<Grid Margin="16">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
@@ -30,6 +30,8 @@ public partial class TriggerSettingsPageViewModel : ViewModel
[ObservableProperty] private string[] _selectChatOptionTypeNames = [SelectChatOptionTypes.UseMouse, SelectChatOptionTypes.UseInteractionKey];
[ObservableProperty] private string[] _pickItemRecognitionModeNames = [PickItemRecognitionModeEnum.Ocr.ToString(), PickItemRecognitionModeEnum.Classify.ToString()];
[ObservableProperty] private string[] _pickOcrEngineNames = [PickOcrEngineEnum.Paddle.ToString(), PickOcrEngineEnum.Yap.ToString()];
[ObservableProperty] private List<string> _pickButtonNames;