11from typing import Any , Dict , List , Optional , TypeVar
22
3+ from mindee .geometry import Polygon , get_bbox_as_polygon
4+
5+ TypePrediction = Dict [str , Any ]
6+
37
48class Field :
59 value : Optional [Any ] = None
610 """Raw field value"""
711 confidence : float = 0.0
812 """Confidence score"""
9- bbox : List [ List [ float ]] = []
13+ bbox : Polygon = []
1014 """Bounding box coordinates containing the field"""
15+ polygon : Polygon = []
16+ """coordinates of the field"""
1117
1218 def __init__ (
1319 self ,
14- abstract_prediction : Dict [ str , Any ] ,
20+ abstract_prediction : TypePrediction ,
1521 value_key : str = "value" ,
1622 reconstructed : bool = False ,
1723 page_n : Optional [int ] = None ,
@@ -25,26 +31,28 @@ def __init__(
2531 :param page_n: Page number for multi-page PDF
2632 """
2733 self .page_n = page_n
34+ self .reconstructed = reconstructed
2835
2936 if (
3037 value_key not in abstract_prediction
3138 or abstract_prediction [value_key ] == "N/A"
3239 ):
33- self .value = None
34- self .confidence = 0.0
35- self .bbox = []
36- else :
37- self .value = abstract_prediction [value_key ]
38- try :
39- self .confidence = float (abstract_prediction ["confidence" ])
40- except (KeyError , TypeError ):
41- self .confidence = 0.0
42- try :
43- self .bbox = abstract_prediction ["polygon" ]
44- except KeyError :
45- self .bbox = []
40+ return
4641
47- self .reconstructed = reconstructed
42+ self .value = abstract_prediction [value_key ]
43+ try :
44+ self .confidence = float (abstract_prediction ["confidence" ])
45+ except (KeyError , TypeError ):
46+ pass
47+ self ._set_bbox (abstract_prediction )
48+
49+ def _set_bbox (self , abstract_prediction : TypePrediction ) -> None :
50+ try :
51+ self .polygon = abstract_prediction ["polygon" ]
52+ except KeyError :
53+ pass
54+ if self .polygon :
55+ self .bbox = get_bbox_as_polygon (self .polygon )
4856
4957 def __eq__ (self , other : Any ) -> bool :
5058 if not isinstance (other , Field ):
0 commit comments