def rider_reformat(path):
    """Combine riders' bboxes with the nearest motorcycle/bicycle"""
    for filename in glob.iglob(path + '\**\*.json', recursive=True):
        images = decode_annot_array(filename)
        for i in range(0, len(images)):
            image = images[i]

            to_delete = []
            for j in range(0, len(image.labels)):
                label = image.labels[j]
                if not (label.category == 'rider'):
                    continue

                rider = label
                nearest_idx = _find_nearest_cycle(image, label, to_delete)
                if nearest_idx < 0:
                    continue

                to_delete.append(nearest_idx)
                cycle = image.labels[nearest_idx]
                rider.box = _get_common_box(rider.box, cycle.box)

            to_delete.sort(reverse=True)
            for idx in to_delete:
                del image.labels[idx]

        encode_annot_array(images, filename, '_rider')
def common_category_names(path):
    cat_dict = get_category_sets_dict()

    for filename in glob.iglob(path + '\**\*.json', recursive=True):
        images = decode_annot_array(filename)
        for i in range(0, len(images)):
            image = images[i]

            for j in range(0, len(image.labels)):
                label = image.labels[j]

                common_category = cat_dict.get(frozenset(get_category_set(label.category)))
                label.category = common_category

        encode_annot_array(images, filename, '_common')
Beispiel #3
0
    def load_folder(self, path, size_new):
        images = []
        print(path)
        for filename in glob.iglob(path + '/**/*.json', recursive=True):
            images_new = decode_annot_array(filename)
            print(len(images_new))

            # # resize bounding boxes
            # print(path + ' ' + images_new[0].name)
            # img = next(glob.iglob(path + '/**/' + images_new[0].name, recursive=True))
            # height, width, _ = img.shape

            # i# mages_new = self.resize_bboxes(images_new, (height, width), size_new)

            images.extend(images_new)
        return images
def crop_bbox_correction(path, x_offset):
    for filename in glob.iglob(path + '/**/*.json', recursive=True):
        print(filename)
        images = decode_annot_array(filename)

        for i in range(0, len(images)):
            image = images[i]
            print(image.name)

            for j in range(0, len(image.labels)):
                box = image.labels[j].box

                box.x1 -= x_offset
                box.x2 -= x_offset

        encode_annot_array(images, filename)
def cityscapes_citypersons_union(path):
    subfolders = ['both', 'train', 'val']

    for subfolder in subfolders:
        sub_path = os.path.join(path, subfolder)

        images_both = []
        for filename in glob.iglob(sub_path + '\**\*.json', recursive=True):
            images_both.append(decode_annot_array(filename))

        images_first = {}
        for image in images_both[0]:
            images_first.update({image.name: image})

        for i in range(0, len(images_both[1])):
            images_both[1][i].labels.extend(images_first.get(images_both[1][i].name).labels)

        encode_annot_array(images_both[1], os.path.join(sub_path, 'cs_2.json'))
def crop_dataset(path, x_offset, width, height, only_json=False):

    images_path = os.path.join(path, "images")

    if not only_json:
        for filename in glob.iglob(images_path + '/**/*.png', recursive=True):
            print(filename)
            img = cv2.imread(filename)
            crop_img = img[0:height, x_offset:x_offset + width]
            splitted = os.path.splitext(filename)
            cv2.imwrite(splitted[0] + '_cropped' + splitted[1], crop_img)

    for filename in glob.iglob(path + '/**/*.json', recursive=True):
        print(filename)
        images = decode_annot_array(filename)

        for i in range(0, len(images)):
            image = images[i]
            print(image.name)

            to_delete = []
            for j in range(0, len(image.labels)):
                box = image.labels[j].box

                if (float(box.x1) < x_offset and float(box.x2) < x_offset) or (float(box.x1) > x_offset + width and float(box.x2) > x_offset + width):
                    to_delete.append(j)
                    continue

                box.x1 = max(x_offset, float(box.x1))
                box.x2 = max(x_offset, float(box.x2))

                box.x1 = min(x_offset + width, float(box.x1))
                box.x2 = min(x_offset + width, float(box.x2))

                box.y1 = min(height, float(box.y1))
                box.y2 = min(height, float(box.y2))

            to_delete.sort(reverse=True)
            for idx in to_delete:
                del image.labels[idx]

        encode_annot_array(images, filename)
def keep_only_needed_categories(path, categories):
    cats = set([])
    for category in categories:
        print(category)
        cats = cats.union(get_category_set(category))

    for filename in glob.iglob(path + '\**\*.json', recursive=True):

        images = decode_annot_array(filename)

        for i in range(0, len(images)):
            image = images[i]

            labels_to_delete = []
            for j in range(0, len(image.labels)):
                label = image.labels[j]
                if not (label.category in cats):
                    labels_to_delete.append(j)

            labels_to_delete.sort(reverse=True)
            for idx in labels_to_delete:
                del image.labels[idx]

        encode_annot_array(images, filename, '_needed')
def keep_only_images_w_categories(path):
    """Remove images where there were no trains/trams annotated"""
    for filename in glob.iglob(path + '/**/*.json', recursive=True):

        categories = [['person', 'person_group'], ['two_wheeler'], ['on_rails'], ['car'], ['truck']]
        for cat in categories:
            cats = set([])
            for category in cat:
                print(category)
                cats = cats.union(get_category_set(category))

            images = decode_annot_array(filename)

            images_w_cats = []
            for image in images:
                for label in image.labels:
                    if label.category in cats:
                        images_w_cats.append(image)
                        break

            postfix = ''
            for category in cat:
                postfix += '_' + category
            encode_annot_array(images_w_cats, filename, postfix)