def __parse_annotation(self, annotation):
        """
        Data augument.
        :param annotation: Image' path and bboxes' coordinates, categories.
        ex. [image_path xmin,ymin,xmax,ymax,class_ind xmin,ymin,xmax,ymax,class_ind ...]
        :return: Return the enhanced image and bboxes. bbox'shape is [xmin, ymin, xmax, ymax, class_ind]
        """
        anno = annotation.strip().split(" ")

        img_path = anno[0]
        img = cv2.imread(img_path)  # H*W*C and C=BGR
        assert img is not None, "File Not Found " + img_path
        bboxes = np.array(
            [list(map(float, box.split(","))) for box in anno[1:]]
        )

        img, bboxes = dataAug.RandomHorizontalFilp()(
            np.copy(img), np.copy(bboxes), img_path
        )
        img, bboxes = dataAug.RandomCrop()(np.copy(img), np.copy(bboxes))
        img, bboxes = dataAug.RandomAffine()(np.copy(img), np.copy(bboxes))
        img, bboxes = dataAug.Resize((self.img_size, self.img_size), True)(
            np.copy(img), np.copy(bboxes)
        )

        return img, bboxes
Exemple #2
0
    def __data_aug(self, img, bboxes):
        img, bboxes = dataAug.RandomHorizontalFilp()(np.copy(img),
                                                     np.copy(bboxes))
        img, bboxes = dataAug.RandomCrop()(np.copy(img), np.copy(bboxes))
        img, bboxes = dataAug.RandomAffine()(np.copy(img), np.copy(bboxes))
        img, bboxes = dataAug.Resize((self.img_size, self.img_size),
                                     True)(np.copy(img), np.copy(bboxes))

        return img, bboxes
Exemple #3
0
    def __parse_data(self, img_path, anno):

        img = cv2.imread(img_path)  # H*W*C and C=BGR
        img_h, img_w = img.shape[0], img.shape[1]
        assert img is not None, 'File Not Found ' + img_path
        # permutation = [4, 0, 1, 2, 3]
        # idx = np.empty_like(permutation)
        # idx[permutation] = np.arange(len(permutation))
        # anno = anno[:, idx]
        if len(anno) == 0:
            img, bboxes = dataAug.Resize((self.img_size, self.img_size),
                                         True)(np.copy(img), np.copy(anno))
            return img, bboxes

        bboxes = anno[:, [
            1, 2, 3, 4, 0
        ]]  # return a copy so it will not change the original anno which we will use inthe next epoch
        bboxes[:, :4:2] = bboxes[:, :4:2] * img_w
        bboxes[:, 1:4:2] = bboxes[:, 1:4:2] * img_h
        bboxes[:, 0:4] = dataAug.xywh2xyxy_np(bboxes[:, 0:4])
        bboxes = np.round(bboxes)

        if self.augmentation:
            img, bboxes = dataAug.RandomHorizontalFilp()(np.copy(img),
                                                         np.copy(bboxes))
            img, bboxes = dataAug.RandomCrop()(np.copy(img), np.copy(bboxes))
            img, bboxes = dataAug.RandomAffine()(np.copy(img), np.copy(bboxes))

            bboxes = np.where(bboxes < 0.0, 0.0, bboxes)
            bboxes[:, 2] = np.where(bboxes[:, 2] > img_w, img_w, bboxes[:, 2])
            bboxes[:, 3] = np.where(bboxes[:, 3] > img_h, img_h, bboxes[:, 3])
            sample = self.albu_aug(image=np.copy(img), bboxes=np.copy(bboxes))
            img, bboxes = sample['image'], sample['bboxes']
            bboxes = np.array(bboxes)
            if len(bboxes) == 0:
                bboxes = np.array(bboxes)
                bboxes = bboxes.reshape(
                    -1, 5
                )  # so that it will not error when go to Resize when it is blank

            # Apply random erasor
            # if len(bboxes) != 0:
            #     img = self.random_erasor_I(np.copy(img))
            #     img = self.random_erasor_O(np.copy(img), bboxes)

        # Resize the image to target size and transforms it into a color channel(BGR->RGB),
        # as well as pixel value normalization([0,1])
        img, bboxes = dataAug.Resize((self.img_size, self.img_size),
                                     True)(np.copy(img), np.copy(bboxes))

        return img, bboxes