Files
better-genshin-impact/BetterGenshinImpact/GameTask/AutoFishing/Model/Fishpond.cs
Takaranoao 5b3bac478d 升级多个依赖&增加额外的推理加速功能&迁移OCR (#1459)
* 更新多个NuGet包至最新版本

* 重构模型加载以适应yolosharp

* feat: 改变接口。TensorRT缓存的初步支持,修改配置项。

* 更新依赖并调整变量顺序,修复加载问题

* 更新AvalonEdit和Microsoft.ML.OnnxRuntime包至最新版本,以修复问题

* fix: downgrade Microsoft.ML.OnnxRuntime.DirectML to version 1.21.0

* typo

* fix: change log level from warning to error for ONNX provider loading failure

* 增加 paddle ocr 的 onnx 模型

* feat: add PaddleOCR models for Chinese, English, and Latin recognition

* 使用cv的DNN生成Tensor,加速Yap文字识别

* feat: 尝试搓一个onnx的ocr

* clean up code

* chore: update OpenCvSharp4 package versions to 4.10.0.20241108

* 修复因格式化代码而丢的引用

* chore: update Microsoft.ML.OnnxRuntime.DirectML package to version 1.21.1 and improve logging for ONNX provider initialization

* chore: 等yolosharp更新再升级onnx

* chore: add Microsoft.ML.OnnxRuntime.Managed package and clean up logging in Det class

* fix: refactor output tensor handling in Det class for improved clarity

* 补充注释,修复DML的OCR问题

* 默认OCR推理使用CPU,整理配置

* fix error NETSDK1152: 找到了多个具有相同相对路径的发布输出文件

* fix(logging): enhance debug log for ONNX initialization with provider details

* 修复TensorRT模型缓存的加载问题

* fix(onnx): improve cached model retrieval and add file existence check

* fix(ocr): replace SrcGreyMat with SrcMat for region of interest processing

* fix(onnx): add file existence check for cached model and adjust session options for DirectML provider

* 增加硬件加速配置UI界面

* 移除旧的OCR模型

* 错别字

---------

Co-authored-by: 辉鸭蛋 <huiyadanli@gmail.com>
2025-05-11 01:08:37 +08:00

149 lines
4.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using OpenCvSharp;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Compunet.YoloSharp.Data;
namespace BetterGenshinImpact.GameTask.AutoFishing.Model;
public class Fishpond
{
/// <summary>
/// 鱼池位置
/// </summary>
public Rect FishpondRect { get; set; }
/// <summary>
/// 抛竿落点位置
/// </summary>
public Rect? TargetRect { get; set; }
/// <summary>
/// 鱼池中的鱼
/// </summary>
public List<OneFish> Fishes { get; set; } = [];
public Fishpond(List<OneFish> fishes)
{
Fishes = fishes;
FishpondRect = CalculateFishpondRect();
}
/// <summary>
/// </summary>
/// <param name="result"></param>
/// <param name="includeTarget">是否包含抛竿落点</param>
/// <param name="ignoreObtained">是否忽略“获得”物品的图标</param>
public Fishpond(YoloResult<Detection> result, bool includeTarget = false, bool ignoreObtained = false)
{
Print(result);
foreach (var box in result)
{
// 可信度太低的直接放弃
if (box.Confidence < 0.4)
{
continue;
}
Rect rect = new Rect(box.Bounds.X, box.Bounds.Y, box.Bounds.Width, box.Bounds.Height);
if (box.Name.Name == "rod" || box.Name.Name == "err rod")
{
TargetRect = rect;
continue;
}
else if (ignoreObtained)
{
// todo特殊鱼的图标以及新获得的图标是不一样的要特殊处理
// todo不是很重要但有机会可以从构造函数里分离逻辑
// 忽略界面左侧提示的“获得”物品的图标,当上一竿获得鱼时,会对当前竿产生干扰
// 使用估算大小和位置的方式来判断并剔除
if (box.Bounds.Width < result.ImageSize.Width * 0.036 && box.Bounds.Height < result.ImageSize.Width * 0.036)
{
Rect huode = new Rect((int)(0.04375 * result.ImageSize.Width), (int)(0.4666 * result.ImageSize.Height), (int)(0.1 * result.ImageSize.Width), (int)(0.1 * result.ImageSize.Width));
if (huode.Contains(rect))
{
continue;
}
}
// 忽略界面中央提示的“获得”物品的图标
if (box.Bounds.Width > result.ImageSize.Width * 0.03 && box.Bounds.Width < result.ImageSize.Width * 0.06 &&
box.Bounds.Height > result.ImageSize.Width * 0.03 && box.Bounds.Height < result.ImageSize.Width * 0.06)
{
Rect huode = new Rect((int)(0.4 * result.ImageSize.Width), (int)(0.445 * result.ImageSize.Height), (int)(0.2 * result.ImageSize.Width), (int)(0.06125 * result.ImageSize.Width));
if (huode.Contains(rect))
{
continue;
}
}
}
if (includeTarget)
{
if (box.Name.Name == "koi") //进入抛竿的时候只看koihead
{
continue;
}
}
var fish = new OneFish(box.Name.Name, rect, box.Confidence);
Fishes.Add(fish);
}
// 可信度最高的鱼放在最前面
Fishes = [.. Fishes.OrderByDescending(fish => fish.Confidence)];
FishpondRect = CalculateFishpondRect();
}
private void Print(YoloResult<Detection> result)
{
Debug.Write("鱼塘YOLO识别结果");
foreach (var box in result)
{
Debug.Write(box.ToString());
}
Debug.WriteLine("");
}
/// <summary>
/// 计算鱼塘位置
/// </summary>
/// <returns></returns>
public Rect CalculateFishpondRect()
{
if (Fishes.Count == 0)
{
return default;
}
var left = int.MaxValue;
var top = int.MaxValue;
var right = int.MinValue;
var bottom = int.MinValue;
foreach (var fish in Fishes)
{
if (fish.Rect.Left < left)
{
left = fish.Rect.Left;
}
if (fish.Rect.Top < top)
{
top = fish.Rect.Top;
}
if (fish.Rect.Right > right)
{
right = fish.Rect.Right;
}
if (fish.Rect.Bottom > bottom)
{
bottom = fish.Rect.Bottom;
}
}
return new Rect(left, top, right - left, bottom - top);
}
}