2
2
3
3
import os
4
4
import tempfile
5
+ from functools import cached_property
5
6
from pathlib import PurePath
6
7
from typing import Any , BinaryIO , Collection , List , Optional , Union , cast
7
8
@@ -149,7 +150,6 @@ def __init__(
149
150
self .number = number
150
151
self .detection_model = detection_model
151
152
self .element_extraction_model = element_extraction_model
152
- self .elements : Collection [LayoutElement ] = []
153
153
self .elements_array : LayoutElements | None = None
154
154
self .password = password
155
155
# NOTE(alan): Dropped LocationlessLayoutElement that was created for chipper - chipper has
@@ -159,10 +159,18 @@ def __init__(
159
159
def __str__ (self ) -> str :
160
160
return "\n \n " .join ([str (element ) for element in self .elements ])
161
161
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
+
162
170
def get_elements_using_image_extraction (
163
171
self ,
164
172
inplace = True ,
165
- ) -> Optional [List [LayoutElement ]]:
173
+ ) -> Optional [list [LayoutElement ]]:
166
174
"""Uses end-to-end text element extraction model to extract the elements on the page."""
167
175
if self .element_extraction_model is None :
168
176
raise ValueError (
@@ -178,8 +186,7 @@ def get_elements_using_image_extraction(
178
186
def get_elements_with_detection_model (
179
187
self ,
180
188
inplace : bool = True ,
181
- array_only : bool = False ,
182
- ) -> Optional [List [LayoutElement ]]:
189
+ ) -> Optional [LayoutElements ]:
183
190
"""Uses specified model to detect the elements on the page."""
184
191
if self .detection_model is None :
185
192
model = get_model ()
@@ -198,11 +205,9 @@ def get_elements_with_detection_model(
198
205
199
206
if inplace :
200
207
self .elements_array = inferred_layout
201
- if not array_only :
202
- self .elements = inferred_layout .as_list ()
203
208
return None
204
209
205
- return inferred_layout . as_list ()
210
+ return inferred_layout
206
211
207
212
def _get_image_array (self ) -> Union [np .ndarray [Any , Any ], None ]:
208
213
"""Converts the raw image into a numpy array."""
0 commit comments