vkit.pipeline.text_detection.page_barcode

  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, Mapping, Any, Optional
 15
 16import attrs
 17from numpy.random import Generator as RandomGenerator
 18
 19from vkit.element import ScoreMap
 20from vkit.engine.barcode import (
 21    barcode_qr_engine_executor_factory,
 22    barcode_code39_engine_executor_factory,
 23)
 24
 25from ..interface import PipelineStep, PipelineStepFactory
 26from .page_layout import PageLayoutStepOutput
 27
 28
 29@attrs.define
 30class PageBarcodeStepConfig:
 31    barcode_qr_config: Optional[Mapping[str, Any]] = None
 32    barcode_code39_config: Optional[Mapping[str, Any]] = None
 33
 34
 35@attrs.define
 36class PageBarcodeStepInput:
 37    page_layout_step_output: PageLayoutStepOutput
 38
 39
 40@attrs.define
 41class PageBarcodeStepOutput:
 42    height: int
 43    width: int
 44    barcode_qr_score_maps: Sequence[ScoreMap]
 45    barcode_code39_score_maps: Sequence[ScoreMap]
 46
 47
 48class PageBarcodeStep(
 49    PipelineStep[
 50        PageBarcodeStepConfig,
 51        PageBarcodeStepInput,
 52        PageBarcodeStepOutput,
 53    ]
 54):  # yapf: disable
 55
 56    def __init__(self, config: PageBarcodeStepConfig):
 57        super().__init__(config)
 58
 59        self.barcode_qr_engine_executor = barcode_qr_engine_executor_factory.create(
 60            self.config.barcode_qr_config
 61        )
 62        self.barcode_code39_engine_executor = barcode_code39_engine_executor_factory.create(
 63            self.config.barcode_code39_config
 64        )
 65
 66    def run(self, input: PageBarcodeStepInput, rng: RandomGenerator):
 67        page_layout_step_output = input.page_layout_step_output
 68        page_layout = page_layout_step_output.page_layout
 69
 70        barcode_qr_score_maps: List[ScoreMap] = []
 71        for layout_barcode_qr in page_layout.layout_barcode_qrs:
 72            box = layout_barcode_qr.box
 73            assert box.height == box.width
 74
 75            barcode_qr_score_map = self.barcode_qr_engine_executor.run(
 76                {
 77                    'height': box.height,
 78                    'width': box.width,
 79                },
 80                rng=rng,
 81            )
 82            barcode_qr_score_map = barcode_qr_score_map.to_box_attached(box)
 83            barcode_qr_score_maps.append(barcode_qr_score_map)
 84
 85        barcode_code39_score_maps: List[ScoreMap] = []
 86        for layout_barcode_code39 in page_layout.layout_barcode_code39s:
 87            box = layout_barcode_code39.box
 88
 89            barcode_code39_score_map = self.barcode_code39_engine_executor.run(
 90                {
 91                    'height': box.height,
 92                    'width': box.width,
 93                },
 94                rng=rng,
 95            )
 96            barcode_code39_score_map = barcode_code39_score_map.to_box_attached(box)
 97            barcode_code39_score_maps.append(barcode_code39_score_map)
 98
 99        return PageBarcodeStepOutput(
100            height=page_layout.height,
101            width=page_layout.width,
102            barcode_qr_score_maps=barcode_qr_score_maps,
103            barcode_code39_score_maps=barcode_code39_score_maps,
104        )
105
106
107page_barcode_step_factory = PipelineStepFactory(PageBarcodeStep)
class PageBarcodeStepConfig:
31class PageBarcodeStepConfig:
32    barcode_qr_config: Optional[Mapping[str, Any]] = None
33    barcode_code39_config: Optional[Mapping[str, Any]] = None
PageBarcodeStepConfig( barcode_qr_config: Union[Mapping[str, Any], NoneType] = None, barcode_code39_config: Union[Mapping[str, Any], NoneType] = None)
2def __init__(self, barcode_qr_config=attr_dict['barcode_qr_config'].default, barcode_code39_config=attr_dict['barcode_code39_config'].default):
3    self.barcode_qr_config = barcode_qr_config
4    self.barcode_code39_config = barcode_code39_config

Method generated by attrs for class PageBarcodeStepConfig.

class PageBarcodeStepInput:
37class PageBarcodeStepInput:
38    page_layout_step_output: PageLayoutStepOutput
PageBarcodeStepInput( 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 PageBarcodeStepInput.

class PageBarcodeStepOutput:
42class PageBarcodeStepOutput:
43    height: int
44    width: int
45    barcode_qr_score_maps: Sequence[ScoreMap]
46    barcode_code39_score_maps: Sequence[ScoreMap]
PageBarcodeStepOutput( height: int, width: int, barcode_qr_score_maps: Sequence[vkit.element.score_map.ScoreMap], barcode_code39_score_maps: Sequence[vkit.element.score_map.ScoreMap])
2def __init__(self, height, width, barcode_qr_score_maps, barcode_code39_score_maps):
3    self.height = height
4    self.width = width
5    self.barcode_qr_score_maps = barcode_qr_score_maps
6    self.barcode_code39_score_maps = barcode_code39_score_maps

Method generated by attrs for class PageBarcodeStepOutput.

 49class PageBarcodeStep(
 50    PipelineStep[
 51        PageBarcodeStepConfig,
 52        PageBarcodeStepInput,
 53        PageBarcodeStepOutput,
 54    ]
 55):  # yapf: disable
 56
 57    def __init__(self, config: PageBarcodeStepConfig):
 58        super().__init__(config)
 59
 60        self.barcode_qr_engine_executor = barcode_qr_engine_executor_factory.create(
 61            self.config.barcode_qr_config
 62        )
 63        self.barcode_code39_engine_executor = barcode_code39_engine_executor_factory.create(
 64            self.config.barcode_code39_config
 65        )
 66
 67    def run(self, input: PageBarcodeStepInput, rng: RandomGenerator):
 68        page_layout_step_output = input.page_layout_step_output
 69        page_layout = page_layout_step_output.page_layout
 70
 71        barcode_qr_score_maps: List[ScoreMap] = []
 72        for layout_barcode_qr in page_layout.layout_barcode_qrs:
 73            box = layout_barcode_qr.box
 74            assert box.height == box.width
 75
 76            barcode_qr_score_map = self.barcode_qr_engine_executor.run(
 77                {
 78                    'height': box.height,
 79                    'width': box.width,
 80                },
 81                rng=rng,
 82            )
 83            barcode_qr_score_map = barcode_qr_score_map.to_box_attached(box)
 84            barcode_qr_score_maps.append(barcode_qr_score_map)
 85
 86        barcode_code39_score_maps: List[ScoreMap] = []
 87        for layout_barcode_code39 in page_layout.layout_barcode_code39s:
 88            box = layout_barcode_code39.box
 89
 90            barcode_code39_score_map = self.barcode_code39_engine_executor.run(
 91                {
 92                    'height': box.height,
 93                    'width': box.width,
 94                },
 95                rng=rng,
 96            )
 97            barcode_code39_score_map = barcode_code39_score_map.to_box_attached(box)
 98            barcode_code39_score_maps.append(barcode_code39_score_map)
 99
100        return PageBarcodeStepOutput(
101            height=page_layout.height,
102            width=page_layout.width,
103            barcode_qr_score_maps=barcode_qr_score_maps,
104            barcode_code39_score_maps=barcode_code39_score_maps,
105        )

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

57    def __init__(self, config: PageBarcodeStepConfig):
58        super().__init__(config)
59
60        self.barcode_qr_engine_executor = barcode_qr_engine_executor_factory.create(
61            self.config.barcode_qr_config
62        )
63        self.barcode_code39_engine_executor = barcode_code39_engine_executor_factory.create(
64            self.config.barcode_code39_config
65        )
def run( self, input: vkit.pipeline.text_detection.page_barcode.PageBarcodeStepInput, rng: numpy.random._generator.Generator):
 67    def run(self, input: PageBarcodeStepInput, rng: RandomGenerator):
 68        page_layout_step_output = input.page_layout_step_output
 69        page_layout = page_layout_step_output.page_layout
 70
 71        barcode_qr_score_maps: List[ScoreMap] = []
 72        for layout_barcode_qr in page_layout.layout_barcode_qrs:
 73            box = layout_barcode_qr.box
 74            assert box.height == box.width
 75
 76            barcode_qr_score_map = self.barcode_qr_engine_executor.run(
 77                {
 78                    'height': box.height,
 79                    'width': box.width,
 80                },
 81                rng=rng,
 82            )
 83            barcode_qr_score_map = barcode_qr_score_map.to_box_attached(box)
 84            barcode_qr_score_maps.append(barcode_qr_score_map)
 85
 86        barcode_code39_score_maps: List[ScoreMap] = []
 87        for layout_barcode_code39 in page_layout.layout_barcode_code39s:
 88            box = layout_barcode_code39.box
 89
 90            barcode_code39_score_map = self.barcode_code39_engine_executor.run(
 91                {
 92                    'height': box.height,
 93                    'width': box.width,
 94                },
 95                rng=rng,
 96            )
 97            barcode_code39_score_map = barcode_code39_score_map.to_box_attached(box)
 98            barcode_code39_score_maps.append(barcode_code39_score_map)
 99
100        return PageBarcodeStepOutput(
101            height=page_layout.height,
102            width=page_layout.width,
103            barcode_qr_score_maps=barcode_qr_score_maps,
104            barcode_code39_score_maps=barcode_code39_score_maps,
105        )