def output(self, ordered_keys=None): """ Arguments: ordered_keys(list(str)) """ for filename in tqdm(glob.glob(osp.join(self.input_dir, "*.json"))): # print("Generating dataset from:", filename) label_file = labelme.LabelFile(filename=filename) base = osp.splitext(osp.basename(filename))[0] out_img_file = osp.join(self.output_dir, "JPEGImages", base + ".jpg") out_lbl_file = osp.join(self.output_dir, "SegmentationClass", base + ".npy") out_png_file = osp.join(self.output_dir, "SegmentationClassPNG", base + ".png") if not self.noviz: out_viz_file = osp.join( self.output_dir, "SegmentationClassVisualization", base + ".jpg", ) with open(out_img_file, "wb") as f: f.write(label_file.imageData) img = labelme.utils.img_data_to_arr(label_file.imageData) if ordered_keys is not None: newshapes = [] for ok in ordered_keys: for shape in label_file.shapes: if shape["label"] == ok: newshapes.append(shape) label_file.shapes = newshapes if self.debug: print(label_file.shapes) break lbl, _ = labelme.utils.shapes_to_label( img_shape=img.shape, shapes=label_file.shapes, label_name_to_value=self.class_name_to_id, ) labelme.utils.lblsave(out_png_file, lbl) # np.save(out_lbl_file, lbl) if not self.noviz: if img.shape[0] == 1: # gray img img = imgviz.rgb2gray(img) viz = imgviz.label2rgb( label=lbl, # img=imgviz.rgb2gray(img), img=img, font_size=15, label_names=self.class_names, loc="rb", ) imgviz.io.imsave(out_viz_file, viz)
def main(gpu=-1): print("gpu:", gpu) # uint8 img = scipy.misc.face() H, W = img.shape[:2] img_org = img.copy() if gpu >= 0: img = cuda.to_gpu(img) img = resize_image(img, (2 * H, 2 * W), order="HWC") img = cuda.to_cpu(img) assert img.shape == (2 * H, 2 * W, 3) plt.figure() plt.subplot(121) plt.imshow(img_org) plt.subplot(122) plt.imshow(img) plt.suptitle("uint8") plt.tight_layout() # float32 img = scipy.misc.face() img_org = img.copy() img = img.astype(np.float32) / 255 if gpu >= 0: img = cuda.to_gpu(img) img = resize_image(img, (2 * H, 2 * W), order="HWC") img = cuda.to_cpu(img) img = (img * 255).round().astype(np.uint8) assert img.shape == (2 * H, 2 * W, 3) plt.figure() plt.subplot(121) plt.imshow(img_org) plt.subplot(122) plt.imshow(img) plt.suptitle("float32") plt.tight_layout() # bool img = scipy.misc.face() gray = imgviz.rgb2gray(img) mask = gray > 127 mask_org = mask.copy() if gpu >= 0: mask = cuda.to_gpu(mask) mask = cuda.to_cpu(mask) mask = resize_image(mask, (2 * H, 2 * W), order="HW") plt.figure() plt.subplot(121) plt.imshow(mask_org, cmap="gray") plt.subplot(122) plt.imshow(mask, cmap="gray") plt.suptitle("bool") plt.tight_layout() plt.show()
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)) 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.rgb2gray(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(): 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.rgb2gray(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(): parser = argparse.ArgumentParser( formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument('input_dir', help='input annotated directory') parser.add_argument('output_dir', help='output dataset directory') parser.add_argument('--labels', help='labels file', required=True) parser.add_argument('--noviz', help='no visualization', action='store_true') args = parser.parse_args() if osp.exists(args.output_dir): print('Output directory already exists:', args.output_dir) sys.exit(1) os.makedirs(args.output_dir) os.makedirs(osp.join(args.output_dir, 'JPEGImages')) os.makedirs(osp.join(args.output_dir, 'SegmentationClass')) os.makedirs(osp.join(args.output_dir, 'SegmentationClassPNG')) if not args.noviz: os.makedirs(osp.join(args.output_dir, 'SegmentationClassVisualization')) print('Creating dataset:', args.output_dir) class_names = [] class_name_to_id = {} for i, line in enumerate(open(args.labels).readlines()): class_id = i - 1 # starts with -1 class_name = line.strip() class_name_to_id[class_name] = class_id if class_id == -1: assert class_name == '__ignore__' continue elif class_id == 0: assert class_name == '_background_' class_names.append(class_name) class_names = tuple(class_names) print('class_names:', class_names) out_class_names_file = osp.join(args.output_dir, 'class_names.txt') with open(out_class_names_file, 'w') as f: f.writelines('\n'.join(class_names)) print('Saved class_names:', out_class_names_file) for filename in glob.glob(osp.join(args.input_dir, '*.json')): print('Generating dataset from:', filename) label_file = labelme.LabelFile(filename=filename) base = osp.splitext(osp.basename(filename))[0] out_img_file = osp.join(args.output_dir, 'JPEGImages', base + '.jpg') out_lbl_file = osp.join(args.output_dir, 'SegmentationClass', base + '.npy') out_png_file = osp.join(args.output_dir, 'SegmentationClassPNG', base + '.png') if not args.noviz: out_viz_file = osp.join( args.output_dir, 'SegmentationClassVisualization', base + '.jpg', ) with open(out_img_file, 'wb') as f: f.write(label_file.imageData) img = labelme.utils.img_data_to_arr(label_file.imageData) lbl = labelme.utils.shapes_to_label( img_shape=img.shape, shapes=label_file.shapes, label_name_to_value=class_name_to_id, ) labelme.utils.lblsave(out_png_file, lbl) np.save(out_lbl_file, lbl) if not args.noviz: viz = imgviz.label2rgb( label=lbl, img=imgviz.rgb2gray(img), font_size=15, label_names=class_names, loc='rb', ) imgviz.io.imsave(out_viz_file, viz)
def main(): parser = argparse.ArgumentParser( formatter_class=argparse.ArgumentDefaultsHelpFormatter ) parser.add_argument("input_dir", help="input annotated directory") parser.add_argument("output_dir", help="output dataset directory") parser.add_argument("--labels", help="labels file", required=True) parser.add_argument( "--noviz", help="no visualization", action="store_true" ) args = parser.parse_args() # Ignore output directory existance check ''' if osp.exists(args.output_dir): print("Output directory already exists:", args.output_dir) sys.exit(1) ''' os.makedirs(args.output_dir) os.makedirs(osp.join(args.output_dir, "JPEGImages")) os.makedirs(osp.join(args.output_dir, "masks")) os.makedirs(osp.join(args.output_dir, "SegmentationClass")) os.makedirs(osp.join(args.output_dir, "SegmentationClassPNG")) if not args.noviz: os.makedirs( osp.join(args.output_dir, "SegmentationClassVisualization") ) os.makedirs(osp.join(args.output_dir, "SegmentationObject")) os.makedirs(osp.join(args.output_dir, "SegmentationObjectPNG")) if not args.noviz: os.makedirs( osp.join(args.output_dir, "SegmentationObjectVisualization") ) print("Creating dataset:", args.output_dir) class_names = [] class_name_to_id = {} for i, line in enumerate(open(args.labels).readlines()): class_id = i - 1 # starts with -1 class_name = line.strip() class_name_to_id[class_name] = class_id if class_id == -1: assert class_name == "__ignore__" continue elif class_id == 0: assert class_name == "_background_" class_names.append(class_name) class_names = tuple(class_names) print("class_names:", class_names) out_class_names_file = osp.join(args.output_dir, "class_names.txt") with open(out_class_names_file, "w") as f: f.writelines("\n".join(class_names)) print("Saved class_names:", out_class_names_file) for filename in glob.glob(osp.join(args.input_dir, "*.json")): print("Generating dataset from:", filename) label_file = labelme.LabelFile(filename=filename) base = osp.splitext(osp.basename(filename))[0] out_img_file = osp.join(args.output_dir, "JPEGImages", base + ".jpg") out_cls_file = osp.join( args.output_dir, "SegmentationClass", base + ".npy" ) out_clsp_file = osp.join( args.output_dir, "SegmentationClassPNG", base + ".png" ) if not args.noviz: out_clsv_file = osp.join( args.output_dir, "SegmentationClassVisualization", base + ".jpg", ) out_ins_file = osp.join( args.output_dir, "SegmentationObject", base + ".npy" ) out_insp_file = osp.join( args.output_dir, "SegmentationObjectPNG", base + ".png" ) if not args.noviz: out_insv_file = osp.join( args.output_dir, "SegmentationObjectVisualization", base + ".jpg", ) img = labelme.utils.img_data_to_arr(label_file.imageData) imgviz.io.imsave(out_img_file, img) cls, ins = labelme.utils.shapes_to_label( filename, img_shape=img.shape, shapes=label_file.shapes, label_name_to_value=class_name_to_id, ) ins[cls == -1] = 0 # ignore it. # class label labelme.utils.lblsave(out_clsp_file, cls) np.save(out_cls_file, cls) if not args.noviz: clsv = imgviz.label2rgb( label=cls, img=imgviz.rgb2gray(img), label_names=class_names, font_size=15, loc="rb", ) imgviz.io.imsave(out_clsv_file, clsv) # instance label labelme.utils.lblsave(out_insp_file, ins) np.save(out_ins_file, ins) if not args.noviz: instance_ids = np.unique(ins) instance_names = [str(i) for i in range(max(instance_ids) + 1)] insv = imgviz.label2rgb( label=ins, img=imgviz.rgb2gray(img), label_names=instance_names, font_size=15, loc="rb", ) imgviz.io.imsave(out_insv_file, insv)
def setUp(self): self.image_uint8 = scipy.misc.face() self.image_float32 = self.image_uint8.astype(np.float32) / 255 self.image_bool = imgviz.rgb2gray(self.image_uint8) > 127
def main(): parser = argparse.ArgumentParser( formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument("--input_dir", default="data_annotated", help="input annotated directory") parser.add_argument("--output_dir", default="data_dataset_voc", help="output dataset directory") parser.add_argument("--labels", default="labels.txt", help="labels file") parser.add_argument("--noviz", help="no visualization", action="store_true") args = parser.parse_args() if osp.exists(args.output_dir): print("Output directory already exists:", args.output_dir) sys.exit(1) os.makedirs(args.output_dir) os.makedirs(osp.join(args.output_dir, "JPEGImages")) os.makedirs(osp.join(args.output_dir, "SegmentationClass")) os.makedirs(osp.join(args.output_dir, "SegmentationClassPNG")) if not args.noviz: os.makedirs(osp.join(args.output_dir, "SegmentationClassVisualization")) print("Creating dataset:", args.output_dir) class_names = [] class_name_to_id = {} for i, line in enumerate(open(args.labels).readlines()): class_id = i - 1 # starts with -1 class_name = line.strip() class_name_to_id[class_name] = class_id if class_id == -1: assert class_name == "__ignore__" continue elif class_id == 0: assert class_name == "_background_" class_names.append(class_name) class_names = tuple(class_names) print("class_names:", class_names) out_class_names_file = osp.join(args.output_dir, "class_names.txt") with open(out_class_names_file, "w") as f: f.writelines("\n".join(class_names)) print("Saved class_names:", out_class_names_file) for filename in glob.glob(osp.join(args.input_dir, "*.json")): print("Generating dataset from:", filename) label_file = labelme.LabelFile(filename=filename) base = osp.splitext(osp.basename(filename))[0] out_img_file = osp.join(args.output_dir, "JPEGImages", base + ".jpg") out_lbl_file = osp.join(args.output_dir, "SegmentationClass", base + ".npy") out_png_file = osp.join(args.output_dir, "SegmentationClassPNG", base + ".png") if not args.noviz: out_viz_file = osp.join( args.output_dir, "SegmentationClassVisualization", base + ".jpg", ) with open(out_img_file, "wb") as f: f.write(label_file.imageData) img = labelme.utils.img_data_to_arr(label_file.imageData) lbl, _ = labelme.utils.shapes_to_label( img_shape=img.shape, shapes=label_file.shapes, label_name_to_value=class_name_to_id, ) labelme.utils.lblsave(out_png_file, lbl) np.save(out_lbl_file, lbl) if not args.noviz: viz = imgviz.label2rgb( label=lbl, img=imgviz.rgb2gray(img), font_size=15, label_names=class_names, loc="rb", ) imgviz.io.imsave(out_viz_file, viz)
def json_to_image(json_file, out_dir, viz_image = True, label_image = False): # Load json data = json.load(open(json_file)) imageData = data.get('imageData') # Convert image to array 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 = img_b64_to_arr(imageData) # print("The dimension of image is {}".format(img.ndim)) # Generate labels 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 = shapes_to_label(img.shape, data['shapes'], label_name_to_value) # Generate label names label_names = [None] * (max(label_name_to_value.values()) + 1) for name, value in label_name_to_value.items(): label_names[value] = name # RGB or Gray images if img.ndim == 3: lbl_viz = imgviz.label2rgb( label=lbl, img=imgviz.rgb2gray(img), label_names=label_names, loc='rb') elif img.ndim == 2: lbl_viz = imgviz.label2rgb( label=lbl, img=img, label_names=label_names, loc='rb') # Save raw image from Json structure # PIL.Image.fromarray(img).save(osp.join(out_dir, json_file.split('\\')[-1].split('.')[0] + '_img.png')) # PIL.Image.fromarray(img).save(osp.join(out_dir, json_file.split('\\')[-1].split('.')[0] + '_img.bmp')) # Save mask if label_image: # print(out_dir) # print(json_file) # print(json_file.split('\\')[-1].split('.')[0]) # print(json_file.split('\\')) # print('\n') # print(out_dir + json_file.split('\\')[-1].split('.')[0] + '_mask.png') # PATH = osp.join(out_dir, json_file.split('\\')[-1].split('.')[0] + '_mask.png') # print(PATH) # Premise, / is use, .json is the ext name filename = json_file.split('/')[-1] adjusted_filename = filename[0:-5] PATH = out_dir + adjusted_filename + '_mask.png' lblsave(PATH, lbl) # Save heatmap if viz_image: path = osp.join(out_dir, json_file.split('\\')[-1].split('.')[0] + '_viz.png') PIL.Image.fromarray(lbl_viz).save(path)
def main(): # Receive parameters parser = argparse.ArgumentParser() parser.add_argument('json_file') parser.add_argument('-o', '--out', default=None) args = parser.parse_args() # json_file and output folder name json_file = args.json_file # Generate folder from 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) # Load json 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 = img_b64_to_arr(imageData) # print("The dimension of image is {}".format(img.ndim)) # Generagte labels 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 = 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 # RGB or Gray images if img.ndim == 3: lbl_viz = imgviz.label2rgb( label=lbl, img=imgviz.rgb2gray(img), label_names=label_names, loc='rb') elif img.ndim == 2: lbl_viz = imgviz.label2rgb( label=lbl, img=img, label_names=label_names, loc='rb') # Visualize label images PIL.Image.fromarray(img).save(osp.join(out_dir, 'img.png')) args.json_file.split('.')[0] print(out_dir) lblsave(osp.join(out_dir, args.json_file.split('.')[0] + '_label.png'), lbl) PIL.Image.fromarray(lbl_viz).save(osp.join(out_dir, 'label_viz.png'))
def do_semantic_segmentation_voc(self): output_dir = self.output_directory.line_edit.text() input_dir = self.input_directory.line_edit.text() labels = self.labels_file.line_edit.text() noviz = not self.output_visualization.isChecked() if '' in [output_dir, input_dir, labels]: return if len(os.listdir(output_dir)) > 0: diag = QMessageBox() diag.setText("Output directory must be empty") diag.exec() return os.makedirs(osp.join(output_dir, "JPEGImages")) os.makedirs(osp.join(output_dir, "SegmentationClass")) os.makedirs(osp.join(output_dir, "SegmentationClassPNG")) if not noviz: os.makedirs( osp.join(output_dir, "SegmentationClassVisualization") ) class_names, class_name_to_id = self.retrieve_labels(labels) out_class_names_file = osp.join(output_dir, "class_names.txt") with open(out_class_names_file, "w") as f: f.writelines("\n".join(class_names)) for filename in glob.glob(osp.join(input_dir, "*.json")): label_file = labelme.LabelFile(filename=filename) base = osp.splitext(osp.basename(filename))[0] out_img_file = osp.join(output_dir, "JPEGImages", base + ".jpg") out_lbl_file = osp.join( output_dir, "SegmentationClass", base + ".npy" ) out_png_file = osp.join( output_dir, "SegmentationClassPNG", base + ".png" ) if not noviz: out_viz_file = osp.join( output_dir, "SegmentationClassVisualization", base + ".jpg", ) with open(out_img_file, "wb") as f: f.write(label_file.imageData) img = labelme.utils.img_data_to_arr(label_file.imageData) lbl, _ = labelme.utils.shapes_to_label( img_shape=img.shape, shapes=label_file.shapes, label_name_to_value=class_name_to_id, ) labelme.utils.lblsave(out_png_file, lbl) np.save(out_lbl_file, lbl) if not noviz: viz = imgviz.label2rgb( label=lbl, img=imgviz.rgb2gray(img), font_size=15, label_names=class_names, loc="rb", ) imgviz.io.imsave(out_viz_file, viz) self.hide()
def do_instance_segmentation_voc(self): output_dir = self.output_directory.line_edit.text() input_dir = self.input_directory.line_edit.text() labels = self.labels_file.line_edit.text() noviz = not self.output_visualization.isChecked() if '' in [output_dir, input_dir, labels]: return if len(os.listdir(output_dir)) > 0: diag = QMessageBox() diag.setText("Output directory must be empty") diag.exec() return os.makedirs(osp.join(output_dir, "JPEGImages")) os.makedirs(osp.join(output_dir, "SegmentationClass")) os.makedirs(osp.join(output_dir, "SegmentationClassPNG")) if not noviz: os.makedirs( osp.join(output_dir, "SegmentationClassVisualization") ) os.makedirs(osp.join(output_dir, "SegmentationObject")) os.makedirs(osp.join(output_dir, "SegmentationObjectPNG")) if not noviz: os.makedirs( osp.join(output_dir, "SegmentationObjectVisualization") ) class_names, class_name_to_id = self.retrieve_labels(labels) out_class_names_file = osp.join(output_dir, "class_names.txt") with open(out_class_names_file, "w") as f: f.writelines("\n".join(class_names)) for filename in glob.glob(osp.join(input_dir, "*.json")): label_file = labelme.LabelFile(filename=filename) base = osp.splitext(osp.basename(filename))[0] out_img_file = osp.join(output_dir, "JPEGImages", base + ".jpg") out_cls_file = osp.join( output_dir, "SegmentationClass", base + ".npy" ) out_clsp_file = osp.join( output_dir, "SegmentationClassPNG", base + ".png" ) if not noviz: out_clsv_file = osp.join( output_dir, "SegmentationClassVisualization", base + ".jpg", ) out_ins_file = osp.join( output_dir, "SegmentationObject", base + ".npy" ) out_insp_file = osp.join( output_dir, "SegmentationObjectPNG", base + ".png" ) if not noviz: out_insv_file = osp.join( output_dir, "SegmentationObjectVisualization", base + ".jpg", ) img = labelme.utils.img_data_to_arr(label_file.imageData) imgviz.io.imsave(out_img_file, img) cls, ins = labelme.utils.shapes_to_label( img_shape=img.shape, shapes=label_file.shapes, label_name_to_value=class_name_to_id, ) ins[cls == -1] = 0 # ignore it. # class label labelme.utils.lblsave(out_clsp_file, cls) np.save(out_cls_file, cls) if not noviz: clsv = imgviz.label2rgb( label=cls, img=imgviz.rgb2gray(img), label_names=class_names, font_size=15, loc="rb", ) imgviz.io.imsave(out_clsv_file, clsv) # instance label labelme.utils.lblsave(out_insp_file, ins) np.save(out_ins_file, ins) if not noviz: instance_ids = np.unique(ins) instance_names = [str(i) for i in range(max(instance_ids) + 1)] insv = imgviz.label2rgb( label=ins, img=imgviz.rgb2gray(img), label_names=instance_names, font_size=15, loc="rb", ) imgviz.io.imsave(out_insv_file, insv) self.hide()
def main(): parser = argparse.ArgumentParser( formatter_class=argparse.ArgumentDefaultsHelpFormatter ) parser.add_argument('input_dir', help='input annotated directory') parser.add_argument('output_dir', help='output dataset directory') parser.add_argument('--labels', help='labels file', required=True) parser.add_argument( '--noviz', help='no visualization', action='store_true' ) args = parser.parse_args() if osp.exists(args.output_dir): print('Output directory already exists:', args.output_dir) sys.exit(1) os.makedirs(args.output_dir) os.makedirs(osp.join(args.output_dir, 'JPEGImages')) os.makedirs(osp.join(args.output_dir, 'SegmentationClass')) os.makedirs(osp.join(args.output_dir, 'SegmentationClassPNG')) if not args.noviz: os.makedirs( osp.join(args.output_dir, 'SegmentationClassVisualization') ) os.makedirs(osp.join(args.output_dir, 'SegmentationObject')) os.makedirs(osp.join(args.output_dir, 'SegmentationObjectPNG')) if not args.noviz: os.makedirs( osp.join(args.output_dir, 'SegmentationObjectVisualization') ) print('Creating dataset:', args.output_dir) class_names = [] class_name_to_id = {} for i, line in enumerate(open(args.labels).readlines()): class_id = i - 1 # starts with -1 class_name = line.strip() class_name_to_id[class_name] = class_id if class_id == -1: assert class_name == '__ignore__' continue elif class_id == 0: assert class_name == '_background_' class_names.append(class_name) class_names = tuple(class_names) print('class_names:', class_names) out_class_names_file = osp.join(args.output_dir, 'class_names.txt') with open(out_class_names_file, 'w') as f: f.writelines('\n'.join(class_names)) print('Saved class_names:', out_class_names_file) for label_file in glob.glob(osp.join(args.input_dir, '*.json')): print('Generating dataset from:', label_file) with open(label_file) as f: base = osp.splitext(osp.basename(label_file))[0] out_img_file = osp.join( args.output_dir, 'JPEGImages', base + '.jpg') out_cls_file = osp.join( args.output_dir, 'SegmentationClass', base + '.npy') out_clsp_file = osp.join( args.output_dir, 'SegmentationClassPNG', base + '.png') if not args.noviz: out_clsv_file = osp.join( args.output_dir, 'SegmentationClassVisualization', base + '.jpg', ) out_ins_file = osp.join( args.output_dir, 'SegmentationObject', base + '.npy') out_insp_file = osp.join( args.output_dir, 'SegmentationObjectPNG', base + '.png') if not args.noviz: out_insv_file = osp.join( args.output_dir, 'SegmentationObjectVisualization', base + '.jpg', ) data = json.load(f) img_file = osp.join(osp.dirname(label_file), data['imagePath']) img = np.asarray(PIL.Image.open(img_file)) PIL.Image.fromarray(img).save(out_img_file) cls, ins = labelme.utils.shapes_to_label( img_shape=img.shape, shapes=data['shapes'], label_name_to_value=class_name_to_id, type='instance', ) ins[cls == -1] = 0 # ignore it. # class label labelme.utils.lblsave(out_clsp_file, cls) np.save(out_cls_file, cls) if not args.noviz: clsv = imgviz.label2rgb( label=cls, img=imgviz.rgb2gray(img), label_names=class_names, font_size=15, loc='rb', ) imgviz.io.imsave(out_clsv_file, clsv) # instance label labelme.utils.lblsave(out_insp_file, ins) np.save(out_ins_file, ins) if not args.noviz: instance_ids = np.unique(ins) instance_names = [str(i) for i in range(max(instance_ids) + 1)] insv = imgviz.label2rgb( label=ins, img=imgviz.rgb2gray(img), label_names=instance_names, font_size=15, loc='rb', ) imgviz.io.imsave(out_insv_file, insv)
def _process_frame(self, frame): meta = frame["meta"] color = frame["color"] depth = frame["depth"] depth_viz = imgviz.depth2rgb(depth, min_value=0, max_value=2) label = frame["label"] label_viz = imgviz.label2rgb(label) labels = meta["cls_indexes"].astype(np.int32) # NOTE: cls_mask is the same as ins_mask in YCB_Video_Dataset masks = np.asarray([label == cls_id for cls_id in labels]) bboxes = morefusion.geometry.masks_to_bboxes(masks) keep = ~(bboxes == 0).all(axis=1) labels = labels[keep] bboxes = bboxes[keep] masks = masks[keep] gray = imgviz.gray2rgb(imgviz.rgb2gray(color)) ins_viz = imgviz.instances2rgb(gray, labels=labels, bboxes=bboxes, masks=masks) vertmap = meta["vertmap"] vertmap[label == 0] = np.nan vert_viz_x = imgviz.depth2rgb(vertmap[:, :, 0]) vert_viz_y = imgviz.depth2rgb(vertmap[:, :, 1]) vert_viz_z = imgviz.depth2rgb(vertmap[:, :, 2]) roi_viz_color = [] roi_viz_depth = [] roi_viz_label = [] for bbox, mask in zip(bboxes, masks): y1, x1, y2, x2 = bbox.round().astype(int) mask_roi = mask[y1:y2, x1:x2] color_roi = color[y1:y2, x1:x2].copy() color_roi[~mask_roi] = 0 depth_roi = depth_viz[y1:y2, x1:x2].copy() depth_roi[~mask_roi] = 0 label_roi = label_viz[y1:y2, x1:x2].copy() label_roi[~mask_roi] = 0 roi_viz_color.append(color_roi) roi_viz_depth.append(depth_roi) roi_viz_label.append(label_roi) roi_viz_color = imgviz.tile(roi_viz_color, border=(255, 255, 255)) roi_viz_depth = imgviz.tile(roi_viz_depth, border=(255, 255, 255)) roi_viz_label = imgviz.tile(roi_viz_label, border=(255, 255, 255)) viz = imgviz.tile( [ color, depth_viz, label_viz, ins_viz, vert_viz_x, vert_viz_y, vert_viz_z, np.zeros_like(color), roi_viz_color, roi_viz_depth, roi_viz_label, np.zeros_like(roi_viz_color), ], shape=(3, 4), border=(255, 255, 255), ) viz = imgviz.centerize(viz, (1000, 1000)) return viz
for i, line in enumerate(open(label_path).readlines()): class_id = i - 1 # starts with -1 class_name = line.strip() class_name_to_id[class_name] = class_id if class_id == -1: assert class_name == "__ignore__" continue elif class_id == 0: assert class_name == "_background_" class_names.append(class_name) class_names = tuple(class_names) label_img = cv2.imread( "/home/luolu/Desktop/tmp_dataset/SegmentationClassPNG/bao1-16-1566_m002_s.png" ) src_img = cv2.imread( "/home/luolu/Desktop/tmp_dataset/JPEGImages/bao1-16-1566_m002_s.jpg") print("label_img shape:", label_img.shape) print("src_img.shape:", src_img.shape) viz = imgviz.label2rgb( label=label_img, img=imgviz.rgb2gray(src_img), font_size=25, label_names=class_names, loc="rb", ) out_viz_file = "/home/luolu/Desktop/temp" if __name__ == '__main__': imgviz.io.imsave(out_viz_file, viz)