From b8bbe3acf51ee7e2f0cf7f982c182a68944bd706 Mon Sep 17 00:00:00 2001 From: Codex Agent Date: Fri, 16 Oct 2026 11:16:52 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9B=94=20=E4=BF=AE=E6=8E=89=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=E8=AE=A9=E7=8B=82=E6=B0=94=E9=98=B2=E7=BA=BF=E4=B8=80?= =?UTF-8?q?=E7=9B=B4=E5=93=91=E7=9D=80=E7=9A=84=20bug:=20OCR=20=E6=8A=8A?= =?UTF-8?q?=20LUNACY=20=E8=AF=BB=E6=88=90=20UNACY?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 按计划任务的方式(pythonw, 无控制台)实跑, 发现"合成前狂气(OCR): None"重试 72 秒仍然读不到 —— 而同一块底栏的体力(read_enkephalin)却一直读得到。 同屏两个读数一个成一个不成, 说明是我的读取逻辑坏了, 不是画面问题。 dump 底栏 OCR 一眼就看到了: UNACY @(674,795) <- 本来应该是 "LUNACY", 首字母 L 被吃掉 9699 @(664,823) <- 数字明明在 而代码写的是 `'LUNACY' in text` —— "UNACY" 不含 "LUNACY", 所以每次都被判成 "读不到", 狂气校验直接退化成 None -> 跳过比对。**第三道防线一直是形同虚设的**, 而且因为日志里只写"读不到", 看起来像是画面没渲染好, 很难往这个方向想。 修法: 认词根 "NACY"(LUNACY / UNACY / NACY 都能命中, 这个词根不会出现在别处); 再兜一层按位置找标签 —— 底栏那一带(y 775~815, x<700)最靠左的短文本元素。 实测修复后: 找到 'UNACY' @(674,795), read_lunacy() 连读三次都是 9699(稳定)。 新增 5 个用例(截断标签能认、完整标签能认、截断标签下能读出数字、标签完全读不出 时按位置兜底、彻底没有标签时返回 None)。全套 272/272 通过。 --- scripts/convert_modules.py | 44 +++++++++++++++++++++++++++-------- tests/test_convert_modules.py | 42 +++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 10 deletions(-) diff --git a/scripts/convert_modules.py b/scripts/convert_modules.py index 0d5598e..b9e4cbb 100644 --- a/scripts/convert_modules.py +++ b/scripts/convert_modules.py @@ -165,23 +165,47 @@ def panel_open(txt): # ---------------------------------------------------------------- 狂气 +LABEL_KEY = 'NACY' # 见下: LUNACY 的首字母常被 OCR 吃掉, 只认后半段 + + +def find_lunacy_label(): + """找狂气标签所在的那个 OCR 元素。 + + ⛔ 实测坑(2026-10-16 11:11): OCR 把 "LUNACY" 读成了 **"UNACY"** —— 首字母 L + 被吃掉了。而代码原本写的是 `'LUNACY' in text`, "UNACY" 不含 "LUNACY", + 于是每次都被判成"读不到", 狂气校验直接退化成 None -> 跳过比对, 防线形同虚设。 + (同一块底栏的体力却一直读得到, 所以只看日志很难发现。) + + 改成认后半段 "NACY": LUNACY / UNACY / 甚至 NACY 都能命中, 而这个词根 + 在游戏界面里不会出现在别处。 + """ + its = items() + for it in its: + if LABEL_KEY in it.get('text', '').upper(): + return it + # 再退一步: 底栏那一带最靠左的短文本元素(标签位置很固定) + near = [it for it in its + if 775 < it.get('y', 0) < 815 and it.get('x', 0) < 700 + and (it.get('w') or 0) < 100] + if near: + return sorted(near, key=lambda it: it.get('x', 0))[-1] + return None + + def read_lunacy(reads=5, pause=1.0): """读狂气余额, 取多次读数的众数。 - 这个数字是像素风艺术字, OCR 很容易把 6 认成 9、3 认成 8 —— 实测同一个画面 - 上真实 6636 会被读成 9699、真实 6388 被读成 8859。所以: - * 取众数而不是"最后读到什么就是什么"; - * 合成前后各读一次做**相对**比对(同一画面同一字体, 误读方式一致), - 这样即使绝对值读错, 也能发现"数字变了"。 + 两个实测坑: + 1. 标签首字母会被 OCR 吃掉(LUNACY -> UNACY), 所以不能整词匹配, 见 find_lunacy_label。 + 2. 数字是像素风艺术字, OCR 很容易把 6 认成 9、3 认成 8 —— 实测同一个画面上 + 真实 6636 会被读成 9699、真实 6388 被读成 8859。所以取众数, 并且合成前后 + 各读一次做**相对**比对(同一画面同一字体, 误读方式一致), 这样即使绝对值 + 读错, 也能发现"数字变了"。 """ votes = {} for i in range(reads): its = items() - lab = None - for it in its: - if 'LUNACY' in it.get('text', '').upper(): - lab = it - break + lab = find_lunacy_label() if lab: best = None for it in its: diff --git a/tests/test_convert_modules.py b/tests/test_convert_modules.py index 0617530..7627c89 100644 --- a/tests/test_convert_modules.py +++ b/tests/test_convert_modules.py @@ -43,6 +43,48 @@ class ScreenDetectTest(QuietLogMixin, unittest.TestCase): self.assertFalse(cm.panel_open('Window | Sinners | Drive')) +class LunacyLabelTest(QuietLogMixin, unittest.TestCase): + """⛔ 实测坑: OCR 会把 "LUNACY" 读成 "UNACY"(首字母 L 被吃掉)。 + + 原来写的是 `'LUNACY' in text`, 而 "UNACY" 不含 "LUNACY" —— 于是每次都被判成 + "读不到", 狂气校验退化成 None -> 跳过比对, 第三道防线形同虚设。同一块底栏的 + 体力却一直读得到, 所以只看日志很难发现, 是 dump 了底栏 OCR 才抓到的。 + """ + + def _with(self, its): + with mock.patch.object(cm, 'items', lambda: its): + return cm.find_lunacy_label() + + def test_matches_truncated_label(self): + lab = self._with([{'text': 'UNACY', 'x': 674, 'y': 795, 'w': 44, 'h': 22}]) + self.assertIsNotNone(lab) + self.assertEqual(lab['text'], 'UNACY') + + def test_matches_full_label(self): + self.assertIsNotNone(self._with([{'text': 'LUNACY', 'x': 670, 'y': 795}])) + + def test_full_read_works_with_truncated_label(self): + its = [{'text': 'UNACY', 'x': 674, 'y': 795, 'w': 44, 'h': 22}, + {'text': '9699', 'x': 664, 'y': 823, 'w': 70, 'h': 37}] + with mock.patch.object(cm, 'items', lambda: its), \ + mock.patch.object(cm.time, 'sleep', lambda s: None): + self.assertEqual(cm.read_lunacy(), 9699) + + def test_falls_back_to_position_when_label_unreadable(self): + # 标签整个读不出来时, 按位置兜底: 底栏那一带最靠左的短文本 + its = [{'text': '???', 'x': 674, 'y': 795, 'w': 44, 'h': 22}, + {'text': '9699', 'x': 664, 'y': 823, 'w': 70, 'h': 37}] + lab = self._with(its) + self.assertIsNotNone(lab) + with mock.patch.object(cm, 'items', lambda: its), \ + mock.patch.object(cm.time, 'sleep', lambda s: None): + self.assertEqual(cm.read_lunacy(), 9699) + + def test_no_label_at_all_returns_none(self): + its = [{'text': 'Window', 'x': 994, 'y': 809}] + self.assertIsNone(self._with(its)) + + class ReadOwnedTest(QuietLogMixin, unittest.TestCase): def test_reads_owned(self):