vkit.mechanism.distortion_policy.photometric.blur

  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 GaussianBlurConfigGeneratorConfig:
 26    sigma_min: float = 0.5
 27    sigma_max: float = 1.0
 28
 29
 30class GaussianBlurConfigGenerator(
 31    DistortionConfigGenerator[
 32        GaussianBlurConfigGeneratorConfig,
 33        distortion.GaussianBlurConfig,
 34    ]
 35):  # yapf: disable
 36
 37    def __call__(self, shape: Tuple[int, int], rng: RandomGenerator):
 38        sigma = sample_float(
 39            level=self.level,
 40            value_min=self.config.sigma_min,
 41            value_max=self.config.sigma_max,
 42            prob_reciprocal=None,
 43            rng=rng,
 44        )
 45
 46        return distortion.GaussianBlurConfig(sigma=sigma)
 47
 48
 49gaussian_blur_policy_factory = DistortionPolicyFactory(
 50    distortion.gaussian_blur,
 51    GaussianBlurConfigGenerator,
 52)
 53
 54
 55@attrs.define
 56class DefocusBlurConfigGeneratorConfig:
 57    radius_min: int = 1
 58    radius_max: int = 2
 59
 60
 61class DefocusBlurConfigGenerator(
 62    DistortionConfigGenerator[
 63        DefocusBlurConfigGeneratorConfig,
 64        distortion.DefocusBlurConfig,
 65    ]
 66):  # yapf: disable
 67
 68    def __call__(self, shape: Tuple[int, int], rng: RandomGenerator):
 69        radius = sample_int(
 70            level=self.level,
 71            value_min=self.config.radius_min,
 72            value_max=self.config.radius_max,
 73            prob_negative=None,
 74            rng=rng,
 75        )
 76
 77        return distortion.DefocusBlurConfig(radius=radius)
 78
 79
 80defocus_blur_policy_factory = DistortionPolicyFactory(
 81    distortion.defocus_blur,
 82    DefocusBlurConfigGenerator,
 83)
 84
 85
 86@attrs.define
 87class MotionBlurConfigGeneratorConfig:
 88    radius_min: int = 1
 89    radius_max: int = 2
 90
 91
 92class MotionBlurConfigGenerator(
 93    DistortionConfigGenerator[
 94        MotionBlurConfigGeneratorConfig,
 95        distortion.MotionBlurConfig,
 96    ]
 97):  # yapf: disable
 98
 99    def __call__(self, shape: Tuple[int, int], rng: RandomGenerator):
100        radius = sample_int(
101            level=self.level,
102            value_min=self.config.radius_min,
103            value_max=self.config.radius_max,
104            prob_negative=None,
105            rng=rng,
106        )
107        angle = rng.integers(0, 360)
108
109        return distortion.MotionBlurConfig(
110            radius=radius,
111            angle=angle,
112        )
113
114
115motion_blur_policy_factory = DistortionPolicyFactory(
116    distortion.motion_blur,
117    MotionBlurConfigGenerator,
118)
119
120
121@attrs.define
122class GlassBlurConfigGeneratorConfig:
123    sigma_min: float = 0.5
124    sigma_max: float = 1.0
125    delta_min: int = 1
126    delta_max: int = 1
127    loop_min: int = 1
128    loop_max: int = 4
129
130
131class GlassBlurConfigGenerator(
132    DistortionConfigGenerator[
133        GlassBlurConfigGeneratorConfig,
134        distortion.GlassBlurConfig,
135    ]
136):  # yapf: disable
137
138    def __call__(self, shape: Tuple[int, int], rng: RandomGenerator):
139        sigma = sample_float(
140            level=self.level,
141            value_min=self.config.sigma_min,
142            value_max=self.config.sigma_max,
143            prob_reciprocal=None,
144            rng=rng,
145        )
146        delta = sample_int(
147            level=self.level,
148            value_min=self.config.delta_min,
149            value_max=self.config.delta_max,
150            prob_negative=None,
151            rng=rng,
152        )
153        loop = sample_int(
154            level=self.level,
155            value_min=self.config.loop_min,
156            value_max=self.config.loop_max,
157            prob_negative=None,
158            rng=rng,
159        )
160
161        return distortion.GlassBlurConfig(
162            sigma=sigma,
163            delta=delta,
164            loop=loop,
165        )
166
167
168glass_blur_policy_factory = DistortionPolicyFactory(
169    distortion.glass_blur,
170    GlassBlurConfigGenerator,
171)
172
173
174@attrs.define
175class ZoomInBlurConfigGeneratorConfig:
176    ratio_min: float = 0.01
177    ratio_max: float = 0.1
178    step_min: float = 0.002
179    step_max: float = 0.02
180    alpha_min: float = 0.5
181    alpha_max: float = 0.7
182
183
184class ZoomInBlurConfigGenerator(
185    DistortionConfigGenerator[
186        ZoomInBlurConfigGeneratorConfig,
187        distortion.ZoomInBlurConfig,
188    ]
189):  # yapf: disable
190
191    def __call__(self, shape: Tuple[int, int], rng: RandomGenerator):
192        ratio = sample_float(
193            level=self.level,
194            value_min=self.config.ratio_min,
195            value_max=self.config.ratio_max,
196            prob_reciprocal=None,
197            rng=rng,
198        )
199        step = sample_float(
200            level=self.level,
201            value_min=self.config.step_min,
202            value_max=self.config.step_max,
203            prob_reciprocal=None,
204            rng=rng,
205        )
206        alpha = rng.uniform(self.config.alpha_min, self.config.alpha_max)
207
208        return distortion.ZoomInBlurConfig(
209            ratio=ratio,
210            step=step,
211            alpha=alpha,
212        )
213
214
215zoom_in_blur_policy_factory = DistortionPolicyFactory(
216    distortion.zoom_in_blur,
217    ZoomInBlurConfigGenerator,
218)
class GaussianBlurConfigGeneratorConfig:
26class GaussianBlurConfigGeneratorConfig:
27    sigma_min: float = 0.5
28    sigma_max: float = 1.0
GaussianBlurConfigGeneratorConfig(sigma_min: float = 0.5, sigma_max: float = 1.0)
2def __init__(self, sigma_min=attr_dict['sigma_min'].default, sigma_max=attr_dict['sigma_max'].default):
3    self.sigma_min = sigma_min
4    self.sigma_max = sigma_max

Method generated by attrs for class GaussianBlurConfigGeneratorConfig.

31class GaussianBlurConfigGenerator(
32    DistortionConfigGenerator[
33        GaussianBlurConfigGeneratorConfig,
34        distortion.GaussianBlurConfig,
35    ]
36):  # yapf: disable
37
38    def __call__(self, shape: Tuple[int, int], rng: RandomGenerator):
39        sigma = sample_float(
40            level=self.level,
41            value_min=self.config.sigma_min,
42            value_max=self.config.sigma_max,
43            prob_reciprocal=None,
44            rng=rng,
45        )
46
47        return distortion.GaussianBlurConfig(sigma=sigma)

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 DefocusBlurConfigGeneratorConfig:
57class DefocusBlurConfigGeneratorConfig:
58    radius_min: int = 1
59    radius_max: int = 2
DefocusBlurConfigGeneratorConfig(radius_min: int = 1, radius_max: int = 2)
2def __init__(self, radius_min=attr_dict['radius_min'].default, radius_max=attr_dict['radius_max'].default):
3    self.radius_min = radius_min
4    self.radius_max = radius_max

Method generated by attrs for class DefocusBlurConfigGeneratorConfig.

62class DefocusBlurConfigGenerator(
63    DistortionConfigGenerator[
64        DefocusBlurConfigGeneratorConfig,
65        distortion.DefocusBlurConfig,
66    ]
67):  # yapf: disable
68
69    def __call__(self, shape: Tuple[int, int], rng: RandomGenerator):
70        radius = sample_int(
71            level=self.level,
72            value_min=self.config.radius_min,
73            value_max=self.config.radius_max,
74            prob_negative=None,
75            rng=rng,
76        )
77
78        return distortion.DefocusBlurConfig(radius=radius)

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 MotionBlurConfigGeneratorConfig:
88class MotionBlurConfigGeneratorConfig:
89    radius_min: int = 1
90    radius_max: int = 2
MotionBlurConfigGeneratorConfig(radius_min: int = 1, radius_max: int = 2)
2def __init__(self, radius_min=attr_dict['radius_min'].default, radius_max=attr_dict['radius_max'].default):
3    self.radius_min = radius_min
4    self.radius_max = radius_max

Method generated by attrs for class MotionBlurConfigGeneratorConfig.

 93class MotionBlurConfigGenerator(
 94    DistortionConfigGenerator[
 95        MotionBlurConfigGeneratorConfig,
 96        distortion.MotionBlurConfig,
 97    ]
 98):  # yapf: disable
 99
100    def __call__(self, shape: Tuple[int, int], rng: RandomGenerator):
101        radius = sample_int(
102            level=self.level,
103            value_min=self.config.radius_min,
104            value_max=self.config.radius_max,
105            prob_negative=None,
106            rng=rng,
107        )
108        angle = rng.integers(0, 360)
109
110        return distortion.MotionBlurConfig(
111            radius=radius,
112            angle=angle,
113        )

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 GlassBlurConfigGeneratorConfig:
123class GlassBlurConfigGeneratorConfig:
124    sigma_min: float = 0.5
125    sigma_max: float = 1.0
126    delta_min: int = 1
127    delta_max: int = 1
128    loop_min: int = 1
129    loop_max: int = 4
GlassBlurConfigGeneratorConfig( sigma_min: float = 0.5, sigma_max: float = 1.0, delta_min: int = 1, delta_max: int = 1, loop_min: int = 1, loop_max: int = 4)
2def __init__(self, sigma_min=attr_dict['sigma_min'].default, sigma_max=attr_dict['sigma_max'].default, delta_min=attr_dict['delta_min'].default, delta_max=attr_dict['delta_max'].default, loop_min=attr_dict['loop_min'].default, loop_max=attr_dict['loop_max'].default):
3    self.sigma_min = sigma_min
4    self.sigma_max = sigma_max
5    self.delta_min = delta_min
6    self.delta_max = delta_max
7    self.loop_min = loop_min
8    self.loop_max = loop_max

Method generated by attrs for class GlassBlurConfigGeneratorConfig.

132class GlassBlurConfigGenerator(
133    DistortionConfigGenerator[
134        GlassBlurConfigGeneratorConfig,
135        distortion.GlassBlurConfig,
136    ]
137):  # yapf: disable
138
139    def __call__(self, shape: Tuple[int, int], rng: RandomGenerator):
140        sigma = sample_float(
141            level=self.level,
142            value_min=self.config.sigma_min,
143            value_max=self.config.sigma_max,
144            prob_reciprocal=None,
145            rng=rng,
146        )
147        delta = sample_int(
148            level=self.level,
149            value_min=self.config.delta_min,
150            value_max=self.config.delta_max,
151            prob_negative=None,
152            rng=rng,
153        )
154        loop = sample_int(
155            level=self.level,
156            value_min=self.config.loop_min,
157            value_max=self.config.loop_max,
158            prob_negative=None,
159            rng=rng,
160        )
161
162        return distortion.GlassBlurConfig(
163            sigma=sigma,
164            delta=delta,
165            loop=loop,
166        )

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 ZoomInBlurConfigGeneratorConfig:
176class ZoomInBlurConfigGeneratorConfig:
177    ratio_min: float = 0.01
178    ratio_max: float = 0.1
179    step_min: float = 0.002
180    step_max: float = 0.02
181    alpha_min: float = 0.5
182    alpha_max: float = 0.7
ZoomInBlurConfigGeneratorConfig( ratio_min: float = 0.01, ratio_max: float = 0.1, step_min: float = 0.002, step_max: float = 0.02, alpha_min: float = 0.5, alpha_max: float = 0.7)
2def __init__(self, ratio_min=attr_dict['ratio_min'].default, ratio_max=attr_dict['ratio_max'].default, step_min=attr_dict['step_min'].default, step_max=attr_dict['step_max'].default, alpha_min=attr_dict['alpha_min'].default, alpha_max=attr_dict['alpha_max'].default):
3    self.ratio_min = ratio_min
4    self.ratio_max = ratio_max
5    self.step_min = step_min
6    self.step_max = step_max
7    self.alpha_min = alpha_min
8    self.alpha_max = alpha_max

Method generated by attrs for class ZoomInBlurConfigGeneratorConfig.

185class ZoomInBlurConfigGenerator(
186    DistortionConfigGenerator[
187        ZoomInBlurConfigGeneratorConfig,
188        distortion.ZoomInBlurConfig,
189    ]
190):  # yapf: disable
191
192    def __call__(self, shape: Tuple[int, int], rng: RandomGenerator):
193        ratio = sample_float(
194            level=self.level,
195            value_min=self.config.ratio_min,
196            value_max=self.config.ratio_max,
197            prob_reciprocal=None,
198            rng=rng,
199        )
200        step = sample_float(
201            level=self.level,
202            value_min=self.config.step_min,
203            value_max=self.config.step_max,
204            prob_reciprocal=None,
205            rng=rng,
206        )
207        alpha = rng.uniform(self.config.alpha_min, self.config.alpha_max)
208
209        return distortion.ZoomInBlurConfig(
210            ratio=ratio,
211            step=step,
212            alpha=alpha,
213        )

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