File size: 2,910 Bytes
b5af7d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
"""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