From 8799f34ce899d315a6d37b3cad3b8a543f2efdb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=BE=89=E9=B8=AD=E8=9B=8B?= Date: Sun, 22 Jun 2025 02:31:37 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20Yap=20=E6=97=A0=E6=B3=95?= =?UTF-8?q?=E6=AD=A3=E5=B8=B8=E4=BD=BF=E7=94=A8=E7=9A=84=E9=97=AE=E9=A2=98?= =?UTF-8?q?=20#1459?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ONNX/SVTR/TextInferenceFactory.cs | 56 +++++++++---------- .../GameTask/AutoPick/AutoPickTrigger.cs | 2 +- .../Simple/OcrTest.cs | 14 ++--- 3 files changed, 36 insertions(+), 36 deletions(-) diff --git a/BetterGenshinImpact/Core/Recognition/ONNX/SVTR/TextInferenceFactory.cs b/BetterGenshinImpact/Core/Recognition/ONNX/SVTR/TextInferenceFactory.cs index a83620a2..d4606e45 100644 --- a/BetterGenshinImpact/Core/Recognition/ONNX/SVTR/TextInferenceFactory.cs +++ b/BetterGenshinImpact/Core/Recognition/ONNX/SVTR/TextInferenceFactory.cs @@ -16,32 +16,32 @@ public class TextInferenceFactory _ => throw new ArgumentOutOfRangeException(nameof(type), type, null), }; } - public static Mat PreProcessForInference(Mat mat) - { - if (mat.Channels() == 3) - { - mat = mat.CvtColor(ColorConversionCodes.BGR2GRAY); - } - else if (mat.Channels() == 4) - { - mat = mat.CvtColor(ColorConversionCodes.BGRA2GRAY); - } - else if (mat.Channels() != 1) - { - throw new ArgumentException("mat must be 1, 3 or 4 channels"); - } - - // Yap 已经改用灰度图了 https://github.com/Alex-Beng/Yap/commit/c2ad1e7b1442aaf2d80782a032e00876cd1c6c84 - // 二值化 - // Cv2.Threshold(mat, mat, 0, 255, ThresholdTypes.Otsu | ThresholdTypes.Binary); - //Cv2.AdaptiveThreshold(mat, mat, 255, AdaptiveThresholdTypes.GaussianC, ThresholdTypes.Binary, 31, 3); // 效果不错 但是和模型不搭 - //mat = OpenCvCommonHelper.Threshold(mat, Scalar.FromRgb(235, 235, 235), Scalar.FromRgb(255, 255, 255)); // 识别物品不太行 - // 不知道为什么要强制拉伸到 221x32 - mat = ResizeHelper.ResizeTo(mat, 221, 32); - // 填充到 384x32 - var padded = new Mat(new Size(384, 32), MatType.CV_8UC1, Scalar.Black); - padded[new Rect(0, 0, mat.Width, mat.Height)] = mat; - //Cv2.ImWrite(Global.Absolute("padded.png"), padded); - return padded; - } + // public static Mat PreProcessForInference(Mat mat) + // { + // if (mat.Channels() == 3) + // { + // mat = mat.CvtColor(ColorConversionCodes.BGR2GRAY); + // } + // else if (mat.Channels() == 4) + // { + // mat = mat.CvtColor(ColorConversionCodes.BGRA2GRAY); + // } + // else if (mat.Channels() != 1) + // { + // throw new ArgumentException("mat must be 1, 3 or 4 channels"); + // } + // + // // Yap 已经改用灰度图了 https://github.com/Alex-Beng/Yap/commit/c2ad1e7b1442aaf2d80782a032e00876cd1c6c84 + // // 二值化 + // // Cv2.Threshold(mat, mat, 0, 255, ThresholdTypes.Otsu | ThresholdTypes.Binary); + // //Cv2.AdaptiveThreshold(mat, mat, 255, AdaptiveThresholdTypes.GaussianC, ThresholdTypes.Binary, 31, 3); // 效果不错 但是和模型不搭 + // //mat = OpenCvCommonHelper.Threshold(mat, Scalar.FromRgb(235, 235, 235), Scalar.FromRgb(255, 255, 255)); // 识别物品不太行 + // // 不知道为什么要强制拉伸到 221x32 + // mat = ResizeHelper.ResizeTo(mat, 221, 32); + // // 填充到 384x32 + // var padded = new Mat(new Size(384, 32), MatType.CV_8UC1, Scalar.Black); + // padded[new Rect(0, 0, mat.Width, mat.Height)] = mat; + // //Cv2.ImWrite(Global.Absolute("padded.png"), padded); + // return padded; + // } } diff --git a/BetterGenshinImpact/GameTask/AutoPick/AutoPickTrigger.cs b/BetterGenshinImpact/GameTask/AutoPick/AutoPickTrigger.cs index f21751dd..bac9811d 100644 --- a/BetterGenshinImpact/GameTask/AutoPick/AutoPickTrigger.cs +++ b/BetterGenshinImpact/GameTask/AutoPick/AutoPickTrigger.cs @@ -198,7 +198,7 @@ public partial class AutoPickTrigger : ITaskTrigger if (config.OcrEngine == PickOcrEngineEnum.Yap.ToString()) { var textMat = new Mat(content.CaptureRectArea.CacheGreyMat, textRect); - text = _pickTextInference.Inference(TextInferenceFactory.PreProcessForInference(textMat)); + text = _pickTextInference.Inference(textMat); } else { diff --git a/Test/BetterGenshinImpact.Test/Simple/OcrTest.cs b/Test/BetterGenshinImpact.Test/Simple/OcrTest.cs index 0985abe1..b0409ee0 100644 --- a/Test/BetterGenshinImpact.Test/Simple/OcrTest.cs +++ b/Test/BetterGenshinImpact.Test/Simple/OcrTest.cs @@ -10,13 +10,13 @@ public class OcrTest { public static void TestYap() { - Mat mat = Cv2.ImRead(@"E:\HuiTask\更好的原神\临时文件\fuben_jueyuan.png"); - var text = TextInferenceFactory.Pick.Inference(TextInferenceFactory.PreProcessForInference(mat)); - Debug.WriteLine(text); - - Mat mat2 = Cv2.ImRead(@"E:\HuiTask\更好的原神\临时文件\fuben_jueyuan.png"); - var text2 = OcrFactory.Paddle.Ocr(mat2); - Debug.WriteLine(text2); + // Mat mat = Cv2.ImRead(@"E:\HuiTask\更好的原神\临时文件\fuben_jueyuan.png"); + // var text = TextInferenceFactory.Pick.Inference(TextInferenceFactory.PreProcessForInference(mat)); + // Debug.WriteLine(text); + // + // Mat mat2 = Cv2.ImRead(@"E:\HuiTask\更好的原神\临时文件\fuben_jueyuan.png"); + // var text2 = OcrFactory.Paddle.Ocr(mat2); + // Debug.WriteLine(text2); }