babalae/better-genshin-impact · error · Exception

识别浓缩树脂数量失败: {condensedResinCount}

Error message

识别浓缩树脂数量失败: {condensedResinCount}

What it means

Thrown by GoToCraftingBenchTask.GoCraftResinOnce when a 3-attempt NewRetry.WaitForAction cannot OCR the condensed-resin count into the valid 0-5 range. The retry crops the area to the right of the CondensedResinCount template and runs Paddle OcrWithoutDetector, storing into condensedResinCount.

Source

Thrown at BetterGenshinImpact/GameTask/Common/Job/GoToCraftingBenchTask.cs:129

                //浓缩纠缠重试
                var condensed =await NewRetry.WaitForAction(() =>
                {
                    var condensedResinCountRa = ra.Find(ElementRecognition.Get("CondensedResinCount", ra));
                    if (!condensedResinCountRa.IsEmpty())
                    {
                        // 图像右侧就是浓缩树脂数量
                        var countArea = ra.DeriveCrop(condensedResinCountRa.X + condensedResinCountRa.Width,
                            condensedResinCountRa.Y, condensedResinCountRa.Width*5/3, condensedResinCountRa.Height);
                        var count = OcrFactory.Paddle.OcrWithoutDetector(countArea.CacheGreyMat);
                        condensedResinCount = StringUtils.TryParseInt(count);
                    }
                    return condensedResinCount >= 0 && condensedResinCount <=5;
                },ct,3,200); 
                if (!condensed)
                {
                    Simulation.SendInput.Keyboard.KeyPress(User32.VK.VK_ESCAPE);
                    await new ReturnMainUiTask().Start(ct);
                    throw new Exception($"识别浓缩树脂数量失败: {condensedResinCount}");
                }
                
                // 每次合成消耗的数量
                const int resinConsumedPerCraft = 60;
                // 需要保留的最小数量
                 int minResinToKeep = SelectedConfig.MinResinToKeep;
                // 可以用来合成的树脂数量
                int resinAvailableForCrafting = fragileResinCount - minResinToKeep;
                // 最大可合成次数
                int maxCraftsPossible = 5 - condensedResinCount;
                // 计算需要合成的次数
                int craftsNeeded = resinAvailableForCrafting / resinConsumedPerCraft;
                if (craftsNeeded < 0)
                {
                    craftsNeeded = 0;
                }
                // 计算最大合成次数
                craftsNeeded = Math.Min(maxCraftsPossible, craftsNeeded);

View on GitHub (pinned to a7cb36712d)

Solutions

  1. Tighten the OCR crop width (Width*5/3) so it covers only the count digit, not adjacent '/5'.
  2. Add a regex to extract only the leading digit(s) before any '/' before the range check.
  3. Re-verify the CondensedResinCount template still matches at the current resolution/scale.
  4. Increase retry attempts/delay and log the raw OCR string each attempt.

Example fix

// before
var count = OcrFactory.Paddle.OcrWithoutDetector(countArea.CacheGreyMat);
condensedResinCount = StringUtils.TryParseInt(count);
...
return condensedResinCount >= 0 && condensedResinCount <=5;

// after: strip '/N' before parsing
var count = OcrFactory.Paddle.OcrWithoutDetector(countArea.CacheGreyMat);
var digitMatch = System.Text.RegularExpressions.Regex.Match(count, @"^\s*(\d+)");
condensedResinCount = digitMatch.Success ? StringUtils.TryParseInt(digitMatch.Groups[1].Value) : 0;
return condensedResinCount >= 0 && condensedResinCount <= 5;
Defensive patterns

Strategy: fallback

Validate before calling

// Pre-check the condensed-resin template anchors a valid region
var anchor = ra.Find(ElementRecognition.Get("CondensedResinCount", ra));
if (anchor.IsEmpty())
    Logger.LogWarning("浓缩树脂计数锚点未找到,OCR 可能失败");

Type guard

static bool IsValidCondensedCount(int n) => n is >= 0 and <= 5;

Try / catch

try { await GoCraftResinOnce(country, ct); }
catch (Exception e) when (e.Message.Contains("识别浓缩树脂数量失败"))
{ /* fall back to a conservative craft count (e.g. 0) and continue */ }

Prevention

When it happens

Trigger: WaitForAction predicate (condensedResinCount >= 0 && <= 5) is false for all 3 attempts at 200ms. The cropped OCR region yields a number outside 0-5 (often a misread like 50 or a parse of nearby UI digits), or StringUtils.TryParseInt keeps returning a value > 5.

Common situations: The condensed-resin count region (right of the CondensedResinCount template) shifted and now includes adjacent digits; the OCR reads a stale/garbage value; the template match for CondensedResinCount anchored the crop incorrectly at the current scale; condensed-resin display shows '/5' and OCR captured the denominator.

Related errors


AI-assisted analysis of babalae/better-genshin-impact@a7cb36712d (2026-08-13). Data as JSON: /api/errors/268862ad53439217. Report an issue: GitHub.