"""Fixed probe + a live cross-check against an independent grabber. Stream is restarted every 30 s (a single long-lived one freezes). Every 60 s an `import` grab of the same display is taken and compared: if the two disagree by more than a few percent, the stream is lying and the run's numbers are void. """ import subprocess, sys, time, os import numpy as np from PIL import Image W,H,SURF = 1280,720,45 LIMIT = float(sys.argv[1]) if len(sys.argv)>1 else 540 RESTART = 30 def _open(): return subprocess.Popen(["ffmpeg","-loglevel","error","-f","x11grab","-draw_mouse","0", "-video_size",f"{W}x{H}","-i",":98","-r","4","-f","rawvideo","-pix_fmt","rgb24","-"], stdout=subprocess.PIPE, bufsize=W*H*3*2) def independent(): subprocess.run(["/usr/local/bin/screenshot","/tmp/xcheck.png"],capture_output=True) try: return float(np.asarray(Image.open("/tmp/xcheck.png").convert("RGB")).astype(int)[SURF:].mean()) except Exception: return -1.0 p=_open(); n=W*H*3 t0=time.time(); seg=t0; lastx=t0; lastr=-1 frames=0; best=0; disagree=0; checks=0 while time.time()-t0 < LIMIT: if time.time()-seg > RESTART: p.kill(); p=_open(); seg=time.time() buf=p.stdout.read(n) if len(buf)130)&(g-r>45)&(g-b>45)).sum()) if c>best: best=c if c>=400: Image.fromarray(a[SURF:].astype(np.uint8)).save("/sylph-home/re/shots/probe3-title.png") print(f"*** TITLE t={time.time()-t0:.1f}s glyph={c}",flush=True) if time.time()-lastx > 60: lastx=time.time(); checks+=1 iv=independent() bad = iv>=0 and abs(iv-sm) > max(3.0, 0.15*max(iv,sm)) if bad: disagree+=1 print(f" t={int(time.time()-t0):4d}s stream {sm:7.2f} | import {iv:7.2f} " f"{'<-- DISAGREE' if bad else 'agree'} glyph {c}",flush=True) p.kill(); dt=time.time()-t0 print(f"{frames} frames in {dt:.1f}s = {frames/dt:.2f} fps") print(f"cross-checks {checks}, disagreements {disagree}") print(f"max glyph {best}")