Datasets:
Download scripts/florence_caption.py from bep40/grob-products-updated: direct link, hf CLI and curl.
- Browser
- Download file 1.22 kB
-
https://huggingface.co/datasets/bep40/grob-products-updated/resolve/main/scripts/florence_caption.py
- Command line
-
hf download hf://datasets/bep40/grob-products-updated/scripts/florence_caption.py
-
curl -L -o florence_caption.py https://huggingface.co/datasets/bep40/grob-products-updated/resolve/main/scripts/florence_caption.py
1.22 kB
| import requests, io | |
| from PIL import Image | |
| from transformers import AutoProcessor, AutoModelForCausalLM | |
| pdf_r = requests.get("https://huggingface.co/datasets/bep40/grob-products-updated/resolve/main/catpages/imudex-2026/page_0080.jpg", timeout=120) | |
| img = Image.open(io.BytesIO(pdf_r.content)) | |
| iw, ih = img.size | |
| sx = iw / 1133.86; sy = ih / 793.70 | |
| regions = { | |
| "A 7.801.700-703 (pantry basket)": (57, 73, 228, 235), | |
| "B 7.801.100-101 (draining rack)": (57, 302, 228, 440), | |
| "C 7.804.100-103 (plastic dish rack)": (57, 532, 244, 645), | |
| "D 7.804.104-108 (steel draining rack)": (622, 79, 809, 203), | |
| } | |
| print("loading florence...") | |
| proc = AutoProcessor.from_pretrained("microsoft/Florence-2-base", trust_remote_code=True) | |
| model = AutoModelForCausalLM.from_pretrained("microsoft/Florence-2-base", trust_remote_code=True) | |
| for label, (x0, y0, x1, y1) in regions.items(): | |
| box = (int(x0*sx), int(y0*sy), int(x1*sx), int(y1*sy)) | |
| cr = img.crop(box).copy() | |
| inp = proc(text="<MORE_DETAILED_CAPTION>", images=cr, return_tensors="pt") | |
| out = model.generate(**inp, max_new_tokens=60, num_beams=3) | |
| txt = proc.batch_decode(out, skip_special_tokens=True)[0] | |
| print("RESULT", label, "|", txt) | |
| print("DONE") |