-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom_augmentation.py
65 lines (54 loc) · 3.06 KB
/
custom_augmentation.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
60
61
62
63
64
65
import cv2
from albumentations import Compose, PadIfNeeded, ShiftScaleRotate, ImageCompression, KeypointParams, \
LongestMaxSize
from albumentations.imgaug.transforms import IAAAffine
class COCOTransformation:
def __init__(self, width, height):
self.aug = Compose([
ShiftScaleRotate(p=0.5, rotate_limit=5, scale_limit=0.05, border_mode=cv2.BORDER_CONSTANT),
ImageCompression(quality_lower=95, quality_upper=100, p=1),
IAAAffine(shear=0.2, always_apply=False, p=0.3),
LongestMaxSize(max_size=width if width > height else height),
PadIfNeeded(min_height=height, min_width=width, border_mode=cv2.BORDER_CONSTANT)
], keypoint_params=KeypointParams(format='xy', label_fields=['pose_id', "join_id"],
remove_invisible=True))
def __call__(self, image, keypoints):
cood, pose, join = keypoints
transformed = self.aug(image=image, keypoints=cood, pose_id=pose, join_id=join)
return transformed["image"], (transformed['keypoints'], transformed['pose_id'], \
transformed['join_id'])
class HandTransformation:
def __init__(self, width, height):
self.aug = Compose([
ShiftScaleRotate(p=0.5, rotate_limit=5, scale_limit=0.05, border_mode=cv2.BORDER_CONSTANT),
ImageCompression(quality_lower=95, quality_upper=100, p=1),
IAAAffine(shear=0.2, always_apply=False, p=0.3),
LongestMaxSize(max_size=width if width > height else height),
PadIfNeeded(min_height=height, min_width=width, border_mode=cv2.BORDER_CONSTANT)
], keypoint_params=KeypointParams(format='xy', label_fields=["join_id"],
remove_invisible=True))
def __call__(self, image, keypoints):
cood, join = keypoints
transformed = self.aug(image=image, keypoints=cood, join_id=join)
return transformed["image"], (transformed['keypoints'], transformed['join_id'])
class COCOTransformationTest:
def __init__(self, width, height):
self.aug = Compose([
LongestMaxSize(max_size=width if width > height else height),
PadIfNeeded(min_height=height, min_width=width, border_mode=cv2.BORDER_CONSTANT)
], keypoint_params=KeypointParams(format='xy', label_fields=['pose_id', "join_id"],
remove_invisible=True))
def __call__(self, image, keypoints):
cood, pose, join = keypoints
transformed = self.aug(image=image, keypoints=cood, pose_id=pose, join_id=join)
return transformed["image"], (transformed['keypoints'], transformed['pose_id'], \
transformed['join_id'])
class InferenceTransformation:
def __init__(self, width, height):
self.aug = Compose([
LongestMaxSize(max_size=width if width > height else height),
PadIfNeeded(min_height=height, min_width=width, border_mode=cv2.BORDER_CONSTANT)
])
def __call__(self, image):
transformed = self.aug(image=image)
return transformed["image"]