fig.set_size_inches(50, 20) plt.savefig(os.path.join(args.base_dir, 'event_rate_plot.png'), dpi=300, bbox_inches='tight') #plt.show() id_ = 0 for i, b in enumerate(mpeaks): if (mpeaks[i] <= 1): continue time = i * discretization if (time > last_ts or time < first_ts): continue sl, _ = pydvs.get_slice(cloud, idx, time, slice_width, 1, discretization) eimg = dvs_img(sl, global_shape, K, D) cimg = eimg[:, :, 0] + eimg[:, :, 2] avg = nz_avg(cimg) cimg *= 127 / avg #cimg *= 50 cimg = cimg.astype(np.uint8) cv2.imwrite( os.path.join(vis_dir, 'frame_' + str(id_).rjust(10, '0') + '.png'), cimg) id_ += 1
pydvs.replace_dir(slice_dir) pydvs.replace_dir(vis_dir) print("The recording range:", first_ts, "-", last_ts) print("The gt range:", gt_ts[0], "-", gt_ts[-1]) print("Discretization resolution:", discretization) for i, time in enumerate(gt_ts): if (time > last_ts or time < first_ts): continue depth = pydvs.undistort_img(depth_gt[i], K, D) mask = pydvs.undistort_img(mask_gt[i], K, D) sl, _ = pydvs.get_slice(cloud, idx, time, args.width, args.mode, discretization) eimg = dvs_img(sl, global_shape, K, D) cv2.imwrite( os.path.join(slice_dir, 'frame_' + str(i).rjust(10, '0') + '.png'), eimg) cv2.imwrite( os.path.join(slice_dir, 'depth_' + str(i).rjust(10, '0') + '.png'), depth.astype(np.uint16)) cv2.imwrite( os.path.join(slice_dir, 'mask_' + str(i).rjust(10, '0') + '.png'), mask.astype(np.uint16)) nmin = np.nanmin(depth) nmax = np.nanmax(depth)
def data_creator(sequence: str): print(f"{pydvs.okb('STARTING DATASET CREATION')} for sequence: {sequence}") dataset_txt = eval(open(os.path.join(sequence, 'meta.txt')).read()) K = np.array([[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 1.0]]) D = np.array([0.0, 0.0, 0.0, 0.0]) K[0][0] = dataset_txt['meta']['fx'] K[1][1] = dataset_txt['meta']['fy'] K[0][2] = dataset_txt['meta']['cx'] K[1][2] = dataset_txt['meta']['cy'] D[0] = dataset_txt['meta']['k1'] D[1] = dataset_txt['meta']['k2'] D[2] = dataset_txt['meta']['k3'] D[3] = dataset_txt['meta']['k4'] RES_X = dataset_txt['meta']['res_x'] RES_Y = dataset_txt['meta']['res_y'] NUM_FRAMES = len(dataset_txt['frames']) frames_meta = dataset_txt['frames'] oids = [] for key in frames_meta[0]: if (key == 'cam'): continue if (type(frames_meta[0][key]) == type(dict()) and 'pos' in frames_meta[0][key]): oids.append(key) print(pydvs.okb("Resolution:"), RES_X, 'x', RES_Y) print(pydvs.okb("Frames:"), NUM_FRAMES) print(pydvs.okb("Object ids:"), oids) print(pydvs.okb("Calibration:")) print(K) print(D) # Create a plot #save_plot(frames_meta, oids, os.path.join(args.base_dir, 'position_plots.pdf'), tp='pos') #save_plot(dataset_txt['full_trajectory'], oids, os.path.join(args.base_dir, 'position_plots.pdf'), tp='pos') # Read depth / masks print(pydvs.bld("Reading the depth and masks:")) depths = np.zeros((NUM_FRAMES, ) + (RES_X, RES_Y), dtype=np.uint16) masks = np.zeros((NUM_FRAMES, ) + (RES_X, RES_Y), dtype=np.uint16) classical = np.zeros((NUM_FRAMES, ) + (RES_X, RES_Y, 3), dtype=np.uint8) classical_read = 0 for i, frame in enumerate(frames_meta): print("frame\t", i + 1, "/", NUM_FRAMES, "\t", end='\r') gt_frame_name = os.path.join(sequence, f"img/{frame['gt_frame']}") gt_img = cv2.imread(gt_frame_name, cv2.IMREAD_UNCHANGED) if (gt_img.dtype != depths.dtype or gt_img.dtype != masks.dtype): print("\tType mismatch! Expected", depths.dtype, " but have", gt_img.dtype) sys.exit(-1) depths[i, :, :] = gt_img[:, :, 0] # depth is in mm masks[i, :, :] = gt_img[:, :, 2] # mask is object ids * 1000 if ('classical_frame' in frame.keys()): classical_frame_name = os.path.join( sequence, f"img/{frame['classical_frame']}") classical_img = cv2.imread(classical_frame_name, cv2.IMREAD_UNCHANGED) classical[i, :, :, :] = classical_img if (gt_img.dtype != depths.dtype or gt_img.dtype != masks.dtype): print("\tType mismatch! Expected", classical.dtype, " but have", classical_img.dtype) sys.exit(-1) classical_read += 1 print("\n") if (classical_read > 0): print(pydvs.okb("Read "), classical_read, "/", NUM_FRAMES, pydvs.okb(" classical frames")) else: classical = None # Read event cloud cloud, idx = pydvs.read_event_file_txt( os.path.join(sequence, 'events.txt'), args.discretization) tmin = frames_meta[0]['ts'] tmax = frames_meta[-1]['ts'] if (cloud.shape[0] > 0): tmin = cloud[0][0] tmax = cloud[-1][0] print(pydvs.okb("The recording range:"), tmin, "-", tmax) print(pydvs.okb("The gt range:"), frames_meta[0]['ts'], "-", frames_meta[-1]['ts']) print(pydvs.okb("Discretization resolution:"), args.discretization) """ # Save .npz file print (pydvs.bld("Saving...")) np.savez_compressed(os.path.join(args.base_dir, 'dataset.npz'), events=cloud, index=idx, classical=classical, discretization=args.discretization, K=K, D=D, depth=depths, mask=masks, meta=dataset_txt) print ("\n") """ # Generate images: slice_dir = os.path.join(sequence, 'slices') #vis_dir = os.path.join(args.base_dir, 'vis') pydvs.replace_dir(slice_dir) #pydvs.replace_dir(vis_dir) for i, frame in enumerate(frames_meta): print("Saving sanity check frames\t", i + 1, "/", NUM_FRAMES, "\t", end='\r') time = frame['ts'] if (time > tmax or time < tmin): continue cv2.imwrite( os.path.join(slice_dir, 'mask_' + str(i).rjust(10, '0') + '.png'), masks[i].astype(np.uint16)) if (cloud.shape[0] > 0): sl, _ = pydvs.get_slice(cloud, idx, time, args.slice_width, 1, args.discretization) eimg = dvs_img(sl, (RES_X, RES_Y), None, None, args.slice_width, mode=1) cv2.imwrite( os.path.join(slice_dir, 'frame_' + str(i).rjust(10, '0') + '.png'), eimg) depth = depths[i].astype(np.float) mask = masks[i].astype(np.float) col_mask = mask_to_color(mask) """ # normalize for visualization mask = (255 * (mask.astype(np.float) - np.nanmin(mask)) / (np.nanmax(mask) - np.nanmin(mask))).astype(np.uint8) depth = (255 * (depth.astype(np.float) - np.nanmin(depth)) / (np.nanmax(depth) - np.nanmin(depth))).astype(np.uint8) if ((classical_read > 0) and (classical is not None)): grayscale_img = cv2.cvtColor(classical[i], cv2.COLOR_BGR2GRAY).astype(np.float) rgb_img = np.dstack((grayscale_img, grayscale_img, grayscale_img)) rgb_img[mask > 0] = rgb_img[mask > 0] * 0.2 + col_mask[mask > 0] * 0.8 #rgb_img = np.rot90(rgb_img, k=2) #depth = np.rot90(depth, k=2) eimg = np.hstack((rgb_img.astype(np.uint8), np.dstack((depth,depth,depth)))) else: eimg = dvs_img(sl, (RES_X, RES_Y), None, None, args.slice_width, mode=0) eimg[mask > 0] = eimg[mask > 0] * 0.5 + col_mask[mask > 0] * 0.5 eimg = np.hstack((eimg.astype(np.uint8), np.dstack((depth,depth,depth)))) #footer = gen_text_stub(eimg.shape[1], frame) #eimg = np.vstack((eimg, footer)) cv2.imwrite(os.path.join(vis_dir, 'frame_' + str(i).rjust(10, '0') + '.png'), eimg) """ print(F"{pydvs.okg('FINISHED DATASET CREATION')} for sequence {sequence}")
K = sl_npz['K'] D = sl_npz['D'] first_ts = cloud[0][0] last_ts = cloud[-1][0] print "The recording range:", first_ts, "-", last_ts print "The gt range:", gt_ts[0], "-", gt_ts[-1] print "gt frame count:", len(gt_ts) print "Discretization resolution:", discretization if (args.t1 < first_ts or args.t2 > last_ts): print "The time boundaries have to be within range" exit(0) width = args.t2 - args.t1 sl, idx_, t0 = pydvs.get_slice(cloud, idx, args.t1, width, 0, discretization) t1 = t0 + sl[-1][0] - sl[0][ 0] # The t1 - t2 ragne can be shifted due to discretization idx_lo = 0 for i, t in enumerate(gt_ts): if t > t0: idx_lo = i break idx_hi = 0 for i, t in enumerate(gt_ts): if t > t1: idx_hi = i break print "Saving", depth_gt_.shape[0], "gt slices"