def _crop_to_max_and_pad(self, img, boxes, landmarks, image_size, is_random_pad=False): im_h, im_w = img.shape[0:2] rx, ry, rw, rh = min_bbox( rel_bbox_coords(abs_box, [im_h, im_w]) for abs_box in boxes) rcx, rcy = [int((rx + (rw / 2)) * im_w), int((ry + (rh / 2)) * im_h)] crop_x0 = int(max(0, rcx - (image_size / 2))) crop_y0 = int(max(0, rcy - (image_size / 2))) crop_x1 = int(min(im_w, rcx + (image_size / 2))) crop_y1 = int(min(im_h, rcy + (image_size / 2))) img = img[crop_y0:crop_y1, crop_x0:crop_x1, :] boxes = [[x - crop_x0, y - crop_y0, w, h] for x, y, w, h in boxes] landmarks = [[[x - crop_x0, y - crop_y0] for x, y in l] for l in landmarks] boxes = filter_abs_boxes_out_of_borders(boxes, img.shape[0:2]) landmarks = filter_abs_landmarks_out_of_borders( landmarks, img.shape[0:2]) img, boxes, landmarks = pad_to_square( img, boxes, landmarks, image_size, mode='random' if is_random_pad else 'center') return img, boxes, landmarks
def crop(img, abs_boxes, abs_landmarks, crop_range=0.0, is_bbox_safe=True, min_box_target_size=0): im_h, im_w = img.shape[:2] target_rel_boxes = [rel_bbox_coords(abs_box, [im_h, im_w]) for abs_box in filter_abs_boxes(abs_boxes, min_box_target_size)] if len(target_rel_boxes) < 1: return img, abs_boxes, abs_landmarks roi = min_bbox(target_rel_boxes) if is_bbox_safe else random.choice(target_rel_boxes) rx, ry, rw, rh = roi min_x, min_y, max_x, max_y = [num_in_range(v, 0, 1) for v in [rx, ry, rx + rw, ry + rh]] min_x, max_x = [int(v * im_w) for v in [min_x, max_x]] min_y, max_y = [int(v * im_h) for v in [min_y, max_y]] crop_range = num_in_range(crop_range, 0, 1) crop_x0 = random.randint(round(crop_range * min_x), min_x) crop_y0 = random.randint(round(crop_range * min_y), min_y) crop_x1 = random.randint(0, round((1.0 - crop_range) * abs(im_w - max_x))) + max_x crop_y1 = random.randint(0, round((1.0 - crop_range) * abs(im_h - max_y))) + max_y cropped_img = img[crop_y0:crop_y1, crop_x0:crop_x1] shifted_abs_boxes = [[x - crop_x0, y - crop_y0, w, h] for x, y, w, h in abs_boxes] filtered_abs_boxes = filter_abs_boxes_out_of_borders(shifted_abs_boxes, cropped_img.shape[0:2]) shifted_abs_landmarks = [[(x - crop_x0, y - crop_y0) for x, y in l] for l in abs_landmarks] filtered_abs_landmarks = filter_abs_landmarks_out_of_borders(shifted_abs_landmarks, cropped_img.shape[0:2]) return cropped_img, filtered_abs_boxes, filtered_abs_landmarks
def augment(self, img, boxes=[], landmarks=[], image_size=None): try: _boxes = fix_boxes( [abs_bbox_coords(box, img.shape[0:2]) for box in boxes], max(img.shape[0:2]), 1) _landmarks = [ abs_landmarks_coords(l, img.shape[0:2]) for l in landmarks ] out = self._augment_abs_boxes( img, _boxes, _landmarks, image_size, return_augmentation_history=self.return_augmentation_history) if self.return_augmentation_history: for aug in (['inputs'] + out['augmentations']): _img, _boxes, _landmarks = out[aug] _boxes = [ rel_bbox_coords(box, _img.shape[0:2]) for box in _boxes ] _landmarks = [ rel_landmarks_coords(l, _img.shape[0:2]) for l in _landmarks ] out[aug] = [_img, _boxes, _landmarks] return out else: _img, _boxes, _landmarks = out _boxes = [ rel_bbox_coords(box, _img.shape[0:2]) for box in _boxes ] _landmarks = [ rel_landmarks_coords(l, _img.shape[0:2]) for l in _landmarks ] return _img, _boxes, _landmarks except Exception as e: if not self.fallback_on_augmentation_exception: raise e if not self.ignore_log_augmentation_exception: print("failed to augment") print(e) return self.resize_and_to_square(img, boxes=boxes, landmarks=landmarks, image_size=image_size)
def resize_to_fixed(img, boxes, landmarks, image_size, is_relative_coords=False): im_h, im_w = img.shape[0:2] s = image_size / max(im_h, im_w) if is_relative_coords: boxes = [abs_bbox_coords(box, img.shape[0:2]) for box in boxes] landmarks = [abs_landmarks_coords(l, img.shape[0:2]) for l in landmarks] img, boxes, landmarks = resize_by_ratio(img, boxes, landmarks, s, s) if is_relative_coords: boxes = [rel_bbox_coords(box, img.shape[0:2]) for box in boxes] landmarks = [rel_landmarks_coords(l, img.shape[0:2]) for l in landmarks] return img, boxes, landmarks
def _crop_and_random_pad_to_square(self, img, boxes, image_size): im_h, im_w = img.shape[0:2] rx, ry, rw, rh = min_bbox( rel_bbox_coords(abs_box, [im_h, im_w]) for abs_box in boxes) rcx, rcy = [int((rx + (rw / 2)) * im_w), int((ry + (rh / 2)) * im_h)] crop_x0 = int(max(0, rcx - (image_size / 2))) crop_y0 = int(max(0, rcy - (image_size / 2))) crop_x1 = int(min(im_w, rcx + (image_size / 2))) crop_y1 = int(min(im_h, rcy + (image_size / 2))) img = img[crop_y0:crop_y1, crop_x0:crop_x1, :] boxes = self._fix_abs_boxes([(x - crop_x0, y - crop_y0, w, h) for x, y, w, h in boxes], img.shape[0:2]) img, boxes = random_pad_to_square(img, boxes, image_size) return img, boxes
def resize_and_to_square(self, img, boxes=[], image_size=None): boxes = fix_boxes([ abs_bbox_coords(box, img.shape[0:2]) for box in self._fix_rel_boxes(boxes) ], max(img.shape[0:2]), 1) if self.resize_mode == 'resize_to_max_and_center_pad': img, boxes = self._resize_to_max_and_center_pad( img, boxes, image_size) elif self.resize_mode == 'crop_and_random_pad_to_square': img, boxes = self._crop_and_random_pad_to_square( img, boxes, image_size) else: raise Exception('unkown resize_mode ' + str(self.resize_mode)) boxes = [rel_bbox_coords(box, img.shape[0:2]) for box in boxes] return img, boxes
def augment(self, img, boxes=[], image_size=None): try: _boxes = fix_boxes([ abs_bbox_coords(box, img.shape[0:2]) for box in self._fix_rel_boxes(boxes) ], max(img.shape[0:2]), 1) _img, _boxes = self._augment_abs_boxes(img, _boxes, image_size) _boxes = [rel_bbox_coords(box, _img.shape[0:2]) for box in _boxes] return _img, _boxes except Exception as e: if not self.ignore_log_augmentation_exception: print("failed to augment") print(e) return self.resize_and_to_square(img, boxes=boxes, image_size=image_size)
def run2(): while True: abs_boxes = filter_abs_boxes( [abs_bbox_coords(rel_box, img.shape[0:2]) for rel_box in boxes], min_box_px_size) rel_boxes = [ rel_bbox_coords(abs_box, img.shape[0:2]) for abs_box in abs_boxes ] rel_landmarks = [] outputs = augmentor.augment(img, rel_boxes, rel_landmarks, 640) print(outputs['augmentations']) keys = [ 'inputs', 'crop', 'pre_downscale', 'rotate', 'anchor_based_sampling', 'flip', 'stretch', 'crop_and_random_pad_to_square', 'color_distortion' ] for aug in keys: if aug in outputs: out, out_boxes, out_landmarks = outputs[aug] out, out_boxes, out_landmarks = resize_to_max( out, out_boxes, out_landmarks, 640, is_relative_coords=True) out_boxes = [ abs_bbox_coords(out_box, out.shape[0:2]) for out_box in out_boxes ] # print(out_boxes) for x, y, w, h in out_boxes: cv2.rectangle(out, (x, y), (x + w, y + h), (255, 0, 0), 1) # print(out.shape, len(out_boxes)) if out.shape[0] != 640 or out.shape[1] != 640: print(out.shape) cv2.imshow(aug, out) cv2.waitKey() cv2.destroyAllWindows()
def resize_and_to_square(self, img, boxes=[], landmarks=[], image_size=None): boxes = fix_boxes( [abs_bbox_coords(box, img.shape[0:2]) for box in boxes], max(img.shape[0:2]), 1) landmarks = [ abs_landmarks_coords(l, img.shape[0:2]) for l in landmarks ] resize_mode = self.resize_mode if resize_mode == 'crop_or_resize_to_fixed_and_random_pad': resize_mode = random.choice([ 'crop_and_random_pad_to_square', 'resize_to_fixed_and_random_pad' ]) if resize_mode == 'resize_to_max_and_center_pad': img, boxes, landmarks = self._resize_to_max_and_center_pad( img, boxes, landmarks, image_size) elif resize_mode == 'resize_to_fixed_and_center_pad': img, boxes, landmarks = self._resize_to_fixed_and_pad( img, boxes, landmarks, image_size, is_random_pad=False) elif resize_mode == 'resize_to_fixed_and_random_pad': img, boxes, landmarks = self._resize_to_fixed_and_pad( img, boxes, landmarks, image_size, is_random_pad=True) elif resize_mode == 'crop_and_random_pad_to_square': img, boxes, landmarks = self._crop_to_max_and_pad( img, boxes, landmarks, image_size, is_random_pad=True) else: raise Exception('unknown resize_mode ' + str(resize_mode)) boxes = [rel_bbox_coords(box, img.shape[0:2]) for box in boxes] landmarks = [ rel_landmarks_coords(l, img.shape[0:2]) for l in landmarks ] return img, boxes, landmarks
def _fix_abs_boxes(self, abs_boxes, hw): return [ abs_bbox_coords(rel_box, hw) for rel_box in self._fix_rel_boxes( [rel_bbox_coords(abs_box, hw) for abs_box in abs_boxes]) ]