Spaces:
Paused
Paused
File size: 2,459 Bytes
1d21e88 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | import sys,json
from pathlib import Path
sys.path.insert(0,str(Path(__file__).resolve().parents[1]))
import cv2,numpy as np
from backend.experimental.mesh import fit_mesh,dense_mesh
from backend.experimental.preprocessing import iris_mask,glare_mask
ROOT=Path(__file__).resolve().parents[1];OUT=ROOT/'private_benchmarks/research'
rows=[]
for rec in json.loads((OUT/'matchers-patch.json').read_text()):
if rec['method'] not in ('loftr','loftr_patch') or 'matrix' not in rec:continue
n,method=rec['sample'],rec['method'];pairs=np.load(OUT/f'{n}-{method}-matches.npz');dst,src=pairs['reference'],pairs['moving']
hold=((dst[:,0]//150+dst[:,1]//150).astype(int)%5)==0
coeff=fit_mesh(dst[~hold],src[~hold],np.array(rec['matrix']),(2200,2200))
mapping,good,delta=dense_mesh(coeff,np.array(rec['matrix']),(2200,2200))
pred=cv2.remap(mapping,dst[hold,0,None],dst[hold,1,None],cv2.INTER_LINEAR)[:,0];errors=np.linalg.norm(pred-src[hold],axis=1)
row=dict(sample=n,method=method+'_mesh',matches=rec['matches'],test_count=rec['test_count'],global_median=rec['heldout_median'],global_p95=rec['heldout_p95'],mesh_median=float(np.median(errors)),mesh_p95=float(np.percentile(errors,95)))
path=ROOT/'private_benchmarks/client_iris'/str(n);a=cv2.imread(str(path/'reference.png'));b=cv2.imread(str(path/'moving.png'))
aligned=cv2.remap(b,mapping[...,0],mapping[...,1],cv2.INTER_LINEAR)
roi=iris_mask(a);clean=roi&~glare_mask(a)&~glare_mask(aligned)
row['geometry_valid_fraction']=float(good[roi].mean())
row['status']='candidate - needs human validation' if row['mesh_median']<4 and row['mesh_p95']<12 and row['geometry_valid_fraction']>.99 else 'rejected'
ga=cv2.cvtColor(a,cv2.COLOR_BGR2GRAY);gb=cv2.cvtColor(aligned,cv2.COLOR_BGR2GRAY);usable=clean&(ga>20)&(gb>20)
gain=float(np.clip(np.median(ga[usable]/np.maximum(gb[usable],1)),1,2.5))
adjusted=np.clip(aligned.astype(np.float32)*gain,0,255).astype(np.uint8)
folder=OUT/f'{n}-{method}_mesh';folder.mkdir(exist_ok=True)
cv2.imwrite(str(folder/'aligned.png'),aligned);cv2.imwrite(str(folder/'adjusted.png'),adjusted);cv2.imwrite(str(folder/'overlay.png'),cv2.addWeighted(a,.5,adjusted,.5,0));cv2.imwrite(str(folder/'geometry_valid.png'),good.astype(np.uint8)*255)
# No composite until image-space confidence and manual review are established.
np.savez_compressed(folder/'mapping.npz',map=mapping)
rows.append(row);print(json.dumps(row),flush=True);(OUT/'mesh.json').write_text(json.dumps(rows,indent=2))
|