def next_batch(self, plotting=False):
        while True:
            (
                batch_images,
                joint_ids,
                batch_joints,
                data_items,
                sm_size,
                target_size,
            ) = self.get_batch()

            pipeline = self.build_augmentation_pipeline(height=target_size[0],
                                                        width=target_size[1],
                                                        apply_prob=0.5)

            batch_images, batch_joints = pipeline(images=batch_images,
                                                  keypoints=batch_joints)

            # If you would like to check the augmented images, script for saving
            # the images with joints on:
            if plotting:
                for i in range(self.batch_size):
                    joints = batch_joints[i]
                    kps = KeypointsOnImage(
                        [Keypoint(x=joint[0], y=joint[1]) for joint in joints],
                        shape=batch_images[i].shape,
                    )
                    im = kps.draw_on_image(batch_images[i])
                    # imageio.imwrite(data_items[i].im_path.split('/')[-1],im)
                    imageio.imwrite(
                        os.path.join(self.cfg["project_path"],
                                     str(i) + ".png"), im)

            image_shape = arr(batch_images).shape[1:3]
            batch = {Batch.inputs: arr(batch_images).astype(np.float64)}
            if self.has_gt:
                targetmaps = self.get_targetmaps_update(
                    joint_ids, batch_joints, data_items, sm_size, image_shape)
                batch.update(targetmaps)

            # if returndata:
            #        return batch_images,batch_joints,targetmaps

            batch = {
                key: data_to_input_batch(data)
                for (key, data) in batch.items()
            }
            batch[Batch.data_item] = data_items
            return batch
예제 #2
0
def chapter_examples_keypoints_simple():
    import imgaug as ia
    import imgaug.augmenters as iaa
    from imgaug.augmentables import Keypoint, KeypointsOnImage

    ia.seed(1)

    image = ia.quokka(size=(256, 256))
    kps = KeypointsOnImage([
        Keypoint(x=65, y=100),
        Keypoint(x=75, y=200),
        Keypoint(x=100, y=100),
        Keypoint(x=200, y=80)
    ],
                           shape=image.shape)

    seq = iaa.Sequential([
        iaa.Multiply(
            (1.2, 1.5)),  # change brightness, doesn't affect keypoints
        iaa.Affine(rotate=10, scale=(
            0.5, 0.7
        ))  # rotate by exactly 10deg and scale to 50-70%, affects keypoints
    ])

    # Augment keypoints and images.
    image_aug, kps_aug = seq(image=image, keypoints=kps)

    # print coordinates before/after augmentation (see below)
    # use after.x_int and after.y_int to get rounded integer coordinates
    for i in range(len(kps.keypoints)):
        before = kps.keypoints[i]
        after = kps_aug.keypoints[i]
        print("Keypoint %d: (%.8f, %.8f) -> (%.8f, %.8f)" %
              (i, before.x, before.y, after.x, after.y))

    # image with keypoints before/after augmentation (shown below)
    image_before = kps.draw_on_image(image, size=7)
    image_after = kps_aug.draw_on_image(image_aug, size=7)

    # ------------

    save("examples_keypoints",
         "simple.jpg",
         grid([image_before, image_after], cols=2, rows=1),
         quality=90)
kps = KeypointsOnImage([
    Keypoint(x=x1, y=y1),
    Keypoint(x=x2, y=y1),
    Keypoint(x=x2, y=y2),
    Keypoint(x=x1, y=y2)
],
                       shape=image.shape)

seq = iaa.Sequential([
    iaa.Multiply((1.2, 1.5)),  # change brightness, doesn't affect keypoints
    iaa.Affine(rotate=45)
])

# Augment keypoints and images.
image_aug, kps_aug = seq(image=image, keypoints=kps)

# print coordinates before/after augmentation (see below)
# use after.x_int and after.y_int to get rounded integer coordinates
for i in range(len(kps.keypoints)):
    before = kps.keypoints[i]
    after = kps_aug.keypoints[i]
    print("Keypoint %d: (%.8f, %.8f) -> (%.8f, %.8f)" %
          (i, before.x, before.y, after.x, after.y))

# image with keypoints before/after augmentation (shown below)
image_before = kps.draw_on_image(image, size=7)
image_after = kps_aug.draw_on_image(image_aug, size=7)

ia.imshow(image_before)
ia.imshow(image_after)
    def next_batch(self, plotting=False):
        while True:
            (
                batch_images,
                joint_ids,
                batch_joints,
                data_items,
            ) = self.get_batch()

            # Scale is sampled only once (per batch) to transform all of the images into same size.
            target_size, sm_size = self.calc_target_and_scoremap_sizes()
            scale = np.mean(target_size / self.default_size)
            augmentation.update_crop_size(self.pipeline, *target_size)
            batch_images, batch_joints = self.pipeline(images=batch_images,
                                                       keypoints=batch_joints)
            batch_images = np.asarray(batch_images)
            image_shape = batch_images.shape[1:3]
            # Discard keypoints whose coordinates lie outside the cropped image
            batch_joints_valid = []
            joint_ids_valid = []
            for joints, ids in zip(batch_joints, joint_ids):
                inside = np.logical_and.reduce((
                    joints[:, 0] < image_shape[1],
                    joints[:, 0] > 0,
                    joints[:, 1] < image_shape[0],
                    joints[:, 1] > 0,
                ))
                batch_joints_valid.append(joints[inside])
                temp = []
                start = 0
                for array in ids:
                    end = start + array.size
                    temp.append(array[inside[start:end]])
                    start = end
                joint_ids_valid.append(temp)

            # If you would like to check the augmented images, script for saving
            # the images with joints on:
            if plotting:
                for i in range(self.batch_size):
                    joints = batch_joints_valid[i]
                    kps = KeypointsOnImage(
                        [Keypoint(x=joint[0], y=joint[1]) for joint in joints],
                        shape=batch_images[i].shape,
                    )
                    im = kps.draw_on_image(batch_images[i])
                    # imageio.imwrite(data_items[i].im_path.split('/')[-1],im)
                    imageio.imwrite(
                        os.path.join(self.cfg["project_path"],
                                     str(i) + ".png"), im)

            batch = {Batch.inputs: batch_images.astype(np.float64)}
            if self.has_gt:
                targetmaps = self.get_targetmaps_update(
                    joint_ids_valid,
                    batch_joints_valid,
                    data_items,
                    (sm_size[1], sm_size[0]),
                    scale,
                )
                batch.update(targetmaps)

            batch = {key: np.asarray(data) for (key, data) in batch.items()}
            batch[Batch.data_item] = data_items
            return batch
    kpts = []
    for i in individuals:
        for b in bodyparts:
            x, y = Dataframe.iloc[ind][scorer][i][b]['x'], Dataframe.iloc[ind][
                scorer][i][b]['y']
            if np.isfinite(x) and np.isfinite(y):
                kpts.append(Keypoint(x=x * scale, y=y * scale))

    kps = KeypointsOnImage(kpts, shape=image.shape)

    cells = []

    # image with keypoints before augmentation
    image_before = kps.draw_on_image(image,
                                     color=color,
                                     size=size,
                                     alpha=alpha)
    cells.append(image_before)

    for name, seq in Augmentations:
        image_aug, kps_aug = seq(image=image, keypoints=kps)
        image_after = kps_aug.draw_on_image(image_aug,
                                            color=color,
                                            size=size,
                                            alpha=alpha)
        cells.append(image_after)

    grid_image = np.hstack(cells)  # Horizontally stack the images
    imageio.imwrite(
        'augmentationexamples/' + str(imfolder) + '_' +
        imname.split('.png')[0] + '_joint.jpg', grid_image)