def export(mesh_file, agg_file, seg_file, label_map_file, type, output_file): label_map = util.read_label_mapping(opt.label_map_file, label_from='raw_category', label_to='nyu40id') mesh_vertices = util_3d.read_mesh_vertices(mesh_file) object_id_to_segs, label_to_segs = read_aggregation(agg_file) seg_to_verts, num_verts = read_segmentation(seg_file) label_ids = np.zeros(shape=(num_verts), dtype=np.uint32) # 0: unannotated for label, segs in label_to_segs.items(): label_id = label_map[label] for seg in segs: verts = seg_to_verts[seg] label_ids[verts] = label_id if type == 'label': util_3d.export_ids(output_file, label_ids) elif type == 'instance': instance_ids = np.zeros(shape=(num_verts), dtype=np.uint32) # 0: unannotated for object_id, segs in object_id_to_segs.items(): for seg in segs: verts = seg_to_verts[seg] instance_ids[verts] = object_id util_3d.export_ids(output_file, label_ids * 1000 + instance_ids) #util_3d.export_instance_ids_for_eval(output_file, label_ids, instance_ids) else: raise
def export(mesh_file, agg_file, seg_file, label_map_file, label_map_file2, type, output_file): label_map = util.read_label_mapping(opt.label_map_file, label_from='raw_category', label_to='nyu40id') label_map2 = load_mapping(label_map_file2) label_map2[0] = 0 mesh_vertices = util_3d.read_mesh_vertices(mesh_file) object_id_to_segs, label_to_segs = read_aggregation(agg_file) seg_to_verts, num_verts = read_segmentation(seg_file) label_ids = np.zeros(shape=(num_verts), dtype=np.uint32) # 0: unannotated for label, segs in label_to_segs.items(): label_id = label_map[label] for seg in segs: verts = seg_to_verts[seg] label_ids[verts] = label_id if type == 'label': util_3d.export_ids(output_file, label_ids) elif type == 'instance': instance_ids = np.zeros(shape=(num_verts), dtype=np.uint32) # 0: unannotated for object_id, segs in object_id_to_segs.items(): for seg in segs: verts = seg_to_verts[seg] instance_ids[verts] = object_id assert len(instance_ids) == len(label_ids) for i in range(len(instance_ids)): import ipdb ipdb.set_trace() instance_ids[i] = label_map2[label_ids[i]] * 1000 + instance_ids[i] util_3d.export_ids(output_file, instance_ids) else: raise
def main(): image = np.array(imageio.imread(opt.input_file)) label_map = util.read_label_mapping(opt.label_map_file, label_from='id', label_to='nyu40id') mapped_image = map_label_image(image, label_map) imageio.imwrite(opt.output_file, mapped_image)
def main(): instance_image = np.array(imageio.imread(opt.input_instance_file)) label_image = np.array(imageio.imread(opt.input_label_file)) label_map = util.read_label_mapping(opt.label_map_file, label_from='id', label_to='nyu40id') mapped_label = map_label_image(label_image, label_map) output_instance_image = make_instance_image(mapped_label, instance_image) imageio.imwrite(opt.output_file, output_instance_image)
def main(): if not os.path.exists(opt.output_path): os.makedirs(opt.output_path) label_mapping = None if opt.export_label_images: label_map = util.read_label_mapping(opt.label_map_file, label_from='id', label_to='nyu40id') scenes = [d for d in os.listdir(opt.scannet_path) if os.path.isdir(os.path.join(opt.scannet_path, d))] print('Found %d scenes' % len(scenes)) for i in range(len(scenes)): sens_file = os.path.join(opt.scannet_path, scenes[i], scenes[i] + '.sens') label_path = os.path.join(opt.scannet_path, scenes[i], opt.label_type) if opt.export_label_images and not os.path.isdir(label_path): print_error('Error: using export_label_images option but label path %s does not exist' % label_path) output_color_path = os.path.join(opt.output_path, scenes[i], 'color') if not os.path.isdir(output_color_path): os.makedirs(output_color_path) output_depth_path = os.path.join(opt.output_path, scenes[i], 'depth') if not os.path.isdir(output_depth_path): os.makedirs(output_depth_path) output_pose_path = os.path.join(opt.output_path, scenes[i], 'pose') if not os.path.isdir(output_pose_path): os.makedirs(output_pose_path) output_label_path = os.path.join(opt.output_path, scenes[i], 'label') if opt.export_label_images and not os.path.isdir(output_label_path): os.makedirs(output_label_path) # read and export sys.stdout.write('\r[ %d | %d ] %s\tloading...' % ((i + 1), len(scenes), scenes[i])) sys.stdout.flush() sd = SensorData(sens_file) sys.stdout.write('\r[ %d | %d ] %s\texporting...' % ((i + 1), len(scenes), scenes[i])) sys.stdout.flush() sd.export_color_images(output_color_path, image_size=[opt.output_image_height, opt.output_image_width], frame_skip=opt.frame_skip) sd.export_depth_images(output_depth_path, image_size=[opt.output_image_height, opt.output_image_width], frame_skip=opt.frame_skip) sd.export_poses(output_pose_path, frame_skip=opt.frame_skip) if opt.export_label_images: for f in range(0, len(sd.frames), opt.frame_skip): label_file = os.path.join(label_path, str(f) + '.png') image = np.array(imageio.imread(label_file)) image = sktf.resize(image, [opt.output_image_height, opt.output_image_width], order=0, preserve_range=True) mapped_image = map_label_image(image, label_map) imageio.imwrite(os.path.join(output_label_path, str(f) + '.png'), mapped_image) print('')
def main(): if not os.path.exists(opt.output_path): os.makedirs(opt.output_path) label_mapping = None if opt.export_label_images: label_map = util.read_label_mapping(opt.label_map_file, label_from='id', label_to='nyu40id') scenes = [d for d in os.listdir(opt.scannet_path) if os.path.isdir(os.path.join(opt.scannet_path, d))] print('Found %d scenes' % len(scenes)) print(scenes[:5]) # for i in range(len(scenes)): combined_x = [(scenes[i], i, len(scenes), label_map) for i in range(len(scenes))] with Pool(processes=32, initializer=randomize) as pool: # _ = list(tqdm(pool.imap_unordered(process, zip(scenes, range(len(scenes)), [len(scenes)]*len(scenes))), total=len(scenes))) _ = list(tqdm(pool.imap_unordered(process, combined_x), total=len(scenes))) print('')
'--scan_path', required=True, help='path to scannet scene (e.g., data/ScanNet/v2/scene0000_00') parser.add_argument('--output_file', required=True, help='output file') parser.add_argument( '--label_map_file', default='/gruvi/Data/chenliu/ScanNet/tasks/scannetv2-labels.combined.tsv', help='path to scannetv2-labels.combined.tsv') parser.add_argument('--type', default='instance', help='task type [label or instance]') opt = parser.parse_args() assert opt.type in TASK_TYPES label_map = util.read_label_mapping(opt.label_map_file, label_from='raw_category', label_to='nyu40id') # remapper=np.ones(150)*(-100) # for i,x in enumerate([1,2,3,4,5,6,7,8,9,10,11,12,14,16,24,28,33,34,36,39]): # remapper[x]=i def read_aggregation(filename): assert os.path.isfile(filename) object_id_to_segs = {} label_to_segs = {} with open(filename) as f: data = json.load(f) num_objects = len(data['segGroups']) for i in range(num_objects): object_id = data['segGroups'][i][
def main(): if not os.path.exists(opt.output_path): os.makedirs(opt.output_path) label_mapping = None if opt.export_label_images: label_map = util.read_label_mapping(opt.label_map_file, label_from='id', label_to='nyu40id') scenes = [d for d in os.listdir(opt.scannet_path) if os.path.isdir(os.path.join(opt.scannet_path, d))] print('Found %d scenes' % len(scenes)) for i in range(len(scenes)): sens_file = os.path.join(opt.scannet_path, scenes[i], scenes[i] + '.sens') label_path = os.path.join(opt.scannet_path, scenes[i], opt.label_type) if opt.export_label_images and not os.path.isdir(label_path): print_error('Error: using export_label_images option but label path %s does not exist' % label_path) # Variable to indicate what kind of sensor data is to be extracted reextract = [] output_color_path = os.path.join(opt.output_path, scenes[i], 'color') if not os.path.isdir(output_color_path): os.makedirs(output_color_path) reextract.append('color') output_depth_path = os.path.join(opt.output_path, scenes[i], 'depth') if not os.path.isdir(output_depth_path): os.makedirs(output_depth_path) reextract.append('depth') output_pose_path = os.path.join(opt.output_path, scenes[i], 'pose') if not os.path.isdir(output_pose_path): os.makedirs(output_pose_path) reextract.append('pose') output_label_path = os.path.join(opt.output_path, scenes[i], 'label') if opt.export_label_images and not os.path.isdir(output_label_path): os.makedirs(output_label_path) reextract.append('label') # If we only have to extract 'label', we can skip exporting sensor data # We can also continue to the next scene if we do not have to reextract anything if (len(reextract) == 1 and reextract[0] == 'label') or reextract == []: sys.stdout.write('\r[ %d | %d ] %s\tskipping sensordata...' % ((i+1), len(scenes), scenes[i])) sys.stdout.flush() if reextract == []: continue # read and export sys.stdout.write('\r[ %d | %d ] %s\tloading...' % ((i + 1), len(scenes), scenes[i])) sys.stdout.flush() print(sens_file) sd = SensorData(sens_file) else: sys.stdout.write('\r[ %d | %d ] %s\texporting...' % ((i + 1), len(scenes), scenes[i])) sys.stdout.flush() sd.export_color_images(output_color_path, image_size=[opt.output_image_height, opt.output_image_width], frame_skip=opt.frame_skip) sd.export_depth_images(output_depth_path, image_size=[opt.output_image_height, opt.output_image_width], frame_skip=opt.frame_skip) sd.export_poses(output_pose_path, frame_skip=opt.frame_skip) if opt.export_label_images: for f in range(0, len(sd.frames), opt.frame_skip): label_file = os.path.join(label_path, str(f) + '.png') image = np.array(imageio.imread(label_file)) image = sktf.resize(image, [opt.output_image_height, opt.output_image_width], order=0, preserve_range=True) mapped_image = map_label_image(image, label_map) imageio.imwrite(os.path.join(output_label_path, str(f) + '.png'), mapped_image) print('')