def draw_track(self, track): color_map = colormap() color = int(color_map[track['draw_id'] % 79][0]), int( color_map[track['draw_id'] % 79][1]), int( color_map[track['draw_id'] % 79][2]) if track['draw']: if cfg_priv.OTHER.COUNT_DRAW_LESS: new_track = track['track'][-cfg_priv.OTHER.DRAW_TRACK_NUM:] solid = track['solid'][-cfg_priv.OTHER.DRAW_TRACK_NUM:] dotted = track['dotted'][-cfg_priv.OTHER.DRAW_TRACK_NUM:] else: new_track = track['track'] solid = track['solid'] dotted = track['dotted'] if len(new_track) > 5: var_thred = 3 var_x, var_y = np.var(np.array(new_track), axis=(0)) # 如果想当长时间不动就不在显示了 if var_x < var_thred and var_y < var_thred: return self.out_info['solid'].append(solid) self.out_info['dotted'].append(dotted) self.out_info['list_track'].append(new_track) self.out_info['list_box'].append(track['boxes']) self.out_info['list_id'].append(track['draw_id']) self.out_info['list_color'].append(color)
def isolines(f, nc=10, n=1): from colormap import colormap from applylut import applylut maxi = int(np.ceil(f.max())) mini = int(np.floor(f.min())) d = int(np.ceil(1. * (maxi - mini) / nc)) m = np.zeros((d, 1)) m[0:n, :] = 1 m = np.resize(m, (maxi - mini, 1)) m = np.concatenate((np.zeros((mini, 1)), m)) m = np.concatenate((m, np.zeros((256 - maxi, 1)))) m = np.concatenate((m, m, m), 1) ct = m * colormap('hsv') + (1 - m) * colormap('gray') g = applylut(f, ct) return g
def draw_mask(image,target,segms=True): """segms=False 不画mask""" image = np.asarray(image, np.uint8).copy() labels = target["labels"] bboxs = target["boxes"] scores = np.ones(len(labels),np.float32) # 虚假的 if segms: masks = target["masks"] color_list = colormap() # color_list = colormap(rgb=True) / 255 mask_color_id=0 for idx,(label,bbox,score) in enumerate(zip(labels,bboxs,scores)): # label, bbox, score = label.cpu().numpy(), bbox.cpu().numpy(), score.cpu().numpy() class_str="%s:%.3f"%(label,score) pos=list(map(int,bbox)) image=vis_class(image,pos,class_str,0.5,label) if segms: mask = masks[idx]#.cpu().numpy() mask = np.clip(mask * 255., 0, 255).astype(np.uint8) # np.squeeze(mask, 0) color_mask = color_list[mask_color_id % len(color_list), 0:3] mask_color_id += 1 image=vis_mask(image,mask,color_mask) plt.imshow(image) plt.title("img") plt.show()
def __post_init__(self) -> None: self.vcr_tdir = Path(self.vcr_tdir) self.vcr_imgs = self.vcr_tdir / self.vcr_imgs self.img_fn = self.ann['img_fn'] self.meta_fn = self.ann['metadata_fn'] # self.img = self.get_img() self.meta_data = self.get_metadata() self.color_list = colormap(rgb=True).astype(np.int_) self.transparency = 128 self.create_obj_counter()
def draw_segms(image,target): image = np.asarray(image, np.uint8).copy() masks = np.asarray(target, np.uint8).copy() color_list = colormap() unique_label = np.unique(masks) for i, la in enumerate(unique_label): if la == 0: continue # 背景跳过 mask = np.asarray(masks == la) mask = np.clip(mask * 255, 0, 255).astype(np.uint8) # color_mask = color_list[i % len(color_list), 0:3] color_mask = color_list[la % len(color_list), 0:3] image = vis_mask(image, mask, color_mask) plt.imshow(image) plt.title("img") plt.show()
def main(args): """Main entry point allowing external calls Args: args ([str]): command line parameter list """ args = parse_args(args) directory = args.directory name = (args.name).replace('.png', '') + '.png' scale = args.scale if scale == -1: scale = s.SCALE hm = Image.open('heightmap.tif') probableTime = int(1105 * ((10 / scale)**2)) print("Projected time to complete: ", probableTime // 60, "minutes, ", probableTime % 60, "seconds") scaled = heightmap.heightmap(hm, scale) scaled.save(directory / "hm.tif") projected = projection.projectmap(scaled, scale) projected.save(directory / "pm.tif") colors = colormap.colormap(projected, s.gradient, s.smoothness) colors.save(directory / "cm.png") normals = normalmap.normalmap(projected, s.sobelScale) normals.save(directory / "nm.png") shaded = lightmap.lightmap(normals, colors, s.light, s.ambientPercentage) shaded.save(directory / name) scaledpng = topng.to_png(scaled) scaledpng.save(directory / "hm.png") projectedpng = topng.to_png(projected) projectedpng.save(directory / "pm.png")
def draw_mask_on_image(image_path, segms_out, draw_threshold, alpha=0.7): image = Image.open(image_path) draw = ImageDraw.Draw(image) im_width, im_height = image.size mask_color_id = 0 w_ratio = .4 image = np.array(image).astype('float32') for dt in np.array(segms_out): segm, num_id, score = dt.tolist() if score < draw_threshold: continue mask = mask_util.decode(segm) * 255 color_list = colormap(rgb=True) color_mask = color_list[mask_color_id % len(color_list), 0:3] mask_color_id += 1 for c in range(3): color_mask[c] = color_mask[c] * (1 - w_ratio) + w_ratio * 255 idx = np.nonzero(mask) image[idx[0], idx[1], :] *= 1.0 - alpha image[idx[0], idx[1], :] += alpha * color_mask image = Image.fromarray(image.astype('uint8')) return image
def vis_one_image(im_name, fout, bboxes, dpi=200, weights=None): # masks: (N, 28, 28) ... masks for one frame if not len(bboxes): return im = cv2.imread(im_name) H, W, _ = im.shape color_list = colormap(rgb=True) / 255 fig = plt.figure(frameon=False) fig.set_size_inches(im.shape[1] / dpi, im.shape[0] / dpi) ax = plt.Axes(fig, [0., 0., 1., 1.]) ax.axis('off') fig.add_axes(ax) ax.imshow(im) mask_color_id = 0 if weights is None: n_objs = masks.shape[0] obj_ids = range(n_objs) else: obj_ids = np.argsort(weights) ws = [0] for oid in obj_ids: x, y, w, h = bboxes[oid] mask = np.zeros([H, W]) mask[x:x + w, y:y + h] = 1 mask = mask.astype('uint8') if mask.sum() == 0: continue if weights is not None: ws += weights[oid], color_mask = color_list[mask_color_id % len(color_list), 0:3] mask_color_id += 1 w_ratio = .4 for c in range(3): color_mask[c] = color_mask[c] * (1 - w_ratio) + w_ratio e_down = mask e_pil = Image.fromarray(e_down) e_pil_up = e_pil.resize((H, W) if TRANS else (W, H), Image.ANTIALIAS) e = np.array(e_pil_up) _, contour, hier = cv2.findContours(e.copy(), cv2.RETR_CCOMP, cv2.CHAIN_APPROX_NONE) if len(contour) > 1: print('# contour:', len(contour)) for c in contour: if FLIP: assert (c.shape[1] == 1 and c.shape[2] == 2), print('c.shape:', c.shape) for pid in range(c.shape[0]): c[pid][0][0], c[pid][0][1] = c[pid][0][1], c[pid][0][0] linewidth = 1.2 alpha = 0.5 if oid == obj_ids[-1]: # most probable obj edgecolor = (1, 0, 0, 1) # 'r' else: edgecolor = (1, 1, 1, 1) # 'w' if weights is not None: linewidth *= (4**weights[oid]) alpha /= (4**weights[oid]) polygon = Polygon( c.reshape((-1, 2)), fill=True, facecolor=(color_mask[0], color_mask[1], color_mask[2], alpha), edgecolor=edgecolor, linewidth=linewidth, ) xy = polygon.get_xy() ax.add_patch(polygon) fig.savefig(fout.replace('.jpg', '_{:.3f}.jpg'.format(max(ws))), dpi=dpi) plt.close('all')
directory = sys.argv[1] Path("./" + directory).mkdir(parents=True, exist_ok=True) hm = Image.open('heightmap.tif') probableTime = int(1105 * ((10 / s.SCALE)**2)) print("Projected time to complete: ", probableTime // 60, "minutes, ", probableTime % 60, "seconds") scaled = heightmap.heightmap(hm, s.SCALE) scaled.save(directory + "/hm.tif") projected = projection.projectmap(scaled, s.SCALE) projected.save(directory + "/pm.tif") colors = colormap.colormap(projected, s.gradient, s.smoothness) colors.save(directory + "/cm.png") normals = normalmap.normalmap(projected, s.sobelScale) normals.save(directory + "/nm.png") shaded = lightmap.lightmap(normals, colors, s.light, s.ambientPercentage) shaded.save(directory + "/map.png") scaledpng = topng.to_png(scaled) scaledpng.save(directory + "/hm.png") projectedpng = topng.to_png(projected) projectedpng.save(directory + "/pm.png")
HEIGHT = 29467 WIDTH = 128 img = Image.new('I', (WIDTH, HEIGHT), color=(-2147483648)) MAX = 1385517713 MIN = -563154762 stepsize = int((MAX - MIN) / HEIGHT) print(stepsize) map = [] for i in range(MIN, MAX - stepsize, stepsize): map.append(i) line = 0 for value in map: print(line, value) for x in range(WIDTH): img.putpixel((x, line), value) line += 1 img.save("legend.tif") image = colormap.colormap(img, s.gradient, s.smoothness) image.save("colorlegend.png")
def txt2img(visual_path="visual_val_gt"): print("Starting txt2img") valid_labels = {1} ignore_labels = {2, 7, 8, 12} if not os.path.exists(visual_path): os.makedirs(visual_path) color_list = colormap() gt_json_path = '../dataset/MOT17/annotations/val_half.json' img_path = '../dataset/MOT17/train/' show_video_names = [ 'MOT17-02-FRCNN', 'MOT17-04-FRCNN', 'MOT17-05-FRCNN', 'MOT17-09-FRCNN', 'MOT17-10-FRCNN', 'MOT17-11-FRCNN', 'MOT17-13-FRCNN' ] for show_video_name in show_video_names: img_dict = dict() if visual_path == "visual_val_gt": txt_path = 'mot/train/' + show_video_name + '/gt/gt_val_half.txt' elif visual_path == "visual_val_predict": txt_path = 'eval/val_origincode_crowdh_1_1/val/tracks/' + show_video_name + '.txt' else: raise NotImplementedError with open(gt_json_path, 'r') as f: gt_json = json.load(f) for ann in gt_json["images"]: file_name = ann['file_name'] video_name = file_name.split('/')[0] if video_name == show_video_name: img_dict[ann['frame_id']] = img_path + file_name txt_dict = dict() with open(txt_path, 'r') as f: for line in f.readlines(): linelist = line.split(',') mark = int(float(linelist[6])) label = int(float(linelist[7])) vis_ratio = float(linelist[8]) if visual_path == "visual_val_gt": if mark == 0 or label not in valid_labels or label in ignore_labels or vis_ratio <= 0: continue img_id = linelist[0] obj_id = linelist[1] bbox = [ float(linelist[2]), float(linelist[3]), float(linelist[2]) + float(linelist[4]), float(linelist[3]) + float(linelist[5]), int(obj_id) ] if int(img_id) in txt_dict: txt_dict[int(img_id)].append(bbox) else: txt_dict[int(img_id)] = list() for img_id in sorted(txt_dict.keys()): img = cv2.imread(img_dict[img_id]) for bbox in txt_dict[img_id]: cv2.rectangle(img, (int(bbox[0]), int(bbox[1])), (int(bbox[2]), int(bbox[3])), color_list[bbox[4] % 79].tolist(), thickness=2) cv2.putText(img, "{}".format(int(bbox[4])), (int(bbox[0]), int(bbox[1])), cv2.FONT_HERSHEY_SIMPLEX, 0.8, color_list[bbox[4] % 79].tolist(), 2) cv2.imwrite( visual_path + "/" + show_video_name + "{:0>6d}.png".format(img_id), img) print(show_video_name, "Done") print("txt2img Done")
def save_open3d_images_of_pointcloud(category, predicted_mask_basepath, main_output_folder): """ save renderings of an object and predicted parts :param category: (string) shape category :param predicted_mask_basepath: :param main_output_folder: (string) folder location to save output renderings :return: """ # get list of colors color_list = colormap() # (79,3) colors array # print("color_list.shape: ",color_list.shape) o3d.utility.set_verbosity_level(o3d.utility.VerbosityLevel.Warning) # keeping the level to '1' for points path, becoz we care only about the points and all categories have at least level-1 folder points_path = f"./data/partnet/ins_seg_h5_gt/{category}-1/test-00.h5" pts = h5py.File(points_path, mode='r')['pts'][...] # save images for all granularity. level of part segmentation granularity (level=1: coarser, level=3: finer) for level in [1]: # create output folder output_folder = os.path.join(main_output_folder, category, f"Level_{level}") print("output_folder:", os.path.abspath(output_folder)) os.makedirs(output_folder, exist_ok=True) # open predicted part segmentation file predicted_mask_path = f"{predicted_mask_basepath}/{category}/Level_{level}/test-00.h5" predicted_mask_data = h5py.File(predicted_mask_path, mode='r') # save part segmented image for all shapes. save at max 100 images for point_cloud_idx in range(min(pts.shape[0], 100)): pts0 = pts[point_cloud_idx] # save original point cloud for reference # pcd = o3d.geometry.PointCloud() # pcd.points = o3d.utility.Vector3dVector(pts0) # pcd.paint_uniform_color(np.array([[0.0],[0.0],[0.0]])) # save_image(point_cloud=pcd, output_filename=os.path.join(output_folder, f"img_{category}_L{level}_pcd{point_cloud_idx}.png")) # mask size=(num_examples, 200, 10k) mask = predicted_mask_data['mask'][...] # valid size = (num_examples, 200). Boolean array valid = predicted_mask_data['valid'][...] # get predicted all masks of current point cloud pcd_mask = mask[point_cloud_idx] # get valid masks out of 200. ('mask' contains 200 parts, but only some of them represent parts, others are empty) # array/channel ids of such non-empty parts is given in 'valid' pcd_valid_part_ids = np.where(valid[point_cloud_idx] == True)[0] # Colors array of size (part_ids.shape, 3,1) pcd_color_list = color_list[:pcd_valid_part_ids.shape[0]] # collect part point cloud and colors part_pcd = np.zeros((10000, 3)) part_color = np.zeros((10000, 3)) for part_idx in pcd_valid_part_ids: mask0 = np.expand_dims(pcd_mask[part_idx], axis=1) # get part coordinates part = np.multiply(pts0, mask0) part_pcd += part # color color = np.tile(pcd_color_list[part_idx], (part.shape[0], 1)) color = np.multiply(color, mask0) part_color += color # convert numpy array to open3d point cloud pcd = o3d.geometry.PointCloud() pcd.points = o3d.utility.Vector3dVector(part_pcd) # use "paint_uniform_color(self: open3d.geometry.PointCloud, color: numpy.ndarray[float64[3, 1]]) → None" to assign colors to current part-pointcloud pcd.colors = o3d.utility.Vector3dVector(part_color) save_image( point_cloud=pcd, output_filename=os.path.join( output_folder, f"img_{category}_L{level}_pcd{point_cloud_idx}_segmented.png" )) if (point_cloud_idx != 0 and point_cloud_idx % 10 == 0) or point_cloud_idx == pts.shape[0] - 1: print( f"Category: {category} - Level {level}: saved {point_cloud_idx} images" )