publish eve_orehold_watcher.py

This commit is contained in:
brockdarnold 2026-06-15 02:20:36 +00:00
parent 00fa861dc8
commit a44131ad07

View file

@ -301,11 +301,14 @@ def grab_region(region):
return Image.frombytes("RGB", raw.size, raw.bgra, "raw", "BGRX") return Image.frombytes("RGB", raw.size, raw.bgra, "raw", "BGRX")
def _detect_readout(cp, img, mon_left=0, mon_top=0, save=True): def _detect_readout(cp, img, mon_left=0, mon_top=0, save=True, scale=1):
"""Find the ore-hold 'cur / cap m3' readout inside a screenshot. Returns the """Find the ore-hold 'cur / cap m3' readout inside a screenshot. Returns the
absolute-screen region [l,t,w,h] (and saves it) or None. Split out from the absolute-screen region [l,t,w,h] (and saves it) or None. `scale` = how much `img`
screen-grab so it can be unit-tested against a rendered image.""" was upscaled vs screen (coords are divided back). Split out so it's unit-testable."""
import pytesseract import pytesseract
tcmd = cp.get("ocr", "tesseract_cmd", fallback="").strip() if cp else ""
if tcmd:
pytesseract.pytesseract.tesseract_cmd = tcmd
data = pytesseract.image_to_data(img, output_type=pytesseract.Output.DICT) data = pytesseract.image_to_data(img, output_type=pytesseract.Output.DICT)
lines = {} lines = {}
for i in range(len(data["text"])): for i in range(len(data["text"])):
@ -322,10 +325,10 @@ def _detect_readout(cp, img, mon_left=0, mon_top=0, save=True):
cur, cap = parsed cur, cap = parsed
if not (500 <= cap <= 2_000_000): # plausible ore/gas hold capacity if not (500 <= cap <= 2_000_000): # plausible ore/gas hold capacity
continue continue
xs = [data["left"][i] for i in idxs] xs = [data["left"][i] // scale for i in idxs]
ys = [data["top"][i] for i in idxs] ys = [data["top"][i] // scale for i in idxs]
rights = [data["left"][i] + data["width"][i] for i in idxs] rights = [(data["left"][i] + data["width"][i]) // scale for i in idxs]
bots = [data["top"][i] + data["height"][i] for i in idxs] bots = [(data["top"][i] + data["height"][i]) // scale for i in idxs]
pad = 8 pad = 8
region = [mon_left + min(xs) - pad, mon_top + min(ys) - pad, region = [mon_left + min(xs) - pad, mon_top + min(ys) - pad,
(max(rights) - min(xs)) + 2 * pad, (max(bots) - min(ys)) + 2 * pad] (max(rights) - min(xs)) + 2 * pad, (max(bots) - min(ys)) + 2 * pad]
@ -351,10 +354,15 @@ def auto_region(cp):
mon = sct.monitors[0] # full virtual desktop (all screens) mon = sct.monitors[0] # full virtual desktop (all screens)
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")
# crop the center band (where EVE sits) then upscale, so small m³ text is readable
W, Hh = img.size
cx0, cy0 = int(W * 0.22), 0
crop = img.crop((cx0, cy0, int(W * 0.82), int(Hh * 0.97)))
up = crop.resize((crop.width * 2, crop.height * 2))
except Exception as e: except Exception as e:
print(f"[ocr] auto-detect error: {e}") print(f"[ocr] auto-detect error: {e}")
return None return None
return _detect_readout(cp, img, mon["left"], mon["top"]) return _detect_readout(cp, up, mon["left"] + cx0, mon["top"] + cy0, scale=2)
def run_ocr(cp): def run_ocr(cp):