#!/usr/bin/env python3 """Match a captured game frame against candidate .wmv files by frame signature. Static + previously-captured frames. See docs/re/movie-binding.md.""" import numpy as np, subprocess, sys, os from PIL import Image W,H=32,18 def sig_img(path, crop_bottom=False): im=Image.open(path).convert("L") if crop_bottom: # movie frame -> what the capture can see w,h=im.size; im=im.crop((0,0,w,int(h*675/720))) a=np.asarray(im.resize((W,H)),dtype=np.float32).ravel() return (a-a.mean())/(a.std()+1e-6) def movie_sigs(path, fps=1.0): out=f"/tmp/q9/fr_{os.path.basename(path).replace('.wmv','')}" os.makedirs(out, exist_ok=True) if not os.listdir(out): subprocess.run(["ffmpeg","-v","error","-i",path,"-vf",f"fps={fps},scale=160:90", "-y",f"{out}/f%05d.png"],check=True) fs=sorted(os.listdir(out)) return [(int(f[1:6]), sig_img(os.path.join(out,f), crop_bottom=True)) for f in fs]