publish eve_rock_watcher.py

This commit is contained in:
brockdarnold 2026-06-15 01:19:05 +00:00
parent c769680457
commit f434d3d94c

View file

@ -66,8 +66,9 @@ def parse_survey(text):
return rows return rows
# Selected-Item panel (no survey scanner needed): "Quantity 5,593 Units" # Selected-Item panel (no survey scanner needed): "...Quantity 40,370 Units".
QTY_RE = re.compile(r"quantit\w*\s*([\d.,]{2,})\s*units?", re.I) # Match the NUMBER before "Units" — don't require the word "Quantity" (OCR drops it).
QTY_RE = re.compile(r"([\d.,]{2,})\s*units?\b", re.I)
def parse_selected(text): def parse_selected(text):
@ -76,7 +77,7 @@ def parse_selected(text):
if not m: if not m:
return None return None
digits = re.sub(r"\D", "", m.group(1)) # tolerate OCR , vs . in the number digits = re.sub(r"\D", "", m.group(1)) # tolerate OCR , vs . in the number
return int(digits) if digits else None return int(digits) if digits and len(digits) >= 2 else None
def save_rock_region(cp, l, t, wd, ht, mode="survey"): def save_rock_region(cp, l, t, wd, ht, mode="survey"):
@ -96,11 +97,13 @@ def detect_selected_region(cp):
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
S = 2 # upscale so small panel text is found
try: try:
with mss.mss() as sct: with mss.mss() as sct:
mon = sct.monitors[0] mon = sct.monitors[0]
raw = sct.grab(mon) raw = sct.grab(mon)
img = Image.frombytes("RGB", raw.size, raw.bgra, "raw", "BGRX") img = Image.frombytes("RGB", raw.size, raw.bgra, "raw", "BGRX")
img = img.resize((img.width * S, img.height * S))
data = pytesseract.image_to_data(img, output_type=pytesseract.Output.DICT) data = pytesseract.image_to_data(img, output_type=pytesseract.Output.DICT)
except Exception as e: except Exception as e:
print(f"[rock] detect error: {e}") print(f"[rock] detect error: {e}")
@ -113,14 +116,15 @@ def detect_selected_region(cp):
lines.setdefault(k, []).append(i) lines.setdefault(k, []).append(i)
for idxs in lines.values(): for idxs in lines.values():
joined = " ".join(data["text"][i] for i in idxs) joined = " ".join(data["text"][i] for i in idxs)
if QTY_RE.search(joined): # the 'Quantity N Units' line: contains 'unit' + a digit (tolerant to OCR)
xs = [data["left"][i] for i in idxs] if "unit" in joined.lower() and re.search(r"\d", joined):
ys = [data["top"][i] for i in idxs] xs = [data["left"][i] // S for i in idxs] # map 2x coords back to screen
rs = [data["left"][i] + data["width"][i] for i in idxs] ys = [data["top"][i] // S for i in idxs]
bs = [data["top"][i] + data["height"][i] for i in idxs] rs = [(data["left"][i] + data["width"][i]) // S for i in idxs]
pad = 10 bs = [(data["top"][i] + data["height"][i]) // S for i in idxs]
region = [mon["left"] + min(xs) - pad, mon["top"] + min(ys) - pad, padx = 170 # extend LEFT to capture the number
(max(rs) - min(xs)) + 2 * pad, (max(bs) - min(ys)) + 2 * pad] region = [mon["left"] + min(xs) - padx, mon["top"] + min(ys) - 8,
(max(rs) - min(xs)) + padx + 20, (max(bs) - min(ys)) + 16]
save_rock_region(cp, *region, mode="selected") save_rock_region(cp, *region, mode="selected")
print(f"[rock] auto-detected Selected-Item quantity -> {region}") print(f"[rock] auto-detected Selected-Item quantity -> {region}")
return region return region
@ -233,7 +237,12 @@ def get_region(cp):
def read_rows(cp, region, mode): def read_rows(cp, region, mode):
import pytesseract import pytesseract
text = pytesseract.image_to_string(w.grab_region(region)) img = w.grab_region(region)
try: # upscale: small panel text reads better
img = img.resize((img.width * 2, img.height * 2))
except Exception:
pass
text = pytesseract.image_to_string(img, config="--psm 6")
if mode == "selected": if mode == "selected":
q = parse_selected(text) q = parse_selected(text)
return [("rock", q)] if q else [] return [("rock", q)] if q else []