Пример #1
0
    def process(self, annotations_container):
        annotations_container_new = AnnotationsContainer()

        with tqdm(total=len(annotations_container)) as pbar:
            for idx, annts, img_fn, annt_fn in annotations_container.get_data(
            ):
                annotations = []

                for an in annts:

                    # filter labels
                    if self.allowed_labels is not None:
                        if an.label in self.allowed_labels:
                            annotations.append(an)

                    # mapping labels
                    if self.labels_mapping is not None:
                        if an.label in self.labels_mapping:
                            an.label = self.labels_mapping[an.label]
                            annotations.append(an)

                if annotations:
                    annotations_container_new.add_data(
                        annotations=annotations,
                        image_filename=img_fn,
                        annotation_filename=None)

                pbar.update(1)
        return annotations_container_new
Пример #2
0
    def parse(self):
        if not self.struct.check_path():
            print(f'path not exists! {self.data_path}')
            sys.exit()

        annotations_container = AnnotationsContainer()
        filenames = list(
            glob(f'{self.struct.annot_dir}/*{self.struct.annot_ext}'))

        with tqdm(total=len(filenames)) as pbar:
            for fn in filenames:
                annotations = []
                basename, _ = osp.splitext(osp.basename(fn))

                tree = ElementTree.parse(fn)
                root = tree.getroot()

                for obj in root.findall('object'):
                    bbox = obj.find('bndbox')

                    annotations.append(
                        Annotation(bbox=BBox(x1=float(bbox.find("xmin").text),
                                             y1=float(bbox.find("ymin").text),
                                             x2=float(bbox.find("xmax").text),
                                             y2=float(bbox.find("ymax").text)),
                                   label=obj.find("name").text))

                annotations_container.add_data(
                    annotations=annotations,
                    image_filename=self.struct.get_image_file(basename),
                    annotation_filename=self.struct.get_annotation_file(
                        basename))
                pbar.update(1)
        return annotations_container
Пример #3
0
    def parse(self):
        if not self.struct.check_project_path():
            print(f'path not exists! {self.data_path}')
            sys.exit()

        def parse_points(polygon):
            d = {'x': set(), 'y': set()}

            for pt in polygon.findall("pt"):
                d['x'].add(int(float(pt.find('x').text)))
                d['y'].add(int(float(pt.find('y').text)))

            x1 = min(d['x'])
            y1 = min(d['y'])
            x2 = max(d['x'])
            y2 = max(d['y'])

            return x1, y1, x2, y2

        annotations_container = AnnotationsContainer()
        filenames = list(
            glob(f'{self.struct.annot_dir_project}/*{self.struct.annot_ext}'))

        with tqdm(total=len(filenames)) as pbar:
            for fn in filenames:
                annotations = []
                basename, _ = osp.splitext(osp.basename(fn))

                tree = ElementTree.parse(fn)
                root = tree.getroot()

                for obj in root.findall('object'):
                    if obj.find('type').text == 'bounding_box':
                        polygon = obj.find('polygon')
                        x1, y1, x2, y2 = parse_points(polygon)

                        annotations.append(
                            Annotation(bbox=BBox(x1=x1, y1=y1, x2=x2, y2=y2),
                                       label=obj.find("name").text))

                annotations_container.add_data(
                    annotations=annotations,
                    image_filename=self.struct.get_image_file(basename),
                    annotation_filename=self.struct.get_annotation_file(
                        basename))
                pbar.update(1)
        return annotations_container
Пример #4
0
    def process(self, annotations_container):
        annotations_container_new = AnnotationsContainer()

        with tqdm(total=len(annotations_container)) as pbar:
            for idx, annts, img_fn, annt_fn in annotations_container.get_data():
                annotations = []

                for idx, an in enumerate(annts):
                    if an.bbox.height >= self.min_height:
                        try:
                            crop_img_name = self.make_crop(img_fn, an)
                            annotations_container_new.add_data(
                                annotations=[an],
                                annotation_filename=None,
                                image_filename=crop_img_name
                            )
                        except Exception as e:
                            pass

                pbar.update(1)
        return annotations_container_new
Пример #5
0
    def parse(self):
        if not self.struct.check_path():
            print(f'path not exists! {self.data_path}')
            sys.exit()

        annotations_container = AnnotationsContainer()

        with tqdm(total=len(self.struct)) as pbar:
            for annt_ids, img_id in self.struct.provider():
                annotations = []

                for annt_id in annt_ids:
                    annotations.append(
                        Annotation(bbox=self.struct.get_bbox(annt_id),
                                   label=self.struct.get_label(annt_id)))

                annotations_container.add_data(
                    annotations=annotations,
                    image_filename=self.struct.get_image_name(img_id),
                    annotation_filename=None)
                pbar.update(1)
        return annotations_container
Пример #6
0
    def process(self, annotations_container):
        annotations_container_new = AnnotationsContainer()

        with tqdm(total=len(annotations_container)) as pbar:
            for idx, annts, img_fn, annt_fn in annotations_container.get_data():
                annotations = []

                for an in annts:
                    if an.bbox.width * an.bbox.height >= self.min_area:
                        annotations.append(an)
                    else:
                        print('min_area!', an.bbox.width * an.bbox.height)

                if annotations:
                    annotations_container_new.add_data(
                        annotations=annotations,
                        image_filename=img_fn,
                        annotation_filename=None
                    )

                pbar.update(1)
        return annotations_container_new
Пример #7
0
    def parse(self):
        # if not self.struct.check_path():
        #     print(f'path not exists! {self.data_path}')
        #     sys.exit()

        annotations_container = AnnotationsContainer()
        annotation_file = Path(self.struct.path, self.struct.annotations)
        if not annotation_file.exists():
            print(f'{self.annotations} is not exists!')
            sys.exit()

        data = pd.read_csv(annotation_file)

        unique_filenames = data["image"].unique()

        with tqdm(total=len(unique_filenames)) as pbar:
            for unique_filename in unique_filenames:
                annotations = []
                # basename, _ = unique_filename

                df = data[data["image"] == unique_filename]
                for index, row in df.iterrows():
                    annotations.append(
                        Annotation(bbox=BBox(x1=row["x1"],
                                             y1=row["y1"],
                                             x2=row["x2"],
                                             y2=row["y2"]),
                                   label=row["label"]))

                annotations_container.add_data(
                    annotations=annotations,
                    image_filename=str(
                        Path(self.struct.path,
                             self.struct.images + Path(unique_filename).name)),
                    annotation_filename=None)
                pbar.update(1)
            return annotations_container
Пример #8
0
    def process(self, annotations_container):
        annotations_container_new = AnnotationsContainer()

        tmp_img_path = '/tmp/random_crop_action/'
        if not osp.exists(tmp_img_path):
            os.makedirs(tmp_img_path)

        count = 0
        with tqdm(total=annotations_container.get_len()) as pbar:
            for idx, annts, img_fn, annt_fn in annotations_container.get_data():
                img = cv2.imread(img_fn)

                boxes = []
                labels = []
                crop_boxes = []
                crop_labels = []
                crop_images = []

                for an in annts:
                    boxes.append([an.bbox.x1, an.bbox.y1, an.bbox.x2, an.bbox.y2])
                    labels.append(an.label)

                for step in range(self.max_iters):
                    try:
                        annotations = {'image': img, 'bboxes': boxes, 'labels': labels}
                        augmented = self.get_aug(**annotations)
                    except Exception as e:
                        continue

                    if len(augmented['bboxes']) == 0:
                        continue

                    crop_images.append(augmented['image'])
                    crop_boxes.append(augmented['bboxes'])
                    crop_labels.append(augmented['labels'])

                    if len(crop_images) == self.n_crops:
                        break

                for img, labels, boxes in zip(crop_images, crop_labels, crop_boxes):
                    # save current tile to tmp folder
                    img_name = osp.join(tmp_img_path, str(count)+'.png')
                    cv2.imwrite(img_name, img)

                    annotations = []

                    for box, label in zip(boxes, labels):
                        box=list(map(lambda x:int(x), box))
                        annotations.append(
                            Annotation(
                                bbox=BBox(x1=box[0], y1=box[1], x2=box[2], y2=box[3]),
                                label=label
                            )
                        )

                    annotations_container_new.add_data(
                        annotations=annotations,
                        image_filename=img_name,
                        annotation_filename=None
                    )

                    count += 1
                pbar.update(1)
        return annotations_container_new