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 new bbs coords using imaug functions #311

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
47 changes: 18 additions & 29 deletions preprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ def __getitem__(self, idx):

def on_epoch_end(self):
if self.shuffle: np.random.shuffle(self.images)

def aug_image(self, train_instance, jitter):
image_name = train_instance['filename']
image = cv2.imread(image_name)
Expand All @@ -259,45 +259,34 @@ def aug_image(self, train_instance, jitter):
all_objs = copy.deepcopy(train_instance['object'])

if jitter:
### scale the image
scale = np.random.uniform() / 10. + 1.
image = cv2.resize(image, (0,0), fx = scale, fy = scale)

### translate the image
max_offx = (scale-1.) * w
max_offy = (scale-1.) * h
offx = int(np.random.uniform() * max_offx)
offy = int(np.random.uniform() * max_offy)
image = image[offy : (offy + h), offx : (offx + w)]

### flip the image
flip = np.random.binomial(1, .5)
if flip > 0.5: image = cv2.flip(image, 1)
# augment image and get new bbs
bbs = ia.BoundingBoxesOnImage([
ia.BoundingBox(x1=int(obj["xmin"]), y1=int(obj["ymin"]), x2=int(obj["xmax"]), y2=int(obj["ymax"])) for obj in all_objs
], shape=image.shape)

seq_det = self.aug_pipe.to_deterministic()
image = seq_det.augment_image(image)
bbs_aug = seq_det.augment_bounding_boxes([bbs])[0]
#bbs_aug = bbs_aug.remove_out_of_image().cut_out_of_image()

for i, bbs in enumerate(bbs_aug.bounding_boxes):
all_objs[i]["xmin"] = bbs.x1
all_objs[i]["ymin"] = bbs.y1
all_objs[i]["xmax"] = bbs.x2
all_objs[i]["ymax"] = bbs.y2

image = self.aug_pipe.augment_image(image)

# resize the image to standard size
image = cv2.resize(image, (self.config['IMAGE_H'], self.config['IMAGE_W']))
image = image[:,:,::-1]

# fix object's position and size
#fix object's position and size
for obj in all_objs:
for attr in ['xmin', 'xmax']:
if jitter: obj[attr] = int(obj[attr] * scale - offx)

for attr in ['xmin', 'xmax']:
obj[attr] = int(obj[attr] * float(self.config['IMAGE_W']) / w)
obj[attr] = max(min(obj[attr], self.config['IMAGE_W']), 0)

for attr in ['ymin', 'ymax']:
if jitter: obj[attr] = int(obj[attr] * scale - offy)

obj[attr] = int(obj[attr] * float(self.config['IMAGE_H']) / h)
obj[attr] = max(min(obj[attr], self.config['IMAGE_H']), 0)

if jitter and flip > 0.5:
xmin = obj['xmin']
obj['xmin'] = self.config['IMAGE_W'] - obj['xmax']
obj['xmax'] = self.config['IMAGE_W'] - xmin

return image, all_objs