Spaces:
Paused
Paused
Download hf_workspace/scripts/refine_client_research.py from modventures/iris: direct link, hf CLI and curl.
- Browser
- Download file 3.26 kB
-
https://huggingface.co/spaces/modventures/iris/resolve/main/hf_workspace/scripts/refine_client_research.py
- Command line
-
hf download hf://spaces/modventures/iris/hf_workspace/scripts/refine_client_research.py
-
curl -L -o refine_client_research.py https://huggingface.co/spaces/modventures/iris/resolve/main/hf_workspace/scripts/refine_client_research.py
3.26 kB
| """Evaluate both matchers plus masked local refinement. Research only.""" | |
| import sys,json,time | |
| from pathlib import Path | |
| sys.path.insert(0,str(Path(__file__).resolve().parents[1])) | |
| import cv2,numpy as np | |
| from backend.experimental.refinement import local_map | |
| 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.json').read_text()): | |
| n,method=rec['sample'],rec['method'];folder=OUT/f'{n}-{method}';folder.mkdir(exist_ok=True) | |
| row=dict(rec) | |
| if 'matrix' not in rec or rec.get('train_inliers',0)<12 or rec['train_inliers']/rec['train_count']<.3 or rec['test_count']<8: | |
| row['status']='insufficient evidence';rows.append(row);continue | |
| source=ROOT/'private_benchmarks/client_iris'/str(n) | |
| a=cv2.imread(str(source/'reference.png'));b=cv2.imread(str(source/'moving.png')) | |
| matrix=np.array(rec['matrix']);h,w=a.shape[:2] | |
| # Reject implausible global geometry before attempting to produce a preview. | |
| corners=np.float32([[0,0],[w,0],[w,h],[0,h]]) | |
| wc=cv2.perspectiveTransform(corners[None],matrix)[0] | |
| area=cv2.contourArea(wc,oriented=True)/(h*w) | |
| if not .5<area<2: | |
| row['status']='implausible geometry';rows.append(row);continue | |
| mapping,trust,info,global_image=local_map(a,b,matrix) | |
| 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 | |
| predicted=cv2.remap(mapping,dst[hold,0,None],dst[hold,1,None],cv2.INTER_LINEAR)[:,0] | |
| errors=np.linalg.norm(predicted-src[hold],axis=1) | |
| row.update(local=info,final_heldout_median=float(np.median(errors)),final_heldout_p95=float(np.percentile(errors,95))) | |
| # Test matches were never used for geometric fitting; they are still automatic, | |
| # potentially erroneous labels. This is consistency, not anatomical accuracy. | |
| row['status']='candidate - needs human validation' if np.median(errors)<4 and np.percentile(errors,95)<12 else 'heldout disagreement' | |
| aligned=cv2.remap(b,mapping[...,0],mapping[...,1],cv2.INTER_LINEAR) | |
| roi=iris_mask(a);glarea=glare_mask(a);glareb=glare_mask(aligned) | |
| ga=cv2.cvtColor(a,cv2.COLOR_BGR2GRAY);gb=cv2.cvtColor(aligned,cv2.COLOR_BGR2GRAY) | |
| clean=roi&~glarea&~glareb&(ga>20)&(gb>20) | |
| gain=float(np.clip(np.median(ga[clean]/np.maximum(gb[clean],1)),1,2.5)) | |
| adjusted=np.clip(aligned.astype(np.float32)*gain,0,255).astype(np.uint8) | |
| weight=(trust&clean).astype(np.float32)*.35 | |
| # Conservative baseline composite; no speculative reflection removal here. | |
| composite=np.rint(a*(1-weight[...,None])+adjusted*weight[...,None]).astype(np.uint8) | |
| outputs=dict(aligned=aligned,adjusted=adjusted,overlay=cv2.addWeighted(a,.5,adjusted,.5,0),composite=composite,confidence=trust.astype(np.uint8)*255,global_aligned=global_image) | |
| for key,value in outputs.items():cv2.imwrite(str(folder/(key+'.png')),value) | |
| np.savez_compressed(folder/'mapping.npz',map=mapping) | |
| row['exposure_gain']=gain | |
| rows.append(row);(OUT/'refinement.json').write_text(json.dumps(rows,indent=2));print(json.dumps(row),flush=True) | |
| (OUT/'refinement.json').write_text(json.dumps(rows,indent=2)) | |