from pathlib import Path
import hashlib,json,time
from PIL import Image
from rembg import remove,new_session,__version__
ROOT=Path(__file__).resolve().parent;out=ROOT/'output';out.mkdir(exist_ok=True)
start=time.perf_counter();session=new_session('u2netp',providers=['CPUExecutionProvider'])
source=Image.open(ROOT/'input.png').convert('RGB');result=remove(source,session=session)
assert result.mode=='RGBA' and result.size==source.size
result.save(out/'cutout.png');result.getchannel('A').save(out/'alpha.png')
record={'tool':'rembg','version':__version__,'model':'u2netp','provider':'CPUExecutionProvider',
 'seconds':round(time.perf_counter()-start,3),'source_sha256':hashlib.sha256((ROOT/'input.png').read_bytes()).hexdigest(),
 'model_run':True,'visual_approval':'not automatically approved; inspect cable and mug handle'}
(out/'run.json').write_text(json.dumps(record,indent=2)+'\n');print(json.dumps(record))
