diff --git a/eve_rock_watcher.py b/eve_rock_watcher.py index ada6ba7..c77b015 100644 --- a/eve_rock_watcher.py +++ b/eve_rock_watcher.py @@ -81,27 +81,54 @@ def parse_selected(text): return int(digits) if digits and len(digits) >= 2 else None -# The selected rock's info popup shows "Quantity 47,765 Units". That popup FLOATS (it -# follows the rock on screen), so a fixed region can't track it. Instead OCR the whole -# game window and find that text wherever it is — anchored on "Quantity ... Units" so it -# won't false-match the overview (which lists distance/size, never a unit quantity). +# The selected rock's info popup shows "Quantity 47,765 Units" (with "Total Value ... ISK" +# and "Distance ..." above it). That popup FLOATS, so a fixed region can't track it — OCR +# the whole window and find the text wherever it is. Primary anchor: "Quantity N Units". +# Fallback: any "N Units" (3+ digits) in a plausible asteroid range — the overview lists +# distance/size, never "Units", so this won't false-match. QUANTITY_RE = re.compile(r"quantit[yvſ]\s*[:.]?\s*([\d.,]{2,})\s*units?", re.I) +UNITS_RE = re.compile(r"([\d.,]{3,})\s*units?\b", re.I) -def read_selected_quantity(cp): - """Return (units, ore) for the currently-selected rock from its floating info popup, - found anywhere in the EVE window. (None, None) if no rock is selected/visible.""" +def _ocr_window_text(cp): + """OCR the EVE window with sparse-text mode (psm 11) — far better than uniform-block + (psm 6) at picking scattered UI text off a busy nebula background. Returns flat text.""" import pytesseract tcmd = cp.get("ocr", "tesseract_cmd", fallback="").strip() if tcmd: pytesseract.pytesseract.tesseract_cmd = tcmd img = w.capture_window() if img is None: + return None + out = [] + for psm in (11, 6): # sparse first, then block fallback + try: + out.append(pytesseract.image_to_string(img, config=f"--psm {psm}")) + except Exception: + pass + return " ".join(out).replace("\n", " ") + + +def read_selected_quantity(cp): + """Return (units, ore) for the currently-selected rock from its floating info popup, + found anywhere in the EVE window. (None, None) if no rock is selected/visible.""" + flat = _ocr_window_text(cp) + if flat is None: return None, None - text = pytesseract.image_to_string(img, config="--psm 6") - flat = text.replace("\n", " ") m = QUANTITY_RE.search(flat) + if not m: # "Quantity" garbled? take any "N Units" + for cand in UNITS_RE.finditer(flat): + v = int(re.sub(r"\D", "", cand.group(1)) or 0) + if 100 <= v <= 5_000_000: # plausible asteroid remaining + m = cand + break if not m: + # leave a breadcrumb so we can see what OCR produced when it misses + try: + with open(os.path.join(w.HERE, "_rockocr.txt"), "w", encoding="utf-8") as fh: + fh.write(flat[:4000]) + except Exception: + pass return None, None digits = re.sub(r"\D", "", m.group(1)) if len(digits) < 2: