{"record":{"id":"6b4c49a839eb2b54","repo":"babalae/better-genshin-impact","slug":"fieldname-ocr-maxocrattempts-requir","errorCode":null,"errorMessage":"{fieldName} OCR 在 {MaxOcrAttempts} 次内未达到连续 {RequiredStableOcrCount} 次一致，最大连续次数={stableValues.MaxConsecutiveCount}，末次结果={lastText}","messagePattern":"(.+?) OCR 在 (.+?) 次内未达到连续 (.+?) 次一致，最大连续次数=(.+?)，末次结果=(.+?)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"BetterGenshinImpact/GameTask/CharacterDevelopment/CharacterDevelopmentTask.cs","lineNumber":942,"sourceCode":"                if (isStable)\n                {\n                    return (level, limit);\n                }\n            }\n            else\n            {\n                stableValues.Reset();\n                _logger.LogDebug(\"角色养成识别：{FieldName} OCR 第 {Attempt}/{MaxAttempts} 次解析失败，结果={Text}\",\n                    fieldName, attempt, MaxOcrAttempts, lastText);\n            }\n\n            if (attempt < MaxOcrAttempts)\n            {\n                await Delay(OcrRetryDelayMilliseconds, _ct);\n            }\n        }\n\n        throw new InvalidOperationException(\n            $\"{fieldName} OCR 在 {MaxOcrAttempts} 次内未达到连续 {RequiredStableOcrCount} 次一致，\" +\n            $\"最大连续次数={stableValues.MaxConsecutiveCount}，末次结果={lastText}\");\n    }\n\n    /// <summary>\n    /// 重复 OCR 武器名，并先通过标准武器表纠错；纠错后的名称连续三次一致才返回。\n    /// </summary>\n    private async Task<string> ReadWeaponNameWithRetry()\n    {\n        var stableNames = new StableValueAccumulator<string>(RequiredStableOcrCount);\n        var lastOcrText = string.Empty;\n        var lastMatchedName = string.Empty;\n        for (var attempt = 1; attempt <= MaxOcrAttempts; attempt++)\n        {\n            using var capture = CaptureToRectArea();\n            lastOcrText = OcrText(\n                capture,\n                Rect1080(1465, 132, 346, 42)).Trim();","sourceCodeStart":924,"sourceCodeEnd":960,"githubUrl":"https://github.com/babalae/better-genshin-impact/blob/a7cb36712dcb409be610257d877fcea3597e9d6b/BetterGenshinImpact/GameTask/CharacterDevelopment/CharacterDevelopmentTask.cs#L924-L960","documentation":"`ReadLevelPairWithRetry` samples the level text up to `MaxOcrAttempts` (10) times, requiring `RequiredStableOcrCount` (3) consecutive identical parses. If it never achieves three-in-a-row it throws, reporting the best streak and the last raw text. This protects against returning a noisy/wrong level/limit pair.","triggerScenarios":"Persistent OCR jitter on the level ROI (e.g. 'Lv.90/90' flickering between parses); the ROI covering the wrong region so it never parses two numbers; animations/particles overlapping the level text.","commonSituations":"Wrong capture resolution breaking the 1080p-scaled ROI; a game UI change moving the level text; slow render making early frames unreadable; anti-aliasing/overlay interference.","solutions":["Confirm the capture resolution and `_assetScale` produce an ROI that actually covers the level text.","Raise `MaxOcrAttempts` / add a longer `OcrRetryDelayMilliseconds` if jitter is transient.","Update the level ROI constants if the in-game layout changed.","Preprocess the ROI (threshold/upscale) to stabilize PaddleOCR output."],"exampleFix":"// before: ROI misaligned, never stable\nvar (lvl, lim) = await ReadLevelPairWithRetry(Rect1080(1467,207,172,35), \"角色等级\");\n\n// after: correct ROI for current layout, or increase attempts\nprivate const int MaxOcrAttempts = 20;","handlingStrategy":"retry","validationCode":"// The loop already retries 10×; prevent by ensuring the ROI is correct:\n// verify _assetScale and the level ROI cover 'Lv.X/Y' at the current resolution.","typeGuard":null,"tryCatchPattern":"try { await ReadLevelPairWithRetry(roi, fieldName); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"OCR\"))\n{ /* log lastText, check ROI/scale, optionally raise MaxOcrAttempts */ }","preventionTips":["Confirm capture resolution and _assetScale place the ROI on the level text.","Update level ROI constants when the in-game layout changes.","Increase MaxOcrAttempts / retry delay for genuinely noisy captures."],"tags":["ocr","retry-exhausted","paddleocr","level","stability","character-development"],"backgroundTag":null,"analyzedSha":"a7cb36712dcb409be610257d877fcea3597e9d6b","analyzedAt":"2026-08-13T16:44:57.548Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}