vkit.pipeline.text_detection.page_shape

 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
15import math
16
17import attrs
18from numpy.random import Generator as RandomGenerator
19
20from vkit.utility import rng_choice
21from ..interface import PipelineStep, PipelineStepFactory
22
23
24@attrs.define
25class PageShapeStepConfig:
26    aspect_ratios: Sequence[float] = attrs.field(factory=lambda: (1 / 1.4142, 1.4142))
27    # NOTE: to ensure the minimum font size >= 18 pixels.
28    area: int = 2522**2
29
30
31@attrs.define
32class PageShapeStepInput:
33    pass
34
35
36@attrs.define
37class PageShapeStepOutput:
38    height: int
39    width: int
40
41
42class PageShapeStep(
43    PipelineStep[
44        PageShapeStepConfig,
45        PageShapeStepInput,
46        PageShapeStepOutput,
47    ]
48):  # yapf: disable
49
50    def run(self, input: PageShapeStepInput, rng: RandomGenerator):
51        aspect_ratio = rng_choice(rng, self.config.aspect_ratios)
52        height = round(math.sqrt(self.config.area / aspect_ratio))
53        width = round(aspect_ratio * height)
54        assert height > 0 and width > 0
55
56        return PageShapeStepOutput(height=height, width=width)
57
58
59page_shape_step_factory = PipelineStepFactory(PageShapeStep)
class PageShapeStepConfig:
26class PageShapeStepConfig:
27    aspect_ratios: Sequence[float] = attrs.field(factory=lambda: (1 / 1.4142, 1.4142))
28    # NOTE: to ensure the minimum font size >= 18 pixels.
29    area: int = 2522**2
PageShapeStepConfig(aspect_ratios: Sequence[float] = NOTHING, area: int = 6360484)
2def __init__(self, aspect_ratios=NOTHING, area=attr_dict['area'].default):
3    if aspect_ratios is not NOTHING:
4        self.aspect_ratios = aspect_ratios
5    else:
6        self.aspect_ratios = __attr_factory_aspect_ratios()
7    self.area = area

Method generated by attrs for class PageShapeStepConfig.

class PageShapeStepInput:
33class PageShapeStepInput:
34    pass
PageShapeStepInput()
2def __init__(self, ):
3    pass

Method generated by attrs for class PageShapeStepInput.

class PageShapeStepOutput:
38class PageShapeStepOutput:
39    height: int
40    width: int
PageShapeStepOutput(height: int, width: int)
2def __init__(self, height, width):
3    self.height = height
4    self.width = width

Method generated by attrs for class PageShapeStepOutput.

43class PageShapeStep(
44    PipelineStep[
45        PageShapeStepConfig,
46        PageShapeStepInput,
47        PageShapeStepOutput,
48    ]
49):  # yapf: disable
50
51    def run(self, input: PageShapeStepInput, rng: RandomGenerator):
52        aspect_ratio = rng_choice(rng, self.config.aspect_ratios)
53        height = round(math.sqrt(self.config.area / aspect_ratio))
54        width = round(aspect_ratio * height)
55        assert height > 0 and width > 0
56
57        return PageShapeStepOutput(height=height, width=width)

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

def run( self, input: vkit.pipeline.text_detection.page_shape.PageShapeStepInput, rng: numpy.random._generator.Generator):
51    def run(self, input: PageShapeStepInput, rng: RandomGenerator):
52        aspect_ratio = rng_choice(rng, self.config.aspect_ratios)
53        height = round(math.sqrt(self.config.area / aspect_ratio))
54        width = round(aspect_ratio * height)
55        assert height > 0 and width > 0
56
57        return PageShapeStepOutput(height=height, width=width)