mirror of
https://github.com/babalae/better-genshin-impact.git
synced 2026-04-21 21:50:12 +08:00
优化文字识别功能,使用新的文字区域检测方式减少CPU消耗
This commit is contained in:
@@ -234,7 +234,7 @@ public class BgiOnnxFactory
|
||||
/// <returns>BgiYoloPredictor</returns>
|
||||
public BgiYoloPredictor CreateYoloPredictor(BgiOnnxModel model)
|
||||
{
|
||||
logger.LogDebug("[Yolo]创建yolo预测器,模型: {ModelName}", model.Name);
|
||||
// logger.LogDebug("[Yolo]创建yolo预测器,模型: {ModelName}", model.Name);
|
||||
if (!EnableCache) return new BgiYoloPredictor(model, model.ModalPath, CreateSessionOptions(model, false));
|
||||
|
||||
var cached = GetCached(model);
|
||||
|
||||
@@ -203,7 +203,18 @@ public partial class AutoPickTrigger : ITaskTrigger
|
||||
else
|
||||
{
|
||||
var textMat = new Mat(content.CaptureRectArea.SrcMat, textRect);
|
||||
text = OcrFactory.Paddle.Ocr(textMat);
|
||||
var boundingRect = GetWhiteTextBoundingRect(textMat);
|
||||
// 如果找到有效区域
|
||||
if (boundingRect.Width > 5 && boundingRect.Height > 5)
|
||||
{
|
||||
// 截取只包含文字的区域
|
||||
var textOnlyMat = new Mat(textMat, boundingRect);
|
||||
text = OcrFactory.Paddle.OcrWithoutDetector(textOnlyMat);
|
||||
}
|
||||
else
|
||||
{
|
||||
text = OcrFactory.Paddle.Ocr(textMat);
|
||||
}
|
||||
}
|
||||
|
||||
speedTimer.Record("文字识别");
|
||||
@@ -258,6 +269,21 @@ public partial class AutoPickTrigger : ITaskTrigger
|
||||
speedTimer.DebugPrint();
|
||||
}
|
||||
|
||||
private static Rect GetWhiteTextBoundingRect(Mat textMat)
|
||||
{
|
||||
// 预处理提取纯白色文字
|
||||
var processedMat = new Mat();
|
||||
// 提取白色文字 (255,255,255)
|
||||
Cv2.InRange(textMat, new Scalar(254, 254, 254), new Scalar(255, 255, 255), processedMat);
|
||||
// 形态学操作,先腐蚀后膨胀,去除噪点并保持文字完整
|
||||
var kernel = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(2, 2));
|
||||
Cv2.MorphologyEx(processedMat, processedMat, MorphTypes.Open, kernel, iterations: 1);
|
||||
Cv2.Dilate(processedMat, processedMat, kernel, iterations: 1);
|
||||
// 寻找非零区域,即文字区域
|
||||
Rect boundingRect = Cv2.BoundingRect(processedMat);
|
||||
return boundingRect;
|
||||
}
|
||||
|
||||
|
||||
private bool HasScrollIcon(ImageRegion captureRectArea)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user