def lblsave(filename: str, lbl: np.ndarray, colormap: np.ndarray = None): ''' save label file as an image with visible colors download from https://github.com/wkentaro/labelme/blob/master/labelme/utils/_io.py, @in -filename -file name to be save as -lbl -label file, in 2D format -colormap -colormap, Nx3 format ''' if os.path.splitext(filename)[1] != ".png": filename += ".png" # Assume label ranses [-1, 254] for int32, # and [0, 255] for uint8 as VOC. if lbl.min() >= -1 and lbl.max() < 255: lbl_pil = PIL.Image.fromarray(lbl.astype(np.uint8), mode="P") clrmap = imgviz.label_colormap().flatten() if colormap is not None: clrmap[0:colormap.size] = colormap.flatten() # colormap = np.pad(colormap, (0, 768-colormap.size), mode='constant') # print(clrmap.size) # print(np.unique(lbl)) lbl_pil.putpalette(clrmap) lbl_pil.save(filename) else: raise ValueError("[%s] Cannot save the pixel-wise class label as PNG. " "Please consider using the .npy format." % filename)
class VoxelGridsToMeshMarkers(topic_tools.LazyTransport): _colormap = imgviz.label_colormap() def __init__(self): super().__init__() self._pub = self.advertise("~output", MarkerArray, queue_size=1) self._tmp_dir = path.Path(tempfile.mkdtemp()) self._post_init() def __del__(self): self._tmp_dir.rmtree_p() def subscribe(self): self._sub = rospy.Subscriber( "~input", VoxelGridArray, self._callback, queue_size=1, buff_size=2 ** 24, ) def unsubscribe(self): self._sub.unregister() def _callback(self, grids_msg): markers_msg = MarkerArray() # marker = Marker( # header=grids_msg.header, # id=-1, # action=Marker.DELETEALL # ) # markers_msg.markers.append(marker) for grid in grids_msg.grids: mesh = grid_msg_to_mesh(grid) if mesh is None: continue color = self._colormap[grid.instance_id + 1] marker = Marker() marker.header = grids_msg.header marker.id = grid.instance_id marker.pose.orientation.w = 1 marker.scale.x = 1 marker.scale.y = 1 marker.scale.z = 1 marker.color.a = 1 marker.color.r = color[0] / 255 marker.color.g = color[1] / 255 marker.color.b = color[2] / 255 marker.type = Marker.TRIANGLE_LIST points = mesh.vertices[mesh.faces.flatten()] marker.points = [Point(*p) for p in points] marker.action = Marker.ADD markers_msg.markers.append(marker) self._pub.publish(markers_msg)
def imwrite(filename, image, mode='P'): import imgviz if image.min() >= -1 and image.max() <= 255: image = Image.fromarray(image.astype(np.uint8), mode=mode) colomap = imgviz.label_colormap() image.putpalette(colomap.flatten()) image.save(filename) else: raise ValueError( '[%s] Cannot save the pixel-wise class label as PNG. ' % filename)
def lblsave(filename, lbl): import imgviz if os.path.splitext(filename)[1] != ".png": filename += ".png" # Assume label ranses [-1, 254] for int32, # and [0, 255] for uint8 as VOC. if lbl.min() >= -1 and lbl.max() < 255: lbl_pil = Image.fromarray(lbl.astype(np.uint8), mode="P") colormap = imgviz.label_colormap() lbl_pil.putpalette(colormap.flatten()) lbl_pil.save(filename) else: raise ValueError("[%s] Cannot save the pixel-wise class label as PNG. " "Please consider using the .npy format." % filename)
def lblsave(lbl): # if osp.splitext(filename)[1] != ".png": # filename += ".png" # Assume label ranses [-1, 254] for int32, # and [0, 255] for uint8 as VOC. if lbl.min() >= -1 and lbl.max() < 255: lbl_pil = PIL.Image.fromarray(lbl.astype(np.uint8), mode="P") colormap = imgviz.label_colormap() lbl_pil.putpalette(colormap.flatten()) # lbl_pil.save(filename) lbl_array = np.array(lbl_pil) # cv2.imshow("label", lbl_array*10) # cv2.waitKey(0) return lbl_array else: raise ValueError("This Cannot save the pixel-wise class label as PNG. " "Please consider using the .npy format.")
def lblsave(filename, lbl): import imgviz if osp.splitext(filename)[1] != '.png': filename += '.png' # Assume label ranses [-1, 254] for int32, # and [0, 255] for uint8 as VOC. if lbl.min() >= 0 and lbl.max() < 255: lbl_pil = Image.fromarray(lbl.astype(np.uint8), mode='P') colormap = imgviz.label_colormap() lbl_pil.putpalette(colormap.flatten()) lbl_pil.save(filename) numpy_file = osp.splitext(filename)[0] + '.npy' np.save(numpy_file, lbl) else: raise ValueError('[%s] Cannot save the pixel-wise class label as PNG. ' 'Please consider using the .npy format.' % filename)
def lblsave_gray(filename, lbl): import imgviz from labelme import utils if osp.splitext(filename)[1] == '.png': filename = osp.splitext(filename)[0] # Assume label ranses [-1, 254] for int32, # and [0, 255] for uint8 as VOC. if lbl.min() >= -1 and lbl.max() < 255: lbl_pil = PIL.Image.fromarray(lbl.astype(np.uint8), mode='P') colormap = imgviz.label_colormap() graymap = utils.label_graymap() lbl_pil.putpalette(graymap.flatten()) lbl_pil.save(filename + '.png') lbl_pil.putpalette(colormap.flatten()) lbl_pil.save(filename + '_color.png') else: raise ValueError('[%s] Cannot save the pixel-wise class label as PNG. ' 'Please consider using the .npy format.' % filename)
def anns_match2(self, df, *, hide_match_dt=False, segmentation=False, shape_attrs=None, colormap=None): """ 按类别区分框颜色 """ import imgviz df = self._anns_init(df, segmentation) if not shape_attrs: shape_attrs = {} def get_attrs(d): return dict(ChainMap(shape_attrs, d)) if not colormap: colormap = imgviz.label_colormap(value=200) m = len(colormap) for idx, row in df.iterrows(): r = row attrs = { 'shape_color': colormap[r['gt_category_id'] % m], 'vertex_fill_color': colormap[r['dt_category_id'] % m] } if r['gt_category_id'] == -1: # 多余的dt self.add_dt_shape(r, get_attrs(attrs)) elif r['dt_category_id'] == -1: # 没有被匹配到的gt self.add_gt_shape(r, get_attrs(attrs)) else: # 匹配到的gt和dt if not hide_match_dt: self.add_dt_shape( r, get_attrs({'shape_color': [191, 191, 191]})) attrs['vertex_fill_color'] = [ int(r['iou'] * v) for v in attrs['vertex_fill_color'] ] self.add_gt_shape(r, get_attrs(attrs))
def visualize(self): # NOQA # scene-level rgb = self._rgb pcd = self._pcd scenes = {} # --------------------------------------------------------------------- # instance-level instance_id = self._instance_id cad = self._cads[instance_id] T_cad2cam_true = self._Ts_cad2cam_true[instance_id] T_cad2cam_pred = self._Ts_cad2cam_pred[instance_id] grid_target = self._grids pitch = self._pitch origin = self._origin scene = trimesh.Scene() # occupied target/untarget voxel = trimesh.voxel.VoxelGrid(grid_target[0], ttf.scale_and_translate(pitch, origin)) geom = voxel.as_boxes((1.0, 0, 0, 0.5)) scene.add_geometry(geom, geom_name="occupied_target") voxel = trimesh.voxel.VoxelGrid( grid_target[1], ttf.scale_and_translate(pitch, origin), ) geom = voxel.as_boxes((0, 1.0, 0, 0.5)) scene.add_geometry(geom, geom_name="occupied_untarget") scenes["instance_occupied"] = scene # empty scene = trimesh.Scene() voxel = trimesh.voxel.VoxelGrid(grid_target[2], ttf.scale_and_translate(pitch, origin)) geom = voxel.as_boxes((0.5, 0.5, 0.5, 0.5)) scene.add_geometry(geom, geom_name="empty") scenes["instance_empty"] = scene scene = trimesh.Scene() # cad_true cad_trans = cad.copy() cad_trans.visual.vertex_colors[:, 3] = 127 scene.add_geometry( cad_trans, transform=T_cad2cam_true, geom_name="cad_true", node_name="cad_true", ) scenes["instance_cad"] = scene # cad_pred for scene in scenes.values(): scene.add_geometry( cad, transform=T_cad2cam_pred, geom_name="cad_pred", node_name="cad_pred", ) # bbox aabb_min = origin - pitch / 2 aabb_max = aabb_min + pitch * np.array(grid_target.shape[1:]) geom = trimesh.path.creation.box_outline(aabb_max - aabb_min) geom.apply_translation((aabb_min + aabb_max) / 2) for scene in scenes.values(): scene.add_geometry(geom, geom_name="bbox") # --------------------------------------------------------------------- # scene_pcd scenes["scene_pcd_only"] = trimesh.Scene() scenes["scene_cad"] = trimesh.Scene() scenes["scene_pcd"] = trimesh.Scene() nonnan = ~np.isnan(pcd).any(axis=2) geom = trimesh.PointCloud(vertices=pcd[nonnan], colors=rgb[nonnan]) scenes["scene_pcd_only"].add_geometry(geom, geom_name="pcd") scenes["scene_pcd"].add_geometry(geom, geom_name="pcd") for instance_id in self._instance_ids: if instance_id not in self._cads: continue cad = self._cads[instance_id] T_cad2cam_pred = self._Ts_cad2cam_pred[instance_id] if cad: for key in ["scene_cad", "scene_pcd"]: scenes[key].add_geometry( cad, transform=T_cad2cam_pred, geom_name=f"cad_pred_{instance_id}", node_name=f"cad_pred_{instance_id}", ) # scene_occupancy colormap = imgviz.label_colormap() scenes["scene_occupied"] = trimesh.Scene() scenes["scene_empty"] = trimesh.Scene() for instance_id in self._mapping.instance_ids: occupied, empty = self._mapping.get_target_pcds(instance_id) color = trimesh.visual.to_rgba(colormap[instance_id]) color[3] = 127 geom = trimesh.voxel.ops.multibox(occupied, pitch=0.01, colors=color) scenes["scene_occupied"].add_geometry( geom, geom_name=f"occupied_{instance_id}") geom = trimesh.PointCloud(vertices=empty, colors=[0.5, 0.5, 0.5]) scenes["scene_empty"].add_geometry( geom, geom_name=f"empty_{instance_id}") # set camera camera = trimesh.scene.Camera( resolution=(640, 480), fov=(60 * 0.7, 45 * 0.7), ) camera_transform = morefusion.extra.trimesh.to_opengl_transform() for scene in scenes.values(): scene.camera = camera scene.camera_transform = camera_transform return scenes
def save_colored_mask(mask, save_path): lbl_pil = Image.fromarray(mask.astype(np.uint8), mode="P") colormap = imgviz.label_colormap() lbl_pil.putpalette(colormap.flatten()) lbl_pil.save(save_path)
import random import cv2 import numpy as np import imgviz realpath = os.path.abspath(__file__) _sep = os.path.sep realpath = realpath.split(_sep) sys.path.append(os.path.join(realpath[0]+_sep, *realpath[1:-1])) sys.path.append(os.path.join(realpath[0]+_sep, *realpath[1:realpath.index('EASY_EAI_nethub')+1])) from boundingbox_tools import Boundingbox_container from keypoint_tools import Keypoint_container COLOR_POOL = imgviz.label_colormap()[1:] def possibilitymap_to_heatmap(label, class_dim): assert (class_dim+1) <= len(label.shape), 'class_dim[{}] exceed label shape[{}]'.format(class_dim, label.shape) if class_dim != len(label.shape): _shape_list = [i for i in range(len(label.shape))] _shape_list[class_dim] = _shape_list[-1] _shape_list[-1] = class_dim label = label.transpose(*_shape_list) _inverse_onehot = label.argmax(axis=-1) onehot = np.eye(label.shape[-1])[_inverse_onehot] onehot = onehot.transpose(*_shape_list)
def visualize_data(): data = contrib.get_data() models = morefusion.datasets.YCBVideoModels() colormap = imgviz.label_colormap() scenes = { "pcd": trimesh.Scene(), "grid_target": trimesh.Scene(), "grid_nontarget_empty": trimesh.Scene(), "cad": trimesh.Scene(), } rgb = data["rgb"] depth = data["depth"] K = data["intrinsic_matrix"] pcd = morefusion.geometry.pointcloud_from_depth(depth, fx=K[0, 0], fy=K[1, 1], cx=K[0, 2], cy=K[1, 2]) nonnan = ~np.isnan(depth) geom = trimesh.PointCloud(vertices=pcd[nonnan], colors=rgb[nonnan]) scenes["pcd"].add_geometry(geom) # scenes["cad"].add_geometry(geom) T_world2cam = None for instance in data["instances"]: if T_world2cam is None: T_world2cam = np.linalg.inv(instance["T_cam2world"]) class_id = instance["class_id"] transform = instance["transform_init"] grid_target = instance["grid_target"] grid_nontarget_empty = instance["grid_nontarget_empty_noground"] cad = models.get_cad(class_id=class_id) # if hasattr(cad.visual, "to_color"): # cad.visual = cad.visual.to_color() scenes["pcd"].add_geometry( cad, node_name=str(instance["id"]), geom_name=str(instance["id"]), transform=transform, ) scenes["cad"].add_geometry( cad, node_name=str(instance["id"]), geom_name=str(instance["id"]), transform=transform, ) transform_vg = ttf.scale_and_translate(scale=instance["pitch"], translate=instance["origin"]) if instance["id"] == 0: color_id = 3 elif instance["id"] == 1: color_id = 1 elif instance["id"] == 2: color_id = 4 else: raise ValueError geom = trimesh.voxel.VoxelGrid( grid_target, transform=transform_vg).as_boxes(colors=colormap[color_id]) scenes["grid_target"].add_geometry(geom) points = np.argwhere(grid_nontarget_empty) points = points * instance["pitch"] + instance["origin"] geom = trimesh.PointCloud(vertices=points, colors=colormap[color_id]) # geom = trimesh.voxel.VoxelGrid( # grid_nontarget_empty, transform=transform_vg # ).as_boxes(colors=colormap[instance["id"] + 1]) scenes["grid_nontarget_empty"].add_geometry(geom) camera_transform = morefusion.extra.trimesh.to_opengl_transform() for scene in scenes.values(): # scene.add_geometry(contrib.grid(scale=0.1), transform=T_world2cam) scene.camera_transform = camera_transform return scenes
class VoxelGridsToMarkers(topic_tools.LazyTransport): _colormap = imgviz.label_colormap() def __init__(self): super().__init__() self._show_bbox = rospy.get_param("~show_bbox", True) self._pub = self.advertise("~output", MarkerArray, queue_size=1) self._post_init() def subscribe(self): self._sub = rospy.Subscriber( "~input", VoxelGridArray, self._callback, queue_size=1, buff_size=2**24, ) def unsubscribe(self): self._sub.unregister() def _callback(self, grids_msg): markers = MarkerArray() marker = Marker(header=grids_msg.header, id=-1, action=Marker.DELETEALL) markers.markers.append(marker) for grid in grids_msg.grids: instance_id = grid.instance_id color = self._colormap[instance_id + 1] origin = np.array([grid.origin.x, grid.origin.y, grid.origin.z]) indices = np.array(grid.indices) k = indices % grid.dims.z j = indices // grid.dims.z % grid.dims.y i = indices // grid.dims.z // grid.dims.y indices = np.column_stack((i, j, k)) points = origin + indices * grid.pitch dims = np.array([grid.dims.x, grid.dims.y, grid.dims.z]) marker = Marker() marker.ns = f"{instance_id}" marker.id = (instance_id + 1) * 2 marker.header = grids_msg.header marker.action = Marker.ADD marker.type = Marker.CUBE_LIST marker.points = [Point(*p) for p in points] marker.scale.x = grid.pitch marker.scale.y = grid.pitch marker.scale.z = grid.pitch marker.color.r = color[0] / 255.0 marker.color.g = color[1] / 255.0 marker.color.b = color[2] / 255.0 marker.color.a = 1 markers.markers.append(marker) if not self._show_bbox: continue center = origin + (dims / 2 - 0.5) * grid.pitch extents = dims * grid.pitch bbox = trimesh.path.creation.box_outline(extents) bbox.apply_translation(center) marker = Marker() marker.ns = f"{instance_id}" marker.id = (instance_id + 1) * 2 + 1 marker.header = grids_msg.header marker.action = Marker.ADD marker.type = Marker.LINE_LIST marker.points = [ Point(*bbox.vertices[i]) for node in bbox.vertex_nodes for i in node ] marker.color.r = color[0] / 255.0 marker.color.g = color[1] / 255.0 marker.color.b = color[2] / 255.0 marker.color.a = 1 marker.scale.x = 0.005 markers.markers.append(marker) self._pub.publish(markers)
labels = instance_ids, alpha = 0.5, line_width = 5, boundary_width = 1 ) amodalinstviz = imgviz.instances2rgb( image = rgb, masks = amodal_masks, labels = instance_ids, alpha = 0.5, line_width = 5, boundary_width = 1, ) color_map = imgviz.label_colormap(len(instance_ids)) for i, amodal_mask in enumerate(amodal_masks): if i == 0: amodalinstviz = rgb amodalinstviz = imgviz.instances2rgb( image = amodalinstviz, masks = [amodal_mask], labels = [instance_ids[i]], alpha = 0.9, line_width = 5, boundary_width = 1, colormap=np.uint8([(0,0,0), color_map[i]]) ) plt.figure(dpi=400)
def draw(): img = imgviz.data.lena() H, W = img.shape[:2] viz = img y1, x1 = 200, 180 y2, x2 = 400, 380 viz = imgviz.draw.rectangle( viz, (y1, x1), (y2, x2), outline=(255, 255, 255), width=5 ) viz = imgviz.draw.text_in_rectangle( viz, loc="lt", text="face", size=30, background=(255, 255, 255), aabb1=(y1, x1), aabb2=(y2, x2), ) # eye, eye, nose, mouse, mouse xys = [(265, 265), (330, 265), (315, 320), (270, 350), (320, 350)] colors = imgviz.label_colormap(value=255)[1:] shapes = ["star", "star", "rectangle", "circle", "triangle"] for xy, color, shape in zip(xys, colors, shapes): size = 20 color = tuple(color) if shape == "star": viz = imgviz.draw.star( viz, center=(xy[1], xy[0]), size=1.2 * size, fill=color ) elif shape == "circle": viz = imgviz.draw.circle( viz, center=(xy[1], xy[0]), diameter=size, fill=color ) elif shape == "triangle": viz = imgviz.draw.triangle( viz, center=(xy[1], xy[0]), size=size, fill=color ) elif shape == "rectangle": viz = imgviz.draw.rectangle( viz, aabb1=(xy[1] - size / 2, xy[0] - size / 2), aabb2=(xy[1] + size / 2, xy[0] + size / 2), fill=color, ) else: raise ValueError("unsupport shape: {}".format(shape)) img = imgviz.draw.text_in_rectangle( img, loc="lt+", text="original", size=30, background=(255, 255, 255), ) viz = imgviz.draw.text_in_rectangle( viz, loc="lt+", text="markers", size=30, background=(255, 255, 255), ) # ------------------------------------------------------------------------- plt.figure(dpi=200) plt.subplot(121) plt.title("original") plt.imshow(img) plt.axis("off") plt.subplot(122) plt.title("markers") plt.imshow(viz) plt.axis("off") img = imgviz.io.pyplot_to_numpy() plt.close() return img
viz = imgviz.draw.rectangle(viz, (y1, x1), (y2, x2), outline=(255, 255, 255), width=5) viz = imgviz.draw.text_in_rectangle( viz, loc='lt', text='face', size=30, background=(255, 255, 255), aabb1=(y1, x1), aabb2=(y2, x2), ) # eye, eye, nose, mouse, mouse xys = [(265, 265), (330, 265), (315, 320), (270, 350), (320, 350)] colors = imgviz.label_colormap(value=255)[1:] shapes = ['star', 'star', 'rectangle', 'circle', 'triangle'] for xy, color, shape in zip(xys, colors, shapes): size = 20 color = tuple(color) if shape == 'star': viz = imgviz.draw.star(viz, center=(xy[1], xy[0]), size=1.2 * size, fill=color) elif shape == 'circle': viz = imgviz.draw.circle(viz, center=(xy[1], xy[0]), diameter=size, fill=color) elif shape == 'triangle':
def lblsave(filename, lbl): lbl_pil = PIL.Image.fromarray(lbl.astype(np.uint8), mode="P") colormap = imgviz.label_colormap() lbl_pil.putpalette(colormap.flatten()) lbl_pil.save(filename)