def main(): parser = argparse.ArgumentParser() parser.add_argument('json_file') args = parser.parse_args() label_file = LabelFile(args.json_file) img = utils.img_data_to_arr(label_file.imageData) label_name_to_value = {'_background_': 0} for shape in sorted(label_file.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, label_file.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, font_size=30, loc='rb', ) plt.subplot(121) plt.imshow(img) plt.subplot(122) plt.imshow(lbl_viz) plt.show()
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))
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 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))
def my_json2dataset(json_file, lbl_names_all): ''' modified from labelme's json_to_dataset.py, 相比于原来的按照图中的标注来对label数字进行排序的方法,我直接指定了全部的label名称及其数字,这样的话所有标注的文件会统一,但是也只能适用于这个单一任务 @in -json_file -json文件 -lbl_names_all -所有的label的名称 ''' print(f'processing:{json_file}', end='') out_dir = osp.basename(json_file).replace(".", "_") out_dir = osp.join(osp.dirname(json_file), out_dir) 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 = labelme.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 label_name_to_value = {} for label_value, label_name in enumerate(lbl_names_all): label_name_to_value[label_name] = label_value lbl, _ = labelme.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")) labelme.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(' done')
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))
def make_label_visualize_image(self, json_file): label_file = LabelFile(json_file) img = utils.img_data_to_arr(label_file.imageData) label_name_to_value = {"_background_": 0} for shape in sorted(label_file.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, label_file.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, font_size=30, loc="rb", ) return lbl_viz fig = plt.figure() ax1 = fig.add_subplot(111) ax1.imshow(img) ax2 = fig.add_subplot(111) ax2.imshow(lbl_viz) fig.canvas.draw() data = np.fromstring(fig.canvas.tostring_rgb(), dtype=np.uint8, sep='') data = data.reshape(fig.canvas.get_width_height()[::-1] + (3,)) # plt.subplot(121) # plt.imshow(img) # plt.subplot(122) # plt.imshow(lbl_viz) # plt.show() # # plt.draw() return data
def main(): parser = argparse.ArgumentParser() parser.add_argument('json_file') args = parser.parse_args() json_file = args.json_file 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 = imgviz.label2rgb( label=lbl, img=imgviz.asgray(img), label_names=label_names, font_size=30, loc='rb', ) plt.subplot(121) plt.imshow(img) plt.subplot(122) plt.imshow(lbl_viz) plt.show()
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}")
def json2data(self, json_file): data = json.load(open(json_file)) imageData = data.get('imageData') 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') return img, lbl, lbl_viz
# 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")
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))
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")
f1_, acc_, mIoU_ = [], [], [] for batch in val_loader: imgs = batch['image'] true_mask = batch['mask'] idx = batch['id'] image = (np.squeeze(imgs.numpy()).transpose( (1, 2, 0)) * 255).astype(np.uint8) imgs = imgs.to(device=device, dtype=torch.float32) mask_pred = torch.squeeze(torch.max(net(imgs), 1)[1]).numpy().astype(np.uint8) true_mask_ = torch.squeeze(torch.max(true_mask, 1)[1]).numpy().astype(np.uint8) # 拉伸到一维计算mIoU true_mask_ = true_mask_.reshape(-1) mask_pred_ = mask_pred.reshape(-1) metric = EvaluationMetric() f1, acc, mIoU = metric.evaluate(true_mask_, mask_pred_) print(f'f1:{f1}; acc:{acc}; mIoU:{mIoU}') f1_.append(f1) acc_.append(acc) mIoU_.append(mIoU) lbl_viz = imgviz.label2rgb(label=mask_pred, img=imgviz.asgray(image), loc="rb") Image.fromarray(lbl_viz).save( f'./Dataset2/NestUnet_pred_dice/val/{idx[0]}.png') f1, acc, mIoU = np.mean(f1_), np.mean(acc_), np.mean(mIoU_) print(f'MEAN f1:{f1}; acc:{acc}; mIoU:{mIoU}')
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))
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))
def main(): logger.warning('This script is aimed to convert the ' 'JSON batch to gray map of DABNet format.') parser = argparse.ArgumentParser() parser.add_argument('--json-list', default=None) parser.add_argument('--label-file', default=None) args = parser.parse_args() # Load .json from list file if not osp.isfile(args.json_list): print("json_list doesn't existed!!") return with open(args.json_list, 'r') as f: json_files = f.readlines() json_files = [x.strip() for x in json_files] # Import label file if not osp.isfile(args.label_file): print("label_file doesn't existed!!") return label_name_to_value = importLabel(args.label_file) # main loop for i in range(0, len(json_files)): json_file = ''.join(json_files[i]) out_dir = json_file.split('.')[0] 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) # check label in json is in the label file or not 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 print(label_name, " is not in the label file") 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_gray(out_dir + '.png', lbl) PIL.Image.fromarray(lbl_viz).save(out_dir + '_viz.png') logger.info('Saved to: {}'.format(out_dir))
def js_to_output(json_file, out_dir=None): # 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." # ) # output directory if out_dir is None: out_dir = osp.basename(json_file).replace(".", "_") out_dir = osp.join(js_data_path, 'js_output', out_dir) else: out_dir = out_dir # create folder if not exist if not osp.exists(out_dir): os.mkdir(out_dir) data = json.load(open(osp.join(js_data_path, 'js_file', 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") 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))
def main(): logger.warning('This script is aimed to remap the ADE20K ' 'annotations to a customize annotation.') parser = argparse.ArgumentParser() parser.add_argument('--label-file', default="rtk") parser.add_argument('--remap-table', default="rtk") parser.add_argument('--image-list', default="training") parser.add_argument('--save-vizImage', default=False) parser.add_argument('--save-oriImage', default=False) # parser.add_argument('--save-colorLabImage', default=False) args = parser.parse_args() # Import label file label_file = 'labels_' + args.label_file + '.txt' label_name_to_value = importLabel(label_file) label_names = [None] * (max(label_name_to_value.values()) + 1) for name, value in label_name_to_value.items(): label_names[value] = name # print(value, " ", name) # Import remap table map_table = 'map_' + args.remap_table + '.txt' mapping = importRemapTable(map_table) # Output direction out_folder = osp.join('annotations_' + args.label_file, args.image_list) if not osp.exists(out_folder): os.makedirs(out_folder, exist_ok=True) logger.info('Saved to: {}'.format(out_folder)) # Load label image list label_list = importLabelList(args.image_list + '.txt') # Main loop for idx in range(0, len(label_list)): # load label image with open(label_list[idx], 'rb') as f: image_name = osp.split(label_list[idx])[1].split('.')[0] imageData = f.read() if not imageData: logger.info('Lebelled Image does not existed') break imageData = base64.b64encode(imageData).decode('utf-8') label_img = utils.img_b64_to_arr(imageData) label_img = remapLabel(label_img, mapping) utils.lblsave_gray(osp.join(out_folder, image_name + '.png'), label_img) # if args.save_colorLabImage: # utils.lblsave(osp.join(out_folder, 'label_color.png'), label_img) # load original image if args.save_oriImage or args.save_vizImage: image_list = label_list[idx].replace('annotations', 'images') image_list = image_list.replace('png', 'jpg') with open(image_list, 'rb') as f: imageData = f.read() if not imageData: logger.info('Original Color Image does not existed') args.save_oriImage = args.save_vizImage = False imageData = base64.b64encode(imageData).decode('utf-8') img = utils.img_b64_to_arr(imageData) if args.save_oriImage: PIL.Image.fromarray(img).save( osp.join(out_folder, image_name + '.jpg')) if args.save_vizImage: lbl_viz = imgviz.label2rgb(label=label_img, img=imgviz.asgray(img), label_names=label_names, loc='rb') PIL.Image.fromarray(lbl_viz).save( osp.join(out_folder, image_name + '_viz.png'))