From 7d03992f2458c524bfaccd477fd1db3ba85bd1f8 Mon Sep 17 00:00:00 2001 From: Yixiong Jiang Date: Tue, 14 Apr 2020 22:44:22 +0100 Subject: [PATCH] Update data_aug.py Add VerticallyFlip and ShearY --- data_aug/data_aug.py | 151 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 151 insertions(+) diff --git a/data_aug/data_aug.py b/data_aug/data_aug.py index 94892b0..23da6b4 100644 --- a/data_aug/data_aug.py +++ b/data_aug/data_aug.py @@ -89,6 +89,107 @@ def __call__(self, img, bboxes): return img, bboxes +class VerticalFlip(object): + + """vertically flips the Image + + + Returns + ------- + + numpy.ndaaray + Flipped image in the numpy format of shape `HxWxC` + + numpy.ndarray + Tranformed bounding box co-ordinates of the format `n x 4` where n is + number of bounding boxes and 4 represents `x1,y1,x2,y2` of the box + + """ + + def __init__(self): + pass + + def __call__(self, img, bboxes): + img_center = np.array(img.shape[:2])[::-1]/2 + img_center = np.hstack((img_center, img_center)) + img = img[::-1, :, :] + bboxes[:, [1, 3]] += 2*(img_center[[1, 3]] - bboxes[:, [1, 3]]) + + box_h = abs(bboxes[:, 1] - bboxes[:, 3]) + + bboxes[:, 1] -= box_h + bboxes[:, 3] += box_h + + return img, bboxes + + +class HorizontalFlip(object): + + """horizontally flips the Image + + Returns + ------- + + numpy.ndaaray + Flipped image in the numpy format of shape `HxWxC` + + numpy.ndarray + Tranformed bounding box co-ordinates of the format `n x 4` where n is + number of bounding boxes and 4 represents `x1,y1,x2,y2` of the box + + """ + + def __init__(self): + pass + + def __call__(self, img, bboxes): + img_center = np.array(img.shape[:2])[::-1]/2 + img_center = np.hstack((img_center, img_center)) + + img = img[:, ::-1, :] + bboxes[:, [0, 2]] += 2*(img_center[[0, 2]] - bboxes[:, [0, 2]]) + + box_w = abs(bboxes[:, 0] - bboxes[:, 2]) + + bboxes[:, 0] -= box_w + bboxes[:, 2] += box_w + + return img, bboxes + +class RandomVerticalFlip(object): + + """Randomly vertically flips the Image with the probability *p* + + + Returns + ------- + + numpy.ndaaray + Flipped image in the numpy format of shape `HxWxC` + + numpy.ndarray + Tranformed bounding box co-ordinates of the format `n x 4` where n is + number of bounding boxes and 4 represents `x1,y1,x2,y2` of the box + + """ + + def __init__(self,p=0.5): + self.p=p + pass + + def __call__(self, img, bboxes): + img_center = np.array(img.shape[:2])[::-1]/2 + img_center = np.hstack((img_center, img_center)) + if random.random() < self.p: + img = img[::-1, :, :] + bboxes[:, [1, 3]] += 2*(img_center[[1, 3]] - bboxes[:, [1, 3]]) + + box_h = abs(bboxes[:, 1] - bboxes[:, 3]) + + bboxes[:, 1] -= box_h + bboxes[:, 3] += box_h + return img, bboxes + class RandomScale(object): """Randomly scales an image @@ -672,6 +773,56 @@ def __call__(self, img, bboxes): return img, bboxes +class ShearY(object): + """Shears an image in Vertical direction + + + Bounding boxes which have an area of less than 25% in the remaining in the + transformed image is dropped. The resolution is maintained, and the remaining + area if any is filled by black color. + + Parameters + ---------- + shear_factor: float + Factor by which the image is sheared in the x-direction + + Returns + ------- + + numpy.ndaaray + Sheared image in the numpy format of shape `HxWxC` + + numpy.ndarray + Tranformed bounding box co-ordinates of the format `n x 4` where n is + number of bounding boxes and 4 represents `x1,y1,x2,y2` of the box + + """ + + def __init__(self, shear_factor = 0.2, vertical=False): + self.shear_factor = shear_factor + + + def __call__(self, img, bboxes): + + shear_factor = self.shear_factor + if shear_factor < 0: + img, bboxes = VerticalFlip()(img, bboxes) + + M = np.array([[1, 0, 0],[abs(shear_factor),1,0]]) + + nH = img.shape[0] + abs(shear_factor*img.shape[1]) + + bboxes[:,[1,3]] += ((bboxes[:,[0,2]])*abs(shear_factor)).astype(int) + + img = cv2.warpAffine(img, M, (img.shape[1],int(nH))) + + if shear_factor < 0: + img, bboxes = VerticalFlip()(img, bboxes) + + + return img, bboxes + + class Resize(object): """Resize the image in accordance to `image_letter_box` function in darknet