Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update data_aug.py #23

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
151 changes: 151 additions & 0 deletions data_aug/data_aug.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down