Exemple #1
0
def main():

    parser = argparse.ArgumentParser()
    parser.add_argument('json_file')
    parser.add_argument('-o', '--out', default=None)
    args = parser.parse_args()
    json_file = args.json_file
    list = os.listdir(json_file)
    for i in range(0, len(list)):
        path = os.path.join(json_file, list[i])
        if os.path.isfile(path):
            data = json.load(open(path))
            img = utils.img_b64_to_arr(data['imageData'])
            lbl, lbl_names = utils.labelme_shapes_to_label(
                img.shape, data['shapes'])
            captions = [
                '%d: %s' % (l, name) for l, name in enumerate(lbl_names)
            ]
            lbl_viz = utils.draw_label(lbl, img, captions)
            out_dir = osp.basename(list[i]).replace('.', '_')
            out_dir = osp.join(osp.dirname(list[i]), out_dir)
            if not osp.exists(out_dir):
                os.mkdir(out_dir)
            PIL.Image.fromarray(img).save(osp.join(out_dir, 'img.png'))
            utils.lblsave(osp.join(out_dir, 'label.png'), lbl)
            PIL.Image.fromarray(lbl_viz).save(
                osp.join(out_dir, 'label_viz.png'))
            with open(osp.join(out_dir, 'label_names.txt'), 'w') as f:
                for lbl_name in lbl_names:
                    f.write(lbl_name + '\n')
            warnings.warn('info.yaml is being replaced by label_names.txt')
            info = dict(label_names=lbl_names)
            with open(osp.join(out_dir, 'info.yaml'), 'w') as f:
                yaml.safe_dump(info, f, default_flow_style=False)
            print('Saved to: %s' % out_dir)
Exemple #2
0
    def Json_to_dataset(self):
        count = os.listdir(self.JsonjpgChange)
        for i in range(0, len(count)):
            path = os.path.join(self.Jsonjpg7, count[i])

            if os.path.isfile(path) and path.endswith('json'):
                data = json.load(open(path))

                if data['imageData']:
                    imageData = data['imageData']
                else:
                    imagePath = os.path.join(os.path.dirname(path), data['imagePath'])
                    with open(imagePath, 'rb') as f:
                        imageData = f.read()
                        imageData = base64.b64encode(imageData).decode('utf-8')
                img = utils.img_b64_to_arr(imageData)
                label_name_to_value = {'_background_': 0}
                for shape in data['shapes']:
                    label_name = shape['label']
                    if label_name in label_name_to_value:
                        label_value = label_name_to_value[label_name]
                    else:
                        label_value = len(label_name_to_value)
                        label_name_to_value[label_name] = label_value

                # label_values must be dense
                label_values, label_names = [], []
                for ln, lv in sorted(label_name_to_value.items(), key=lambda x: x[1]):
                    label_values.append(lv)
                    label_names.append(ln)
                assert label_values == list(range(len(label_values)))

                lbl = utils.shapes_to_label(img.shape, data['shapes'], label_name_to_value)

                captions = ['{}: {}'.format(lv, ln)
                            for ln, lv in label_name_to_value.items()]
                lbl_viz = draw.draw_label(lbl, img, captions)
                out_dir = osp.basename(count[i]).replace('.', '_')
                out_dir = osp.join(osp.dirname(count[i]), out_dir)
                out_dir = osp.join(self.outputChange, out_dir)

                if not osp.exists(out_dir):
                    os.mkdir(out_dir)

                PIL.Image.fromarray(img).save(osp.join(out_dir, 'img.png'))

                utils.lblsave(osp.join(out_dir, 'label.png'), lbl)
                PIL.Image.fromarray(lbl_viz).save(osp.join(out_dir, 'label_viz.png'))

                with open(osp.join(out_dir, 'label_names.txt'), 'w') as f:
                    for lbl_name in label_names:
                        f.write(lbl_name + '\n')

                warnings.warn('info.yaml is being replaced by label_names.txt')
                info = dict(label_names=label_names)
                with open(osp.join(out_dir, 'info.yaml'), 'w') as f:
                    yaml.safe_dump(info, f, default_flow_style=False)

                print('Saved to: %s' % out_dir)
        self.lineEdit_do_jsontodataset.setText('Json To Dataset Complete!')
Exemple #3
0
def main():
    logger.warning('This script is aimed to demonstrate how to convert the'
                   'JSON file to a single image dataset, and not to handle'
                   'multiple JSON files to generate a real-use dataset.')

    parser = argparse.ArgumentParser()
    parser.add_argument('json_file')
    parser.add_argument('-o', '--out', default=None)
    args = parser.parse_args()

    json_file = args.json_file

    if args.out is None:
        out_dir = osp.basename(json_file).replace('.', '_')
        out_dir = osp.join(osp.dirname(json_file), out_dir)
    else:
        out_dir = args.out
    if not osp.exists(out_dir):
        os.mkdir(out_dir)

    data = json.load(open(json_file))

    if data['imageData']:
        imageData = data['imageData']
    else:
        imagePath = os.path.join(os.path.dirname(json_file), data['imagePath'])
        with open(imagePath, 'rb') as f:
            imageData = f.read()
            imageData = base64.b64encode(imageData).decode('utf-8')
    img = utils.img_b64_to_arr(imageData)

    label_name_to_value = {'_background_': 0}
    for shape in sorted(data['shapes'], key=lambda x: x['label']):
        label_name = shape['label']
        if label_name in label_name_to_value:
            label_value = label_name_to_value[label_name]
        else:
            label_value = len(label_name_to_value)
            label_name_to_value[label_name] = label_value
    lbl = utils.shapes_to_label(img.shape, data['shapes'], label_name_to_value)

    label_names = [None] * (max(label_name_to_value.values()) + 1)
    for name, value in label_name_to_value.items():
        label_names[value] = name
    lbl_viz = utils.draw_label(lbl, img, label_names)

    PIL.Image.fromarray(img).save(osp.join(out_dir, 'img.png'))
    utils.lblsave(osp.join(out_dir, 'label.png'), lbl)
    PIL.Image.fromarray(lbl_viz).save(osp.join(out_dir, 'label_viz.png'))

    with open(osp.join(out_dir, 'label_names.txt'), 'w') as f:
        for lbl_name in label_names:
            f.write(lbl_name + '\n')

    logger.warning('info.yaml is being replaced by label_names.txt')
    info = dict(label_names=label_names)
    with open(osp.join(out_dir, 'info.yaml'), 'w') as f:
        yaml.safe_dump(info, f, default_flow_style=False)

    logger.info('Saved to: {}'.format(out_dir))
Exemple #4
0
def save_label_from_json(json_file, out_dir):
    data = json.load(open(json_file))

    if data['imageData']:
        imageData = data['imageData']
    else:
        imagePath = osp.join(osp.dirname(json_file), data['imagePath'])
        with open(imagePath, 'rb') as f:
            imageData = f.read()
            imageData = base64.b64encode(imageData).decode('utf-8')
    img = utils.img_b64_to_arr(imageData)

    label_name_to_value = {'_background_': 0}
    for shape in sorted(data['shapes'], key=lambda x: x['label']):
        label_name = shape['label']
        if label_name in label_name_to_value:
            label_value = label_name_to_value[label_name]
        else:
            label_value = len(label_name_to_value)
            label_name_to_value[label_name] = label_value
    lbl = utils.shapes_to_label(img.shape, data['shapes'], label_name_to_value)

    label_names = [None] * (max(label_name_to_value.values()) + 1)
    for name, value in label_name_to_value.items():
        label_names[value] = name
    lbl_viz = utils.draw_label(lbl, img, label_names)

    utils.lblsave(osp.join(out_dir, 'label.png'), lbl)
    PIL.Image.fromarray(lbl_viz).save(osp.join(out_dir, 'label_viz.png'))

    with open(osp.join(out_dir, 'label_names.txt'), 'w') as f:
        for lbl_name in label_names:
            f.write(lbl_name + '\n')
Exemple #5
0
def main():
    logger.warning("This script is aimed to demonstrate how to convert the "
                   "JSON file to a single image dataset.")
    logger.warning("It won't handle multiple JSON files to generate a "
                   "real-use dataset.")

    parser = argparse.ArgumentParser()
    parser.add_argument("json_file")
    parser.add_argument("-o", "--out", default=None)
    args = parser.parse_args()

    json_file = args.json_file

    if args.out is None:
        out_dir = osp.basename(json_file).replace(".", "_")
        out_dir = osp.join(osp.dirname(json_file), out_dir)
    else:
        out_dir = args.out
    if not osp.exists(out_dir):
        os.mkdir(out_dir)

    data = json.load(open(json_file))
    imageData = data.get("imageData")

    if not imageData:
        imagePath = os.path.join(os.path.dirname(json_file), data["imagePath"])
        with open(imagePath, "rb") as f:
            imageData = f.read()
            imageData = base64.b64encode(imageData).decode("utf-8")
    img = utils.img_b64_to_arr(imageData)

    label_name_to_value = {"_background_": 0}
    for shape in sorted(data["shapes"], key=lambda x: x["label"]):
        label_name = shape["label"]
        if label_name in label_name_to_value:
            label_value = label_name_to_value[label_name]
        else:
            label_value = len(label_name_to_value)
            label_name_to_value[label_name] = label_value
    lbl, _ = utils.shapes_to_label(img.shape, data["shapes"],
                                   label_name_to_value)

    label_names = [None] * (max(label_name_to_value.values()) + 1)
    for name, value in label_name_to_value.items():
        label_names[value] = name

    lbl_viz = imgviz.label2rgb(label=lbl,
                               img=imgviz.asgray(img),
                               label_names=label_names,
                               loc="rb")

    PIL.Image.fromarray(img).save(osp.join(out_dir, "img.png"))
    utils.lblsave(osp.join(out_dir, "label.png"), lbl)
    PIL.Image.fromarray(lbl_viz).save(osp.join(out_dir, "label_viz.png"))

    with open(osp.join(out_dir, "label_names.txt"), "w") as f:
        for lbl_name in label_names:
            f.write(lbl_name + "\n")

    logger.info("Saved to: {}".format(out_dir))
Exemple #6
0
def main():
    logger.warning('This script is aimed to demonstrate how to convert the'
                   'JSON file to a single image dataset, and not to handle'
                   'multiple JSON files to generate a real-use dataset.')

    parser = argparse.ArgumentParser()
    parser.add_argument('json_file')
    parser.add_argument('-o', '--out', default=None)
    args = parser.parse_args()

    json_file = args.json_file

    if args.out is None:
        out_dir = osp.basename(json_file).replace('.', '_')
        out_dir = osp.join(osp.dirname(json_file), out_dir)
    else:
        out_dir = args.out
    if not osp.exists(out_dir):
        os.mkdir(out_dir)

    data = json.load(open(json_file))

    if data['imageData']:
        imageData = data['imageData']
    else:
        imagePath = os.path.join(os.path.dirname(json_file), data['imagePath'])
        with open(imagePath, 'rb') as f:
            imageData = f.read()
            imageData = base64.b64encode(imageData).decode('utf-8')
    img = utils.img_b64_to_arr(imageData)

    label_name_to_value = {'_background_': 0}
    for shape in sorted(data['shapes'], key=lambda x: x['label']):
        label_name = shape['label']
        if label_name in label_name_to_value:
            label_value = label_name_to_value[label_name]
        else:
            label_value = len(label_name_to_value)
            label_name_to_value[label_name] = label_value
    lbl = utils.shapes_to_label(img.shape, data['shapes'], label_name_to_value)

    label_names = [None] * (max(label_name_to_value.values()) + 1)
    for name, value in label_name_to_value.items():
        label_names[value] = name
    lbl_viz = utils.draw_label(lbl, img, label_names)

    PIL.Image.fromarray(img).save(osp.join(out_dir, 'img.png'))
    utils.lblsave(osp.join(out_dir, 'label.png'), lbl)
    PIL.Image.fromarray(lbl_viz).save(osp.join(out_dir, 'label_viz.png'))

    with open(osp.join(out_dir, 'label_names.txt'), 'w') as f:
        for lbl_name in label_names:
            f.write(lbl_name + '\n')

    logger.warning('info.yaml is being replaced by label_names.txt')
    info = dict(label_names=label_names)
    with open(osp.join(out_dir, 'info.yaml'), 'w') as f:
        yaml.safe_dump(info, f, default_flow_style=False)

    logger.info('Saved to: {}'.format(out_dir))
Exemple #7
0
def main():
    print('This script is aimed to demonstrate how to convert the'
          'JSON file to a single image dataset, and not to handle'
          'multiple JSON files to generate a real-use dataset.')

    path_file_name = glob.glob('*.json')
    file_num = len(path_file_name)
    print('INFO:There are ' + str(file_num) + ' json files')
    file_name = [i for i in range(file_num)]
    for i in range(file_num):
        file_name[i] = path_file_name[i].split('\\')[-1]
        print('INFO:' + file_name[i] + ' is dealt')
        data = json.load(open(path_file_name[i]))
        imageData = data.get('imageData')

        out_dir = osp.basename(path_file_name[i]).replace('.', '_')
        out_dir = osp.join(osp.dirname(path_file_name[i]), out_dir)
        if not os.path.exists(out_dir):
            os.mkdir(out_dir)

        if not imageData:
            imagePath = os.path.join(os.path.dirname(json_file),
                                     data['imagePath'])
            with open(imagePath, 'rb') as f:
                imageData = f.read()
                imageData = base64.b64encode(imageData).decode('utf-8')
        img = utils.img_b64_to_arr(imageData)

        label_name_to_value = {'_background_': 0}
        for shape in sorted(data['shapes'], key=lambda x: x['label']):
            label_name = shape['label']
            if label_name in label_name_to_value:
                label_value = label_name_to_value[label_name]
            else:
                label_value = len(label_name_to_value)
                label_name_to_value[label_name] = label_value
        lbl, _ = utils.shapes_to_label(img.shape, data['shapes'],
                                       label_name_to_value)

        label_names = [None] * (max(label_name_to_value.values()) + 1)
        for name, value in label_name_to_value.items():
            label_names[value] = name

        lbl_viz = imgviz.label2rgb(label=lbl,
                                   img=imgviz.asgray(img),
                                   label_names=label_names,
                                   loc='rb')

        PIL.Image.fromarray(img).save(osp.join(out_dir, 'img.png'))
        utils.lblsave(osp.join(out_dir, 'label.png'), lbl)
        PIL.Image.fromarray(lbl_viz).save(osp.join(out_dir, 'label_viz.png'))

        with open(osp.join(out_dir, 'label_names.txt'), 'w') as f:
            for lbl_name in label_names:
                f.write(lbl_name + '\n')

        print('INFO:Saved to: {}'.format('images/segmentation/test/' +
                                         file_name[i]))

    print('INFO:finished!')
def json2label(json_folder):
    json_list = glob.glob(os.path.join(json_folder, '*.json'))

    annotations_dir = os.path.join(json_folder, "annotations")
    josn2labels_dir = os.path.join(json_folder, "josn2labels")

    if not os.path.exists(annotations_dir):
        os.makedirs(annotations_dir)
    if not os.path.exists(josn2labels_dir):
        os.makedirs(josn2labels_dir)

    for json_file in json_list:
        temp_name = json_file.split(".")[0].split("/")[-1]

        out_dir = os.path.join(josn2labels_dir, temp_name)
        if not osp.exists(out_dir):
            os.mkdir(out_dir)

        data = json.load(open(json_file))

        if data['imageData']:
            imageData = data['imageData']
        else:
            imagePath = os.path.join(os.path.dirname(json_file),
                                     data['imagePath'])
            with open(imagePath, 'rb') as f:
                imageData = f.read()
                imageData = base64.b64encode(imageData).decode('utf-8')
        img = utils.img_b64_to_arr(imageData)

        label_name_to_value = {'_background_': 0, '1': 1, '2': 2, '3': 3}

        lbl = utils.shapes_to_label(img.shape, data['shapes'],
                                    label_name_to_value)

        label_names = [None] * (max(label_name_to_value.values()) + 1)
        for name, value in label_name_to_value.items():
            label_names[value] = name
        lbl_viz = utils.draw_label(lbl, img, label_names)

        PIL.Image.fromarray(img).save(
            osp.join(out_dir, '%s_img.png' % (temp_name)))
        utils.lblsave(osp.join(out_dir, '%s.png' % (temp_name)), lbl)
        utils.lblsave(osp.join(annotations_dir, '%s.png' % (temp_name)), lbl)
        PIL.Image.fromarray(lbl_viz).save(
            osp.join(out_dir, '%s_label_viz.png' % (temp_name)))

        with open(osp.join(out_dir, 'label_names.txt'), 'w') as f:
            for lbl_name in label_names:
                f.write(lbl_name + '\n')

        warnings.warn('info.yaml is being replaced by label_names.txt')
        info = dict(label_names=label_names)
        with open(osp.join(out_dir, 'info.yaml'), 'w') as f:
            yaml.safe_dump(info, f, default_flow_style=False)

        print('Saved to: %s' % out_dir)
Exemple #9
0
def main():
    logger.warning('This script is aimed to demonstrate how to convert the'
                   'JSON file to a single image dataset, and not to handle'
                   'multiple JSON files to generate a real-use dataset.')

    # parser = argparse.ArgumentParser()
    # parser.add_argument('json_file')
    # parser.add_argument('-o', '--out', default=None)
    # args = parser.parse_args()

    json_files = glob.glob(r'C:\Users\Zeran\Desktop\loudi\*.json')
    for json_file in json_files:

        out_dir = osp.basename(json_file).replace('.', '_')
        out_dir = osp.join(osp.dirname(json_file), out_dir)

        # reload(sys)
        # sys.setdefaultencoding('utf8')
        f = open(json_file, encoding='utf-8')
        text = f.read()
        # text = text.decode("gbk").encode("utf-8")
        data = json.loads(text)

        # data = f.read().decode(encoding='gbk').encode(encoding='utf-8')

        # data = json.load(open(json_file))

        if data['imageData']:
            imageData = data['imageData']
        else:
            imagePath = os.path.join(os.path.dirname(json_file),
                                     data['imagePath'])
            with open(imagePath, 'rb') as f:
                imageData = f.read()
                imageData = base64.b64encode(imageData).decode('utf-8')
        img = utils.img_b64_to_arr(imageData)

        label_name_to_value = {'_background_': 0}
        for shape in sorted(data['shapes'], key=lambda x: x['label']):
            label_name = shape['label']
            # if label_name in label_name_to_value:
            #     label_value = label_name_to_value[label_name]
            # else:
            #     label_value = len(label_name_to_value)
            label_name_to_value[label_name] = 255
        lbl = utils.shapes_to_label(img.shape, data['shapes'],
                                    label_name_to_value)

        label_names = [None] * (max(label_name_to_value.values()) + 1)
        for name, value in label_name_to_value.items():
            label_names[value] = name
        lbl_viz = utils.draw_label(lbl, img, label_names)
        saved_name = os.path.splitext(os.path.basename(json_file))[0] + '.png'
        utils.lblsave(
            osp.join('D:\\coslight\\0304_beforetolabel\\label\\', saved_name),
            lbl)
def label_convert(json_file_path, png_label_path, class_names):
    if not os.path.isdir(json_file_path):
        raise ValueError('Input path does not exist!\n')
    os.makedirs(png_label_path, exist_ok=True)

    # all the json annotation file list
    json_files = glob.glob(os.path.join(json_file_path, '*.json'))

    # form a dict of class_name to label value
    label_name_to_value = {}
    for i, class_name in enumerate(class_names):
        label_name_to_value[class_name] = i

    # count class item number
    class_count = OrderedDict([(item, 0) for item in class_names])

    pbar = tqdm(total=len(json_files), desc='Label converting')
    for i, json_file in enumerate(json_files):
        data = json.load(open(json_file))

        # get image info
        imageData = data.get("imageData")
        if not imageData:
            imagePath = os.path.join(os.path.dirname(json_file), data["imagePath"].replace('\\', '/'))
            with open(imagePath, "rb") as f:
                imageData = f.read()
                imageData = base64.b64encode(imageData).decode("utf-8")
        img = utils.img_b64_to_arr(imageData)

        # convert json labels to numpy label array
        # and save to png
        label_array, _ = utils.shapes_to_label(
            img.shape, data["shapes"], label_name_to_value
        )

        # count object class for statistic
        label_list = list(np.unique(label_array))
        for label in label_list:
            class_name = class_names[label]
            class_count[class_name] = class_count[class_name] + 1

        utils.lblsave(os.path.join(png_label_path, os.path.basename(json_file)+".png"), label_array)
        pbar.update(1)

    pbar.close()
    # show item number statistic
    print('Image number for each class:')
    for (class_name, number) in class_count.items():
        if class_name == 'background':
            continue
        print('%s: %d' % (class_name, number))
    print('total number of converted images: ', len(json_files))
Exemple #11
0
def json2img(json_file, out, index):
    if out is None:
        out_dir = osp.basename(json_file).replace(".", "_")
        out_dir = osp.join(osp.dirname(json_file), out_dir)
    else:
        out_dir = out
    mkdir(out_dir)
    data = json.load(open(json_file))
    imageData = data.get("imageData")

    if not imageData:
        imagePath = os.path.join(os.path.dirname(json_file), data["imagePath"])
        with open(imagePath, "rb") as f:
            imageData = f.read()
            imageData = base64.b64encode(imageData).decode("utf-8")
    img = utils.img_b64_to_arr(imageData)

    label_name_to_value = {"_background_": 0}
    for shape in sorted(data["shapes"], key=lambda x: x["label"]):
        label_name = shape["label"]
        if label_name in label_name_to_value:
            label_value = label_name_to_value[label_name]
        else:
            label_value = len(label_name_to_value)
            label_name_to_value[label_name] = label_value
    lbl, _ = utils.shapes_to_label(img.shape, data["shapes"],
                                   label_name_to_value)

    label_names = [None] * (max(label_name_to_value.values()) + 1)
    for name, value in label_name_to_value.items():
        label_names[value] = name

    lbl_viz = imgviz.label2rgb(label=lbl,
                               img=imgviz.asgray(img),
                               label_names=label_names,
                               loc="rb")
    img_name = str(index) + '.jpg'
    out_dir_raw = out_dir + '/raw'
    out_dir_label = out_dir + '/label'
    mkdir(out_dir_raw)
    mkdir(out_dir_label)
    PIL.Image.fromarray(img).save(osp.join(out_dir_raw, img_name))  #保存图片 1.jpg
    utils.lblsave(osp.join(out_dir_label, img_name.replace('.jpg', '.png')),
                  lbl)  #保存标签 1.png
    #PIL.Image.fromarray(lbl_viz).save(osp.join(out_dir, "label_viz.png"))

    with open(osp.join(out_dir, "label_names.txt"), "w") as f:
        for lbl_name in label_names:
            f.write(lbl_name + "\n")

    logger.info("Saved to: {}".format(out_dir))
Exemple #12
0
def generate_datasets(list, i):
    global json_file, json_out, NAME_LABEL_MAP

    path = os.path.join(json_file, list[i])
    filename = list[i][:-5]  # .json
    if os.path.isfile(path):
        data = json.load(open(path))
        img = utils.image.img_b64_to_arr(data['imageData'])
        lbl, lbl_names = utils.shape.labelme_shapes_to_label(
            img.shape, data['shapes'])  # labelme_shapes_to_label

        # modify labels according to NAME_LABEL_MAP
        lbl_tmp = copy.copy(lbl)
        for key_name in lbl_names:
            old_lbl_val = lbl_names[key_name]
            new_lbl_val = NAME_LABEL_MAP[key_name]
            lbl_tmp[lbl == old_lbl_val] = new_lbl_val
        lbl_names_tmp = {}
        for key_name in lbl_names:
            lbl_names_tmp[key_name] = NAME_LABEL_MAP[key_name]

        # Assign the new label to lbl and lbl_names dict
        lbl = np.array(lbl_tmp, dtype=np.int8)
        lbl_names = lbl_names_tmp

        captions = ['%d: %s' % (l, name) for l, name in enumerate(lbl_names)]
        lbl_viz = utils.draw.draw_label(lbl, img, captions)
        out_dir = osp.basename(list[i]).replace('.', '_')
        out_dir = json_out + '/' + osp.join(osp.dirname(list[i]), out_dir)
        print(out_dir)
        if not osp.exists(out_dir):
            os.mkdir(out_dir)

        PIL.Image.fromarray(img).save(
            osp.join(out_dir, '{}.png'.format(filename)))

        utils.lblsave(osp.join(out_dir, '{}_gt.png'.format(filename)), lbl)

        PIL.Image.fromarray(lbl_viz).save(
            osp.join(out_dir, '{}_viz.png'.format(filename)))

        with open(osp.join(out_dir, 'label_names.txt'), 'w') as f:
            for lbl_name in lbl_names:
                f.write(lbl_name + '\n')

        info = dict(label_names=lbl_names)
        with open(osp.join(out_dir, 'info.yaml'), 'w') as f:
            yaml.safe_dump(info, f, default_flow_style=False)

        print('Saved to: %s' % out_dir)
def save_image_and_label(image, lbl, output_dir, label_names):
    """
    save image and label to output_dir
    :param image: image
    :param lbl: label
    :param output_dir: output directory
    :param label_names: label names
    :return:
    """
    PIL.Image.fromarray(image).save(osp.join(output_dir, 'img.png'))
    utils.lblsave(osp.join(output_dir, 'label.png'), lbl)
    lbl_viz = imgviz.label2rgb(lbl, imgviz.asgray(image), label_names=label_names, loc="rb")
    PIL.Image.fromarray(lbl_viz).save(osp.join(output_dir, 'label_viz.png'))

    with open(osp.join(output_dir, 'label_names.txt'), 'w', encoding="utf8") as label_f:
        for lbl_name in label_names:
            label_f.write(lbl_name + '\n')

    print(f"Saved to: {output_dir}")
Exemple #14
0
def writeimages(img, lbl, depth_map, jsonFile, sufix, out_images_dir,
                out_labels_dir, out_labels_raw_dir, debug_dir,
                label_name_to_value, args):
    #Plot visualization if in debug mode
    if args.debug:
        label_names = [None] * (max(label_name_to_value.values()) + 1)
        for name, value in label_name_to_value.items():
            label_names[value] = name
        lbl_viz = utils.draw_label(lbl, img, label_names)
        PIL.Image.fromarray(lbl_viz).save(
            osp.join(
                debug_dir,
                jsonFile.replace('./', '').replace('../', '').replace(
                    '/', '_').replace('.json',
                                      '-' + sufix + '-label_viz.png')))

    jsonFileName = jsonFile[jsonFile.rfind('/') + 1:]

    #Save image
    if out_images_dir is not None:
        PIL.Image.fromarray(img).save(
            osp.join(out_images_dir, jsonFileName.replace('.json', '.jpg')))
    #Save labels
    utils.lblsave(
        osp.join(out_labels_dir,
                 jsonFileName.replace('.json', '-' + sufix + '-label.png')),
        lbl)
    #Save labels with raw annotation
    PIL.Image.fromarray(lbl.astype(dtype=np.uint8)).save(
        osp.join(out_labels_raw_dir,
                 jsonFileName.replace('.json',
                                      '-' + sufix + '-label_raw.png')))
    if args.depth:
        #Save depth image
        #        PIL.Image.fromarray(depth_map.astype(dtype=np.uint8)).save(
        #            osp.join(out_images_dir, jsonFile.replace('.json','-depth.png'))
        #        )
        PIL.Image.fromarray(depth_map.astype(dtype=np.uint8)).save(
            osp.join(out_images_dir,
                     jsonFileName.replace('.json',
                                          '-' + sufix + '-depth.png')))
Exemple #15
0
def voc_to_mask(img_path, contours, png_dir, mask_dir):

    filename = osp.basename(img_path).replace(
        'jpg', 'png')  # get the last filename of the path
    # print(filename)

    with open(img_path, 'rb') as f:
        imageData = f.read()
        imageData = base64.b64encode(imageData).decode('utf-8')
    img = utils.img_b64_to_arr(imageData)
    # print('img shape:', img.shape)
    # print(np.max(img))

    label_name_to_value = {'_background_': 0, 'bone': 1}

    lbl = utils.shapes_to_label(img.shape, contours, label_name_to_value)
    # print('mask shape:', lbl.shape)
    # print(np.max(lbl))

    PIL.Image.fromarray(img).save(osp.join(png_dir, filename))
    utils.lblsave(osp.join(mask_dir, filename), lbl)
def save_label_info(img, name, dir_label, dir_info, dir_vis, shape_):
    label_name_to_value = {'_background_': 0, "1": 1, "2": 2, "3": 3}
    # for shape in shapes:
    #     label_name = shape['label']
    #     if label_name in label_name_to_value:
    #         label_value = label_name_to_value[label_name]
    #     else:
    #         label_value = len(label_name_to_value)
    #         label_name_to_value[label_name] = label_value
    # print(label_name_to_value.items())
    label_values, label_names = [], []
    for ln, lv in sorted(label_name_to_value.items(), key=lambda x: x[1]):
        # print(ln,lv)
        label_values.append(lv)
        label_names.append(ln)
    assert label_values == list(range(len(label_values)))

    lbl = utils.shapes_to_label(img.shape, shape_,
                                label_name_to_value)  # 'instance'
    # print(ins)

    captions = [
        '{}: {}'.format(lv, ln) for ln, lv in label_name_to_value.items()
    ]
    lbl_viz = utils.draw_label(lbl, img, captions)

    # ins_viz=utils.draw_label(lbl, img, captions)
    # PIL.Image.fromarray(img).save('img.png')
    # PIL.Image.fromarray(lbl).save(osp.join(out_dir, 'label.png'))
    utils.lblsave(dir_label + name + '.png', lbl)
    PIL.Image.fromarray(lbl_viz).save(dir_vis + name + '.png')

    # with open('label_names.txt', 'w') as f:
    #     for lbl_name in label_names:
    #         f.write(lbl_name + '\n')

    # warnings.warn('info.yaml is being replaced by label_names.txt')
    info = dict(label_names=label_names)
    with open(dir_info + name + '.yaml', 'w') as f:
        yaml.safe_dump(info, f, default_flow_style=False)
def labelmejson_to_dataset(json_path):
    json_paths = sorted([
        os.path.join(json_path, fname) for fname in os.listdir(json_path)
        if fname.endswith(".json")
    ])

    os.makedirs("converted_dataset\\img", exist_ok=True)
    os.makedirs("converted_dataset\\gt", exist_ok=True)
    for i in range(len(json_paths)):
        data = json.load(open(json_paths[i]))
        imageData = data.get("imageData")

        # ignore "json_file" is not defined warning
        if not imageData:
            imagePath = os.path.join(os.path.dirname(json_file),
                                     data["imagePath"])
            with open(imagePath, "rb") as f:
                imageData = f.read()
                imageData = base64.b64encode(imageData).decode("utf-8")
        img = utils.img_b64_to_arr(imageData)

        label_name_to_value = {"_background_": 0}
        for shape in sorted(data["shapes"], key=lambda x: x["label"]):
            label_name = shape["label"]
            if label_name in label_name_to_value:
                label_value = label_name_to_value[label_name]
            else:
                label_value = len(label_name_to_value)
                label_name_to_value[label_name] = label_value
        lbl, _ = utils.shapes_to_label(img.shape, data["shapes"],
                                       label_name_to_value)

        img = img[:, :, :3]
        PIL.Image.fromarray(img).save(
            os.path.join("converted_dataset\\img", "{}.jpg".format(i)))
        utils.lblsave(
            os.path.join("converted_dataset\\gt", "{}.png".format(i)), lbl)
Exemple #18
0
    # for shape in sorted(data["shapes"], key=lambda x: x["label"]):
    #     label_name = shape["label"]
    #     if label_name in label_name_to_value:
    #         label_value = label_name_to_value[label_name]
    #     else:
    #         label_value = len(label_name_to_value)
    #         label_name_to_value[label_name] = label_value
    lbl, _ = utils.shapes_to_label(
        img.shape, data["shapes"], label_name_to_value
    )
    
    label_names = [None] * (max(label_name_to_value.values()) + 1)
    for name, value in label_name_to_value.items():
        label_names[value] = name
    
    lbl_viz = imgviz.label2rgb(
        label=lbl, img=imgviz.asgray(img), label_names=label_names, loc="rb"
    )
    
    _, name, _ = json_file.replace('.', '_').split('_')
    
    PIL.Image.fromarray(img).save(osp.join(SAVE_DIR, "img_" + name + ".png"))
    utils.lblsave(osp.join(SAVE_DIR, "label_" + name + ".png"), lbl)
    PIL.Image.fromarray(lbl_viz).save(osp.join(SAVE_DIR, "viz_" + name + ".png"))
    
    with open(osp.join(SAVE_DIR, "label_names.txt"), "w") as f:
        for lbl_name in label_names:
            f.write(lbl_name + "\n")
    

                if label_name in label_name_to_value:
                    label_value = label_name_to_value[label_name]
                else:
                    label_value = len(label_name_to_value)
                    label_name_to_value[label_name] = label_value

            # label_values must be dense
            label_values, label_names = [], []
            for ln, lv in sorted(label_name_to_value.items(),
                                 key=lambda x: x[1]):
                label_values.append(lv)
                label_names.append(ln)
            assert label_values == list(range(len(label_values)))

            lbl = utils.shapes_to_label(img.shape, data['shapes'],
                                        label_name_to_value)

            PIL.Image.fromarray(img).save(
                osp.join(jpgs_path, count[i].split(".")[0] + '.jpg'))

            new = np.zeros([np.shape(img)[0], np.shape(img)[1]])
            for name in label_names:
                index_json = label_names.index(name)
                index_all = classes.index(name)
                new = new + index_all * (np.array(lbl) == index_json)

            utils.lblsave(osp.join(pngs_path, count[i].split(".")[0] + '.png'),
                          new)
            print('Saved ' + count[i].split(".")[0] + '.jpg and ' +
                  count[i].split(".")[0] + '.png')
def json_to_dataset(json_file: str, out_dir: str, rename: bool):
    """
    JSON文件转换为DataSet格式文件夹

    @param {str} json_file - 要处理的JSON文件
    @param {str} out_dir - 输出目录
    @param {bool} rename - 遇到输出目录存在是否修改文件名
    """
    _json_path = osp.split(json_file)[0]
    _dir_name = osp.split(json_file)[1].replace(".", "_")
    if out_dir == '':
        _out_dir = osp.join(_json_path, _dir_name)
    else:
        _out_dir = osp.join(out_dir, _dir_name)

    if not osp.exists(_out_dir):
        os.makedirs(_out_dir)
    elif rename:
        # 遇到文件存在的情况,重命名
        _index = 1
        while osp.exists(_out_dir):
            _out_dir = osp.join(_json_path,
                                '%s%d%s' % (_dir_name[0:-5], _index, '_json'))
            _index += 1

        os.makedirs(_out_dir)

    data = json.load(open(json_file))
    imageData = data.get("imageData")

    if not imageData:
        imagePath = os.path.join(os.path.dirname(json_file), data["imagePath"])
        with open(imagePath, "rb") as f:
            imageData = f.read()
            imageData = base64.b64encode(imageData).decode("utf-8")
    img = utils.img_b64_to_arr(imageData)

    label_name_to_value = {"_background_": 0}

    # 修正json_to_dataset的bug, 按形状名进行排序,将hole放在最后面
    data["shapes"].sort(
        key=lambda x: x["label"] if x["label"] != "hole" else "zzzzzz")
    for shape in sorted(data["shapes"], key=lambda x: x["label"]):
        label_name = shape["label"]
        if label_name in label_name_to_value:
            label_value = label_name_to_value[label_name]
        else:
            label_value = len(label_name_to_value)
            label_name_to_value[label_name] = label_value
    lbl, _ = utils.shapes_to_label(img.shape, data["shapes"],
                                   label_name_to_value)

    label_names = [None] * (max(label_name_to_value.values()) + 1)
    for name, value in label_name_to_value.items():
        label_names[value] = name

    lbl_viz = imgviz.label2rgb(label=lbl,
                               img=imgviz.asgray(img),
                               label_names=label_names,
                               loc="rb")

    PIL.Image.fromarray(img).save(osp.join(_out_dir, "img.png"))
    utils.lblsave(osp.join(_out_dir, "label.png"), lbl)
    PIL.Image.fromarray(lbl_viz).save(osp.join(_out_dir, "label_viz.png"))

    with open(osp.join(_out_dir, "label_names.txt"), "w") as f:
        for lbl_name in label_names:
            f.write(lbl_name + "\n")

    logger.info("Saved to: {}".format(_out_dir))
Exemple #21
0
def main():
    count = os.listdir("./BEFORE_DATASET/")
    index = 0
    for i in range(0, len(count)):
        path = os.path.join("./BEFORE_DATASET", count[i])

        if os.path.isfile(path) and path.endswith('json'):
            data = json.load(open(path))

            if data['imageData']:
                imageData = data['imageData']
            else:
                imagePath = os.path.join(os.path.dirname(path),
                                         data['imagePath'])
                with open(imagePath, 'rb') as f:
                    imageData = f.read()
                    imageData = base64.b64encode(imageData).decode('utf-8')
            img = utils.img_b64_to_arr(imageData)
            label_name_to_value = {'_background_': 0}
            for shape in data['shapes']:
                label_name = shape['label']
                if label_name in label_name_to_value:
                    label_value = label_name_to_value[label_name]
                else:
                    label_value = len(label_name_to_value)
                    label_name_to_value[label_name] = label_value

            # label_values must be dense
            label_values, label_names = [], []
            for ln, lv in sorted(label_name_to_value.items(),
                                 key=lambda x: x[1]):
                label_values.append(lv)
                label_names.append(ln)

            assert label_values == list(range(len(label_values)))

            lbl = utils.shapes_to_label(img.shape, data['shapes'],
                                        label_name_to_value)

            captions = [
                '{}: {}'.format(lv, ln)
                for ln, lv in label_name_to_value.items()
            ]
            lbl_viz = utils.draw_label(lbl, img, captions)

            if not os.path.exists("DATASET"):
                os.mkdir("DATASET")
            label_path = "DATASET/mask"
            if not os.path.exists(label_path):
                os.mkdir(label_path)
            img_path = "DATASET/imgs"
            if not os.path.exists(img_path):
                os.mkdir(img_path)
            yaml_path = "DATASET/yaml"
            if not os.path.exists(yaml_path):
                os.mkdir(yaml_path)
            label_viz_path = "DATASET/label_viz"
            if not os.path.exists(label_viz_path):
                os.mkdir(label_viz_path)

            PIL.Image.fromarray(img).save(
                osp.join(img_path,
                         str(index) + '.jpg'))

            utils.lblsave(osp.join(label_path, str(index) + '.png'), lbl)
            PIL.Image.fromarray(lbl_viz).save(
                osp.join(label_viz_path,
                         str(index) + '.png'))

            warnings.warn('info.yaml is being replaced by label_names.txt')
            info = dict(label_names=label_names)
            with open(osp.join(yaml_path, str(index) + '.yaml'), 'w') as f:
                yaml.safe_dump(info, f, default_flow_style=False)
            index = index + 1
            print('Saved : %s' % str(index))
Exemple #22
0
def main(JSON_DIR, SAVE_DIR):

    # read .json file list
    _, _, jsons = next(os.walk(JSON_DIR))
    jsons = [s for s in jsons if ".json" in s]

    # take the label_names.txt
    with open(osp.join(JSON_DIR, "label_names.txt"), "r") as f:
        cnt = 0
        label_name_to_value = {}
        for line in f:
            label_name_to_value[line.rstrip('\n')] = cnt
            cnt += 1

    for json_file in jsons:

        # read json
        data = json.load(open(JSON_DIR + json_file))

        # read image
        imageData = data.get("imageData")

        if not imageData:
            imagePath = os.path.join(JSON_DIR, data["imagePath"])
            img = np.asarray(PIL.Image.open(imagePath))
        else:
            img = utils.img_b64_to_arr(imageData)

        with open(osp.join(JSON_DIR, "label_names.txt"), "r") as f:
            cnt = 0
            label_name_to_value = {}
            for line in f:
                label_name_to_value[line.rstrip('\n')] = cnt
                cnt += 1

        # make a label data
        lbl, _ = utils.shapes_to_label(img.shape, data["shapes"],
                                       label_name_to_value)

        # make a viz data
        label_names = [None] * (max(label_name_to_value.values()) + 1)
        for name, value in label_name_to_value.items():
            label_names[value] = name

        lbl_viz = imgviz.label2rgb(label=lbl,
                                   img=imgviz.asgray(img),
                                   label_names=label_names,
                                   loc="rb")

        # save dataset
        _, name, _ = json_file.replace('.', '_').split('_')

        PIL.Image.fromarray(img).save(
            osp.join(SAVE_DIR, "img_" + name + ".png"))
        utils.lblsave(osp.join(SAVE_DIR, "label_" + name + ".png"), lbl)
        PIL.Image.fromarray(lbl_viz).save(
            osp.join(SAVE_DIR, "viz_" + name + ".png"))

        with open(osp.join(SAVE_DIR, "label_names.txt"), "w") as f:
            for lbl_name in label_names:
                f.write(lbl_name + "\n")
Exemple #23
0
def main():
    #logger.warning('This script is aimed to demonstrate how to convert the'
    #               'JSON file to a single image dataset, and not to handle'
    #               'multiple JSON files to generate a real-use dataset.')

    parser = argparse.ArgumentParser()
    parser.add_argument('json_file')
    parser.add_argument('-o', '--out', default=None)
    # set label names file path
    parser.add_argument('-n', '--names', default=None)
    args = parser.parse_args()

    json_file = args.json_file

    if args.out is None:
        out_dir = osp.basename(json_file).replace('.', '_')
        out_dir = osp.join(osp.dirname(json_file), out_dir)
    else:
        out_dir = args.out
    if not osp.exists(out_dir):
        os.mkdir(out_dir)

    label_name_to_value = {'_background_': 0}

    # add label names to dict
    if args.names is not None:
        with open(args.names, 'r') as f:
            lines = f.readlines()

        count = 1
        for line in lines:
            line = line.strip()
            if line == '':
                continue

            label_name_to_value[line] = count
            count += 1

    data = json.load(open(json_file))
    imageData = data.get('imageData')

    if not imageData:
        imagePath = os.path.join(os.path.dirname(json_file), data['imagePath'])
        with open(imagePath, 'rb') as f:
            imageData = f.read()
            imageData = base64.b64encode(imageData).decode('utf-8')
    img = utils.img_b64_to_arr(imageData)

    for shape in sorted(data['shapes'], key=lambda x: x['label']):
        label_name = shape['label']
        if label_name in label_name_to_value:
            label_value = label_name_to_value[label_name]
        else:
            label_value = len(label_name_to_value)
            label_name_to_value[label_name] = label_value
    lbl = utils.shapes_to_label(img.shape, data['shapes'], label_name_to_value)

    label_names = [None] * (max(label_name_to_value.values()) + 1)
    for name, value in label_name_to_value.items():
        label_names[value] = name
    lbl_viz = utils.draw_label(lbl, img, label_names)

    PIL.Image.fromarray(img).save(osp.join(out_dir, 'img.png'))
    utils.lblsave(osp.join(out_dir, 'label.png'), lbl)
    PIL.Image.fromarray(lbl_viz).save(osp.join(out_dir, 'label_viz.png'))

    with open(osp.join(out_dir, 'label_names.txt'), 'w') as f:
        for lbl_name in label_names:
            f.write(lbl_name + '\n')
Exemple #24
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("json_file")
    parser.add_argument("-o", "--out", default=None)
    args = parser.parse_args()

    json_file = args.json_file

    if args.out is None:
        out_dir = osp.basename(json_file).replace(".", "_")
        out_dir = osp.join(osp.dirname(json_file), out_dir)
    else:
        out_dir = args.out
    if not osp.exists(out_dir):
        os.mkdir(out_dir)

    data = json.load(open(json_file))
    imageData = data.get("imageData")

    if not imageData:
        imagePath = os.path.join(os.path.dirname(json_file), data["imagePath"])
        with open(imagePath, "rb") as f:
            imageData = f.read()
            imageData = base64.b64encode(imageData).decode("utf-8")
    img = utils.img_b64_to_arr(imageData)

    label_name_to_value = {
        "_background_": 0,
        "table": 1,
        "cup": 2,
        "bottle": 3,
        "glass": 4,
        "fork": 5,
        "knife": 6,
        "food": 7,
        "plate": 8
    }
    for shape in sorted(data["shapes"], key=lambda x: x["label"]):
        label_name = shape["label"]
        if label_name in label_name_to_value:
            label_value = label_name_to_value[label_name]
        else:
            label_value = len(label_name_to_value)
            label_name_to_value[label_name] = label_value
    lbl, _ = utils.shapes_to_label(img.shape, data["shapes"],
                                   label_name_to_value)

    label_names = [None] * (max(label_name_to_value.values()) + 1)
    for name, value in label_name_to_value.items():
        label_names[value] = name

    lbl_viz = imgviz.label2rgb(label=lbl,
                               img=imgviz.asgray(img),
                               label_names=label_names,
                               loc="rb")

    PIL.Image.fromarray(img).save(osp.join(out_dir, "img.png"))
    utils.lblsave(osp.join(out_dir, "label.png"), lbl)
    PIL.Image.fromarray(lbl_viz).save(osp.join(out_dir, "label_viz.png"))

    with open(osp.join(out_dir, "label_names.txt"), "w") as f:
        for lbl_name in label_names:
            f.write(lbl_name + "\n")

    logger.info("Saved to: {}".format(out_dir))
Exemple #25
0
def main():
    # Only input:
    # Give a folder with only .json files
    label_path = r"/Users/frederikrogalski/Documents/Privates/Programieren/python/trainseg/data/trainseg/Masks/"

    list_path = os.listdir(label_path)
    for i in range(0, len(list_path)):
            logger.warning('This script is aimed to demonstrate how to convert the'
                           'JSON file to a single image dataset, and not to handle'
                           'multiple JSON files to generate a real-use dataset.')

            parser = argparse.ArgumentParser()
            parser.add_argument('--json_file')
            parser.add_argument('-o', '--out', default=None)
            args = parser.parse_args()

            json_file = label_path + list_path[i]
            print(list_path[i])
            if args.out is None:
                out_dir = osp.basename(json_file).replace('.', '_')  # Return file name
                out_dir = osp.join(osp.dirname(json_file), out_dir)  # Combine directory and file name into one path
            else:
                out_dir = args.out
            if not osp.exists(out_dir):
                os.mkdir(out_dir)  # Used to create directories in digital permission mode

            data = json.load(open(json_file))
            imageData = data.get('imageData')

            if not imageData:
                imagePath = os.path.join(os.path.dirname(json_file), data['imagePath']) # os.path.dirname returns the file path
                with open(imagePath, 'rb') as f:
                    imageData = f.read()
                    imageData = base64.b64encode(imageData).decode('utf-8')
            img = utils.img_b64_to_arr(imageData)

            label_name_to_value = {'_background_': 0}
            for shape in sorted(data['shapes'], key=lambda x: x['label']):
                label_name = shape['label']
                if label_name in label_name_to_value:
                    label_value = label_name_to_value[label_name]
                else:
                    label_value = len(label_name_to_value)
                    label_name_to_value[label_name] = label_value
            lbl, _ = utils.shapes_to_label(
                img.shape, data['shapes'], label_name_to_value
            )

            label_names = [None] * (max(label_name_to_value.values()) + 1)
            for name, value in label_name_to_value.items():
                label_names[value] = name

            lbl_viz = imgviz.label2rgb(
                label=lbl, img=imgviz.asgray(img), label_names=label_names, loc='rb'
            )

            PIL.Image.fromarray(img).save(osp.join(out_dir, "Images", f"Image{i+148}.png"))
            utils.lblsave(osp.join(out_dir, "Masks", f"Mask{i+148}.png"), lbl)
            #PIL.Image.fromarray(lbl_viz).save(osp.join(out_dir, f"Mask{i+148}.png"))

            #with open(osp.join(out_dir, 'label_names.txt'), 'w') as f:
            #    for lbl_name in label_names:
            #        f.write(lbl_name + '\n')

            logger.info('Saved to: {}'.format(out_dir))
Exemple #26
0
def main():
    warnings.warn("This script is aimed to demonstrate how to convert the\n"
                  "JSON file to a single image dataset, and not to handle\n"
                  "multiple JSON files to generate a real-use dataset.")

    parser = argparse.ArgumentParser()
    parser.add_argument('json_file')
    parser.add_argument('-o', '--out', default=None)
    args = parser.parse_args()

    json_file = args.json_file

    alist = os.listdir(json_file)

    for i in range(0, len(alist)):
        path = os.path.join(json_file, alist[i])
        data = json.load(open(path))

        out_dir = osp.basename(path).replace('.', '_')
        out_dir = osp.join(osp.dirname(path), out_dir)

        if not osp.exists(out_dir):
            os.mkdir(out_dir)

        if data['imageData']:
            imageData = data['imageData']
        else:
            imagePath = os.path.join(os.path.dirname(path), data['imagePath'])
            with open(imagePath, 'rb') as f:
                imageData = f.read()
                imageData = base64.b64encode(imageData).decode('utf-8')

        img = utils.img_b64_to_arr(imageData)

        label_name_to_value = {'_background_': 0}
        for shape in data['shapes']:
            label_name = shape['label']
            if label_name in label_name_to_value:
                label_value = label_name_to_value[label_name]
            else:
                label_value = len(label_name_to_value)
                label_name_to_value[label_name] = label_value

        # label_values must be dense
        label_values, label_names = [], []
        for ln, lv in sorted(label_name_to_value.items(), key=lambda x: x[1]):
            label_values.append(lv)
            label_names.append(ln)
        assert label_values == list(range(len(label_values)))

        lbl = utils.shapes_to_label(img.shape, data['shapes'],
                                    label_name_to_value)

        captions = [
            '{}: {}'.format(lv, ln) for ln, lv in label_name_to_value.items()
        ]
        lbl_viz = utils.draw_label(lbl, img, captions)

        PIL.Image.fromarray(img).save(osp.join(out_dir, 'img.png'))
        utils.lblsave(osp.join(out_dir, 'label.png'), lbl)
        PIL.Image.fromarray(lbl_viz).save(osp.join(out_dir, 'label_viz.png'))

        with open(osp.join(out_dir, 'label_names.txt'), 'w') as f:
            for lbl_name in label_names:
                f.write(lbl_name + '\n')

        warnings.warn('info.yaml is being replaced by label_names.txt')
        info = dict(label_names=label_names)
        with open(osp.join(out_dir, 'info.yaml'), 'w') as f:
            yaml.safe_dump(info, f, default_flow_style=False)

        print('Saved to: %s' % out_dir)
Exemple #27
0
def json_to_mask(json_file):
    json_file = json_file
    # 下面类别就是自己要分的类别数目,我的共有9类,而且下面这个字典的关键字(单引号内的)是自己在用labelme时标注的类别
    # 所以我自己又加了0-9的关键字,你的改成你自己的关键字就可以了,然后冒号后面的我猜代表的是颜色的标号,这样就会出来不同的颜色。
    label_name_to_value = {
        '_background_': 0,
        'tape': 1,
        'scissors': 2,
        'pen': 3,
        '3': 4,
        '4': 5,
        '5': 6,
        '6': 7,
        '7': 8,
        '8': 9
    }
    out_dir = osp.join(osp.dirname(json_file),
                       osp.basename(json_file).split('_')[0] + 'mask_set')
    if not osp.exists(out_dir):
        os.mkdir(out_dir)
    count = os.listdir(json_file)
    for i in count:
        path = os.path.join(json_file, i)
        if os.path.isfile(path):
            data = json.load(open(path))

            if data['imageData']:
                imageData = data['imageData']
            else:
                imagePath = os.path.join(os.path.dirname(path),
                                         data['imagePath'])
                with open(imagePath, 'rb') as f:
                    imageData = f.read()
                    imageData = base64.b64encode(imageData).decode('utf-8')
            img = utils.img_b64_to_arr(imageData)

            for shape in data['shapes']:
                label_name = shape['label']
                if label_name in label_name_to_value:
                    label_value = label_name_to_value[label_name]
                else:
                    label_value = len(label_name_to_value)
                    label_name_to_value[label_name] = label_value

            # label_values must be dense
            label_values, label_names = [], []
            for ln, lv in sorted(label_name_to_value.items(),
                                 key=lambda x: x[1]):
                label_values.append(lv)
                label_names.append(ln)
            assert label_values == list(range(len(label_values)))

            lbl = utils.shapes_to_label(img.shape, data['shapes'],
                                        label_name_to_value)

            name = osp.basename(i).replace('.', '_').split('_')
            # PIL.Image.fromarray(lbl).save(osp.join(out_dir, rename+'label.png'))
            utils.lblsave(osp.join(out_dir, name[0] + '_mask.png'), lbl)

    print('Saved to {} mask pictures'.format(len(os.listdir(out_dir))))
    return out_dir
Exemple #28
0
def main():
    # warnings.warn("This script is aimed to demonstrate how to convert the\n"
    #               "JSON file to a single image dataset, and not to handle\n"
    #               "multiple JSON files to generate a real-use dataset.")

    ###############################################  删除的语句  ##################################
    # parser = argparse.ArgumentParser()
    # parser.add_argument('json_file')
    # json_file = args.json_file

    # parser.add_argument('-o', '--out', default=None)
    # args = parser.parse_args()
    ###############################################    end       ##################################

    ###############################################增加的语句######################################
    for json_file in json_list:
        file_name = json_file.split('\\')[-1].split('.')[0]
        ###############################################    end       ##################################

        ###############################################  删除的语句  ##################################
        # if args.out is None:
        #     out_dir = osp.basename(json_file).replace('.', '_')
        #     out_dir = osp.join(osp.dirname(json_file), out_dir)
        # else:
        #     out_dir = args.out
        # if not osp.exists(out_dir):
        #     os.mkdir(out_dir)
        ###############################################    end       ##################################

        if not osp.exists(out_dir_img):
            os.mkdir(out_dir_img)
        if not osp.exists(out_dir_label):
            os.mkdir(out_dir_label)

        data = json.load(open(json_file))

        if data['imageData']:
            imageData = data['imageData']
        else:
            imagePath = os.path.join(os.path.dirname(json_file),
                                     data['imagePath'])
            with open(imagePath, 'rb') as f:
                imageData = f.read()
                imageData = base64.b64encode(imageData).decode('utf-8')
        img = utils.img_b64_to_arr(imageData)

        label_name_to_value = {'_background_': 0}
        for shape in sorted(data['shapes'], key=lambda x: x['label']):
            label_name = shape['label']
            if label_name in label_name_to_value:
                label_value = label_name_to_value[label_name]
            else:
                label_value = len(label_name_to_value)
                label_name_to_value[label_name] = label_value
        lbl = utils.shapes_to_label(img.shape, data['shapes'],
                                    label_name_to_value)

        label_names = [None] * (max(label_name_to_value.values()) + 1)

        PIL.Image.fromarray(img).save(osp.join(out_dir_img,
                                               file_name + '.png'))
        utils.lblsave(osp.join(out_dir_label, file_name + '.png'), lbl)
        ###############################################  删除的语句  ##################################
        # for name, value in label_name_to_value.items():
        #     label_names[value] = name
        # lbl_viz = utils.draw_label(lbl, img, label_names)
        # PIL.Image.fromarray(lbl_viz).save(osp.join(out_dir,file_name+'label_viz.png'))
        # with open(osp.join(out_dir, file_name+'label_names.txt'), 'w') as f:
        #     for lbl_name in label_names:
        #         f.write(lbl_name + '\n')

        # warnings.warn('info.yaml is being replaced by label_names.txt')
        # info = dict(label_names=label_names)
        # with open(osp.join(out_dir, file_name+'info.yaml'), 'w') as f:
        #     yaml.safe_dump(info, f, default_flow_style=False)
        # print('Saved to: %s' % out_dir)
        ###############################################  end  ##################################
        print('Saved to: %s  ,  %s ' % (out_dir_img, out_dir_label))
def main():
    # logger.warning('This script is aimed to demonstrate how to convert the'
    #                'JSON file to a single image dataset, and not to handle'
    #                'multiple JSON files to generate a real-use dataset.')
    #
    # parser = argparse.ArgumentParser()
    # parser.add_argument('json_file')
    # parser.add_argument('-o', '--out', default=None)
    # args = parser.parse_args()

    # json_file = args.json_file
    #
    # if args.out is None:
    #     out_dir = osp.basename(json_file).replace('.', '_')
    #     out_dir = osp.join(osp.dirname(json_file), out_dir)
    # else:
    #     out_dir = args.out
    json_dir = r"F:\pycharm_data\dataset\190423_maskrcnn_for_citie\20190418jpg\maskrcnn_datasets\1_scratch\scratch_json"
    json_list = os.listdir(json_dir)
    print(json_list)
    for json_file in json_list:

        json_file = json_dir + "\\" + json_file
        out_dir = osp.basename(json_file).replace('.', '_')
        print(out_dir)
        base_name = out_dir
        out_dir = osp.join(osp.dirname(json_file), out_dir)
        print(out_dir)
        if not osp.exists(out_dir):
            os.mkdir(out_dir)

        data = json.load(open(json_file))

        if data['imageData']:
            imageData = data['imageData']
        else:
            imagePath = os.path.join(os.path.dirname(json_file),
                                     data['imagePath'])
            with open(imagePath, 'rb') as f:
                imageData = f.read()
                imageData = base64.b64encode(imageData).decode('utf-8')
        img = utils.img_b64_to_arr(imageData)

        label_name_to_value = {'_background_': 0}
        for shape in sorted(data['shapes'], key=lambda x: x['label']):
            label_name = shape['label']
            if label_name in label_name_to_value:
                label_value = label_name_to_value[label_name]
            else:
                label_value = len(label_name_to_value)
                label_name_to_value[label_name] = label_value

        # lbl = utils.shapes_to_label(img.shape, data['shapes'], label_name_to_value)
        lbl = utils.shapes_to_label(img.shape, data['shapes'],
                                    label_name_to_value)

        # label_names = [None] * (max(label_name_to_value.values()) + 1)
        label_names = [None] * (max(label_name_to_value.values()) + 1)

        # for name, value in label_name_to_value.items():
        #     label_names[value] = name
        # lbl_viz = utils.draw_label(lbl, img, label_names)
        for name, value in label_name_to_value.items():
            label_names[value] = name
        lbl_viz = utils.draw_label(lbl, img, label_names)

        print("0001", base_name)
        print("000", out_dir)
        print("0002", osp.join(out_dir, base_name + '.png'))
        print("112", osp.join(out_dir, 'label_viz.png'))
        print("224", osp.join(out_dir, out_dir + '.png'))

        PIL.Image.fromarray(img).save(osp.join(out_dir, 'img.png'))
        PIL.Image.fromarray(img).save(osp.join(out_dir, base_name + '.png'))

        #masks
        # utils.lblsave(osp.join(out_dir, 'label.png'), lbl) #mask
        utils.lblsave(osp.join(out_dir, base_name + '_mask.png'), lbl)

        #piz
        PIL.Image.fromarray(lbl_viz).save(osp.join(out_dir,
                                                   'label_viz.png'))  #mask
        # PIL.Image.fromarray(lbl_viz).save(osp.join(out_dir, base_name+'label_viz.png'))

        with open(osp.join(out_dir, 'label_names.txt'), 'w') as f:
            for lbl_name in label_names:
                f.write(lbl_name + '\n')

        # logger.warning('info.yaml is being replaced by label_names.txt')
        logger.warning('info.yaml is being replaced by label_names.txt')

        # info = dict(label_names=label_names)
        info = dict(label_names=label_names)

        with open(osp.join(out_dir, 'info.yaml'), 'w') as f:
            yaml.safe_dump(info, f, default_flow_style=False)

        logger.info('Saved to: {}'.format(out_dir))
Exemple #30
0
def json2png(json_file, lab2val={'_background_': 0}):

    #    json_file = args.json_file

    label_name_to_value = lab2val
    out_dir = osp.basename(json_file).replace('.', '_')
    out_dir = osp.join(osp.dirname(json_file), out_dir)
    out_png = osp.join(osp.dirname(json_file), 'png')
    out_pngviz = osp.join(osp.dirname(json_file), 'png_viz')
    out_pic = osp.join(osp.dirname(json_file), 'pic')

    if not osp.exists(out_dir):
        os.mkdir(out_dir)

    if not osp.exists(out_png):
        os.mkdir(out_png)

    if not osp.exists(out_pngviz):
        os.mkdir(out_pngviz)

    if not osp.exists(out_pic):
        os.mkdir(out_pic)

    data = json.load(open(json_file))

    if data['imageData']:
        imageData = data['imageData']
    else:
        imagePath = os.path.join(os.path.dirname(json_file), data['imagePath'])
        with open(imagePath, 'rb') as f:
            imageData = f.read()
            imageData = base64.b64encode(imageData).decode('utf-8')
    img = utils.img_b64_to_arr(imageData)

    for shape in sorted(data['shapes'], key=lambda x: x['label']):
        label_name = shape['label']
        if label_name in label_name_to_value:
            label_value = label_name_to_value[label_name]
        else:
            label_value = len(label_name_to_value)
            label_name_to_value[label_name] = label_value
    lbl = utils.shapes_to_label(img.shape, data['shapes'], label_name_to_value)
    label_names = [None] * (max(label_name_to_value.values()) + 1)
    for name, value in label_name_to_value.items():
        label_names[value] = name
    lbl_viz = utils.draw_label(lbl, img, label_names)

    PIL.Image.fromarray(img).save(
        osp.join(out_pic,
                 osp.basename(json_file).replace('.json', '') + '.jpg'))
    PIL.Image.fromarray(img).save(osp.join(out_dir, 'img.png'))
    PIL.Image.fromarray(lbl).save(
        osp.join(out_png,
                 osp.basename(json_file).replace('.json', '') + '.png'))

    #    utils.lblsave(osp.join(out_png, osp.basename(json_file).replace('.json', '')+'.png'), lbl)
    utils.lblsave(osp.join(out_dir, 'label.png'), lbl)
    PIL.Image.fromarray(lbl_viz).save(
        osp.join(out_pngviz,
                 osp.basename(json_file).replace('.json', '') + '.png'))
    PIL.Image.fromarray(lbl_viz).save(osp.join(out_dir, 'label_viz.png'))

    with open(osp.join(out_dir, 'label_names.txt'), 'w') as f:
        for lbl_name in label_names:
            f.write(lbl_name + '\n')

    logger.warning('info.yaml is being replaced by label_names.txt')
    info = dict(label_names=label_names)
    with open(osp.join(out_dir, 'info.yaml'), 'w') as f:
        yaml.safe_dump(info, f, default_flow_style=False)

    logger.info('Saved to: {}'.format(out_dir))
Exemple #31
0
def convert_single_img(i, json_file, save_dir):
    """
    将单个json文件转换为标签图像

    Args:
        i ([type]): [description]
        json_file (str): json文件
        out ([type]): [description]
    """
    # parser = argparse.ArgumentParser()
    # parser.add_argument("json_file")
    # parser.add_argument("-o", "--out", default=None)
    # args = parser.parse_args()

    # json_file = json_file

    if save_dir is None:
        out_dir = osp.basename(json_file).replace(".", "_")
        out_dir = osp.join(osp.dirname(json_file), out_dir)
    if not osp.exists(save_dir):
        os.mkdir(save_dir)
    train_dir = osp.join(save_dir, 'train')
    if not osp.exists(train_dir):
        os.mkdir(train_dir)
    label_dir = osp.join(save_dir, 'label')
    if not osp.exists(label_dir):
        os.mkdir(label_dir)

    data = json.load(open(json_file))
    imageData = data.get("imageData")

    if not imageData:
        imagePath = os.path.join(os.path.dirname(json_file), data["imagePath"])
        with open(imagePath, "rb") as f:
            imageData = f.read()
            imageData = base64.b64encode(imageData).decode("utf-8")
    img = utils.img_b64_to_arr(imageData)

    label_name_to_value = {"_background_": 0}
    for shape in sorted(data["shapes"], key=lambda x: x["label"]):
        label_name = shape["label"]
        if label_name in label_name_to_value:
            label_value = label_name_to_value[label_name]
        else:
            label_value = len(label_name_to_value)
            label_name_to_value[label_name] = label_value
    lbl, _ = utils.shapes_to_label(img.shape, data["shapes"],
                                   label_name_to_value)

    label_names = [None] * (max(label_name_to_value.values()) + 1)
    for name, value in label_name_to_value.items():
        label_names[value] = name

    # lbl_viz = imgviz.label2rgb(label=lbl,
    #                            img=imgviz.asgray(img),
    #                            label_names=label_names,
    #                            loc="rb")

    PIL.Image.fromarray(img).save(osp.join(train_dir, f"{i}.png"))
    utils.lblsave(osp.join(label_dir, f"{i}.png"), lbl)
    # PIL.Image.fromarray(lbl_viz).save(osp.join(out_dir, "label_viz.png"))
    label_name_file = osp.join(save_dir, "label_names.txt")
    if not os.path.isfile(label_name_file):
        with open(label_name_file, "w") as f:
            for lbl_name in label_names:
                f.write(lbl_name + "\n")

    logger.info("Saved to: {}".format(save_dir))