from pathlib import Path
import argparse,json,sys,hashlib
p=argparse.ArgumentParser();p.add_argument('--repo',required=True);p.add_argument('--model',required=True);a=p.parse_args()
sys.path.insert(0,str(Path(a.repo).resolve()))
import cv2
from basicsr.archs.rrdbnet_arch import RRDBNet
from realesrgan import RealESRGANer
ROOT=Path(__file__).resolve().parent;out=ROOT/'output';out.mkdir(exist_ok=True)
network=RRDBNet(num_in_ch=3,num_out_ch=3,num_feat=64,num_block=23,num_grow_ch=32,scale=4)
upsampler=RealESRGANer(scale=4,model_path=a.model,model=network,tile=0,tile_pad=10,pre_pad=0,half=False)
image=cv2.imread(str(ROOT/'input.png'),cv2.IMREAD_UNCHANGED);assert image is not None and image.shape[2]==4
records=[]
for mode in ['realesrgan','bicubic']:
 result,_=upsampler.enhance(image,outscale=4,alpha_upsampler=mode)
 assert cv2.imwrite(str(out/f'alpha-{mode}.png'),result)
 records.append({'alpha_upsampler':mode,'model_run':True,'model_sha256':hashlib.sha256(Path(a.model).read_bytes()).hexdigest(),'fp32':True,'outscale':4})
(out/'run.json').write_text(json.dumps(records,indent=2)+'\n')
