import sys,os,json,pickle,numpy as np,cv2,time sys.path.insert(0,'code'); sys.path.insert(0,os.path.dirname(__file__)) from ocr import VisionOcr from diag import crops S=os.path.dirname(__file__) L=json.load(open(f'{S}/../../../../eval/labels/image6.json')) # distinguishing tokens per SKU (any hit counts); includes siblings for near-unknowns KEY={'sku_l01':['エッグケア','卵を使って'],'sku_l02':['アマニ'],'sku_l03':['からし','Spicy'],'sku_l04':['めんたい'], 'sku_l05':['ゼロ','ノンコレ'],'sku_l06':['フィッテ','内臓脂肪'],'sku_l07':['ラッキー'],'sku_l08':['PERI','ペリ'], 'sku_l09':['コクうま','65'],'sku_l10':['ピュアセレクト','マヨネーズ'],'sku_l11':['ハーフ','1/2'],'kewpie450':['KEWPIE','マヨネーズ']} o=VisionOcr(languages=['ja-JP','en-US']); out={} for res in ['full','live']: rows=[] for fc,c in crops('add/image6.jpg',L,res=='live'): if fc['label']=='exclude' or c.size==0 or min(c.shape[:2])<8: continue for up in [1,2]: cc=cv2.resize(c,None,fx=up,fy=up,interpolation=cv2.INTER_CUBIC) if up>1 else c t=time.perf_counter(); r=o.read(cc); ms=(time.perf_counter()-t)*1000 txt=' '.join(l.text for l in r.lines) rows.append(dict(index=fc['index'],truth=fc['label'],up=up,text=txt,ms=ms)) out[res]=rows pickle.dump(out,open(f'{S}/ocr.pkl','wb')) for res,rows in out.items(): for up in [1,2]: rr=[r for r in rows if r['up']==up]; tr=[r for r in rr if r['truth'] in KEY] hit=sum(any(k in r['text'] for k in KEY[r['truth']]) for r in tr) print(res,'up',up,f'in-catalog key-token hit {hit}/{len(tr)}','median ms',round(np.median([r['ms'] for r in rr]),1)) for r in out['live']: if r['up']==2 and r['truth']!='unknown': print(r['index'],r['truth'],'|',r['text'][:80])