Я это уже написал
Код: Выделить всё
Bounds = tuple[Union[int, None], Union[int, None]]
"""Represents an inclusive [min; max] interval. Units are pixels. Use None to represent an unbounded endpoint."""
@dataclass
class Measurements:
width: int
height: int
head_left: int
head_right: int
head_width: int
head_top: int
head_bottom: int
head_height: int
space_above_head: int
space_below_chin: int
eyes_to_bottom_edge: int
eyes_to_top_edge: int
Constraints = dataclasses.make_dataclass('Constraints', [(f.name, Bounds, dataclasses.field(default=(None, None))) for f in dataclasses.fields(Measurements)])
def get_original_measurements(image: npt.NDArray) -> Measurements:
...
def crop(image: npt.NDArray, constraints: Constraints) -> npt.NDArray:
measurements = get_original_measurements(image)
...
Код: Выделить всё
cropЯ хотел избежать этого дублирования:
Код: Выделить всё
@dataclass
class Measurements:
width: int
height: int
head_left: int
head_right: int
head_width: int
head_top: int
head_bottom: int
head_height: int
space_above_head: int
space_below_chin: int
eyes_to_bottom_edge: int
eyes_to_top_edge: int
@dataclass
class Constraints:
width: Bounds
height: Bounds
head_left: Bounds
head_right: Bounds
head_width: Bounds
head_top: Bounds
head_bottom: Bounds
head_height: Bounds
space_above_head: Bounds
space_below_chin: Bounds
eyes_to_bottom_edge: Bounds
eyes_to_top_edge: Bounds
Код: Выделить всё
Constraints = dataclasses.make_dataclass('Constraints', [(f.name, Bounds, dataclasses.field(default=(None, None))) for f in dataclasses.fields(Measurements)])
Код: Выделить всё
def crop(image: npt.NDArray, constraints: Constraints) -> npt.NDArray:
^
Может ли кто-нибудь придумать лучшее решение ?
Подробнее здесь: https://stackoverflow.com/questions/793 ... mmatically
Мобильная версия