def apply_perspective_transform_to_image_data( image_data: ImageData, perspective_matrix: np.ndarray, result_width: int, result_height: int, allow_negative_and_large_coords: bool, remove_bad_coords: bool) -> ImageData: image = image_data.open_image() image = cv2.warpPerspective(image, perspective_matrix, (result_width, result_height)) image_data = copy.deepcopy(image_data) image_data.keypoints = apply_perspective_transform_to_points( image_data.keypoints, perspective_matrix, result_width, result_height, allow_negative_and_large_coords, remove_bad_coords) image_data.bboxes_data = [ _apply_perspective_transform_to_bbox_data( bbox_data, perspective_matrix, result_width, result_height, allow_negative_and_large_coords, remove_bad_coords) for bbox_data in image_data.bboxes_data ] image_data.bboxes_data = [ bbox_data for bbox_data in image_data.bboxes_data if bbox_data is not None ] image_data.image_path = None image_data.image = image return image_data
def resize_image_data(image_data: ImageData, size: Tuple[int, int], resample: Optional[int] = None) -> ImageData: image_data = copy.deepcopy(image_data) image = image_data.open_image() old_height, old_width, _ = image.shape image = Image.fromarray(image) image = image.resize(size, resample=resample) image = np.array(image) new_height, new_width, _ = image.shape def resize_coords(bbox_data: BboxData): bbox_data.xmin = max( 0, min(int(bbox_data.xmin * (new_width / old_width)), new_width - 1)) bbox_data.ymin = max( 0, min(int(bbox_data.ymin * (new_height / old_height)), new_height - 1)) bbox_data.xmax = max( 0, min(int(bbox_data.xmax * (new_width / old_width)), new_width - 1)) bbox_data.ymax = max( 0, min(int(bbox_data.ymax * (new_height / old_height)), new_height - 1)) bbox_data.keypoints[:, 0] = (bbox_data.keypoints[:, 0] * (new_width / old_width)).astype(int) bbox_data.keypoints[:, 1] = (bbox_data.keypoints[:, 1] * (new_height / old_height)).astype(int) bbox_data.keypoints = bbox_data.keypoints.astype(int) bbox_data.cropped_image = None keypoints = [] for (x, y) in bbox_data.keypoints: x = max(0, min(x, new_width - 1)) y = max(0, min(y, new_height - 1)) keypoints.append([x, y]) bbox_data.keypoints = np.array(keypoints).reshape(-1, 2) for additional_bbox_data in bbox_data.additional_bboxes_data: resize_coords(additional_bbox_data) for bbox_data in image_data.bboxes_data: resize_coords(bbox_data) image_data.keypoints[:, 0] = (image_data.keypoints[:, 0] * (new_width / old_width)).astype(int) image_data.keypoints[:, 1] = (image_data.keypoints[:, 1] * (new_height / old_height)).astype(int) keypoints = [] for (x, y) in image_data.keypoints: x = max(0, min(x, new_width - 1)) y = max(0, min(y, new_height - 1)) keypoints.append([x, y]) image_data.keypoints = np.array(keypoints).reshape(-1, 2) image_data.image_path = None image_data.image = image return image_data
def crop_image_data( image_data: ImageData, xmin: int, ymin: int, xmax: int, ymax: int, allow_negative_and_large_coords: bool, remove_bad_coords: bool, ) -> ImageData: assert 0 <= xmin and 0 <= ymin assert xmin <= xmax and ymin <= ymax image_data = copy.deepcopy(image_data) image = image_data.open_image() height, width, _ = image.shape assert xmax <= width and ymax <= height image = image[ymin:ymax, xmin:xmax] new_height, new_width, _ = image.shape def resize_coords(bbox_data: BboxData): bbox_data.xmin = bbox_data.xmin - xmin bbox_data.ymin = bbox_data.ymin - ymin bbox_data.xmax = bbox_data.xmax - xmin bbox_data.ymax = bbox_data.ymax - ymin bbox_data.keypoints[:, 0] -= xmin bbox_data.keypoints[:, 1] -= ymin bbox_data.cropped_image = None if not allow_negative_and_large_coords: bbox_data.xmin = max(0, min(bbox_data.xmin, new_width - 1)) bbox_data.ymin = max(0, min(bbox_data.ymin, new_height - 1)) bbox_data.xmax = max(0, min(bbox_data.xmax, new_width - 1)) bbox_data.ymax = max(0, min(bbox_data.ymax, new_height - 1)) keypoints = [] for (x, y) in bbox_data.keypoints: x = max(0, min(x, new_width - 1)) y = max(0, min(y, new_height - 1)) keypoints.append([x, y]) bbox_data.keypoints = np.array(keypoints).reshape(-1, 2) for additional_bbox_data in bbox_data.additional_bboxes_data: resize_coords(additional_bbox_data) for bbox_data in image_data.bboxes_data: resize_coords(bbox_data) keypoints = [] for (x, y) in image_data.keypoints: x = max(0, min(x - xmin, new_width - 1)) y = max(0, min(y - ymin, new_height - 1)) keypoints.append([x, y]) image_data.keypoints = np.array(keypoints).reshape(-1, 2) def if_bbox_data_inside_crop(bbox_data: BboxData): bbox_data.keypoints = bbox_data.keypoints[( (bbox_data.keypoints[:, 0] >= 0) & (bbox_data.keypoints[:, 1] >= 0) & (bbox_data.keypoints[:, 0] < new_height) & (bbox_data.keypoints[:, 1] < new_width))] bbox_data.additional_bboxes_data = [ additional_bbox_data for additional_bbox_data in bbox_data.additional_bboxes_data if if_bbox_data_inside_crop(additional_bbox_data) ] return (bbox_data.xmin >= 0 and bbox_data.ymin >= 0 and bbox_data.xmax < new_width and bbox_data.ymax < new_height and bbox_data.xmin < bbox_data.xmax and bbox_data.ymin < bbox_data.ymax) if remove_bad_coords: image_data.bboxes_data = [ bbox_data for bbox_data in image_data.bboxes_data if if_bbox_data_inside_crop(bbox_data) ] image_data.keypoints = image_data.keypoints[( (image_data.keypoints[:, 0] >= 0) & (image_data.keypoints[:, 1] >= 0) & (image_data.keypoints[:, 0] < new_height) & (image_data.keypoints[:, 1] < new_width))] image_data.image_path = None image_data.image = image return image_data