from depth_rgb_to_bboxes import utils_depth as viz if __name__=='__main__': import open3d as o3d import numpy as np # List all files on the directory dir_ = '/data/HectorSanchez/database/PeopleCounter/camara3/00000000/' fpaths = sorted(os.listdir(dir_)) FLAG_FIRST_TIME = False for path in fpaths: if '.png' in path: #print(path) if not FLAG_FIRST_TIME: z = viz.load_dim(dir_+path) x,y = viz.projection_correction(z) x = x[1:-1,1:-1] y = y[1:-1,1:-1] z = z[1:-1,1:-1] background = np.zeros((np.size(x),3)) background[:,0] = np.reshape(x,-1) background[:,1] = np.reshape(y,-1) background[:,2] = np.reshape(z,-1) FLAG_FIRST_TIME = True continue if True: # Uses matplotlib
std_path = os.path.join(mean_std_folder, folder, 'bg_std.npy') bb_generator = udp.BBoxGenerator() bb_generator.set_background(mean_path, std_path) # For heads location files files = sorted(os.listdir(os.path.join(heads_main_folder, folder))) files = [x.replace('.txt', '.png') for x in files] head_files[folder] = files folder_path = os.path.join(ims_main_folder, folder) pbar = tqdm(total=len(head_files[folder])) for file in head_files[folder]: fpath = os.path.join(folder_path, file) dim = udp.load_dim(fpath) bboxes = bb_generator.determine_bboxes(dim) ### # Inspect visualiy that the bboxes are correct if False: print(file) plt.imshow(dim) plt.show() plt.imshow(bb_generator.mask) plt.show() for bbox in bboxes: dim = cv2.rectangle(dim, (int(bbox[0]), int(bbox[1])), (int(bbox[2]), int(bbox[3])), (0, 255, 0), 3)
mpath = os.path.join(mean_std_dir, 'bg_camera1_mean.npy') spath = os.path.join(mean_std_dir, 'bg_camera1_std.npy') # Get all the depth images #fdir = '../ims/' fdir = '/data/HectorSanchez/database/PeopleCounter/camara1/00000025/' files = sorted(os.listdir(fdir)) files = [x for i, x in enumerate(files) if '.png' in x and i > 45] # This is the only thing needed bb_generator = udp.BBoxGenerator() bb_generator.set_background(mpath, spath) bb_generator.set_translation_matrix(trans) bb_generator.set_rotation_matrix(rot) bb_generator.set_focal_lenght(np.array(fl)) bb_generator.set_principal_point(np.array(pp)) #out_dir = '../ims_lbs/' out_dir = '/data/HectorSanchez/database/PeopleCounter/camara1_lbs_v2/00000025/' if not os.path.exists(out_dir): os.makedirs(out_dir) for file in files: print('--->', file) fim = file.replace('depth', 'color') fim = fim.replace('png', 'jpg') dim = udp.load_dim(fdir + file) bboxes = bb_generator.determine_bboxes(dim, dx=-10, dy=30, fx=1, fy=1) create_lbs([fim], [bboxes], out_dir)
files = sorted(os.listdir(fdir)) files = [x for i, x in enumerate(files) if '.png' in x and i > 100] # This is the only thing needed bb_generator = udp.BBoxGenerator() bb_generator.set_background(mpath, spath) for file in tqdm(files): #print('--->',file) fim = file.replace('depth', 'color') fim = fim.replace('png', 'jpg') rgb_im_path = os.path.join(fdir, fim) im = cv2.imread(rgb_im_path) dpt_im_path = os.path.join(fdir, file) dim = udp.load_dim(dpt_im_path, M=M) x, y = udp.projection_correction(dim) bboxes_test = bb_generator.determine_bboxes( dim) #,dx=0,dy=0,fx=1,fy=1) # Show mask in im im = im + np.expand_dims(bb_generator.mask, axis=-1) * 100 for bbox in bboxes_test: im = cv2.rectangle(im, pt1=(bbox[0], bbox[1]), pt2=(bbox[2], bbox[3]), color=(0, 0, 255), thickness=3)
print('{}/{} Folder: {}'.format(i,len(folders),folder)) fdir = os.path.join(main_dir,folder) print('--- Calculating SSIM:') bg_extractor.calculate_sim(fdir) #bg_extractor.ssim_chart() th = ssim_ths[i] #th = float(input('Select a threshold: ')) #th = 0.78 bg_extractor.determine_background(th) print(' ====> BG frames',len(bg_extractor.background_fnames)) if False: # Visualize background depth images for file in bg_extractor.background_fnames: z = udp.load_dim(fdir+file) fig,ax = plt.subplots() ax.imshow(z) plt.show() plt.close(fig) # Calculate Mean and Std dev if len(bg_extractor.background_fnames)==0: continue tmp_dim_path = os.path.join(fdir,bg_extractor.background_fnames[0]) tmp_dim = udp.load_dim(tmp_dim_path) #all_dims = np.zeros((tmp_dim.shape[0],tmp_dim.shape[1],0),dtype=np.float64) all_dims = [] # Stack all the background depth images
# Set main output dir main_out_dir = '/data2/HectorSanchez/database/PeopleCounter/FINAL_RUN/camara3/labels/' for i, folder in enumerate(folders): if i < 67: """ folder 61, 66 have some issues with depth images """ continue print('---> Folder {} of {}: {}'.format(i + 1, len(folders), folder)) #if i<26: # continue folder_dir = os.path.join(folders_dir, folder) files = sorted(os.listdir(folder_dir)) files = [x for x in files if '.png' in x] out_dir = os.path.join(main_out_dir, folder) print('out_dir:', out_dir) if not os.path.exists(out_dir): os.makedirs(out_dir) pbar = tqdm(total=len(files)) for file in files: fim = file.replace('depth', 'color') fim = fim.replace('png', 'jpg') dim = udp.load_dim(os.path.join(folder_dir, file), M=M) bboxes = bb_generator.determine_bboxes( dim) #,dx=-10,dy=30,fx=1,fy=1) create_lbs([os.path.join(out_dir, fim)], [bboxes]) pbar.update(1) pbar.close()
if __name__ == '__main__': #fdir = '../ims/' fdir = '/data/HectorSanchez/database/PeopleCounter/camara1/00000000/' print(fdir) bg_extractor = udp.FindBackgroundDepth() bg_extractor.calculate_sim(fdir) bg_extractor.determine_background(.748) bg_extractor.ssim_chart() print('--->', len(bg_extractor.background_fnames)) if True: # Visualize background depth images for file in bg_extractor.background_fnames: z = udp.load_dim(fdir + file) fig, ax = plt.subplots() ax.imshow(z) plt.show() plt.close(fig) # Calculate Mean and Std dev tmp_dim = udp.load_dim(fdir + bg_extractor.background_fnames[0]) all_dims = np.zeros((tmp_dim.shape[0], tmp_dim.shape[1], 0), dtype=np.float64) # Stack all the background depth images pbar = tqdm(total=len(bg_extractor.background_fnames)) for file in bg_extractor.background_fnames: dim = udp.load_dim(fdir + file)