-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage_utils.py
59 lines (49 loc) · 1.27 KB
/
image_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import numpy as np
from beartype import beartype
from dataclasses import dataclass
@beartype
@dataclass
class BBox:
x: int
y: int
w: int
h: int
def size_to_bbox[T: (int, float)](
img_w: int, img_h: int,
x: T, y: T, w: T, h: T
) -> BBox:
types = [type(x), type(y), type(w), type(h)]
assert all(t is int for t in types) or all(t is float for t in types), "All types should be either int or float"
if isinstance(x, float):
return BBox(
int(x * img_w),
int(y * img_h),
int(w * img_w),
int(h * img_w)
)
else:
return BBox(x, y, w, h) # type: ignore
@beartype
def crop_image[T: (int, float)](
image: np.ndarray, /,
x: T, y: T, w: T,
*,
copy: bool = False
) -> np.ndarray:
img_h, img_w = image.shape[:2]
box = size_to_bbox(img_w, img_h, x, y, w, w)
res = image[box.y:box.y + box.h, box.x:box.x + box.w]
if copy:
return res.copy()
return res
@dataclass
class Pair[T]:
first: T
second: T
@property
def last(self) -> T:
return self.second
def __iter__(self):
return iter((self.first, self.second))
def __getitem__(self, idx: int) -> T:
return (self.first, self.second)[idx]