From 39c6281abe9b74917ee3b771760eace8e6d940fa Mon Sep 17 00:00:00 2001 From: Robert Sachunsky Date: Fri, 30 Oct 2020 22:56:37 +0100 Subject: [PATCH 1/3] ocrd_page: add invalidate_AlternativeImage method --- .../ocrd_models/ocrd_page_generateds.py | 161 +++++++++++++++++- ocrd_models/ocrd_page_user_methods.py | 1 + .../invalidate_AlternativeImage.py | 31 ++++ 3 files changed, 190 insertions(+), 3 deletions(-) create mode 100644 ocrd_models/ocrd_page_user_methods/invalidate_AlternativeImage.py diff --git a/ocrd_models/ocrd_models/ocrd_page_generateds.py b/ocrd_models/ocrd_models/ocrd_page_generateds.py index 4633c6358c..3a7845171d 100644 --- a/ocrd_models/ocrd_models/ocrd_page_generateds.py +++ b/ocrd_models/ocrd_models/ocrd_page_generateds.py @@ -2,8 +2,8 @@ # -*- coding: utf-8 -*- # -# Generated Mon Sep 21 14:00:49 2020 by generateDS.py version 2.35.20. -# Python 3.6.9 (default, Jul 17 2020, 12:50:27) [GCC 8.4.0] +# Generated Sat Oct 31 00:32:57 2020 by generateDS.py version 2.35.20. +# Python 3.6.7 (default, Oct 22 2018, 11:32:17) [GCC 8.2.0] # # Command line options: # ('-f', '') @@ -16,7 +16,7 @@ # ocrd_validators/ocrd_validators/page.xsd # # Command line: -# /home/kba/build/github.com/OCR-D/monorepo/ocrd_all/venv/bin/generateDS -f --root-element="PcGts" -o "ocrd_models/ocrd_models/ocrd_page_generateds.py" --disable-generatedssuper-lookup --user-methods="ocrd_models/ocrd_page_user_methods.py" ocrd_validators/ocrd_validators/page.xsd +# generateDS -f --root-element="PcGts" -o "ocrd_models/ocrd_models/ocrd_page_generateds.py" --disable-generatedssuper-lookup --user-methods="ocrd_models/ocrd_page_user_methods.py" ocrd_validators/ocrd_validators/page.xsd # # Current working directory (os.getcwd()): # core @@ -3036,6 +3036,37 @@ def get_AllRegions(self, classes=None, order='document', depth=0): else: ret = in_reading_order + [r for r in ret if r not in in_reading_order] return ret + def invalidate_AlternativeImage(self, feature_selector=None): + """ + Remove derived images from this segment (due to changed coordinates). + + If ``feature_selector`` is not none, remove only images with + matching ``@comments``, e.g. ``feature_selector=cropped,deskewed``. + """ + existing_images = self.AlternativeImage or [] + removed_images = [] + if feature_selector: + new_images = [] + for image in existing_images: + features = image.get_comments() or '' + if any(feature in features.split(',') + for feature in feature_selector.split(',') if feature): + removed_images.append(image) + else: + new_images.append(image) + self.AlternativeImage = new_images + else: + removed_images = existing_images + self.AlternativeImage = [] + if hasattr(self, 'id'): + name = self.id + elif hasattr(self, 'parent_object_') and hasattr(self.parent_object_, 'pcGtsId'): + name = self.parent_object_.pcGtsId + else: + name = '' + for image in removed_images: + self.gds_collector_.add_message('Removing AlternativeImage %s from "%s"' % ( + image.get_comments() or '', name)) # end class PageType @@ -3633,6 +3664,37 @@ def __hash__(self): else: raise ValueError("Cannot hash %s" % self) return hash(val) + def invalidate_AlternativeImage(self, feature_selector=None): + """ + Remove derived images from this segment (due to changed coordinates). + + If ``feature_selector`` is not none, remove only images with + matching ``@comments``, e.g. ``feature_selector=cropped,deskewed``. + """ + existing_images = self.AlternativeImage or [] + removed_images = [] + if feature_selector: + new_images = [] + for image in existing_images: + features = image.get_comments() or '' + if any(feature in features.split(',') + for feature in feature_selector.split(',') if feature): + removed_images.append(image) + else: + new_images.append(image) + self.AlternativeImage = new_images + else: + removed_images = existing_images + self.AlternativeImage = [] + if hasattr(self, 'id'): + name = self.id + elif hasattr(self, 'parent_object_') and hasattr(self.parent_object_, 'pcGtsId'): + name = self.parent_object_.pcGtsId + else: + name = '' + for image in removed_images: + self.gds_collector_.add_message('Removing AlternativeImage %s from "%s"' % ( + image.get_comments() or '', name)) # end class TextLineType @@ -4047,6 +4109,37 @@ def __hash__(self): else: raise ValueError("Cannot hash %s" % self) return hash(val) + def invalidate_AlternativeImage(self, feature_selector=None): + """ + Remove derived images from this segment (due to changed coordinates). + + If ``feature_selector`` is not none, remove only images with + matching ``@comments``, e.g. ``feature_selector=cropped,deskewed``. + """ + existing_images = self.AlternativeImage or [] + removed_images = [] + if feature_selector: + new_images = [] + for image in existing_images: + features = image.get_comments() or '' + if any(feature in features.split(',') + for feature in feature_selector.split(',') if feature): + removed_images.append(image) + else: + new_images.append(image) + self.AlternativeImage = new_images + else: + removed_images = existing_images + self.AlternativeImage = [] + if hasattr(self, 'id'): + name = self.id + elif hasattr(self, 'parent_object_') and hasattr(self.parent_object_, 'pcGtsId'): + name = self.parent_object_.pcGtsId + else: + name = '' + for image in removed_images: + self.gds_collector_.add_message('Removing AlternativeImage %s from "%s"' % ( + image.get_comments() or '', name)) # end class WordType @@ -4414,6 +4507,37 @@ def __hash__(self): else: raise ValueError("Cannot hash %s" % self) return hash(val) + def invalidate_AlternativeImage(self, feature_selector=None): + """ + Remove derived images from this segment (due to changed coordinates). + + If ``feature_selector`` is not none, remove only images with + matching ``@comments``, e.g. ``feature_selector=cropped,deskewed``. + """ + existing_images = self.AlternativeImage or [] + removed_images = [] + if feature_selector: + new_images = [] + for image in existing_images: + features = image.get_comments() or '' + if any(feature in features.split(',') + for feature in feature_selector.split(',') if feature): + removed_images.append(image) + else: + new_images.append(image) + self.AlternativeImage = new_images + else: + removed_images = existing_images + self.AlternativeImage = [] + if hasattr(self, 'id'): + name = self.id + elif hasattr(self, 'parent_object_') and hasattr(self.parent_object_, 'pcGtsId'): + name = self.parent_object_.pcGtsId + else: + name = '' + for image in removed_images: + self.gds_collector_.add_message('Removing AlternativeImage %s from "%s"' % ( + image.get_comments() or '', name)) # end class GlyphType @@ -8867,6 +8991,37 @@ def __hash__(self): else: raise ValueError("Cannot hash %s" % self) return hash(val) + def invalidate_AlternativeImage(self, feature_selector=None): + """ + Remove derived images from this segment (due to changed coordinates). + + If ``feature_selector`` is not none, remove only images with + matching ``@comments``, e.g. ``feature_selector=cropped,deskewed``. + """ + existing_images = self.AlternativeImage or [] + removed_images = [] + if feature_selector: + new_images = [] + for image in existing_images: + features = image.get_comments() or '' + if any(feature in features.split(',') + for feature in feature_selector.split(',') if feature): + removed_images.append(image) + else: + new_images.append(image) + self.AlternativeImage = new_images + else: + removed_images = existing_images + self.AlternativeImage = [] + if hasattr(self, 'id'): + name = self.id + elif hasattr(self, 'parent_object_') and hasattr(self.parent_object_, 'pcGtsId'): + name = self.parent_object_.pcGtsId + else: + name = '' + for image in removed_images: + self.gds_collector_.add_message('Removing AlternativeImage %s from "%s"' % ( + image.get_comments() or '', name)) # end class RegionType diff --git a/ocrd_models/ocrd_page_user_methods.py b/ocrd_models/ocrd_page_user_methods.py index 1c5e524289..655cfb5006 100644 --- a/ocrd_models/ocrd_page_user_methods.py +++ b/ocrd_models/ocrd_page_user_methods.py @@ -109,6 +109,7 @@ def _add_method(class_re, method_name, file_name=None): _add_method(r'^(PageType)$', 'get_AllRegions'), _add_method(r'^(PcGtsType)$', 'get_AllAlternativeImagePaths'), _add_method(r'^(PcGtsType)$', 'prune_ReadingOrder'), + _add_method(r'^(PageType|RegionType|TextLineType|WordType|GlyphType)$', 'invalidate_AlternativeImage'), ) diff --git a/ocrd_models/ocrd_page_user_methods/invalidate_AlternativeImage.py b/ocrd_models/ocrd_page_user_methods/invalidate_AlternativeImage.py new file mode 100644 index 0000000000..a398ef4c6e --- /dev/null +++ b/ocrd_models/ocrd_page_user_methods/invalidate_AlternativeImage.py @@ -0,0 +1,31 @@ +def invalidate_AlternativeImage(self, feature_selector=None): + """ + Remove derived images from this segment (due to changed coordinates). + + If ``feature_selector`` is not none, remove only images with + matching ``@comments``, e.g. ``feature_selector=cropped,deskewed``. + """ + existing_images = self.AlternativeImage or [] + removed_images = [] + if feature_selector: + new_images = [] + for image in existing_images: + features = image.get_comments() or '' + if any(feature in features.split(',') + for feature in feature_selector.split(',') if feature): + removed_images.append(image) + else: + new_images.append(image) + self.AlternativeImage = new_images + else: + removed_images = existing_images + self.AlternativeImage = [] + if hasattr(self, 'id'): + name = self.id + elif hasattr(self, 'parent_object_') and hasattr(self.parent_object_, 'pcGtsId'): + name = self.parent_object_.pcGtsId + else: + name = '' + for image in removed_images: + self.gds_collector_.add_message('Removing AlternativeImage %s from "%s"' % ( + image.get_comments() or '', name)) From a8cd8485f011613d9b49f49f29523e78cca229fc Mon Sep 17 00:00:00 2001 From: Robert Sachunsky Date: Sat, 31 Oct 2020 00:08:52 +0100 Subject: [PATCH 2/3] =?UTF-8?q?ocrd=5Fpage:=20invalidate=20`AlternativeIma?= =?UTF-8?q?ge`s=20automatically=E2=80=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - whenever overwriting `Border`'s `Coords` or `Coords/@points`, remove all the `Page`'s derived images with `cropped` - whenever overwriting `Region`'s or `TextLine`'s or `Word`'s or `Glyph`'s `Coords` or `Coords/@points`, remove all its derived images - whenever overwriting `Page`'s or `Region`'s `@orientation`, remove all its derived images with `deskewed` - add a warning to the GdsCollector each time --- .../ocrd_models/ocrd_page_generateds.py | 231 +++++++++++++++++- ocrd_models/ocrd_page_user_methods.py | 4 + .../ocrd_page_user_methods/set_Coords.py | 14 ++ .../ocrd_page_user_methods/set_orientation.py | 11 + .../ocrd_page_user_methods/set_points.py | 16 ++ 5 files changed, 275 insertions(+), 1 deletion(-) create mode 100644 ocrd_models/ocrd_page_user_methods/set_Coords.py create mode 100644 ocrd_models/ocrd_page_user_methods/set_orientation.py create mode 100644 ocrd_models/ocrd_page_user_methods/set_points.py diff --git a/ocrd_models/ocrd_models/ocrd_page_generateds.py b/ocrd_models/ocrd_models/ocrd_page_generateds.py index 3a7845171d..6208e34499 100644 --- a/ocrd_models/ocrd_models/ocrd_page_generateds.py +++ b/ocrd_models/ocrd_models/ocrd_page_generateds.py @@ -2,7 +2,7 @@ # -*- coding: utf-8 -*- # -# Generated Sat Oct 31 00:32:57 2020 by generateDS.py version 2.35.20. +# Generated Sat Oct 31 00:47:39 2020 by generateDS.py version 2.35.20. # Python 3.6.7 (default, Oct 22 2018, 11:32:17) [GCC 8.2.0] # # Command line options: @@ -3067,6 +3067,17 @@ def invalidate_AlternativeImage(self, feature_selector=None): for image in removed_images: self.gds_collector_.add_message('Removing AlternativeImage %s from "%s"' % ( image.get_comments() or '', name)) + def set_orientation(self, orientation): + """ + Set deskewing angle to given number. + Moreover, invalidate self's AlternativeImages + (because they will have been rotated and enlarged + with the angle of the previous value). + """ + if hasattr(self, 'invalidate_AlternativeImage'): + # PageType, RegionType: + self.invalidate_AlternativeImage(feature_selector='deskewed') + self.orientation = orientation # end class PageType @@ -3218,6 +3229,22 @@ def __hash__(self): else: raise ValueError("Cannot hash %s" % self) return hash(val) + def set_points(self, points): + """ + Set coordinate polygon by given string. + Moreover, invalidate the parent's AlternativeImages + (because they will have been cropped with a bbox + of the previous polygon). + """ + if hasattr(self, 'parent_object_'): + parent = self.parent_object_ + if hasattr(parent, 'invalidate_AlternativeImage'): + # RegionType, TextLineType, WordType, GlyphType: + parent.invalidate_AlternativeImage() + elif hasattr(parent, 'parent_object_') and hasattr(parent.parent_object_, 'invalidate_AlternativeImage'): + # BorderType: + parent.parent_object_.invalidate_AlternativeImage(feature_selector='cropped') + self.points = points # end class CoordsType @@ -3695,6 +3722,20 @@ def invalidate_AlternativeImage(self, feature_selector=None): for image in removed_images: self.gds_collector_.add_message('Removing AlternativeImage %s from "%s"' % ( image.get_comments() or '', name)) + def set_Coords(self, Coords): + """ + Set coordinate polygon by given object. + Moreover, invalidate self's AlternativeImages + (because they will have been cropped with a bbox + of the previous polygon). + """ + if hasattr(self, 'invalidate_AlternativeImage'): + # RegionType, TextLineType, WordType, GlyphType: + self.invalidate_AlternativeImage() + elif hasattr(self, 'parent_object_') and hasattr(self.parent_object_, 'invalidate_AlternativeImage'): + # BorderType: + self.parent_object_.invalidate_AlternativeImage(feature_selector='cropped') + self.Coords = Coords # end class TextLineType @@ -4140,6 +4181,20 @@ def invalidate_AlternativeImage(self, feature_selector=None): for image in removed_images: self.gds_collector_.add_message('Removing AlternativeImage %s from "%s"' % ( image.get_comments() or '', name)) + def set_Coords(self, Coords): + """ + Set coordinate polygon by given object. + Moreover, invalidate self's AlternativeImages + (because they will have been cropped with a bbox + of the previous polygon). + """ + if hasattr(self, 'invalidate_AlternativeImage'): + # RegionType, TextLineType, WordType, GlyphType: + self.invalidate_AlternativeImage() + elif hasattr(self, 'parent_object_') and hasattr(self.parent_object_, 'invalidate_AlternativeImage'): + # BorderType: + self.parent_object_.invalidate_AlternativeImage(feature_selector='cropped') + self.Coords = Coords # end class WordType @@ -4538,6 +4593,20 @@ def invalidate_AlternativeImage(self, feature_selector=None): for image in removed_images: self.gds_collector_.add_message('Removing AlternativeImage %s from "%s"' % ( image.get_comments() or '', name)) + def set_Coords(self, Coords): + """ + Set coordinate polygon by given object. + Moreover, invalidate self's AlternativeImages + (because they will have been cropped with a bbox + of the previous polygon). + """ + if hasattr(self, 'invalidate_AlternativeImage'): + # RegionType, TextLineType, WordType, GlyphType: + self.invalidate_AlternativeImage() + elif hasattr(self, 'parent_object_') and hasattr(self.parent_object_, 'invalidate_AlternativeImage'): + # BorderType: + self.parent_object_.invalidate_AlternativeImage(feature_selector='cropped') + self.Coords = Coords # end class GlyphType @@ -7153,6 +7222,20 @@ def __hash__(self): else: raise ValueError("Cannot hash %s" % self) return hash(val) + def set_Coords(self, Coords): + """ + Set coordinate polygon by given object. + Moreover, invalidate self's AlternativeImages + (because they will have been cropped with a bbox + of the previous polygon). + """ + if hasattr(self, 'invalidate_AlternativeImage'): + # RegionType, TextLineType, WordType, GlyphType: + self.invalidate_AlternativeImage() + elif hasattr(self, 'parent_object_') and hasattr(self.parent_object_, 'invalidate_AlternativeImage'): + # BorderType: + self.parent_object_.invalidate_AlternativeImage(feature_selector='cropped') + self.Coords = Coords # end class BorderType @@ -9022,6 +9105,20 @@ def invalidate_AlternativeImage(self, feature_selector=None): for image in removed_images: self.gds_collector_.add_message('Removing AlternativeImage %s from "%s"' % ( image.get_comments() or '', name)) + def set_Coords(self, Coords): + """ + Set coordinate polygon by given object. + Moreover, invalidate self's AlternativeImages + (because they will have been cropped with a bbox + of the previous polygon). + """ + if hasattr(self, 'invalidate_AlternativeImage'): + # RegionType, TextLineType, WordType, GlyphType: + self.invalidate_AlternativeImage() + elif hasattr(self, 'parent_object_') and hasattr(self.parent_object_, 'invalidate_AlternativeImage'): + # BorderType: + self.parent_object_.invalidate_AlternativeImage(feature_selector='cropped') + self.Coords = Coords # end class RegionType @@ -10877,6 +10974,17 @@ def __hash__(self): else: raise ValueError("Cannot hash %s" % self) return hash(val) + def set_orientation(self, orientation): + """ + Set deskewing angle to given number. + Moreover, invalidate self's AlternativeImages + (because they will have been rotated and enlarged + with the angle of the previous value). + """ + if hasattr(self, 'invalidate_AlternativeImage'): + # PageType, RegionType: + self.invalidate_AlternativeImage(feature_selector='deskewed') + self.orientation = orientation # end class AdvertRegionType @@ -11018,6 +11126,17 @@ def __hash__(self): else: raise ValueError("Cannot hash %s" % self) return hash(val) + def set_orientation(self, orientation): + """ + Set deskewing angle to given number. + Moreover, invalidate self's AlternativeImages + (because they will have been rotated and enlarged + with the angle of the previous value). + """ + if hasattr(self, 'invalidate_AlternativeImage'): + # PageType, RegionType: + self.invalidate_AlternativeImage(feature_selector='deskewed') + self.orientation = orientation # end class MusicRegionType @@ -11131,6 +11250,17 @@ def __hash__(self): else: raise ValueError("Cannot hash %s" % self) return hash(val) + def set_orientation(self, orientation): + """ + Set deskewing angle to given number. + Moreover, invalidate self's AlternativeImages + (because they will have been rotated and enlarged + with the angle of the previous value). + """ + if hasattr(self, 'invalidate_AlternativeImage'): + # PageType, RegionType: + self.invalidate_AlternativeImage(feature_selector='deskewed') + self.orientation = orientation # end class MapRegionType @@ -11273,6 +11403,17 @@ def __hash__(self): else: raise ValueError("Cannot hash %s" % self) return hash(val) + def set_orientation(self, orientation): + """ + Set deskewing angle to given number. + Moreover, invalidate self's AlternativeImages + (because they will have been rotated and enlarged + with the angle of the previous value). + """ + if hasattr(self, 'invalidate_AlternativeImage'): + # PageType, RegionType: + self.invalidate_AlternativeImage(feature_selector='deskewed') + self.orientation = orientation # end class ChemRegionType @@ -11415,6 +11556,17 @@ def __hash__(self): else: raise ValueError("Cannot hash %s" % self) return hash(val) + def set_orientation(self, orientation): + """ + Set deskewing angle to given number. + Moreover, invalidate self's AlternativeImages + (because they will have been rotated and enlarged + with the angle of the previous value). + """ + if hasattr(self, 'invalidate_AlternativeImage'): + # PageType, RegionType: + self.invalidate_AlternativeImage(feature_selector='deskewed') + self.orientation = orientation # end class MathsRegionType @@ -11558,6 +11710,17 @@ def __hash__(self): else: raise ValueError("Cannot hash %s" % self) return hash(val) + def set_orientation(self, orientation): + """ + Set deskewing angle to given number. + Moreover, invalidate self's AlternativeImages + (because they will have been rotated and enlarged + with the angle of the previous value). + """ + if hasattr(self, 'invalidate_AlternativeImage'): + # PageType, RegionType: + self.invalidate_AlternativeImage(feature_selector='deskewed') + self.orientation = orientation # end class SeparatorRegionType @@ -11766,6 +11929,17 @@ def __hash__(self): else: raise ValueError("Cannot hash %s" % self) return hash(val) + def set_orientation(self, orientation): + """ + Set deskewing angle to given number. + Moreover, invalidate self's AlternativeImages + (because they will have been rotated and enlarged + with the angle of the previous value). + """ + if hasattr(self, 'invalidate_AlternativeImage'): + # PageType, RegionType: + self.invalidate_AlternativeImage(feature_selector='deskewed') + self.orientation = orientation # end class ChartRegionType @@ -12015,6 +12189,17 @@ def __hash__(self): else: raise ValueError("Cannot hash %s" % self) return hash(val) + def set_orientation(self, orientation): + """ + Set deskewing angle to given number. + Moreover, invalidate self's AlternativeImages + (because they will have been rotated and enlarged + with the angle of the previous value). + """ + if hasattr(self, 'invalidate_AlternativeImage'): + # PageType, RegionType: + self.invalidate_AlternativeImage(feature_selector='deskewed') + self.orientation = orientation # end class TableRegionType @@ -12194,6 +12379,17 @@ def __hash__(self): else: raise ValueError("Cannot hash %s" % self) return hash(val) + def set_orientation(self, orientation): + """ + Set deskewing angle to given number. + Moreover, invalidate self's AlternativeImages + (because they will have been rotated and enlarged + with the angle of the previous value). + """ + if hasattr(self, 'invalidate_AlternativeImage'): + # PageType, RegionType: + self.invalidate_AlternativeImage(feature_selector='deskewed') + self.orientation = orientation # end class GraphicRegionType @@ -12373,6 +12569,17 @@ def __hash__(self): else: raise ValueError("Cannot hash %s" % self) return hash(val) + def set_orientation(self, orientation): + """ + Set deskewing angle to given number. + Moreover, invalidate self's AlternativeImages + (because they will have been rotated and enlarged + with the angle of the previous value). + """ + if hasattr(self, 'invalidate_AlternativeImage'): + # PageType, RegionType: + self.invalidate_AlternativeImage(feature_selector='deskewed') + self.orientation = orientation # end class LineDrawingRegionType @@ -12565,6 +12772,17 @@ def __hash__(self): else: raise ValueError("Cannot hash %s" % self) return hash(val) + def set_orientation(self, orientation): + """ + Set deskewing angle to given number. + Moreover, invalidate self's AlternativeImages + (because they will have been rotated and enlarged + with the angle of the previous value). + """ + if hasattr(self, 'invalidate_AlternativeImage'): + # PageType, RegionType: + self.invalidate_AlternativeImage(feature_selector='deskewed') + self.orientation = orientation # end class ImageRegionType @@ -13045,6 +13263,17 @@ def __hash__(self): else: raise ValueError("Cannot hash %s" % self) return hash(val) + def set_orientation(self, orientation): + """ + Set deskewing angle to given number. + Moreover, invalidate self's AlternativeImages + (because they will have been rotated and enlarged + with the angle of the previous value). + """ + if hasattr(self, 'invalidate_AlternativeImage'): + # PageType, RegionType: + self.invalidate_AlternativeImage(feature_selector='deskewed') + self.orientation = orientation # end class TextRegionType diff --git a/ocrd_models/ocrd_page_user_methods.py b/ocrd_models/ocrd_page_user_methods.py index 655cfb5006..a9fe643398 100644 --- a/ocrd_models/ocrd_page_user_methods.py +++ b/ocrd_models/ocrd_page_user_methods.py @@ -110,6 +110,10 @@ def _add_method(class_re, method_name, file_name=None): _add_method(r'^(PcGtsType)$', 'get_AllAlternativeImagePaths'), _add_method(r'^(PcGtsType)$', 'prune_ReadingOrder'), _add_method(r'^(PageType|RegionType|TextLineType|WordType|GlyphType)$', 'invalidate_AlternativeImage'), + _add_method(r'^(BorderType|RegionType|TextLineType|WordType|GlyphType)$', 'set_Coords'), + _add_method(r'^(CoordsType)$', 'set_points'), + # for some reason, pagecontent.xsd does not declare @orientation at the abstract/base RegionType: + _add_method(r'^(PageType|AdvertRegionType|MusicRegionType|MapRegionType|ChemRegionType|MathsRegionType|SeparatorRegionType|ChartRegionType|TableRegionType|GraphicRegionType|LineDrawingRegionType|ImageRegionType|TextRegionType)$', 'set_orientation'), ) diff --git a/ocrd_models/ocrd_page_user_methods/set_Coords.py b/ocrd_models/ocrd_page_user_methods/set_Coords.py new file mode 100644 index 0000000000..ce16f76fbf --- /dev/null +++ b/ocrd_models/ocrd_page_user_methods/set_Coords.py @@ -0,0 +1,14 @@ +def set_Coords(self, Coords): + """ + Set coordinate polygon by given object. + Moreover, invalidate self's AlternativeImages + (because they will have been cropped with a bbox + of the previous polygon). + """ + if hasattr(self, 'invalidate_AlternativeImage'): + # RegionType, TextLineType, WordType, GlyphType: + self.invalidate_AlternativeImage() + elif hasattr(self, 'parent_object_') and hasattr(self.parent_object_, 'invalidate_AlternativeImage'): + # BorderType: + self.parent_object_.invalidate_AlternativeImage(feature_selector='cropped') + self.Coords = Coords diff --git a/ocrd_models/ocrd_page_user_methods/set_orientation.py b/ocrd_models/ocrd_page_user_methods/set_orientation.py new file mode 100644 index 0000000000..d9beaad07b --- /dev/null +++ b/ocrd_models/ocrd_page_user_methods/set_orientation.py @@ -0,0 +1,11 @@ +def set_orientation(self, orientation): + """ + Set deskewing angle to given number. + Moreover, invalidate self's AlternativeImages + (because they will have been rotated and enlarged + with the angle of the previous value). + """ + if hasattr(self, 'invalidate_AlternativeImage'): + # PageType, RegionType: + self.invalidate_AlternativeImage(feature_selector='deskewed') + self.orientation = orientation diff --git a/ocrd_models/ocrd_page_user_methods/set_points.py b/ocrd_models/ocrd_page_user_methods/set_points.py new file mode 100644 index 0000000000..bad9873305 --- /dev/null +++ b/ocrd_models/ocrd_page_user_methods/set_points.py @@ -0,0 +1,16 @@ +def set_points(self, points): + """ + Set coordinate polygon by given string. + Moreover, invalidate the parent's AlternativeImages + (because they will have been cropped with a bbox + of the previous polygon). + """ + if hasattr(self, 'parent_object_'): + parent = self.parent_object_ + if hasattr(parent, 'invalidate_AlternativeImage'): + # RegionType, TextLineType, WordType, GlyphType: + parent.invalidate_AlternativeImage() + elif hasattr(parent, 'parent_object_') and hasattr(parent.parent_object_, 'invalidate_AlternativeImage'): + # BorderType: + parent.parent_object_.invalidate_AlternativeImage(feature_selector='cropped') + self.points = points From 031433c555185e3a4a04c4c0f7330c4250dc9b1d Mon Sep 17 00:00:00 2001 From: Robert Sachunsky Date: Sat, 31 Oct 2020 00:59:51 +0100 Subject: [PATCH 3/3] =?UTF-8?q?ocrd=5Fpage:=20invalidate=20`AlternativeIma?= =?UTF-8?q?ge`s=20automatically=20(cont'd)=E2=80=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - whenever overwriting `Page`'s `Border`, remove all its derived images with `cropped` --- ocrd_models/ocrd_models/ocrd_page_generateds.py | 11 ++++++++++- ocrd_models/ocrd_page_user_methods.py | 1 + ocrd_models/ocrd_page_user_methods/set_Border.py | 9 +++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 ocrd_models/ocrd_page_user_methods/set_Border.py diff --git a/ocrd_models/ocrd_models/ocrd_page_generateds.py b/ocrd_models/ocrd_models/ocrd_page_generateds.py index 6208e34499..53b2b7e978 100644 --- a/ocrd_models/ocrd_models/ocrd_page_generateds.py +++ b/ocrd_models/ocrd_models/ocrd_page_generateds.py @@ -2,7 +2,7 @@ # -*- coding: utf-8 -*- # -# Generated Sat Oct 31 00:47:39 2020 by generateDS.py version 2.35.20. +# Generated Sat Oct 31 00:59:08 2020 by generateDS.py version 2.35.20. # Python 3.6.7 (default, Oct 22 2018, 11:32:17) [GCC 8.2.0] # # Command line options: @@ -3067,6 +3067,15 @@ def invalidate_AlternativeImage(self, feature_selector=None): for image in removed_images: self.gds_collector_.add_message('Removing AlternativeImage %s from "%s"' % ( image.get_comments() or '', name)) + def set_Border(self, Border): + """ + Set coordinate polygon by given object. + Moreover, invalidate self's AlternativeImages + (because they will have been cropped with a bbox + of the previous polygon). + """ + self.invalidate_AlternativeImage(feature_selector='cropped') + self.Border = Border def set_orientation(self, orientation): """ Set deskewing angle to given number. diff --git a/ocrd_models/ocrd_page_user_methods.py b/ocrd_models/ocrd_page_user_methods.py index a9fe643398..44899076b3 100644 --- a/ocrd_models/ocrd_page_user_methods.py +++ b/ocrd_models/ocrd_page_user_methods.py @@ -111,6 +111,7 @@ def _add_method(class_re, method_name, file_name=None): _add_method(r'^(PcGtsType)$', 'prune_ReadingOrder'), _add_method(r'^(PageType|RegionType|TextLineType|WordType|GlyphType)$', 'invalidate_AlternativeImage'), _add_method(r'^(BorderType|RegionType|TextLineType|WordType|GlyphType)$', 'set_Coords'), + _add_method(r'^(PageType)$', 'set_Border'), _add_method(r'^(CoordsType)$', 'set_points'), # for some reason, pagecontent.xsd does not declare @orientation at the abstract/base RegionType: _add_method(r'^(PageType|AdvertRegionType|MusicRegionType|MapRegionType|ChemRegionType|MathsRegionType|SeparatorRegionType|ChartRegionType|TableRegionType|GraphicRegionType|LineDrawingRegionType|ImageRegionType|TextRegionType)$', 'set_orientation'), diff --git a/ocrd_models/ocrd_page_user_methods/set_Border.py b/ocrd_models/ocrd_page_user_methods/set_Border.py new file mode 100644 index 0000000000..568fd1d02d --- /dev/null +++ b/ocrd_models/ocrd_page_user_methods/set_Border.py @@ -0,0 +1,9 @@ +def set_Border(self, Border): + """ + Set coordinate polygon by given object. + Moreover, invalidate self's AlternativeImages + (because they will have been cropped with a bbox + of the previous polygon). + """ + self.invalidate_AlternativeImage(feature_selector='cropped') + self.Border = Border