1313
1414from flow_preprocessor .exceptions .exceptions import ParseTextLinesException
1515from flow_preprocessor .utils .logging .preprocessing_logger import logger
16+ # from flow_preprocessor.preprocessing_logic.segmentation import SegmenterYOLO
1617
1718
1819# ===============================================================================
@@ -33,8 +34,8 @@ def __init__(self, x: float, y: float):
3334 :param x: x coordinate
3435 :param y: y coordinate
3536 """
36- self .x = x
37- self .y = y
37+ self .x = float ( x )
38+ self .y = float ( y )
3839
3940 def __eq__ (self , other ) -> bool :
4041 """Override the equality operator.
@@ -48,14 +49,41 @@ def __eq__(self, other) -> bool:
4849 return False
4950 return self .x == other .x and self .y == other .y
5051
51- @classmethod
52- def min_x (cls , coordinates : List ['Coordinate' ]) -> float :
52+ @staticmethod
53+ def get_width (coordinates : List ['Coordinate' ]) -> float :
54+ """
55+ get the width (x_max - x_min)
56+
57+ :param coordinates: list of coordinates
58+ :return: width
59+ """
60+ min_x = Coordinate .min_x (coordinates )
61+ max_x = Coordinate .max_x (coordinates )
62+
63+ return float (max_x - min_x )
64+
65+ @staticmethod
66+ def get_bbox (coordinates : List ['Coordinate' ]) -> tuple [float , float , float , float ]:
67+ """
68+ get the bounding box of the coordinates.
69+
70+ :param coordinates: list of coordinates
71+ :return: bounding box (left, lower, right, upper)
72+ """
73+ min_x = Coordinate .min_x (coordinates )
74+ max_x = Coordinate .max_x (coordinates )
75+ min_y = Coordinate .min_y (coordinates )
76+ max_y = Coordinate .max_y (coordinates )
77+ return min_x , min_y , max_x , max_y
78+
79+ @staticmethod
80+ def min_x (coordinates : List ['Coordinate' ]) -> float :
5381 """set minimum x coordinate.
5482
5583 :param coordinates: list of coordinates.
5684 :return: minimum x coordinate.
5785 """
58- return min (coord .x for coord in coordinates )
86+ return min ([ coord .x for coord in coordinates ] )
5987
6088 @staticmethod
6189 def max_x (coordinates : List ['Coordinate' ]) -> float :
@@ -64,17 +92,17 @@ def max_x(coordinates: List['Coordinate']) -> float:
6492 :param coordinates: list of coordinates.
6593 :return: minimum y coordinate.
6694 """
67- return max (coord .x for coord in coordinates )
95+ return max ([ coord .x for coord in coordinates ] )
6896
6997 @staticmethod
7098 def min_y (coordinates : List ['Coordinate' ]) -> float :
7199 """set minimum y coordinate."""
72- return min (coord .y for coord in coordinates )
100+ return min ([ coord .y for coord in coordinates ] )
73101
74102 @staticmethod
75103 def max_y (coordinates : List ['Coordinate' ]) -> float :
76104 """set maximum y coordinate."""
77- return max (coord .y for coord in coordinates )
105+ return max ([ coord .y for coord in coordinates ] )
78106
79107
80108# ===============================================================================
@@ -198,7 +226,11 @@ class Page:
198226 An XML Page.
199227 """
200228
201- def __init__ (self , image_file_name , lines , metadata ):
229+ def __init__ (self ,
230+ image_file_name : str ,
231+ lines : List [Line ],
232+ metadata : Metadata
233+ ) -> None :
202234 """
203235 initialise class parameters.
204236
@@ -252,20 +284,29 @@ def parse_xml_file(self, xml_file: str, segment: bool = False) -> None:
252284 self .namespace_uri = self .root .tag .split ('}' )[0 ][1 :]
253285 self .namespace = {'prefix' : self .namespace_uri }
254286 self .xmlns = {'ns' : self .namespace_uri }
287+ image_filename = self .get_image_file_name ()
255288
256- # TODO: Write Segmenter package
257289 """
258290 if segment:
259291 existing_segmentation = self.check_segmentation()
260292 if existing_segmentation == 'ground_truth':
261293 pass
294+ else:
295+ segmenter = SegmenterYOLO(
296+ models=['Riksarkivet/yolov9-regions-1', 'Riksarkivet/yolov9-lines-within-regions-1'],
297+ batch_sizes=4,
298+ order_lines=True,
299+ )
300+ self.tree = segmenter.segment(self.tree, image_filename)
301+ self.root = self.tree.getroot()
302+
262303 elif existing_segmentation == 'segmented':
263304 segmenter = Segmenter('linemasks')
264305 self.root = segmenter.segment(self.root)
265306 else:
266307 segmenter = Segmenter('yolo')
267308 self.root = segmenter.segment(self.root)
268- """
309+ """
269310
270311 except (et .XMLSyntaxError , et .ParseError ) as e :
271312 self .failed_processing .append (xml_file )
@@ -437,13 +478,13 @@ def get_line_text_string(self, text_line: et.Element) -> str:
437478
438479 unicode_text = text_line .find ('./ns:TextEquiv/ns:Unicode' , namespaces = self .xmlns )
439480 if unicode_text is not None and hasattr (unicode_text , 'text' ):
440- logger .info ('%s - Got Unicode text: %s' , self .__class__ .__name__ , unicode_text .text )
481+ logger .debug ('%s - Got Unicode text: %s' , self .__class__ .__name__ , unicode_text .text )
441482 if unicode_text .text is not None :
442483 text : str = unicode_text .text .strip ()
443484 else :
444485 text : str = ''
445486 else :
446- logger .info ('%s - No Unicode text found' , self .__class__ .__name__ )
487+ logger .debug ('%s - No Unicode text found' , self .__class__ .__name__ )
447488 text : str = ''
448489 return text
449490
@@ -543,8 +584,19 @@ def get_page(self):
543584 """
544585 Get the Page-object from the XML file.
545586 """
546- return Page (
547- self .get_image_file_name (),
548- self .process_lines_from_xml_file (),
549- self .get_metadata ()
550- )
587+ try :
588+ return Page (
589+ self .get_image_file_name (),
590+ self .process_lines_from_xml_file (),
591+ self .get_metadata ()
592+ )
593+ except ParseTextLinesException as e :
594+ logger .error (
595+ '%s - Error parsing file %s' ,
596+ self .__class__ .__name__ ,
597+ self .xml_filename ,
598+ exc_info = True ,
599+ )
600+ self .failed_processing .append (self .xml_filename )
601+ raise ParseTextLinesException (f'Error parsing file { self .xml_filename } : { e } ' ) from e
602+
0 commit comments