Datasets:
File size: 1,964 Bytes
2b4b5f2 | 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 | import requests, re, json, os
H = "https://huggingface.co"
from pymupdf import Document as MuDoc
def norm(s): return re.sub(r'[^0-9]','',str(s))
# Simulate frontend search + verify each returned Imudex product's image maps to the
# correct column-aware region on its page (the exact fix logic).
pdf=requests.get(H+"/datasets/bep40/grob-products-updated/resolve/main/pdfs/imudex_2026.pdf", timeout=600).content
open("/tmp/imudex.pdf","wb").write(pdf)
doc=MuDoc("/tmp/imudex.pdf")
# page images per pdf index
def page_imgs(pgn):
pg=doc[pgn]; out=[]
for ip in pg.get_image_info():
bb=ip["bbox"]; w=ip.get("width") or 0; h=ip.get("height") or 0
if w>=160 and h>=110:
out.append({"x0":bb[0],"y0":bb[1],"x1":bb[2],"y1":bb[3],"w":w,"h":h})
return out
PAGE_IMGS={pgn:page_imgs(pgn) for pgn in range(len(doc))}
def side(x): return 'L' if x<567 else 'R'
def expected_img(sku_page_no, code_x, code_y):
# find the image whose column matches code side, nearest vertically
pgn=sku_page_no-1
imgs=PAGE_IMGS[pgn]; s=side(code_x)
cand=[im for im in imgs if side((im["x0"]+im["x1"])/2)==s]
if not cand: return None
bd=1e15; best=None
for im in cand:
icy=(im["y0"]+im["y1"])/2
dd=abs(icy-code_y)
if dd<bd: bd=dd; best=im
return best
# re-dump page 81 code rows to validate mapping expectation for key SKUs
code_pat=re.compile(r'^[\d\s.]{6,}$')
d=doc[80].get_text("dict")
rows={}
for b in d["blocks"]:
if b["type"]!=0: continue
for l in b["lines"]:
txt="".join(s["text"] for s in l["spans"]).strip()
if txt and code_pat.match(txt.strip()):
x0,y0,x1,y1=l["bbox"]
rows[norm(txt)]=(round(x0,1),round(y0,1))
for sk,row in rows.items():
im=expected_img(81, row[0], row[1])
print(f"page81 code {sk} -> img bbox {[round(im['x0']),round(im['y0']),round(im['x1']),round(im['y1'])] if im else None} label={ 'L' if row[0]<567 else 'R'}")
|