Exemple #1
0
def add_entry(out, path, boxes, points, mirror):
    '''add the image, points, boxes to the output file'''
    w = image.shape[1]

    # handle xml file
    if isinstance(out, Xml):
        out.append(path, boxes, [points])

        if mirror:
            path = utils.mirror_image(stack[0], path)

            points = [(w - x, y) for x, y in points]
            out.append(path, boxes, [points])
    else:
        out.write(f'{path}\n')

        for x, y in points:
            out.write(f'{x} {y}')
            out.write("\n")

        if mirror:
            path = utils.mirror_image(stack[0], path)

            out.write(f'{path}\n')

            for x, y in points:
                out.write(f'{w - x} {y}')
                out.write("\n")
Exemple #2
0
        def get_random_sample(image, shape, rotation_stddev=10):
            # Read a random image with landmarks and bb
            image = menpo.image.Image(image.transpose((2, 0, 1)), copy=False)
            image.landmarks['PTS'] = PointCloud(shape)

            if np.random.rand() < .5:
                image = utils.mirror_image(image)
            if np.random.rand() < .5:
                theta = np.random.normal(scale=rotation_stddev)
                rot = menpo.transform.rotate_ccw_about_centre(
                    image.landmarks['PTS'], theta)
                image = image.warp_to_shape(image.shape, rot)
            bb = image.landmarks['PTS'].bounding_box().points
            miny, minx = np.min(bb, 0)
            maxy, maxx = np.max(bb, 0)
            bbsize = max(maxx - minx, maxy - miny)
            center = [(miny + maxy) / 2., (minx + maxx) / 2.]
            shift = (np.random.rand(2) - 0.5) * 0.6 * bbsize
            image.landmarks['bb'] = PointCloud([
                [
                    center[0] - bbsize * 0.5 + shift[0],
                    center[1] - bbsize * 0.5 + shift[1]
                ],
                [
                    center[0] + bbsize * 0.5 + shift[0],
                    center[1] + bbsize * 0.5 + shift[1]
                ],
            ]).bounding_box()
            proportion = 1.0 / 6.0 + float(np.random.rand() - 0.5) / 6.
            image = image.crop_to_landmarks_proportion(proportion, group='bb')
            image = image.resize((112, 112))

            random_image = image.pixels.transpose(1, 2, 0).astype('float32')
            random_shape = image.landmarks['PTS'].points.astype('float32')
            return random_image, random_shape
 def get_mirrored_image(image, shape, init):
     # Read a random image with landmarks and bb
     image_m = menpo.image.Image(image.transpose((2, 0, 1)))
     image_m.landmarks['init'] = PointCloud(init)
     image_m = utils.mirror_image(image_m)
     mirrored_image = image_m.pixels.transpose(1, 2,
                                               0).astype('float32')
     mirrored_init = image_m.landmarks['init'].points.astype('float32')
     return image, init, mirrored_image, mirrored_init, shape
Exemple #4
0
def load_image(path,
               reference_shape,
               is_training=False,
               group='PTS',
               mirror_image=False):
    """Load an annotated image.

    In the directory of the provided image file, there
    should exist a landmark file (.pts) with the same
    basename as the image file.

    Args:
      path: a path containing an image file.
      reference_shape: a numpy array [num_landmarks, 2]
      is_training: whether in training mode or not.
      group: landmark group containing the grounth truth landmarks.
      mirror_image: flips horizontally the image's pixels and landmarks.
    Returns:
      pixels: a numpy array [width, height, 3].
      estimate: an initial estimate a numpy array [68, 2].
      gt_truth: the ground truth landmarks, a numpy array [68, 2].
    """
    im = mio.import_image(path)
    bb_root = im.path.parent.relative_to(im.path.parent.parent.parent)
    if 'set' not in str(bb_root):
        bb_root = im.path.parent.relative_to(im.path.parent.parent)

    im.landmarks['bb'] = mio.import_landmark_file(
        str(Path('bbs') / bb_root / (im.path.stem + '.pts')))

    im = im.crop_to_landmarks_proportion(0.3, group='bb')
    reference_shape = PointCloud(reference_shape)

    bb = im.landmarks['bb'].lms.bounding_box()

    im.landmarks['__initial'] = align_shape_with_bounding_box(
        reference_shape, bb)
    im = im.rescale_to_pointcloud(reference_shape, group='__initial')

    if mirror_image:
        im = utils.mirror_image(im)

    lms = im.landmarks[group].lms
    initial = im.landmarks['__initial'].lms

    # if the image is greyscale then convert to rgb.
    pixels = grey_to_rgb(im).pixels.transpose(1, 2, 0)

    gt_truth = lms.points.astype(np.float32)
    estimate = initial.points.astype(np.float32)
    return pixels.astype(np.float32).copy(), gt_truth, estimate
        def get_random_sample(image, shape, rotation_stddev=10):
            # Read a random image with landmarks and bb
            image = menpo.image.Image(image.transpose((2, 0, 1)), copy=False)
            image.landmarks['PTS'] = PointCloud(shape)

            if np.random.rand() < .5:
                image = utils.mirror_image(image)
            if np.random.rand() < .5:
                theta = np.random.normal(scale=rotation_stddev)
                rot = menpo.transform.rotate_ccw_about_centre(
                    image.landmarks['PTS'], theta)
                image = image.warp_to_shape(image.shape, rot)
            bb = image.landmarks['PTS'].bounding_box().points
            miny, minx = np.min(bb, 0)
            maxy, maxx = np.max(bb, 0)
            bbsize = max(maxx - minx, maxy - miny)
            center = [(miny + maxy) / 2., (minx + maxx) / 2.]
            shift = (np.random.rand(2) - 0.5) / 6. * bbsize
            image.landmarks['bb'] = PointCloud([
                [
                    center[0] - bbsize * 0.5 + shift[0],
                    center[1] - bbsize * 0.5 + shift[1]
                ],
                [
                    center[0] + bbsize * 0.5 + shift[0],
                    center[1] + bbsize * 0.5 + shift[1]
                ],
            ]).bounding_box()
            proportion = 1.0 / 6.0 + float(np.random.rand() - 0.5) / 10.0
            image = image.crop_to_landmarks_proportion(proportion, group='bb')
            image = image.resize((112, 112))
            random_image = image.pixels.transpose(1, 2, 0).astype('float32')
            random_shape = image.landmarks['PTS'].points.astype('float32')

            # Occlude
            _O_AREA = 0.15
            _O_MIN_H = 0.15
            _O_MAX_H = 1.0
            if np.random.rand() < .3:
                rh = min(
                    112,
                    int((np.random.rand() * (_O_MAX_H - _O_MIN_H) + _O_MIN_H) *
                        112))
                rw = min(112, int(12544 * _O_AREA / rh))
                dy = int(np.random.rand() * (112 - rh))
                dx = int(np.random.rand() * (112 - rw))
                idx = int(np.random.rand() * _num_negatives)
                random_image[dy:dy + rh, dx:dx + rw] = np.minimum(
                    1.0, _negatives[idx][dy:dy + rh, dx:dx + rw])

            return random_image, random_shape
Exemple #6
0
    def get_random_sample(num, rotation_stddev=10):
        # idx = np.random.randint(low=0, high=len(_images))
        images = []
        shapes = []
        inits =[]
        shape_3D = []
        inits_3D = []
        for i in range(FLAGS.batch_size):
            rand_num = np.random.randint(0, num)
            pixel_list = []
            shape_list = []
            # print('get_random_set:', idx)
            im = menpo.image.Image(_images[rand_num].transpose(2, 0, 1), copy=False)
            lms = _shapes[rand_num]

            init = _inits[rand_num]

            im.landmarks['PTS'] = lms

            im.landmarks['inital'] = init



            if np.random.rand() < .5:
               im = utils.mirror_image(im)

            if np.random.rand() < .5:
               theta = np.random.normal(scale=rotation_stddev)
               rot = menpo.transform.rotate_ccw_about_centre(lms, theta)
               im = im.warp_to_shape(im.shape, rot)

            pixels = im.pixels.transpose(1, 2, 0).astype('float32')

            shape = im.landmarks['PTS'].lms.points.astype('float32')

            init_2 = im.landmarks['inital'].lms.points.astype('float32')

            pixel_list.append(pixels)

            pixel_list = np.array(pixel_list)

            inits.append(init_2)
            images.append(pixel_list)
            shapes.append(shape)



        return images, shapes, inits
Exemple #7
0
def load_image(path, reference_shape, is_training=False, group='PTS',
               mirror_image=False):
    """Load an annotated image.

    In the directory of the provided image file, there
    should exist a landmark file (.pts) with the same
    basename as the image file.

    Args:
      path: a path containing an image file.
      reference_shape: a numpy array [num_landmarks, 2]
      is_training: whether in training mode or not.
      group: landmark group containing the grounth truth landmarks.
      mirror_image: flips horizontally the image's pixels and landmarks.
    Returns:
      pixels: a numpy array [width, height, 3].
      estimate: an initial estimate a numpy array [68, 2].
      gt_truth: the ground truth landmarks, a numpy array [68, 2].
    """
    im = mio.import_image(path)
    bb_root = im.path.parent.relative_to(im.path.parent.parent.parent)
    if 'set' not in str(bb_root):
        bb_root = im.path.parent.relative_to(im.path.parent.parent)

    im.landmarks['bb'] = mio.import_landmark_file(str(Path('bbs') / bb_root / (
        im.path.stem + '.pts')))

    im = im.crop_to_landmarks_proportion(0.3, group='bb')
    reference_shape = PointCloud(reference_shape)

    bb = im.landmarks['bb'].lms.bounding_box()

    im.landmarks['__initial'] = align_shape_with_bounding_box(reference_shape,
                                                              bb)
    im = im.rescale_to_pointcloud(reference_shape, group='__initial')

    if mirror_image:
        im = utils.mirror_image(im)

    lms = im.landmarks[group].lms
    initial = im.landmarks['__initial'].lms

    # if the image is greyscale then convert to rgb.
    pixels = grey_to_rgb(im).pixels.transpose(1, 2, 0)

    gt_truth = lms.points.astype(np.float32)
    estimate = initial.points.astype(np.float32)
    return pixels.astype(np.float32).copy(), gt_truth, estimate
Exemple #8
0
        def get_random_sample(rotation_stddev=10):
            idx = np.random.randint(low=0, high=len(_images))
            im = menpo.image.Image(_images[idx].transpose(2, 0, 1), copy=False)
            lms = _shapes[idx]
            im.landmarks['PTS'] = lms
            if np.random.rand() < .5:
                im = utils.mirror_image(im)

            if np.random.rand() < .5:
              theta = np.random.normal(scale=rotation_stddev)
              rot = menpo.transform.rotate_ccw_about_centre(lms, theta)
              im = im.warp_to_shape(im.shape, rot)

            pixels = im.pixels.transpose(1, 2, 0).astype('float32')
            shape = im.landmarks['PTS'].lms.points.astype('float32')
            return pixels, shape
Exemple #9
0
        def get_random_sample(rotation_stddev=10):
            idx = np.random.randint(low=0, high=len(_images))
            im = menpo.image.Image(_images[idx].transpose(2, 0, 1), copy=False)
            lms = _shapes[idx]
            im.landmarks['PTS'] = lms

            if np.random.rand() < .5:
               im = utils.mirror_image(im)

            if np.random.rand() < .5:
              theta = np.random.normal(scale=rotation_stddev)
              rot = menpo.transform.rotate_ccw_about_centre(lms, theta)
              im = im.warp_to_shape(im.shape, rot)

            pixels = im.pixels.transpose(1, 2, 0).astype('float32')
            shape = im.landmarks['PTS'].lms.points.astype('float32')
            return pixels, shape
def process_images(queue, i, augment, paths):
    print('begin p{}'.format(i), os.getpid(), os.getppid())
    cnt = 0
    for path in paths:
        mp_image = mio.import_image(path)
        landmark_path = path.parent.parent / 'Fix3' / (path.stem + '.txt')
        mp_image.landmarks['PTS'] = mshape.PointCloud(np.genfromtxt(landmark_path)[:, [1, 0]])
        for j in range(augment):
            try:
                mp_image_i = mp_image.copy()
                if j % 2 == 1:
                    mp_image_i = utils.mirror_image(mp_image_i)
                if np.random.rand() < .5:
                    theta = np.random.normal(scale=20)
                    rot = menpo.transform.rotate_ccw_about_centre(mp_image_i.landmarks['PTS'], theta)
                    mp_image_i = mp_image_i.warp_to_shape(mp_image_i.shape, rot, warp_landmarks=True)

                # Bounding box perturbation
                bb = mp_image_i.landmarks['PTS'].bounding_box().points.astype(np.float32)
                miny, minx = np.min(bb, 0)
                maxy, maxx = np.max(bb, 0)
                bbsize = max(maxx - minx, maxy - miny)
                center = [(miny + maxy) / 2., (minx + maxx) / 2.]
                shift = np.random.normal(0, 0.05, 2) * bbsize
                proportion = (1.0 / 6.0 + float(np.random.normal(0, 0.15))) * bbsize
                mp_image_i.landmarks['bb'] = mshape.PointCloud(
                    [
                        [
                            center[0] - bbsize * 0.5 - proportion + shift[0],
                            center[1] - bbsize * 0.5 - proportion + shift[1]
                        ],
                        [
                            center[0] + bbsize * 0.5 + proportion + shift[0],
                            center[1] + bbsize * 0.5 + proportion + shift[1]
                        ],
                    ]
                ).bounding_box()

                # Padding, Crop, Resize
                pady = int(
                    max(
                        -min(center[0] - bbsize * 0.5 - proportion + shift[0], 0),
                        max(center[0] + bbsize * 0.5 + proportion + shift[0] - mp_image_i.height, 0)
                    )
                ) + 100
                padx = int(
                    max(
                        -min(center[1] - bbsize * 0.5 - proportion + shift[1], 0),
                        max(center[1] + bbsize * 0.5 + proportion + shift[1] - mp_image_i.width, 0)
                    )
                ) + 100
                c, h, w = mp_image_i.pixels.shape
                pad_image = np.random.rand(c, h + pady + pady, w + padx + padx)
                pad_image[:, pady: pady + h, padx: padx + w] = mp_image_i.pixels.astype(np.float32)
                pad_shape = mp_image_i.landmarks['PTS'].points.astype(np.float32) + np.array([pady, padx])
                pad_bb = mp_image_i.landmarks['bb'].points.astype(np.float32) + np.array([pady, padx])

                mp_image_i = menpo.image.Image(pad_image)
                mp_image_i.landmarks['PTS'] = mshape.PointCloud(pad_shape)
                mp_image_i.landmarks['bb'] = mshape.PointCloud(pad_bb).bounding_box()
                mp_image_i = mp_image_i.crop_to_landmarks_proportion(0, group='bb')
                mp_image_i = mp_image_i.resize((112, 112))
                mp_image_i = grey_to_rgb(mp_image_i)

                image = mp_image_i.pixels.transpose(1, 2, 0).astype(np.float32)
                shape = mp_image_i.landmarks['PTS'].points.astype(np.float32)
            except Exception as e:
                traceback.print_exc()
                raise e
            done = False
            while not done:
                try:
                    queue.put_nowait((image, shape))
                    done = True
                except Exception:
                    print('p{} wait'.format(i))
                    traceback.print_exc()
                    time.sleep(0.5)
        cnt += 1
        print('calc{} done {}/{}'.format(i, cnt, len(paths)))
    print('end p{}'.format(i), len(paths))