vkit.mechanism.distortion_policy.photometric.effect

  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 Tuple
 15
 16import attrs
 17from numpy.random import Generator as RandomGenerator
 18
 19from vkit.mechanism import distortion
 20from ..type import DistortionConfigGenerator, DistortionPolicyFactory
 21from ..opt import sample_int, sample_float
 22
 23
 24@attrs.define
 25class JpegQualityConfigGeneratorConfig:
 26    quality_min: int = 1
 27    quality_max: int = 50
 28
 29
 30class JpegQualityConfigGenerator(
 31    DistortionConfigGenerator[
 32        JpegQualityConfigGeneratorConfig,
 33        distortion.JpegQualityConfig,
 34    ]
 35):  # yapf: disable
 36
 37    def __call__(self, shape: Tuple[int, int], rng: RandomGenerator):
 38        quality = sample_int(
 39            level=self.level,
 40            value_min=self.config.quality_min,
 41            value_max=self.config.quality_max,
 42            prob_negative=None,
 43            rng=rng,
 44            inverse_level=True
 45        )
 46
 47        return distortion.JpegQualityConfig(quality=quality)
 48
 49
 50jpeg_quality_policy_factory = DistortionPolicyFactory(
 51    distortion.jpeg_quality,
 52    JpegQualityConfigGenerator,
 53)
 54
 55
 56@attrs.define
 57class PixelationConfigGeneratorConfig:
 58    ratio_min: float = 0.3
 59    ratio_max: float = 1.0
 60
 61
 62class PixelationConfigGenerator(
 63    DistortionConfigGenerator[
 64        PixelationConfigGeneratorConfig,
 65        distortion.PixelationConfig,
 66    ]
 67):  # yapf: disable
 68
 69    def __call__(self, shape: Tuple[int, int], rng: RandomGenerator):
 70        ratio = sample_float(
 71            level=self.level,
 72            value_min=self.config.ratio_min,
 73            value_max=self.config.ratio_max,
 74            prob_reciprocal=None,
 75            rng=rng,
 76            inverse_level=True,
 77        )
 78
 79        return distortion.PixelationConfig(ratio=ratio)
 80
 81
 82pixelation_policy_factory = DistortionPolicyFactory(
 83    distortion.pixelation,
 84    PixelationConfigGenerator,
 85)
 86
 87
 88@attrs.define
 89class FogConfigGeneratorConfig:
 90    roughness_min: float = 0.2
 91    roughness_max: float = 0.85
 92    ratio_max_min: float = 0.2
 93    ratio_max_max: float = 0.75
 94
 95
 96class FogConfigGenerator(
 97    DistortionConfigGenerator[
 98        FogConfigGeneratorConfig,
 99        distortion.FogConfig,
100    ]
101):  # yapf: disable
102
103    def __call__(self, shape: Tuple[int, int], rng: RandomGenerator):
104        roughness = sample_float(
105            level=self.level,
106            value_min=self.config.roughness_min,
107            value_max=self.config.roughness_max,
108            prob_reciprocal=None,
109            rng=rng,
110        )
111        ratio_max = sample_float(
112            level=self.level,
113            value_min=self.config.ratio_max_min,
114            value_max=self.config.ratio_max_max,
115            prob_reciprocal=None,
116            rng=rng,
117        )
118
119        return distortion.FogConfig(
120            roughness=roughness,
121            ratio_max=ratio_max,
122        )
123
124
125fog_policy_factory = DistortionPolicyFactory(
126    distortion.fog,
127    FogConfigGenerator,
128)
class JpegQualityConfigGeneratorConfig:
26class JpegQualityConfigGeneratorConfig:
27    quality_min: int = 1
28    quality_max: int = 50
JpegQualityConfigGeneratorConfig(quality_min: int = 1, quality_max: int = 50)
2def __init__(self, quality_min=attr_dict['quality_min'].default, quality_max=attr_dict['quality_max'].default):
3    self.quality_min = quality_min
4    self.quality_max = quality_max

Method generated by attrs for class JpegQualityConfigGeneratorConfig.

31class JpegQualityConfigGenerator(
32    DistortionConfigGenerator[
33        JpegQualityConfigGeneratorConfig,
34        distortion.JpegQualityConfig,
35    ]
36):  # yapf: disable
37
38    def __call__(self, shape: Tuple[int, int], rng: RandomGenerator):
39        quality = sample_int(
40            level=self.level,
41            value_min=self.config.quality_min,
42            value_max=self.config.quality_max,
43            prob_negative=None,
44            rng=rng,
45            inverse_level=True
46        )
47
48        return distortion.JpegQualityConfig(quality=quality)

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

class PixelationConfigGeneratorConfig:
58class PixelationConfigGeneratorConfig:
59    ratio_min: float = 0.3
60    ratio_max: float = 1.0
PixelationConfigGeneratorConfig(ratio_min: float = 0.3, ratio_max: float = 1.0)
2def __init__(self, ratio_min=attr_dict['ratio_min'].default, ratio_max=attr_dict['ratio_max'].default):
3    self.ratio_min = ratio_min
4    self.ratio_max = ratio_max

Method generated by attrs for class PixelationConfigGeneratorConfig.

63class PixelationConfigGenerator(
64    DistortionConfigGenerator[
65        PixelationConfigGeneratorConfig,
66        distortion.PixelationConfig,
67    ]
68):  # yapf: disable
69
70    def __call__(self, shape: Tuple[int, int], rng: RandomGenerator):
71        ratio = sample_float(
72            level=self.level,
73            value_min=self.config.ratio_min,
74            value_max=self.config.ratio_max,
75            prob_reciprocal=None,
76            rng=rng,
77            inverse_level=True,
78        )
79
80        return distortion.PixelationConfig(ratio=ratio)

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

class FogConfigGeneratorConfig:
90class FogConfigGeneratorConfig:
91    roughness_min: float = 0.2
92    roughness_max: float = 0.85
93    ratio_max_min: float = 0.2
94    ratio_max_max: float = 0.75
FogConfigGeneratorConfig( roughness_min: float = 0.2, roughness_max: float = 0.85, ratio_max_min: float = 0.2, ratio_max_max: float = 0.75)
2def __init__(self, roughness_min=attr_dict['roughness_min'].default, roughness_max=attr_dict['roughness_max'].default, ratio_max_min=attr_dict['ratio_max_min'].default, ratio_max_max=attr_dict['ratio_max_max'].default):
3    self.roughness_min = roughness_min
4    self.roughness_max = roughness_max
5    self.ratio_max_min = ratio_max_min
6    self.ratio_max_max = ratio_max_max

Method generated by attrs for class FogConfigGeneratorConfig.

 97class FogConfigGenerator(
 98    DistortionConfigGenerator[
 99        FogConfigGeneratorConfig,
100        distortion.FogConfig,
101    ]
102):  # yapf: disable
103
104    def __call__(self, shape: Tuple[int, int], rng: RandomGenerator):
105        roughness = sample_float(
106            level=self.level,
107            value_min=self.config.roughness_min,
108            value_max=self.config.roughness_max,
109            prob_reciprocal=None,
110            rng=rng,
111        )
112        ratio_max = sample_float(
113            level=self.level,
114            value_min=self.config.ratio_max_min,
115            value_max=self.config.ratio_max_max,
116            prob_reciprocal=None,
117            rng=rng,
118        )
119
120        return distortion.FogConfig(
121            roughness=roughness,
122            ratio_max=ratio_max,
123        )

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