22
33import os
44import tempfile
5+ from functools import cached_property
56from pathlib import PurePath
67from typing import Any , BinaryIO , Collection , List , Optional , Union , cast
78
@@ -149,7 +150,6 @@ def __init__(
149150 self .number = number
150151 self .detection_model = detection_model
151152 self .element_extraction_model = element_extraction_model
152- self .elements : Collection [LayoutElement ] = []
153153 self .elements_array : LayoutElements | None = None
154154 self .password = password
155155 # NOTE(alan): Dropped LocationlessLayoutElement that was created for chipper - chipper has
@@ -159,10 +159,18 @@ def __init__(
159159 def __str__ (self ) -> str :
160160 return "\n \n " .join ([str (element ) for element in self .elements ])
161161
162+ @cached_property
163+ def elements (self ) -> Collection [LayoutElement ]:
164+ """return a list of layout elements from the array data structure; intended for backward
165+ compatibility"""
166+ if self .elements_array is None :
167+ return []
168+ return self .elements_array .as_list ()
169+
162170 def get_elements_using_image_extraction (
163171 self ,
164172 inplace = True ,
165- ) -> Optional [List [LayoutElement ]]:
173+ ) -> Optional [list [LayoutElement ]]:
166174 """Uses end-to-end text element extraction model to extract the elements on the page."""
167175 if self .element_extraction_model is None :
168176 raise ValueError (
@@ -178,8 +186,7 @@ def get_elements_using_image_extraction(
178186 def get_elements_with_detection_model (
179187 self ,
180188 inplace : bool = True ,
181- array_only : bool = False ,
182- ) -> Optional [List [LayoutElement ]]:
189+ ) -> Optional [LayoutElements ]:
183190 """Uses specified model to detect the elements on the page."""
184191 if self .detection_model is None :
185192 model = get_model ()
@@ -198,11 +205,9 @@ def get_elements_with_detection_model(
198205
199206 if inplace :
200207 self .elements_array = inferred_layout
201- if not array_only :
202- self .elements = inferred_layout .as_list ()
203208 return None
204209
205- return inferred_layout . as_list ()
210+ return inferred_layout
206211
207212 def _get_image_array (self ) -> Union [np .ndarray [Any , Any ], None ]:
208213 """Converts the raw image into a numpy array."""
0 commit comments