예제 #1
0
def visualize_h5f(h5_path):
    '''
    :param h5_path:
    :return: void
    visualize bgr + colorized depth + colorized LiDAR
    '''
    h5f = h5py.File(h5_path, "r")
    rgb_data = np.array(h5f['rgb'], dtype=np.uint8)
    # Originally, shape of rgb_data: (3, 480, 640)
    #print("Shape of rgb: ", rgb_data.shape)
    rgb = np.transpose(rgb_data, (1, 2, 0))
    bgr = rgb[:, :, ::-1]

    depth = np.array(h5f['depth'], dtype=np.float32)
    #print("Shape of depth: ", depth.shape)
    max_depth = np.amax(depth)
    min_depth = np.amin(depth)
    colored_depth = np.array(colored_depthmap(depth, min_depth, max_depth), dtype=np.uint8)
    #print("Shape of colored_depth: ", colored_depth.shape)
    colored_depth = colored_depth[:, :, ::-1]

    projected = np.array(h5f['scan_projected'], dtype=np.float32)
    #print("Shape of projected: ", projected.shape)
    # max_depth = np.amax(projected)
    # min_depth = np.amin(projected)
    colored_pc = np.array(colored_depthmap(projected, min_depth, max_depth), dtype=np.uint8)
    #print("Shape of colored_pc: ", colored_pc.shape)
    colored_pc = colored_pc[:, :, ::-1]
    im_result = cv2.hconcat([bgr, colored_depth, colored_pc])
    cv2.imshow("visualized", im_result)
    cv2.waitKey(0)
예제 #2
0
def save_h5f2RGB(h5_path, file, save_path):
    '''
     :param h5_path, file, save_path:
     :return: void
     save concated bgr + colorized depth + colorized LiDAR as .png
     '''
    h5f = h5py.File(h5_path, "r")
    rgb_data = np.array(h5f['rgb'], dtype=np.uint8)
    # Originally, shape of rgb_data: (3, 480, 640)
    #print("Shape of rgb: ", rgb_data.shape)
    rgb = np.transpose(rgb_data, (1, 2, 0))
    bgr = rgb[:, :, ::-1]

    depth = np.array(h5f['depth'], dtype=np.float32)
    #print("Shape of depth: ", depth.shape)
    max_depth = np.amax(depth)
    min_depth = np.amin(depth)
    colored_depth = np.array(colored_depthmap(depth, min_depth, max_depth), dtype=np.uint8)
    #print("Shape of colored_depth: ", colored_depth.shape)
    colored_depth = colored_depth[:, :, ::-1]

    projected = np.array(h5f['scan_projected'], dtype=np.float32)
    #print("Shape of projected: ", projected.shape)
    # max_depth = np.amax(projected)
    # min_depth = np.amin(projected)
    colored_pc = np.array(colored_depthmap(projected, min_depth, max_depth), dtype=np.uint8)
    #print("Shape of colored_pc: ", colored_pc.shape)
    colored_pc = colored_pc[:, :, ::-1]
    im_result = cv2.hconcat([bgr, colored_depth, colored_pc])
    save_file_path = os.path.join(save_path, file[:-2]) + 'png'
    print(save_file_path)
    cv2.imwrite(save_file_path, im_result)
예제 #3
0
 def test(self):
     n_test = len(self.testset)
     device = torch.device('cuda:0' if self.use_gpu else 'cpu')
     self.net.to(device)
     self.net.eval()
     self.print("<-------------Test the model-------------->")
     colormaps = {'nyu': plt.cm.jet, 'kitti': plt.cm.plasma}
     cm = colormaps[self.params.dataset]
     #measure_list = ['a1', 'a2', 'a3', 'rmse', 'rmse_log', 'log10', 'abs_rel', 'sq_rel']
     measures = {key: 0 for key in measure_list}
     test_total_time = 0
     with torch.no_grad():
         #sys.stdout.flush()
         #tbar = tqdm(self.testloader)
         for step, data in enumerate(self.testloader):
             images, labels = data[0].to(device), data[1].to(device)
             before_op_time = time.time()
             scales = [1]
             if self.params.use_ms:
                 scales = [1, 1.25]
             depths = predict_multi_scale(self.net, images, scales,
                                          self.params.classes,
                                          self.params.use_flip)
             duration = time.time() - before_op_time
             test_total_time += duration
             # accuracy
             new = self.eval_func(labels, depths)
             print_str = "Test step [{}/{}], a1: {:.5f}, rmse: {:.5f}.".format(
                 step + 1, n_test, new['a1'], new['rmse'])
             self.print(print_str)
             #tbar.set_description(print_str)
             #sys.stdout.flush()
             images = images.cpu().numpy().squeeze().transpose(1, 2, 0)
             labels = labels.cpu().numpy().squeeze()
             depths = depths.cpu().numpy().squeeze()
             labels = colored_depthmap(labels, cmap=cm).squeeze()
             depths = colored_depthmap(depths, cmap=cm).squeeze()
             #fuse = merge_images(images, labels, depths, self.params.min_depth, self.params.max_depth)
             plt.imsave(
                 os.path.join(self.resdir, '{:04}_rgb.png'.format(step)),
                 images)
             plt.imsave(
                 os.path.join(self.resdir, '{:04}_gt.png'.format(step)),
                 labels)
             plt.imsave(
                 os.path.join(self.resdir, '{:04}_depth.png'.format(step)),
                 depths)
             for k, v in new.items():
                 measures[k] += v.item()
     fps = n_test / test_total_time
     measures = {
         key: round(value / n_test, 5)
         for key, value in measures.items()
     }
     self.print('Testing done, fps {:4.2f} | {}'.format(fps, measures))
     return
예제 #4
0
def save_depth_filled_csv(h5_path, file, save_path):
    '''
     :param h5_path, file, save_path:
     :return: void
     save new_depth2csv file (depth-filled)
     '''
    h5f = h5py.File(h5_path, "r")
    rgb_data = np.array(h5f['rgb'], dtype=np.uint8)
    # Originally, shape of rgb_data: (3, 480, 640)
    rgb = np.transpose(rgb_data, (1, 2, 0))
    bgr = rgb[:, :, ::-1]
    bgr_input = bgr / 255

    depth = np.array(h5f['depth'], dtype=np.float32)

    new_depth = fill_depth_colorization(bgr_input, depth, 0.1)
    new_depth = new_depth * 1000
    new_depth = new_depth.astype(int)
    max_depth = np.amax(new_depth)
    min_depth = np.amin(new_depth)
    colored_depth = np.array(colored_depthmap(new_depth, min_depth, max_depth),
                             dtype=np.uint8)
    colored_depth = colored_depth[:, :, ::-1]

    save_file_path = os.path.join(save_path, file[:-2]) + 'png'
    print(save_file_path)
    cv2.imwrite(save_file_path, colored_depth)

    np.savetxt(os.path.join(save_path, file[:-2]) + 'csv',
               new_depth,
               delimiter=',')
예제 #5
0
def visualize_h5f2(h5_path):
    '''
     :param h5_path:
     :return: void
     NOT USED! needs modification.
     Basically, First attempt was to use ONLY this function to save final image.
     Problem : bgr type changed to CV_8SC3 (Don't know how to convert it to CV_8UC3 again.)
     '''
    h5f = h5py.File(h5_path, "r")
    rgb_data = np.array(h5f['rgb'], dtype=np.uint8)
    # Originally, shape of rgb_data: (3, 480, 640)
    #print("Shape of rgb: ", rgb_data.shape)
    rgb = np.transpose(rgb_data, (1, 2, 0))
    bgr = rgb[:, :, ::-1]
    # print(type((255,0,0)))
    # bgr_new = cv2.circle(bgr, (100, 100), 10, (255,0,0),-1)
    depth = np.array(h5f['depth'], dtype=np.float32)
    #print("Shape of depth: ", depth.shape)
    max_depth = np.amax(depth)
    min_depth = np.amin(depth)
    colored_depth = np.array(colored_depthmap(depth, min_depth, max_depth), dtype=np.uint8)
    #print("Shape of colored_depth: ", colored_depth.shape)
    colored_depth = colored_depth[:, :, ::-1]

    projected = np.array(h5f['scan_projected'], dtype=np.float32)
    #print("Shape of projected: ", projected.shape)
    # max_depth = np.amax(projected)
    # min_depth = np.amin(projected)
    colored_pc = np.array(colored_depthmap(projected, min_depth, max_depth), dtype=np.uint8)
    print("Shape of colored_pc: ", colored_pc.shape)
    colored_pc = colored_pc[:, :, ::-1]
    for i in range(480):
        for j in range(640):
            #get_color = tuple(map(int, colored_pc[i, j, :]))
            reference_color = colored_pc[0, 0, :].tolist()
            get_color = colored_pc[i, j, :].tolist()
            #print(get_color, type(get_color))
            if get_color != reference_color:
                bgr = cv2.circle(bgr, (j, i), 2, get_color, -1)

    bgr = np.asarray(bgr)
    im_result = cv2.hconcat([bgr, colored_depth, colored_pc])
    cv2.imshow("visualized", im_result)
    #cv2.imshow("bgr", bgr_new)
    cv2.waitKey(0)
예제 #6
0
def visualize_depth_filled_h5f(h5_path):
    '''
     :param h5_path:
     :return: void
     visualize bgr + depth-filled colorized depth + colorized LiDAR
     '''
    h5f = h5py.File(h5_path, "r")
    rgb_data = np.array(h5f['rgb'], dtype=np.uint8)
    # Originally, shape of rgb_data: (3, 480, 640)
    print("Shape of rgb: ", rgb_data.shape)
    rgb = np.transpose(rgb_data, (1, 2, 0))
    bgr = rgb[:, :, ::-1]

    bgr_input = bgr / 255
    assert isinstance(bgr_input.shape, object)
    #print("Shape of bgr_input: ", bgr_input.shape)

    depth = np.array(h5f['depth'], dtype=np.float32)
    #print("Shape of depth: ", depth.shape)
    max_depth = np.amax(depth)
    min_depth = np.amin(depth)
    new_depth = fill_depth_colorization(bgr_input, depth, 1)
    colored_depth = np.array(colored_depthmap(new_depth, min_depth, max_depth), dtype=np.uint8)
    colored_depth = colored_depth[:, :, ::-1]

    projected = np.array(h5f['scan_projected'], dtype=np.float32)
    #print("Shape of projected: ", projected.shape)
    # max_depth = np.amax(projected)
    # min_depth = np.amin(projected)
    colored_pc = np.array(colored_depthmap(projected, min_depth, max_depth), dtype=np.uint8)
    #print("Shape of colored_pc: ", colored_pc.shape)
    colored_pc = colored_pc[:, :, ::-1]

    im_result = cv2.hconcat([bgr, colored_depth, colored_pc])
    cv2.imshow("visualized", im_result)
    cv2.waitKey(0)
예제 #7
0
def save_colored_depth(rel_path):

    fList = os.listdir(rel_path)
    NewList = sorted(fList, key=lambda x: int(x[7:-4]))

    count = 0

    for file in NewList:
        depth_path = rel_path + file
        depth_array = convert_img2csv_str2nparray(depth_path)
        new_depth_image = colored_depthmap(depth_array)

        save_path = save_rel_path + file[:-3] +'png'
        cv2.imwrite(save_path, new_depth_image)

        count = count + 1
예제 #8
0
def visualize_depth_and_projected_img(img_path, h5_path):
    '''
     :param h5_path, LiDAR-projected RGB img_path:
     :return: void
     visualize bgr with colorized LiDAR projected + colorized depth
     '''
    h5f = h5py.File(h5_path, "r")
    depth = np.array(h5f['depth'], dtype=np.float32)
    #print("Shape of depth: ", depth.shape)
    max_depth = np.amax(depth)
    min_depth = np.amin(depth)
    colored_depth = np.array(colored_depthmap(depth, min_depth, max_depth), dtype=np.uint8)
    #print("Shape of colored_depth: ", colored_depth.shape)
    colored_depth = colored_depth[:, :, ::-1]

    projected_img = cv2.imread(img_path, cv2.IMREAD_COLOR)
    print(projected_img.shape)

    im_result = cv2.hconcat([projected_img, colored_depth])
    cv2.imshow("visualized", im_result)
    cv2.waitKey(0)
예제 #9
0
def save_concat_img(h5_path, img_path, file, save_path):
    '''
     :param h5_path, img_path, file, save_path:
     :return: void
     concat LiDAR-projected bgr img + colorized depth img  and save it as .png
     '''

    h5f = h5py.File(h5_path, "r")
    depth = np.array(h5f['depth'], dtype=np.float32)
    #print("Shape of depth: ", depth.shape)
    max_depth = np.amax(depth)
    min_depth = np.amin(depth)
    colored_depth = np.array(colored_depthmap(depth, min_depth, max_depth), dtype=np.uint8)
    #print("Shape of colored_depth: ", colored_depth.shape)
    colored_depth = colored_depth[:, :, ::-1]

    projected_img = cv2.imread(img_path, cv2.IMREAD_COLOR)
    print(projected_img.shape)

    im_result = cv2.hconcat([projected_img, colored_depth])
    save_file_path = os.path.join(save_path, file[:-3]) + '.png'
    print(save_file_path)
    cv2.imwrite(save_file_path, im_result)
예제 #10
0
def save_projected_img(h5_path, file, save_path):
    '''
     :param h5_path, file name, save_path:
     :return: void
     save bgr img with colorized LiDAR projected
     '''
    h5f = h5py.File(h5_path, "r")
    rgb_data = np.array(h5f['rgb'], dtype=np.uint8)
    # Originally, shape of rgb_data: (3, 480, 640)
    rgb = np.transpose(rgb_data, (1, 2, 0))
    bgr = rgb[:, :, ::-1]
    bgr = np.ascontiguousarray(
        bgr, dtype=np.uint8)  #Don't know why i should put this.
    '''Used for finding min and max'''
    depth = np.array(h5f['depth'], dtype=np.float32)
    max_depth = np.amax(depth)
    min_depth = np.amin(depth)

    max_depth = 3
    min_depth = 0

    projected = np.array(h5f['scan_projected'], dtype=np.float32)
    # max_depth = np.amax(projected)
    # min_depth = np.amin(projected)
    colored_pc = np.array(colored_depthmap(projected, min_depth, max_depth),
                          dtype=np.uint8)
    colored_pc = colored_pc[:, :, ::-1]

    for i in range(480):
        for j in range(640):
            reference_color = colored_pc[0, 0, :].tolist()
            get_color = colored_pc[i, j, :].tolist()
            if get_color != reference_color:
                bgr = cv2.circle(bgr, (j, i), 5, get_color, -1)

    save_file_path = os.path.join(save_path, file[:-2]) + 'png'
    cv2.imwrite(save_file_path, bgr)
예제 #11
0
def plot_colored_depth(rel_path):

    fList = os.listdir(rel_path)
    NewList = sorted(fList, key=lambda x: int(x[7:-4]))

    count = 0

    for file in NewList:
        depth_path = rel_path + file
        depth_array = convert_img2csv_str2nparray(depth_path)

        new_depth_image = colored_depthmap(depth_array)


        '''
        Use the following
        if you want to plot rgb and depth concurrently: 
        
        img_path = img_rel_path + file[:-3] + 'png'
        img = imread(img_path)
        f = plt.figure()
        f.set_size_inches(18.5, 10.5, forward=True)
        f.add_subplot(1, 2, 1)
        plt.imshow(new_depth_image, cmap='jet')
        f.add_subplot(1, 2, 2)
        plt.imshow(img)
        plt.show()
        plt.clf()
        plt.close()
        '''

        cv2.imshow("colored depth", new_depth_image)
        cv2.waitKey(0)

        count = count + 1
        if count == 30:
            break
예제 #12
0
from utils import colored_depthmap
from convert_str2nparray import convert_img2csv_str2nparray

rel_path = '/home/hj/ICRA2020/190403/rp_img2csv/'
img_rel_path = '/home/hj/ICRA2020/190403/img/'
save_rel_path = '/home/hj/ICRA2020/190403/debug/'

fList = os.listdir(rel_path)
NewList = sorted(fList, key=lambda x: int(x[7:-4]))

count = 0

for file in NewList:
    depth_path = rel_path + file
    NpArray = convert_img2csv_str2nparray(depth_path)
    CMap = colored_depthmap(NpArray, 0)

    img_path = img_rel_path + file[:-3] + 'png'
    img = imread(img_path)

    save_path = save_rel_path + file[:-3] + 'png'

    f = plt.figure()
    f.set_size_inches(18.5, 10.5, forward=True)
    f.add_subplot(1, 2, 1)
    plt.imshow(CMap, 'bwr', origin='lower')
    f.add_subplot(1, 2, 2)
    plt.imshow(img)
    plt.show()

    #f.savefig(save_path)