publish eve_rock_watcher.py
This commit is contained in:
parent
97397ecb37
commit
11d569c8ac
1 changed files with 36 additions and 9 deletions
|
|
@ -81,27 +81,54 @@ def parse_selected(text):
|
||||||
return int(digits) if digits and len(digits) >= 2 else None
|
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
|
# The selected rock's info popup shows "Quantity 47,765 Units" (with "Total Value ... ISK"
|
||||||
# follows the rock on screen), so a fixed region can't track it. Instead OCR the whole
|
# and "Distance ..." above it). That popup FLOATS, so a fixed region can't track it — OCR
|
||||||
# game window and find that text wherever it is — anchored on "Quantity ... Units" so it
|
# the whole window and find the text wherever it is. Primary anchor: "Quantity N Units".
|
||||||
# won't false-match the overview (which lists distance/size, never a unit quantity).
|
# 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)
|
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):
|
def _ocr_window_text(cp):
|
||||||
"""Return (units, ore) for the currently-selected rock from its floating info popup,
|
"""OCR the EVE window with sparse-text mode (psm 11) — far better than uniform-block
|
||||||
found anywhere in the EVE window. (None, None) if no rock is selected/visible."""
|
(psm 6) at picking scattered UI text off a busy nebula background. Returns flat text."""
|
||||||
import pytesseract
|
import pytesseract
|
||||||
tcmd = cp.get("ocr", "tesseract_cmd", fallback="").strip()
|
tcmd = cp.get("ocr", "tesseract_cmd", fallback="").strip()
|
||||||
if tcmd:
|
if tcmd:
|
||||||
pytesseract.pytesseract.tesseract_cmd = tcmd
|
pytesseract.pytesseract.tesseract_cmd = tcmd
|
||||||
img = w.capture_window()
|
img = w.capture_window()
|
||||||
if img is None:
|
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
|
return None, None
|
||||||
text = pytesseract.image_to_string(img, config="--psm 6")
|
|
||||||
flat = text.replace("\n", " ")
|
|
||||||
m = QUANTITY_RE.search(flat)
|
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:
|
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
|
return None, None
|
||||||
digits = re.sub(r"\D", "", m.group(1))
|
digits = re.sub(r"\D", "", m.group(1))
|
||||||
if len(digits) < 2:
|
if len(digits) < 2:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue