Пример #1
0
def cp_to_common(path, labels_folder=True):
    """ Read original json files and reformat them into the common format"""
    if labels_folder:
        labels_path = os.path.join(path, "labels")
    else:
        labels_path = path

    images = []
    # IMAGES
    for filename in glob.iglob(labels_path + '\**\*.json', recursive=True):
        with open(filename) as json_file:
            j_image = json.load(json_file)

            labels = []
            # LABELS
            for j_label in j_image['objects']:
                j_box = j_label['bbox']
                box = CommonBox((j_box[0], j_box[1]), (int(j_box[0]) + int(j_box[2]), int(j_box[1]) + int(j_box[3])))

                label = CommonLabel(j_label['label'], box)
                labels.append(label)

        base = os.path.basename(filename)
        index = base.find('_gtBboxCityPersons.json')
        image = CommonImage(os.path.splitext(base)[0][:index] + '.png', labels)
        images.append(image)

    # print(images[0])
    encode_annot_array(images, os.path.join(labels_path, 'cp_common.json'))
Пример #2
0
def bdd100k_to_common(path):
    """ Read original json files and reformat them into the common format"""
    labels_path = os.path.join(path, "labels")

    # JSON FILES
    for filename in glob.iglob(labels_path + '\**\*.json', recursive=True):
        with open(filename) as json_file:

            j_images = json.load(json_file)
            images = []
            # IMAGES
            for j_image in j_images:

                labels = []
                # LABELS
                for j_label in j_image['labels']:
                    try:
                        j_box = j_label['box2d']
                    except KeyError:
                        continue

                    box = CommonBox((j_box['x1'], j_box['y1']), (j_box['x2'], j_box['y2']))

                    label = CommonLabel(j_label['category'], box)
                    labels.append(label)

                image = CommonImage(j_image['name'], labels)
                images.append(image)

            # print(images[0])
            encode_annot_array(images, filename, '_common')
Пример #3
0
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')
Пример #4
0
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')
Пример #5
0
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)
Пример #6
0
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'))
Пример #7
0
def cs_to_common(path, labels_folder=True):
    """ Read original json files and reformat them into the common format"""
    if labels_folder:
        labels_path = os.path.join(path, "labels")
    else:
        labels_path = path

    images = []
    # IMAGES
    for filename in glob.iglob(labels_path + '\**\*.json', recursive=True):
        with open(filename) as json_file:
            j_image = json.load(json_file)

            labels = []
            # LABELS
            for j_label in j_image['objects']:
                # POLYGON's XYs
                xmin = sys.maxsize
                ymin = sys.maxsize
                xmax = -sys.maxsize - 1
                ymax = -sys.maxsize - 1
                for j_xy in j_label['polygon']:
                    x, y = j_xy[0], j_xy[1]
                    if x > xmax:
                        xmax = x
                    elif x < xmin:
                        xmin = x
                    if y > ymax:
                        ymax = y
                    elif y < ymin:
                        ymin = y

                box = CommonBox((xmin, ymin), (xmax, ymax))

                label = CommonLabel(j_label['label'], box)
                labels.append(label)

        base = os.path.basename(filename)
        index = base.find('_gtFine_polygons.json')
        image = CommonImage(os.path.splitext(base)[0][:index] + '.png', labels)
        images.append(image)

    # print(images[0])
    encode_annot_array(images, os.path.join(labels_path, 'cs_common.json'))
Пример #8
0
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)
Пример #9
0
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')
Пример #10
0
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)
Пример #11
0
def kitti_to_comon(path):
    labels_path = os.path.join(path, "labels")

    images = []
    # IMAGES
    for filename in glob.iglob(labels_path + '\**\*.txt', recursive=True):
        with open(filename, "r") as f:

            labels = []
            # LABELS
            for line in f:
                label = line.split(' ')
                box = CommonBox((label[4], label[5]), (label[6], label[7]))

                label = CommonLabel(label[0], box)
                labels.append(label)

        base = os.path.basename(filename)
        index = base.find('.txt')
        image = CommonImage(os.path.splitext(base)[0][:index] + '.png', labels)
        images.append(image)

    # print(images[0])
    encode_annot_array(images, os.path.join(labels_path, 'kitti_common.json'))