vkit.pipeline.text_detection.page_background
1# Copyright 2022 vkit-x Administrator. All Rights Reserved. 2# 3# This project (vkit-x/vkit) is dual-licensed under commercial and SSPL licenses. 4# 5# The commercial license gives you the full rights to create and distribute software 6# on your own terms without any SSPL license obligations. For more information, 7# please see the "LICENSE_COMMERCIAL.txt" file. 8# 9# This project is also available under Server Side Public License (SSPL). 10# The SSPL licensing is ideal for use cases such as open source projects with 11# SSPL distribution, student/academic purposes, hobby projects, internal research 12# projects without external distribution, or other projects where all SSPL 13# obligations can be met. For more information, please see the "LICENSE_SSPL.txt" file. 14from typing import Sequence, Mapping, Any, Union 15from enum import Enum, unique 16 17import attrs 18from numpy.random import Generator as RandomGenerator 19 20from vkit.utility import ( 21 normalize_to_keys_and_probs, 22 rng_choice, 23 PathType, 24) 25from vkit.element import Image 26from vkit.engine.image import image_engine_executor_aggregator_factory 27from .page_shape import PageShapeStepOutput 28from ..interface import PipelineStep, PipelineStepFactory 29 30 31@attrs.define 32class PageBackgroundStepConfig: 33 image_configs: Union[Sequence[Mapping[str, Any]], PathType] 34 weight_image: float = 0.8 35 weight_random_grayscale: float = 0.2 36 grayscale_min: int = 127 37 grayscale_max: int = 255 38 39 40@attrs.define 41class PageBackgroundStepInput: 42 page_shape_step_output: PageShapeStepOutput 43 44 45@attrs.define 46class PageBackgroundStepOutput: 47 background_image: Image 48 49 50@unique 51class PageBackgroundStepKey(Enum): 52 IMAGE = 'image' 53 RANDOM_GRAYSCALE = 'random_grayscale' 54 55 56class PageBackgroundStep( 57 PipelineStep[ 58 PageBackgroundStepConfig, 59 PageBackgroundStepInput, 60 PageBackgroundStepOutput, 61 ] 62): # yapf: disable 63 64 def __init__(self, config: PageBackgroundStepConfig): 65 super().__init__(config) 66 67 self.image_engine_executor_aggregator = image_engine_executor_aggregator_factory.create( 68 self.config.image_configs 69 ) 70 71 self.keys, self.probs = normalize_to_keys_and_probs([ 72 ( 73 PageBackgroundStepKey.IMAGE, 74 self.config.weight_image, 75 ), 76 ( 77 PageBackgroundStepKey.RANDOM_GRAYSCALE, 78 self.config.weight_random_grayscale, 79 ), 80 ]) 81 82 def run(self, input: PageBackgroundStepInput, rng: RandomGenerator): 83 page_shape_step_output = input.page_shape_step_output 84 height = page_shape_step_output.height 85 width = page_shape_step_output.width 86 87 key = rng_choice(rng, self.keys, probs=self.probs) 88 if key == PageBackgroundStepKey.IMAGE: 89 background_image = self.image_engine_executor_aggregator.run( 90 { 91 'height': height, 92 'width': width, 93 }, 94 rng, 95 ) 96 97 elif key == PageBackgroundStepKey.RANDOM_GRAYSCALE: 98 grayscale_value = rng.integers(self.config.grayscale_min, self.config.grayscale_max + 1) 99 background_image = Image.from_shape( 100 (height, width), 101 num_channels=3, 102 value=grayscale_value, 103 ) 104 105 else: 106 raise NotImplementedError() 107 108 return PageBackgroundStepOutput(background_image=background_image) 109 110 111page_background_step_factory = PipelineStepFactory(PageBackgroundStep)
class
PageBackgroundStepConfig:
33class PageBackgroundStepConfig: 34 image_configs: Union[Sequence[Mapping[str, Any]], PathType] 35 weight_image: float = 0.8 36 weight_random_grayscale: float = 0.2 37 grayscale_min: int = 127 38 grayscale_max: int = 255
PageBackgroundStepConfig( image_configs: Union[Sequence[Mapping[str, Any]], str, os.PathLike], weight_image: float = 0.8, weight_random_grayscale: float = 0.2, grayscale_min: int = 127, grayscale_max: int = 255)
2def __init__(self, image_configs, weight_image=attr_dict['weight_image'].default, weight_random_grayscale=attr_dict['weight_random_grayscale'].default, grayscale_min=attr_dict['grayscale_min'].default, grayscale_max=attr_dict['grayscale_max'].default): 3 self.image_configs = image_configs 4 self.weight_image = weight_image 5 self.weight_random_grayscale = weight_random_grayscale 6 self.grayscale_min = grayscale_min 7 self.grayscale_max = grayscale_max
Method generated by attrs for class PageBackgroundStepConfig.
class
PageBackgroundStepInput:
PageBackgroundStepInput( page_shape_step_output: vkit.pipeline.text_detection.page_shape.PageShapeStepOutput)
Method generated by attrs for class PageBackgroundStepInput.
class
PageBackgroundStepOutput:
PageBackgroundStepOutput(background_image: vkit.element.image.Image)
Method generated by attrs for class PageBackgroundStepOutput.
class
PageBackgroundStepKey(enum.Enum):
An enumeration.
IMAGE =
<PageBackgroundStepKey.IMAGE: 'image'>
RANDOM_GRAYSCALE =
<PageBackgroundStepKey.RANDOM_GRAYSCALE: 'random_grayscale'>
Inherited Members
- enum.Enum
- name
- value
57class PageBackgroundStep( 58 PipelineStep[ 59 PageBackgroundStepConfig, 60 PageBackgroundStepInput, 61 PageBackgroundStepOutput, 62 ] 63): # yapf: disable 64 65 def __init__(self, config: PageBackgroundStepConfig): 66 super().__init__(config) 67 68 self.image_engine_executor_aggregator = image_engine_executor_aggregator_factory.create( 69 self.config.image_configs 70 ) 71 72 self.keys, self.probs = normalize_to_keys_and_probs([ 73 ( 74 PageBackgroundStepKey.IMAGE, 75 self.config.weight_image, 76 ), 77 ( 78 PageBackgroundStepKey.RANDOM_GRAYSCALE, 79 self.config.weight_random_grayscale, 80 ), 81 ]) 82 83 def run(self, input: PageBackgroundStepInput, rng: RandomGenerator): 84 page_shape_step_output = input.page_shape_step_output 85 height = page_shape_step_output.height 86 width = page_shape_step_output.width 87 88 key = rng_choice(rng, self.keys, probs=self.probs) 89 if key == PageBackgroundStepKey.IMAGE: 90 background_image = self.image_engine_executor_aggregator.run( 91 { 92 'height': height, 93 'width': width, 94 }, 95 rng, 96 ) 97 98 elif key == PageBackgroundStepKey.RANDOM_GRAYSCALE: 99 grayscale_value = rng.integers(self.config.grayscale_min, self.config.grayscale_max + 1) 100 background_image = Image.from_shape( 101 (height, width), 102 num_channels=3, 103 value=grayscale_value, 104 ) 105 106 else: 107 raise NotImplementedError() 108 109 return PageBackgroundStepOutput(background_image=background_image)
Abstract base class for generic types.
A generic type is typically declared by inheriting from this class parameterized with one or more type variables. For example, a generic mapping type might be defined as::
class Mapping(Generic[KT, VT]): def __getitem__(self, key: KT) -> VT: ... # Etc.
This class can then be used as follows::
def lookup_name(mapping: Mapping[KT, VT], key: KT, default: VT) -> VT: try: return mapping[key] except KeyError: return default
PageBackgroundStep( config: vkit.pipeline.text_detection.page_background.PageBackgroundStepConfig)
65 def __init__(self, config: PageBackgroundStepConfig): 66 super().__init__(config) 67 68 self.image_engine_executor_aggregator = image_engine_executor_aggregator_factory.create( 69 self.config.image_configs 70 ) 71 72 self.keys, self.probs = normalize_to_keys_and_probs([ 73 ( 74 PageBackgroundStepKey.IMAGE, 75 self.config.weight_image, 76 ), 77 ( 78 PageBackgroundStepKey.RANDOM_GRAYSCALE, 79 self.config.weight_random_grayscale, 80 ), 81 ])
def
run( self, input: vkit.pipeline.text_detection.page_background.PageBackgroundStepInput, rng: numpy.random._generator.Generator):
83 def run(self, input: PageBackgroundStepInput, rng: RandomGenerator): 84 page_shape_step_output = input.page_shape_step_output 85 height = page_shape_step_output.height 86 width = page_shape_step_output.width 87 88 key = rng_choice(rng, self.keys, probs=self.probs) 89 if key == PageBackgroundStepKey.IMAGE: 90 background_image = self.image_engine_executor_aggregator.run( 91 { 92 'height': height, 93 'width': width, 94 }, 95 rng, 96 ) 97 98 elif key == PageBackgroundStepKey.RANDOM_GRAYSCALE: 99 grayscale_value = rng.integers(self.config.grayscale_min, self.config.grayscale_max + 1) 100 background_image = Image.from_shape( 101 (height, width), 102 num_channels=3, 103 value=grayscale_value, 104 ) 105 106 else: 107 raise NotImplementedError() 108 109 return PageBackgroundStepOutput(background_image=background_image)