vkit.pipeline.text_detection.page_seal_impression

 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, List, Union, Mapping, Any
15
16import attrs
17from numpy.random import Generator as RandomGenerator
18
19from vkit.utility import PathType
20from vkit.element import Box
21from vkit.engine.seal_impression import (
22    seal_impression_engine_executor_aggregator_factory,
23    SealImpression,
24)
25from ..interface import PipelineStep, PipelineStepFactory
26from .page_layout import PageLayoutStepOutput
27
28
29@attrs.define
30class PageSealImpresssionStepConfig:
31    seal_impression_configs: Union[Sequence[Mapping[str, Any]], PathType]
32
33
34@attrs.define
35class PageSealImpresssionStepInput:
36    page_layout_step_output: PageLayoutStepOutput
37
38
39@attrs.define
40class PageSealImpresssionStepOutput:
41    seal_impressions: Sequence[SealImpression]
42    boxes: Sequence[Box]
43    angles: Sequence[int]
44
45
46class PageSealImpresssionStep(
47    PipelineStep[
48        PageSealImpresssionStepConfig,
49        PageSealImpresssionStepInput,
50        PageSealImpresssionStepOutput,
51    ]
52):  # yapf: disable
53
54    def __init__(self, config: PageSealImpresssionStepConfig):
55        super().__init__(config)
56
57        self.seal_impression_engine_executor_aggregator = \
58            seal_impression_engine_executor_aggregator_factory.create(
59                self.config.seal_impression_configs
60            )
61
62    def run(self, input: PageSealImpresssionStepInput, rng: RandomGenerator):
63        page_layout_step_output = input.page_layout_step_output
64        page_layout = page_layout_step_output.page_layout
65
66        seal_impressions: List[SealImpression] = []
67        boxes: List[Box] = []
68        angles: List[int] = []
69        for layout_seal_impression in page_layout.layout_seal_impressions:
70            box = layout_seal_impression.box
71            seal_impressions.append(
72                self.seal_impression_engine_executor_aggregator.run(
73                    {
74                        'height': box.height,
75                        'width': box.width
76                    },
77                    rng,
78                )
79            )
80            boxes.append(box)
81            angles.append(layout_seal_impression.angle)
82
83        return PageSealImpresssionStepOutput(
84            seal_impressions=seal_impressions,
85            boxes=boxes,
86            angles=angles,
87        )
88
89
90page_seal_impresssion_step_factory = PipelineStepFactory(PageSealImpresssionStep)
class PageSealImpresssionStepConfig:
31class PageSealImpresssionStepConfig:
32    seal_impression_configs: Union[Sequence[Mapping[str, Any]], PathType]
PageSealImpresssionStepConfig( seal_impression_configs: Union[Sequence[Mapping[str, Any]], str, os.PathLike])
2def __init__(self, seal_impression_configs):
3    self.seal_impression_configs = seal_impression_configs

Method generated by attrs for class PageSealImpresssionStepConfig.

class PageSealImpresssionStepInput:
36class PageSealImpresssionStepInput:
37    page_layout_step_output: PageLayoutStepOutput
PageSealImpresssionStepInput( page_layout_step_output: vkit.pipeline.text_detection.page_layout.PageLayoutStepOutput)
2def __init__(self, page_layout_step_output):
3    self.page_layout_step_output = page_layout_step_output

Method generated by attrs for class PageSealImpresssionStepInput.

class PageSealImpresssionStepOutput:
41class PageSealImpresssionStepOutput:
42    seal_impressions: Sequence[SealImpression]
43    boxes: Sequence[Box]
44    angles: Sequence[int]
PageSealImpresssionStepOutput( seal_impressions: Sequence[vkit.engine.seal_impression.type.SealImpression], boxes: Sequence[vkit.element.box.Box], angles: Sequence[int])
2def __init__(self, seal_impressions, boxes, angles):
3    self.seal_impressions = seal_impressions
4    self.boxes = boxes
5    self.angles = angles

Method generated by attrs for class PageSealImpresssionStepOutput.

47class PageSealImpresssionStep(
48    PipelineStep[
49        PageSealImpresssionStepConfig,
50        PageSealImpresssionStepInput,
51        PageSealImpresssionStepOutput,
52    ]
53):  # yapf: disable
54
55    def __init__(self, config: PageSealImpresssionStepConfig):
56        super().__init__(config)
57
58        self.seal_impression_engine_executor_aggregator = \
59            seal_impression_engine_executor_aggregator_factory.create(
60                self.config.seal_impression_configs
61            )
62
63    def run(self, input: PageSealImpresssionStepInput, rng: RandomGenerator):
64        page_layout_step_output = input.page_layout_step_output
65        page_layout = page_layout_step_output.page_layout
66
67        seal_impressions: List[SealImpression] = []
68        boxes: List[Box] = []
69        angles: List[int] = []
70        for layout_seal_impression in page_layout.layout_seal_impressions:
71            box = layout_seal_impression.box
72            seal_impressions.append(
73                self.seal_impression_engine_executor_aggregator.run(
74                    {
75                        'height': box.height,
76                        'width': box.width
77                    },
78                    rng,
79                )
80            )
81            boxes.append(box)
82            angles.append(layout_seal_impression.angle)
83
84        return PageSealImpresssionStepOutput(
85            seal_impressions=seal_impressions,
86            boxes=boxes,
87            angles=angles,
88        )

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

55    def __init__(self, config: PageSealImpresssionStepConfig):
56        super().__init__(config)
57
58        self.seal_impression_engine_executor_aggregator = \
59            seal_impression_engine_executor_aggregator_factory.create(
60                self.config.seal_impression_configs
61            )
def run( self, input: vkit.pipeline.text_detection.page_seal_impression.PageSealImpresssionStepInput, rng: numpy.random._generator.Generator):
63    def run(self, input: PageSealImpresssionStepInput, rng: RandomGenerator):
64        page_layout_step_output = input.page_layout_step_output
65        page_layout = page_layout_step_output.page_layout
66
67        seal_impressions: List[SealImpression] = []
68        boxes: List[Box] = []
69        angles: List[int] = []
70        for layout_seal_impression in page_layout.layout_seal_impressions:
71            box = layout_seal_impression.box
72            seal_impressions.append(
73                self.seal_impression_engine_executor_aggregator.run(
74                    {
75                        'height': box.height,
76                        'width': box.width
77                    },
78                    rng,
79                )
80            )
81            boxes.append(box)
82            angles.append(layout_seal_impression.angle)
83
84        return PageSealImpresssionStepOutput(
85            seal_impressions=seal_impressions,
86            boxes=boxes,
87            angles=angles,
88        )