def crop(example: BoxedExample, x_min: float = 0, y_min: float = 0, x_max: float = 1, y_max: float = 1) -> BoxedExample: """Normed coordinates""" abs_x_min, abs_x_max = [ int(example.image.width * x) for x in [x_min, x_max] ] abs_y_min, abs_y_max = [ int(example.image.height * y) for y in [y_min, y_max] ] cropped_boxes = (np.array([[ min(max(xmin, abs_x_min), abs_x_max), min(max(ymin, abs_y_min), abs_y_max), min(max(xmax, abs_x_min), abs_x_max), min(max(ymax, abs_y_min), abs_y_max), ] for [xmin, ymin, xmax, ymax] in example.boxes]) - np.array([abs_x_min, abs_y_min, abs_x_min, abs_y_min]) ) if len(example.boxes) > 0 else np.empty((0, 4)) cropped_boxes = cropped_boxes[areas(cropped_boxes) > 0] return example.replace(image=example.image.crop(box=(abs_x_min, abs_y_min, abs_x_max, abs_y_max)), boxes=cropped_boxes)
def flip_color_on_intensity_heuristic(example: BoxedExample) -> BoxedExample: rgb_example_array_normed = to_rgb(example).image_array / 255 intensity = np.sqrt((rgb_example_array_normed**2).sum(axis=2)) median_int = np.median(intensity) mean_int = np.mean(intensity) if mean_int < median_int: example = example.replace(image=ImageOps.invert(to_rgb(example).image)) return example
def resize(example: BoxedExample, x_factor: float = 1, y_factor: float = 1) -> BoxedExample: target_x = int(example.image.width * x_factor) target_y = int(example.image.height * y_factor) resize_vector = np.array([x_factor, y_factor, x_factor, y_factor]) return example.replace(image=example.image.resize((target_x, target_y), Image.BILINEAR), boxes=example.boxes * resize_vector)
def model_input_to_boxed_example( model_input: Tuple[torch.Tensor, Dict[str, torch.Tensor]], normalization_transform: Normalize = Normalize([1, 1, 1], [1, 1, 1]) ) -> BoxedExample: img_tensor, labels = model_input denormalized_img = (img_tensor.permute(1, 2, 0) * torch.tensor( normalization_transform.std)) + torch.tensor( normalization_transform.mean) return BoxedExample(image=Image.fromarray( (denormalized_img.numpy() * 255).astype(np.uint8)), id=int(labels["image_id"]), boxes=labels["boxes"].numpy())
def _fn(example: BoxedExample, *args, **kwargs) -> BoxedExample: transformed_example = transform_fn(example, *args, **kwargs) if isinstance(example, MaskedExample): if example.masks is not None: mask_examples = [ example.replace(image=m) for m in example.masks ] transformed_masked_examples = [ transform_fn(me, *args, **kwargs) for me in mask_examples ] transformed_example = transformed_example.replace( masks=[me.image for me in transformed_masked_examples]) return transformed_example
def rotate(example: BoxedExample, degree=0) -> BoxedExample: angle = -2 * np.pi * degree / 360 rot_mat = np.array([[np.cos(angle), -np.sin(angle)], [np.sin(angle), np.cos(angle)]]) box_rot_mat = np.zeros((4, 4)) box_rot_mat[0:2, 0:2] = rot_mat box_rot_mat[2:4, 2:4] = rot_mat center_vector = np.array([ example.image.width, example.image.height, example.image.width, example.image.height ]) / 2 centered_boxes = example.boxes - center_vector rot_boxes = (box_rot_mat @ centered_boxes.T).T + center_vector rot_boxes = np.array([[ min(x_min, x_max), min(y_min, y_max), max(x_min, x_max), max(y_min, y_max) ] for [x_min, y_min, x_max, y_max] in rot_boxes]) return example.replace(image=example.image.rotate(degree, fillcolor="black"), boxes=rot_boxes)
def max_pixel_to_255(example: BoxedExample) -> BoxedExample: rgb_example = to_rgb(example) max_pixel = rgb_example.image_array.max() factor = 255 / max_pixel factored = rgb_example.image_array * factor return example.replace(image=Image.fromarray(factored.astype("uint8")))
def to_mode(example: BoxedExample) -> BoxedExample: return example.replace(image=example.image.convert(mode))