Skip to content

Commit

Permalink
add PaddleOcr
Browse files Browse the repository at this point in the history
  • Loading branch information
huiyadanli committed Oct 31, 2023
1 parent 4b9722a commit edb9a2e
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 32 deletions.
5 changes: 3 additions & 2 deletions BetterGenshinImpact/BetterGenshinImpact.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@
<PackageReference Include="OpenCvSharp4.Windows" Version="4.8.0.20230708" />
<PackageReference Include="Microsoft.Xaml.Behaviors.Wpf" Version="1.1.77" />
<PackageReference Include="MouseKeyHook" Version="5.7.1" />
<PackageReference Include="Sdcb.PaddleInference.runtime.win64.mkl" Version="2.5.1" />
<PackageReference Include="Sdcb.PaddleInference" Version="2.5.0.1" />
<PackageReference Include="Sdcb.PaddleInference.runtime.win64.openblas" Version="2.5.1" />
<PackageReference Include="Sdcb.PaddleOCR" Version="2.7.0" />
<PackageReference Include="Sdcb.PaddleOCR.Models.Local" Version="2.7.0" />
<PackageReference Include="Sdcb.PaddleOCR.Models.Online" Version="2.7.0.1" />
<PackageReference Include="Serilog.Extensions.Logging" Version="7.0.0" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
<PackageReference Include="Serilog.Sinks.RichTextBox.Wpf" Version="1.1.0" />
Expand Down
4 changes: 2 additions & 2 deletions BetterGenshinImpact/Core/Recognition/OCR/MediaOcrService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class MediaOcrService : IOcrService
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Debug.WriteLine(e.Message);
return null;
}
}
Expand All @@ -56,7 +56,7 @@ public class MediaOcrService : IOcrService
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Debug.WriteLine(e.Message);
return null;
}
}
Expand Down
47 changes: 37 additions & 10 deletions BetterGenshinImpact/Core/Recognition/OCR/PaddleOcrService.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
using System;
using OpenCvSharp;
using System;
using System.Diagnostics;
using System.Drawing;
using OpenCvSharp;
using Sdcb.PaddleInference;
using Sdcb.PaddleOCR;
using Sdcb.PaddleOCR.Models.Local;
using Sdcb.PaddleOCR.Models;
using Sdcb.PaddleOCR.Models.Online;
using Sdcb.PaddleOCR.Models.Details;
using System.Threading;
using BetterGenshinImpact.Core.Config;
using Sdcb.PaddleInference;
using Vanara.PInvoke;
using Microsoft.ML.OnnxRuntime.Tensors;
using System.Collections.Generic;

namespace BetterGenshinImpact.Core.Recognition.OCR;

Expand All @@ -15,11 +23,17 @@ public class PaddleOcrService : IOcrService
/// 模型列表:
/// https://github.com/PaddlePaddle/PaddleOCR/blob/release/2.5/doc/doc_ch/models_list.md
/// </summary>
private readonly PaddleOcrAll _paddleOcrAll = new(LocalFullModels.ChineseV4, PaddleDevice.Mkldnn())
{
AllowRotateDetection = true, /* 允许识别有角度的文字 */
Enable180Classification = false, /* 允许识别旋转角度大于90度的文字 */
};
private PaddleOcrAll? _paddleOcrAll;

//public PaddleOcrService()
//{
// // public static OnlineDetectionModel ChineseV4 => new OnlineDetectionModel("ch_PP-OCRv4_det", new Uri("https://paddleocr.bj.bcebos.com/PP-OCRv4/chinese/ch_PP-OCRv4_det_infer.tar"), ModelVersion.V4);
// var path = Global.Absolute("Model\\PaddleOcr");
// FileDetectionModel localDetModel = new FileDetectionModel(path, ModelVersion.V4);
// FileClassificationModel localClsModel = new FileClassificationModel(path, ModelVersion.V2);
// RecognizationModel localRecModel = (RecognizationModel)new StreamDictFileRecognizationModel(this.RootDirectory, (IReadOnlyList<string>)SharedUtils.LoadDicts(this.DictName), this.Version);
// FullOcrModel model = new FullOcrModel(localDetModel, localClsModel, localRecModel);
//}

public string Ocr(Bitmap bitmap)
{
Expand All @@ -28,8 +42,21 @@ public string Ocr(Bitmap bitmap)

public string Ocr(Mat mat)
{
if (_paddleOcrAll == null)
{
var model = OnlineFullModels.ChineseV4.DownloadAsync().GetAwaiter().GetResult();
_paddleOcrAll = new PaddleOcrAll(model, PaddleDevice.Onnx())
{
AllowRotateDetection = false, /* 允许识别有角度的文字 */
Enable180Classification = false, /* 允许识别旋转角度大于90度的文字 */
};
}

var stopwatch = new Stopwatch();
stopwatch.Start();
var result = _paddleOcrAll.Run(mat);
Console.WriteLine("PaddleOcr结果: " + result.Text);
stopwatch.Stop();
Debug.WriteLine($"PaddleOcr 耗时 {stopwatch.ElapsedMilliseconds}ms 结果: {result.Text}");
return result.Text;
}
}
40 changes: 27 additions & 13 deletions BetterGenshinImpact/GameTask/AutoSkip/AutoSkipTrigger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,31 +58,45 @@ public void OnCapture(CaptureContent content)
Simulation.SendInput.Keyboard.KeyPress(VirtualKeyCode.SPACE);
}

// 领取每日委托奖励
var dailyRewardIconRa = content.CaptureRectArea.Find(_autoSkipAssets.DailyRewardIconRo);
if (!dailyRewardIconRa.IsEmpty())
{
var config = TaskContext.Instance().Config.AutoSkipConfig;
var textRect = new Rect(dailyRewardIconRa.X + dailyRewardIconRa.Width, dailyRewardIconRa.Y, (int)(config.ChatOptionTextWidth * assetScale), dailyRewardIconRa.Height);
using var mat = new Mat(content.CaptureRectArea.SrcMat, textRect);
using var mat = new Mat(content.CaptureRectArea.SrcGreyMat, textRect);
//using var mat = new Mat(content.CaptureRectArea.SrcMat, textRect);
// 只提取橙色
using var bMat = OpenCvCommonHelper.Threshold(mat, new Scalar(247, 198, 50), new Scalar(255, 204, 504));
var text = OcrFactory.Paddle.Ocr(bMat);

if (text.Contains("每日委托"))
{
if (Math.Abs(content.FrameIndex - _prevClickFrameIndex) >= 8)
{
_logger.LogInformation("自动选择:{Text}", text);
}

dailyRewardIconRa.ClickCenter();
}
//using var bMat = OpenCvCommonHelper.Threshold(mat, new Scalar(247, 198, 50), new Scalar(255, 204, 504));
//var whiteCount = OpenCvCommonHelper.CountGrayMatColor(bMat, 255);
//if (whiteCount * 1.0 / (bMat.Width * bMat.Height) <= 0.1)
//{
// dailyRewardIconRa.Dispose();
// // 凯瑟琳聊天框不自动退出
// return;
//}

var text = OcrFactory.Paddle.Ocr(mat);

//if (text.Contains("每日委托"))
//{
// if (Math.Abs(content.FrameIndex - _prevClickFrameIndex) >= 8)
// {
// _logger.LogInformation("自动选择:{Text}", text);
// }

// dailyRewardIconRa.ClickCenter();
//}
_logger.LogInformation("自动选择:{Text}", text);

_prevClickFrameIndex = content.FrameIndex;
dailyRewardIconRa.Dispose();
return;
}

// 领取探索派遣奖励


// 找右下的对话选项按钮
content.CaptureRectArea.Find(_autoSkipAssets.OptionIconRo, (optionButtonRectArea) =>
{
Expand Down
10 changes: 5 additions & 5 deletions Fischless.GameCapture/Fischless.GameCapture.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@

<ItemGroup>
<PackageReference Include="SharpDX.Direct3D11" Version="4.2.0" />
<PackageReference Include="Vanara.PInvoke.DwmApi" Version="3.4.16" />
<PackageReference Include="Vanara.PInvoke.Gdi32" Version="3.4.16" />
<PackageReference Include="Vanara.PInvoke.User32" Version="3.4.16" />
<PackageReference Include="Vanara.PInvoke.SHCore" Version="3.4.16" />
<PackageReference Include="Vanara.Windows.Extensions" Version="3.4.16" />
<PackageReference Include="Vanara.PInvoke.DwmApi" Version="3.4.17" />
<PackageReference Include="Vanara.PInvoke.Gdi32" Version="3.4.17" />
<PackageReference Include="Vanara.PInvoke.User32" Version="3.4.17" />
<PackageReference Include="Vanara.PInvoke.SHCore" Version="3.4.17" />
<PackageReference Include="Vanara.Windows.Extensions" Version="3.4.17" />
</ItemGroup>

</Project>

0 comments on commit edb9a2e

Please sign in to comment.