def test_rescale_to_pointcloud(): img = Image.init_blank((100, 100), n_channels=1) img.landmarks['test'] = bounding_box([0, 0], [10, 10]) pcloud = bounding_box([0, 0], [25, 25]) img_rescaled = img.rescale_to_pointcloud(pcloud) assert_allclose(img_rescaled.shape, (250, 250))
def test_rescale_to_pointcloud(): img = Image.init_blank((100, 100), n_channels=1) img.landmarks["test"] = bounding_box([0, 0], [10, 10]) pcloud = bounding_box([0, 0], [25, 25]) img_rescaled = img.rescale_to_pointcloud(pcloud) assert_allclose(img_rescaled.shape, (250, 250))
def test_compute_overlap_fake_bb(): from research_pyutils import compute_overlap from menpo.shape import bounding_box # # create two bounding boxes. bb1 = bounding_box((0, 0), (1, 1)) bb2 = bounding_box((0, 0), (1.5, 1.5)) # # compute the overlap and ensure it has the appropriate value. ov1 = compute_overlap(bb1.points, bb2.points) assert np.abs(ov1 - 0.64) < 0.0001
def skew_image(image, theta, phi): r""" Method that skews the provided image. Note that the output image has the same size (shape) as the input. Parameters ---------- image : `menpo.image.Image` The image to distort. theta : `float` The skew angle over x axis (tan(theta)). phi : `float` The skew angle over y axis (tan(phi)). Returns ------- skewed_image : `menpo.image.Image` The skewed (distorted) image. """ # Get mask of pixels mask = BooleanImage(image.pixels[0]) # Create the bounding box (pointcloud) of the mask bbox = bounding_box(*mask.bounds_true()) # Skew the bounding box new_bbox = skew_shape(bbox, theta, phi) # Warp the image using TPS pwa = ThinPlateSplines(new_bbox, bbox) return image.warp_to_shape(image.shape, pwa)
def test_crop_to_landmarks(): img = Image.init_blank((100, 100), n_channels=1) img.landmarks['test'] = bounding_box([0, 0], [10, 10]) img_cropped = img.crop_to_landmarks() assert_allclose(img_cropped.shape, (10, 10)) assert 1
def pointgraph_from_circle(fitting, axis_aligned_bb=True): r""" Convert a Pico detection to a menpo.shape.PointDirectedGraph. This enforces a particular point ordering. The Pico detections are circles with a given diameter. Here we convert them to the tightest possible bounding box around the circle. No orientation is currently provided. Parameters ---------- fitting : `cypico.PicoDetection` The Pico detection to convert. Result of calling a pico detection. A namedtuple with a diameter and a centre. Returns ------- bounding_box : `menpo.shape.PointDirectedGraph` A menpo PointDirectedGraph giving the bounding box. """ diameter = fitting.diameter radius = diameter / 2.0 y, x = fitting.center y -= radius x -= radius bb = bounding_box((y, x), (y + diameter, x + diameter)) if not axis_aligned_bb: t = rotate_ccw_about_centre(bb, fitting.orientation, degrees=False) bb = t.apply(bb) return bb
def test_rotate_return_transform(): img = Image.init_blank((100, 100), n_channels=1) img.landmarks["test"] = bounding_box([40, 40], [80, 80]) cropped_img, transform = img.rotate_ccw_about_centre(60, return_transform=True) img_back = cropped_img.warp_to_shape(img.shape, transform.pseudoinverse()) assert_allclose(img_back.shape, img.shape) assert_allclose(img_back.pixels, img.pixels) assert_allclose(img_back.landmarks["test"].points, img.landmarks["test"].points)
def test_rescale_to_diagonal_return_transform(): img = Image.init_blank((100, 100), n_channels=1) img.landmarks['test'] = bounding_box([40, 40], [80, 80]) cropped_img, transform = img.rescale_to_diagonal(100, return_transform=True) img_back = cropped_img.warp_to_shape(img.shape, transform.pseudoinverse()) assert_allclose(img_back.shape, img.shape) assert_allclose(img_back.pixels, img.pixels) assert_allclose(img_back.landmarks['test'].points, img.landmarks['test'].points)
def test_mirror_return_transform(): img = Image.init_blank((100, 100), n_channels=1) img.landmarks['test'] = bounding_box([40, 40], [80, 80]) cropped_img, transform = img.mirror(return_transform=True) img_back = cropped_img.warp_to_shape(img.shape, transform.pseudoinverse()) assert_allclose(img_back.shape, img.shape) assert_allclose(img_back.pixels, img.pixels) assert_allclose(img_back.landmarks['test'].lms.points, img.landmarks['test'].lms.points)
def bounding_box_to_bounding_box(bbox): r""" Apply a single 'all' label to a given bounding box. This bounding box must be as specified by the :map:`bounding_box` method. """ from menpo.shape import bounding_box mapping = OrderedDict() mapping["all"] = np.arange(4) return bounding_box(bbox.points[0], bbox.points[2]), mapping
def test_crop_to_landmarks_return_transform(): img = Image.init_blank((100, 100), n_channels=1) img.landmarks['test'] = bounding_box([40, 40], [80, 80]) cropped_img, transform = img.crop(np.array([20, 30]), np.array([90, 95]), return_transform=True) img_back = cropped_img.warp_to_shape(img.shape, transform.pseudoinverse()) assert_allclose(img_back.shape, img.shape) assert_allclose(img_back.pixels, img.pixels) assert_allclose(img_back.landmarks['test'].points, img.landmarks['test'].points)
def test_crop_to_landmarks_return_transform(): img = Image.init_blank((100, 100), n_channels=1) img.landmarks["test"] = bounding_box([40, 40], [80, 80]) cropped_img, transform = img.crop(np.array([20, 30]), np.array([90, 95]), return_transform=True) img_back = cropped_img.warp_to_shape(img.shape, transform.pseudoinverse()) assert_allclose(img_back.shape, img.shape) assert_allclose(img_back.pixels, img.pixels) assert_allclose(img_back.landmarks["test"].points, img.landmarks["test"].points)
def bounding_box_mirrored_to_bounding_box(bbox): r""" Apply a single 'all' label to a given bounding box that has been mirrored around the vertical axis (flipped around the Y-axis). This bounding box must be as specified by the :map:`bounding_box` method (but mirrored). """ from menpo.shape import bounding_box mapping = OrderedDict() mapping["all"] = np.arange(4) return bounding_box(bbox.points[3], bbox.points[1]), mapping
def rect_to_pointgraph(rect): r""" Convert a dlib.rect to a menpo.shape.PointDirectedGraph. This enforces a particular point ordering. Parameters ---------- rect : `dlib.rect` The bounding box to convert. Returns ------- bounding_box : `menpo.shape.PointDirectedGraph` A menpo PointDirectedGraph giving the bounding box. """ return bounding_box((rect.top(), rect.left()), (rect.bottom(), rect.right()))
def bb_to_pointgraph(bb): r""" Convert a `bob.ip.facedetect.BoundingBox` to a `menpo.shape.PointDirectedGraph`. This enforces a particular point ordering. Parameters ---------- bb : `bob.ip.facedetect.BoundingBox` The bounding box to convert. Returns ------- bounding_box : `menpo.shape.PointDirectedGraph` A menpo PointDirectedGraph giving the bounding box. """ return bounding_box(bb.topleft_f, bb.bottomright_f)
def pointgraph_from_rect(rect): r""" Convert an opencv detection to a menpo.shape.PointDirectedGraph. This enforces a particular point ordering. Parameters ---------- rect : `tuple` The bounding box to convert. Result of calling an opencv detection. Returns ------- bounding_box : `menpo.shape.PointDirectedGraph` A menpo PointDirectedGraph giving the bounding box. """ x, y, w, h = rect return bounding_box((y, x), (y + h, x + w))
def pointgraph_from_rect(rect): r""" Convert an ffld2 detection to a menpo.shape.PointDirectedGraph. This enforces a particular point ordering. Parameters ---------- rect : `tuple` The bounding box to convert. Result of calling an opencv detection. Returns ------- bounding_box : `menpo.shape.PointDirectedGraph` A menpo PointDirectedGraph giving the bounding box. """ return bounding_box((rect.y, rect.x), (rect.y + rect.height, rect.x + rect.width))
def dlib_rect_to_bounding_box(rect): return bounding_box((rect.top(), rect.left()), (rect.bottom(), rect.right()))
def test_rescale_landmarks_to_diagonal_range(): img = Image.init_blank((100, 100), n_channels=1) img.landmarks['test'] = bounding_box([0, 0], [10, 10]) img_rescaled = img.rescale_landmarks_to_diagonal_range(20 * np.sqrt(2)) assert_allclose(img_rescaled.shape, (200, 200))
def test_crop_to_pointcloud_proportion(): img = Image.init_blank((100, 100), n_channels=1) pcloud = bounding_box([0, 0], [50, 50]) img_cropped = img.crop_to_pointcloud_proportion(pcloud, 0.1) assert_allclose(img_cropped.shape, (55, 55))
def augment_face_image(img, image_size=256, crop_size=248, angle_range=30, flip=True, warp_mode='constant'): """basic image augmentation: random crop, rotation and horizontal flip""" #from menpo def round_image_shape(shape, round): if round not in ['ceil', 'round', 'floor']: raise ValueError('round must be either ceil, round or floor') # Ensure that the '+' operator means concatenate tuples return tuple(getattr(np, round)(shape).astype(np.int)) # taken from MDM def mirror_landmarks_68(lms, im_size): return PointCloud(abs(np.array([0, im_size[1]]) - lms.as_vector( ).reshape(-1, 2))[mirrored_parts_68]) # taken from MDM def mirror_image(im): im = im.copy() im.pixels = im.pixels[..., ::-1].copy() for group in im.landmarks: lms = im.landmarks[group] if lms.points.shape[0] == 68: im.landmarks[group] = mirror_landmarks_68(lms, im.shape) return im flip_rand = np.random.random() > 0.5 # rot_rand = np.random.random() > 0.5 # crop_rand = np.random.random() > 0.5 rot_rand = True # like ECT crop_rand = True # like ECT if crop_rand: lim = image_size - crop_size min_crop_inds = np.random.randint(0, lim, 2) max_crop_inds = min_crop_inds + crop_size img = img.crop(min_crop_inds, max_crop_inds) if flip and flip_rand: img = mirror_image(img) if rot_rand: rot_angle = 2 * angle_range * np.random.random_sample() - angle_range # img = img.rotate_ccw_about_centre(rot_angle) # Get image's bounding box coordinates bbox = bounding_box((0, 0), [img.shape[0] - 1, img.shape[1] - 1]) # Translate to origin and rotate counter-clockwise trans = Translation(-img.centre(), skip_checks=True).compose_before( Rotation.init_from_2d_ccw_angle(rot_angle, degrees=True)) rotated_bbox = trans.apply(bbox) # Create new translation so that min bbox values go to 0 t = Translation(-rotated_bbox.bounds()[0]) trans.compose_before_inplace(t) rotated_bbox = trans.apply(bbox) # Output image's shape is the range of the rotated bounding box # while respecting the users rounding preference. shape = round_image_shape(rotated_bbox.range() + 1, 'round') img = img.warp_to_shape( shape, trans.pseudoinverse(), warp_landmarks=True, mode=warp_mode) img = img.resize([image_size, image_size]) return img
def test_bounding_box_creation(): bb = bounding_box([0, 0], [1, 1]) assert_allclose(bb.points, [[0, 0], [1, 0], [1, 1], [0, 1]])
def test_crop_to_landmarks_proportion(): img = Image.init_blank((100, 100), n_channels=1) img.landmarks["test"] = bounding_box([0, 0], [10, 10]) img_cropped = img.crop_to_landmarks_proportion(0.1) assert_allclose(img_cropped.shape, (11, 11))
def test_rescale_landmarks_to_diagonal_range(): img = Image.init_blank((100, 100), n_channels=1) img.landmarks["test"] = bounding_box([0, 0], [10, 10]) img_rescaled = img.rescale_landmarks_to_diagonal_range(20 * np.sqrt(2)) assert_allclose(img_rescaled.shape, (200, 200))