import sys,os,glob,pickle,numpy as np,cv2,json sys.path.insert(0,'code'); sys.path.insert(0,os.path.dirname(__file__)) os.environ['YOLO_OFFLINE']='1' from detector import ShelfProductDetector from variants import Matcher from PIL import Image det=ShelfProductDetector() res={} for cat in sys.argv[1:]: name=os.path.basename(cat); m=Matcher(cat,root=True,geom='affine',scales=(200,400)); rows=[] for p in sorted(glob.glob('add/*')): if 'image6' in p: continue im=cv2.cvtColor(np.array(Image.open(p).convert('RGB')),cv2.COLOR_RGB2BGR) for b in det.detect(im).get('boxes',[]): x1,y1,x2,y2=[int(v) for v in b['box']]; c=im[max(0,y1):y2,max(0,x1):x2] if c.size==0 or min(c.shape[:2])<20: continue rows.append(dict(img=p,ev=m.score(c))) res[name]=rows; print(name,len(rows),'distractor crops') pickle.dump(res,open(f'{os.path.dirname(__file__)}/distract.pkl','wb'))