# Copyright (c) 2026 LlamaIndex, Inc. All rights reserved. # Licensed under the OCRia-1 Model and Code License (LICENSE at the root of this repository). # This is not open-source software: see the license for permitted uses. from pathlib import Path import json,sys import numpy as np from PIL import Image,ImageDraw from ocria_grounding import ground_layout,to_layout_items_page class Capture: def unit_maps(self,image,answer,units): maps=np.zeros((len(units),12,16),dtype=np.float32) for i in range(len(units)):maps[i,3+i//5,2+i%5]=1 return maps,(12,16),np.ones(len(units),dtype=bool) image=Image.new('RGB',(320,240),'white') d=ImageDraw.Draw(image) for y in (60,80,100): for x in range(40,160,24):d.rectangle((x,y,x+17,y+9),fill='black') answer='# Heading\n\nOne two three four five six seven eight.' regions=[{'label':'text','score':.95,'box':[20,40,200,140]}] cases={} for name,kw in [('fallback',{}),('detections',{'detections':regions}),('detector',{'detector':lambda image:regions}),('pdf_words',{'detections':regions,'words':[{'text':'One','box':[40,60,57,69]}]})]: page=ground_layout(Capture(),image,answer,**kw) cases[name]={'page':page,'serialized':to_layout_items_page(page,scale=.5)} assert not any(k.startswith('parse_rl_utils') for k in sys.modules),'default imported fast backend' expected=json.loads((Path(__file__).parent/'legacy_layout_expected.json').read_text()) assert cases == expected print('Four legacy grounding modes and serializers match the original HF snapshot')