图标识别应用两则 (#2154)

This commit is contained in:
FishmanTheMurloc
2025-09-06 01:17:05 +08:00
committed by GitHub
parent 2f55dd0502
commit 37fcde080f
32 changed files with 384 additions and 244 deletions

View File

@@ -98,7 +98,7 @@ namespace BetterGenshinImpact.UnitTest.GameTaskTests.AutoArtifactSalvageTests
CultureInfo cultureInfo = new CultureInfo("zh-Hans");
//
AutoArtifactSalvageTask sut = new AutoArtifactSalvageTask(new AutoArtifactSalvageTaskParam(5, null, null, null, cultureInfo, this.stringLocalizer), new FakeLogger());
AutoArtifactSalvageTask sut = new AutoArtifactSalvageTask(new AutoArtifactSalvageTaskParam(5, null, null, null, null, cultureInfo, this.stringLocalizer), new FakeLogger());
string result = PaddleResultDic.GetOrAdd(screenshot, screenshot_ =>
{
using Mat mat = new Mat(@$"..\..\..\Assets\AutoArtifactSalvage\{screenshot_}");
@@ -203,7 +203,7 @@ namespace BetterGenshinImpact.UnitTest.GameTaskTests.AutoArtifactSalvageTests
*/
//
AutoArtifactSalvageTask sut = new AutoArtifactSalvageTask(new AutoArtifactSalvageTaskParam(5, null, null, null, cultureInfo, this.stringLocalizer), new FakeLogger());
AutoArtifactSalvageTask sut = new AutoArtifactSalvageTask(new AutoArtifactSalvageTaskParam(5, null, null, null, null, cultureInfo, this.stringLocalizer), new FakeLogger());
ArtifactStat result = sut.GetArtifactStat(mat, paddle.Get(cultureInfo.Name), out string _);
//
@@ -237,7 +237,7 @@ namespace BetterGenshinImpact.UnitTest.GameTaskTests.AutoArtifactSalvageTests
CultureInfo cultureInfo = new CultureInfo("zh-Hans");
//
AutoArtifactSalvageTask sut = new AutoArtifactSalvageTask(new AutoArtifactSalvageTaskParam(5, null, null, null, cultureInfo, this.stringLocalizer), new FakeLogger());
AutoArtifactSalvageTask sut = new AutoArtifactSalvageTask(new AutoArtifactSalvageTaskParam(5, null, null, null, null, cultureInfo, this.stringLocalizer), new FakeLogger());
ArtifactStat artifact = sut.GetArtifactStat(mat, paddle.Get(), out string _);
bool result = IsMatchJavaScript(artifact, js);

View File

@@ -1,4 +1,4 @@
using BehaviourTree;
using BehaviourTree;
using BetterGenshinImpact.GameTask.AutoFishing;
using BetterGenshinImpact.GameTask.AutoFishing.Model;
using BetterGenshinImpact.GameTask.Model.Area;
@@ -17,7 +17,7 @@ namespace BetterGenshinImpact.UnitTest.GameTaskTests.AutoFishingTests
{
[Theory]
[InlineData(@"20250225101300361_ChooseBait_Succeeded.png", new string[] { "medaka", "butterflyfish", "butterflyfish", "pufferfish" })]
[InlineData(@"20250226161354285_ChooseBait_Succeeded.png", new string[] { "medaka", "medaka" })]
[InlineData(@"20250226161354285_ChooseBait_Succeeded.png", new string[] { "medaka", "medaka" })] // todo 更新用例
[InlineData(@"202503160917566615@900p.png", new string[] { "pufferfish" })]
/// <summary>
/// 测试各种选取鱼饵,结果为成功
@@ -35,7 +35,7 @@ namespace BetterGenshinImpact.UnitTest.GameTaskTests.AutoFishingTests
};
//
ChooseBait sut = new ChooseBait("-", blackboard, new FakeLogger(), false, systemInfo, new FakeInputSimulator());
ChooseBait sut = new ChooseBait("-", blackboard, new FakeLogger(), false, systemInfo, new FakeInputSimulator(), this.session, this.prototypes);
BehaviourStatus actual = sut.Tick(imageRegion);
//
@@ -69,11 +69,11 @@ namespace BetterGenshinImpact.UnitTest.GameTaskTests.AutoFishingTests
FakeTimeProvider fakeTimeProvider = new FakeTimeProvider(dateTime);
//
ChooseBait sut = new ChooseBait("-", blackboard, new FakeLogger(), false, systemInfo, new FakeInputSimulator(), fakeTimeProvider);
ChooseBait sut = new ChooseBait("-", blackboard, new FakeLogger(), false, systemInfo, new FakeInputSimulator(), this.session, this.prototypes, fakeTimeProvider);
BehaviourStatus actual = sut.Tick(imageRegion);
//
Assert.True(String.IsNullOrEmpty(blackboard.selectedBaitName));
Assert.Null(blackboard.selectedBait);
Assert.True(blackboard.chooseBaitUIOpening);
Assert.Equal(BehaviourStatus.Running, actual);
@@ -84,7 +84,7 @@ namespace BetterGenshinImpact.UnitTest.GameTaskTests.AutoFishingTests
actual = sut.Tick(imageRegion);
//
Assert.False(String.IsNullOrEmpty(blackboard.selectedBaitName));
Assert.NotNull(blackboard.selectedBait);
Assert.True(blackboard.chooseBaitUIOpening);
Assert.Equal(BehaviourStatus.Running, actual);
@@ -95,7 +95,7 @@ namespace BetterGenshinImpact.UnitTest.GameTaskTests.AutoFishingTests
actual = sut.Tick(imageRegion);
//
Assert.True(String.IsNullOrEmpty(blackboard.selectedBaitName));
Assert.Null(blackboard.selectedBait);
Assert.False(blackboard.chooseBaitUIOpening);
Assert.Equal(BehaviourStatus.Failed, actual);
}
@@ -124,15 +124,15 @@ namespace BetterGenshinImpact.UnitTest.GameTaskTests.AutoFishingTests
#region 1
//
ChooseBait sut = new ChooseBait("-", blackboard, new FakeLogger(), false, systemInfo, new FakeInputSimulator(), fakeTimeProvider);
ChooseBait sut = new ChooseBait("-", blackboard, new FakeLogger(), false, systemInfo, new FakeInputSimulator(), this.session, this.prototypes, fakeTimeProvider);
BehaviourStatus actual = sut.Tick(imageRegion);
fakeTimeProvider.SetUtcNow(dateTime.AddSeconds(3));
actual = sut.Tick(imageRegion);
//
Assert.True(String.IsNullOrEmpty(blackboard.selectedBaitName));
Assert.Null(blackboard.selectedBait);
Assert.Equal(BehaviourStatus.Failed, actual);
Assert.Single(blackboard.chooseBaitFailures.Where(f => f == "fake fly bait"));
Assert.Single(blackboard.chooseBaitFailures.Where(f => f == BaitType.FakeFlyBait));
#endregion
#region 2
@@ -146,9 +146,9 @@ namespace BetterGenshinImpact.UnitTest.GameTaskTests.AutoFishingTests
actual = sut.Tick(imageRegion);
//
Assert.True(String.IsNullOrEmpty(blackboard.selectedBaitName));
Assert.Null(blackboard.selectedBait);
Assert.Equal(BehaviourStatus.Failed, actual);
Assert.Equal(2, blackboard.chooseBaitFailures.Where(f => f == "fake fly bait").Count());
Assert.Equal(2, blackboard.chooseBaitFailures.Where(f => f == BaitType.FakeFlyBait).Count());
Assert.False(blackboard.abort);
#endregion
@@ -165,9 +165,9 @@ namespace BetterGenshinImpact.UnitTest.GameTaskTests.AutoFishingTests
actual = sut.Tick(imageRegion);
//
Assert.True(String.IsNullOrEmpty(blackboard.selectedBaitName));
Assert.Null(blackboard.selectedBait);
Assert.Equal(BehaviourStatus.Failed, actual);
Assert.Single(blackboard.chooseBaitFailures.Where(f => f == "spinelgrain bait"));
Assert.Single(blackboard.chooseBaitFailures.Where(f => f == BaitType.SpinelgrainBait));
#endregion
#region sunfish受到遮挡medaka再次出现4medaka
@@ -183,9 +183,9 @@ namespace BetterGenshinImpact.UnitTest.GameTaskTests.AutoFishingTests
actual = sut.Tick(imageRegion);
//
Assert.False(String.IsNullOrEmpty(blackboard.selectedBaitName));
Assert.NotNull(blackboard.selectedBait); // todo 更新用例
Assert.Equal(BehaviourStatus.Succeeded, actual);
Assert.Single(blackboard.chooseBaitFailures.Where(f => f == "spinelgrain bait"));
Assert.Single(blackboard.chooseBaitFailures.Where(f => f == BaitType.SpinelgrainBait));
#endregion
#region sunfish再次出现5
@@ -201,9 +201,9 @@ namespace BetterGenshinImpact.UnitTest.GameTaskTests.AutoFishingTests
actual = sut.Tick(imageRegion);
//
Assert.True(String.IsNullOrEmpty(blackboard.selectedBaitName));
Assert.Null(blackboard.selectedBait);
Assert.Equal(BehaviourStatus.Failed, actual);
Assert.Equal(2, blackboard.chooseBaitFailures.Where(f => f == "spinelgrain bait").Count());
Assert.Equal(2, blackboard.chooseBaitFailures.Where(f => f == BaitType.SpinelgrainBait).Count());
#endregion
}
@@ -230,15 +230,15 @@ namespace BetterGenshinImpact.UnitTest.GameTaskTests.AutoFishingTests
#region 1
//
ChooseBait sut = new ChooseBait("-", blackboard, new FakeLogger(), false, systemInfo, new FakeInputSimulator(), fakeTimeProvider);
ChooseBait sut = new ChooseBait("-", blackboard, new FakeLogger(), false, systemInfo, new FakeInputSimulator(), this.session, this.prototypes, fakeTimeProvider);
BehaviourStatus actual = sut.Tick(imageRegion);
fakeTimeProvider.SetUtcNow(dateTime.AddSeconds(3));
actual = sut.Tick(imageRegion);
//
Assert.True(String.IsNullOrEmpty(blackboard.selectedBaitName));
Assert.Null(blackboard.selectedBait);
Assert.Equal(BehaviourStatus.Failed, actual);
Assert.Single(blackboard.chooseBaitFailures.Where(f => f == "fake fly bait"));
Assert.Single(blackboard.chooseBaitFailures.Where(f => f == BaitType.FakeFlyBait));
#endregion
#region koi受到遮挡2
@@ -254,9 +254,9 @@ namespace BetterGenshinImpact.UnitTest.GameTaskTests.AutoFishingTests
actual = sut.Tick(imageRegion);
//
Assert.True(String.IsNullOrEmpty(blackboard.selectedBaitName));
Assert.Null(blackboard.selectedBait);
Assert.Equal(BehaviourStatus.Failed, actual);
Assert.Single(blackboard.chooseBaitFailures.Where(f => f == "spinelgrain bait"));
Assert.Single(blackboard.chooseBaitFailures.Where(f => f == BaitType.SpinelgrainBait));
Assert.False(blackboard.abort);
#endregion
@@ -273,9 +273,9 @@ namespace BetterGenshinImpact.UnitTest.GameTaskTests.AutoFishingTests
actual = sut.Tick(imageRegion);
//
Assert.True(String.IsNullOrEmpty(blackboard.selectedBaitName));
Assert.Null(blackboard.selectedBait);
Assert.Equal(BehaviourStatus.Failed, actual);
Assert.Equal(2, blackboard.chooseBaitFailures.Where(f => f == "fake fly bait").Count());
Assert.Equal(2, blackboard.chooseBaitFailures.Where(f => f == BaitType.FakeFlyBait).Count());
#endregion
#region 4
@@ -291,9 +291,9 @@ namespace BetterGenshinImpact.UnitTest.GameTaskTests.AutoFishingTests
actual = sut.Tick(imageRegion);
//
Assert.True(String.IsNullOrEmpty(blackboard.selectedBaitName));
Assert.Null(blackboard.selectedBait);
Assert.Equal(BehaviourStatus.Failed, actual);
Assert.Equal(2, blackboard.chooseBaitFailures.Where(f => f == "spinelgrain bait").Count());
Assert.Equal(2, blackboard.chooseBaitFailures.Where(f => f == BaitType.SpinelgrainBait).Count());
#endregion
}
}

View File

@@ -1,8 +1,9 @@
using BetterGenshinImpact.GameTask.AutoFishing;
using BetterGenshinImpact.GameTask.AutoFishing;
using BehaviourTree;
using BetterGenshinImpact.GameTask.Model.Area;
using Microsoft.Extensions.Time.Testing;
using OpenCvSharp;
using BetterGenshinImpact.GameTask.AutoFishing.Model;
namespace BetterGenshinImpact.UnitTest.GameTaskTests.AutoFishingTests
{
@@ -38,10 +39,10 @@ namespace BetterGenshinImpact.UnitTest.GameTaskTests.AutoFishingTests
}
[Theory]
[InlineData("20250225101257889_GetFishpond_Succeeded.png", new string[] { "fruit paste bait", "fruit paste bait", "redrot bait", "redrot bait" }, new string[] { "false worm bait", "false worm bait", "fake fly bait", "fake fly bait" })]
[InlineData("20250225101257889_GetFishpond_Succeeded.png", new BaitType[] { BaitType.FruitPasteBait, BaitType.FruitPasteBait, BaitType.RedrotBait, BaitType.RedrotBait }, new BaitType[] { BaitType.FalseWormBait, BaitType.FalseWormBait, BaitType.FakeFlyBait, BaitType.FakeFlyBait })]
/// 测试鱼的鱼饵均在失败列表中且被忽略,结果为运行中
/// </summary>
public void GetFishpondTest_AllIgnored_ShouldBeRunning(string screenshot1080p, IEnumerable<string> chooseBaitfailures, IEnumerable<string> throwRodNoTargetFishfailures)
public void GetFishpondTest_AllIgnored_ShouldBeRunning(string screenshot1080p, IEnumerable<BaitType> chooseBaitfailures, IEnumerable<BaitType> throwRodNoTargetFishfailures)
{
//
Mat mat = new Mat(@$"..\..\..\Assets\AutoFishing\{screenshot1080p}");

View File

@@ -1,4 +1,4 @@
using BehaviourTree;
using BehaviourTree;
using BetterGenshinImpact.GameTask.AutoFishing;
using BetterGenshinImpact.GameTask.Model.Area.Converter;
using BetterGenshinImpact.GameTask.Model.Area;
@@ -6,18 +6,19 @@ using Microsoft.Extensions.Time.Testing;
using OpenCvSharp;
using BehaviourTree.Composites;
using BehaviourTree.FluentBuilder;
using BetterGenshinImpact.GameTask.AutoFishing.Model;
namespace BetterGenshinImpact.UnitTest.GameTaskTests.AutoFishingTests
{
public partial class BehavioursTests
{
[Theory]
[InlineData(@"20250225101304534_ThrowRod_Succeeded.png", "false worm bait")]
[InlineData(@"20250226162217468_ThrowRod_Succeeded.png", "fruit paste bait")]
[InlineData(@"20250225101304534_ThrowRod_Succeeded.png", BaitType.FalseWormBait)]
[InlineData(@"20250226162217468_ThrowRod_Succeeded.png", BaitType.FruitPasteBait)]
/// <summary>
/// 测试各种抛竿,结果为成功
/// </summary>
public void ThrowRodTest_VariousFish_ShouldSuccess(string screenshot1080p, string selectedBaitName)
public void ThrowRodTest_VariousFish_ShouldSuccess(string screenshot1080p, BaitType selectedBait)
{
//
Mat mat = new Mat(@$"..\..\..\Assets\AutoFishing\{screenshot1080p}");
@@ -25,7 +26,7 @@ namespace BetterGenshinImpact.UnitTest.GameTaskTests.AutoFishingTests
var blackboard = new Blackboard(Predictor, sleep: i => { })
{
selectedBaitName = selectedBaitName
selectedBait = selectedBait
};
FakeTimeProvider fakeTimeProvider = new FakeTimeProvider();
@@ -40,12 +41,12 @@ namespace BetterGenshinImpact.UnitTest.GameTaskTests.AutoFishingTests
}
[Theory]
[InlineData(@"20250225101304534_ThrowRod_Succeeded.png", "redrot bait")]
[InlineData(@"20250225101304534_ThrowRod_Succeeded.png", "fake fly bait")]
[InlineData(@"20250225101304534_ThrowRod_Succeeded.png", BaitType.RedrotBait)]
[InlineData(@"20250225101304534_ThrowRod_Succeeded.png", BaitType.FakeFlyBait)]
/// <summary>
/// 测试各种抛竿未满足HutaoFisher判定结果为运行中
/// </summary>
public void ThrowRodTest_VariousFish_ShouldFail(string screenshot1080p, string selectedBaitName)
public void ThrowRodTest_VariousFish_ShouldFail(string screenshot1080p, BaitType selectedBait)
{
//
Mat mat = new Mat(@$"..\..\..\Assets\AutoFishing\{screenshot1080p}");
@@ -53,7 +54,7 @@ namespace BetterGenshinImpact.UnitTest.GameTaskTests.AutoFishingTests
var blackboard = new Blackboard(Predictor, sleep: i => { })
{
selectedBaitName = selectedBaitName
selectedBait = selectedBait
};
FakeTimeProvider fakeTimeProvider = new FakeTimeProvider();
@@ -68,11 +69,11 @@ namespace BetterGenshinImpact.UnitTest.GameTaskTests.AutoFishingTests
}
[Theory]
[InlineData(@"20250225101304534_ThrowRod_Succeeded.png", "flashing maintenance mek bait")]
[InlineData(@"20250225101304534_ThrowRod_Succeeded.png", BaitType.FlashingMaintenanceMekBait)]
/// <summary>
/// 测试各种抛竿,无鱼饵适用鱼,结果为失败
/// </summary>
public void ThrowRodTest_NoBaitFish_ShouldFail(string screenshot1080p, string selectedBaitName)
public void ThrowRodTest_NoBaitFish_ShouldFail(string screenshot1080p, BaitType selectedBait)
{
//
Mat mat = new Mat(@$"..\..\..\Assets\AutoFishing\{screenshot1080p}");
@@ -80,7 +81,7 @@ namespace BetterGenshinImpact.UnitTest.GameTaskTests.AutoFishingTests
var blackboard = new Blackboard(Predictor, sleep: i => { })
{
selectedBaitName = selectedBaitName
selectedBait = selectedBait
};
FakeTimeProvider fakeTimeProvider = new FakeTimeProvider();
@@ -119,7 +120,7 @@ namespace BetterGenshinImpact.UnitTest.GameTaskTests.AutoFishingTests
var blackboard = new Blackboard(Predictor, sleep: i => { })
{
selectedBaitName = "fake fly bait"
selectedBait = GameTask.AutoFishing.Model.BaitType.FakeFlyBait
};
//

View File

@@ -2,7 +2,9 @@ using BetterGenshinImpact.Core.Recognition.OCR;
using BetterGenshinImpact.Core.Recognition.ONNX;
using BetterGenshinImpact.GameTask.AutoFishing;
using BetterGenshinImpact.UnitTest.CoreTests.RecognitionTests.OCRTests;
using BetterGenshinImpact.UnitTest.GameTaskTests.GetGridIconsTests;
using Microsoft.Extensions.Localization;
using Microsoft.ML.OnnxRuntime;
namespace BetterGenshinImpact.UnitTest.GameTaskTests.AutoFishingTests
{
@@ -15,12 +17,17 @@ namespace BetterGenshinImpact.UnitTest.GameTaskTests.AutoFishingTests
private readonly PaddleFixture paddle;
private readonly IStringLocalizer<AutoFishingTask> stringLocalizer;
private readonly InferenceSession session;
private readonly Dictionary<string, float[]> prototypes;
public BehavioursTests(PaddleFixture paddle, TorchFixture torch, LocalizationFixture localization)
public BehavioursTests(PaddleFixture paddle, TorchFixture torch, LocalizationFixture localization, GridIconModelFixture iconModel)
{
this.paddle = paddle;
this.useTorch = torch.UseTorch;
this.stringLocalizer = localization.CreateStringLocalizer<AutoFishingTask>();
this.session = iconModel.modelLoader.Value.session;
this.prototypes = iconModel.modelLoader.Value.prototypes;
}
private IOcrService OcrService => paddle.Get();