Spaces:
Paused
Paused
Download hf_workspace/scripts/benchmark_client.py from modventures/iris: direct link, hf CLI and curl.
- Browser
- Download file 2.91 kB
-
https://huggingface.co/spaces/modventures/iris/resolve/main/hf_workspace/scripts/benchmark_client.py
- Command line
-
hf download hf://spaces/modventures/iris/hf_workspace/scripts/benchmark_client.py
-
curl -L -o benchmark_client.py https://huggingface.co/spaces/modventures/iris/resolve/main/hf_workspace/scripts/benchmark_client.py
2.91 kB
| """Native-resolution, manually cropped client evaluation. No reference-result pixels enter processing.""" | |
| import sys | |
| from pathlib import Path | |
| sys.path.insert(0,str(Path(__file__).resolve().parents[1])) | |
| import json,time | |
| import cv2 | |
| import numpy as np | |
| from PIL import Image,ImageOps | |
| from backend.registration import register,RegistrationError | |
| ROOT=Path(__file__).resolve().parents[1] | |
| SOURCE=Path('/Users/tstore/Desktop/iris test files') | |
| OUT=ROOT/'private_benchmarks/client_iris' | |
| PAIRS=[ | |
| ('1','AYD04756.JPG',(2800,2100),'AYD04769.JPG',(2960,2050),'AYD04755.jpg'), | |
| ('2','DSC03085.JPG',(2580,2000),'DSC03087.JPG',(2630,2000),'DSC03084.JPG'), | |
| ('3','DSC03094.JPG',(2440,1910),'DSC03096.JPG',(2560,1900),'DSC03092.JPG'), | |
| ('4','AYD04652.JPG',(2610,2110),'AYD04662.JPG',(2720,2180),'AYD04650.JPG'), | |
| ('5','AYD04703.JPG',(1850,2375),'AYD04720.JPG',(3120,2535),'AYD04702.jpg'), | |
| ('6','AYD04725.JPG',(2300,2420),'AYD04727.JPG',(2090,2400),'AYD04724.JPG'), | |
| ] | |
| def read(p): | |
| with Image.open(p) as image:return cv2.cvtColor(np.array(ImageOps.exif_transpose(image).convert('RGB')),cv2.COLOR_RGB2BGR) | |
| def save(p,a): | |
| cv2.imwrite(str(p),a) | |
| def crop(image,center): | |
| x,y=center | |
| return image[y-1100:y+1100,x-1100:x+1100].copy(),[x-1100,y-1100,x+1100,y+1100] | |
| def brightness(image): | |
| y,x=np.indices(image.shape[:2]);r=np.hypot(x-1100,y-1100) | |
| g=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY) | |
| mask=(r>350)&(r<800)&(g>15)&(g<230) | |
| return float(np.median(g[mask])) | |
| rows=[] | |
| for sample,a,ac,b,bc,finished in PAIRS: | |
| folder=OUT/sample;folder.mkdir(parents=True,exist_ok=True) | |
| ia,ba=crop(read(SOURCE/sample/a),ac) | |
| ib,bb=crop(read(SOURCE/sample/b),bc) | |
| if brightness(ib)>brightness(ia):a,b,ia,ib,ba,bb=b,a,ib,ia,bb,ba | |
| row=dict(sample=sample,reference=a,moving=b,manual_result=finished,reference_crop=ba,moving_crop=bb,resolution=list(ia.shape[1::-1]),reference_brightness=brightness(ia),moving_brightness=brightness(ib)) | |
| save(folder/'reference.png',ia);save(folder/'moving.png',ib) | |
| save(folder/'manual_result.png',read(SOURCE/sample/finished)) | |
| save(folder/'before_overlay.png',cv2.addWeighted(ia,.5,ib,.5,0)) | |
| try: | |
| result=register(ia,ib,return_maps=True) | |
| row['status']='OUTPUT PRODUCED - VISUAL REVIEW REQUIRED';row['metrics']=result['metrics'] | |
| for k,v in result.items(): | |
| if k not in ('metrics','sampling_map'):save(folder/(k+'.png'),v) | |
| np.savez_compressed(folder/'sampling_map.npz',map=result['sampling_map']) | |
| # Native detail crop at the same reference position, for inspection only. | |
| for k in ('reference','moving','before_overlay','overlay','composite','adjusted','global_aligned'): | |
| im=cv2.imread(str(folder/(k+'.png'))) | |
| save(folder/(k+'-detail.png'),im[500:1000,1100:1600]) | |
| except RegistrationError as exc: | |
| row.update(status='REJECTED',reason=str(exc)) | |
| rows.append(row) | |
| (OUT/'metrics.json').write_text(json.dumps(rows,indent=2)) | |
| print(json.dumps(row),flush=True) | |
| del ia,ib | |
| if 'result' in locals():del result | |