text: import numpy as np
  import cv2
  cv2.setNumThreads(1)
  import argparse
  from evaluation_utils import *
  
  parser = argparse.ArgumentParser(description='Evaluation on the KITTI dataset')
  parser.add_argument('--split', type=str, help='data split, kitti or eigen', required=True)
  parser.add_argument('--predicted_disp_path', type=str, help='path to estimated disparities', required=True)
  parser.add_argument('--gt_path', type=str, help='path to ground truth disparities', required=True)
  parser.add_argument('--min_depth', type=float, help='minimum depth for evaluation', default=1e-3)
  parser.add_argument('--max_depth', type=float, help='maximum depth for evaluation', default=80)
  parser.add_argument('--eigen_crop', help='if set, crops according to Eigen NIPS14', action='store_true')
  parser.add_argument('--garg_crop', help='if set, crops according to Garg  ECCV16', action='store_true')
  
  
  def evaluate_command_line():
      args = parser.parse_args()
      pred_disparities = np.load(args.predicted_disp_path)
  
      if args.split == 'kitti':
          num_samples = 200
  
          gt_disparities = load_gt_disp_kitti(args.gt_path)
          gt_depths, pred_depths, pred_disparities_resized = convert_disps_to_depths_kitti(gt_disparities, pred_disparities)
  
      elif args.split == 'eigen':
          num_samples = 697
          test_files = read_text_lines('./utils/filenames/eigen_test_files.txt')
          gt_files, gt_calib, im_sizes, im_files, cams = read_file_data(test_files, args.gt_path)
  
          num_test = len(im_files)
          gt_depths = []
          pred_depths = []
          for t_id in range(num_samples):
              camera_id = cams[t_id]  # 2 is left, 3 is right
              depth = generate_depth_map(gt_calib[t_id], gt_files[t_id], im_sizes[t_id], camera_id, False, False)
              gt_depths.append(depth.astype(np.float32))
  
              disp_pred = cv2.resize(pred_disparities[t_id], (im_sizes[t_id][1], im_sizes[t_id][0]), interpolation=cv2.INTER_LINEAR)
              disp_pred = disp_pred * disp_pred.shape[1]
  
              # need to convert from disparity to depth
              focal_length, baseline = get_focal_length_baseline(gt_calib[t_id], camera_id)
              # print("focal length, baseline: {}/{}, mul: {}".format(focal_length, baseline, baseline * focal_length))
              depth_pred = (baseline * focal_length) / disp_pred
              depth_pred[np.isinf(depth_pred)] = 0
  
              pred_depths.append(depth_pred)
  
      rms     = np.zeros(num_samples, np.float32)
      log_rms = np.zeros(num_samples, np.float32)
      abs_rel = np.zeros(num_samples, np.float32)
      sq_rel  = np.zeros(num_samples, np.float32)
      d1_all  = np.zeros(num_samples, np.float32)
      a1      = np.zeros(num_samples, np.float32)
      a2      = np.zeros(num_samples, np.float32)
      a3      = np.zeros(num_samples, np.float32)
  
      for i in range(num_samples):
  
          gt_depth = gt_depths[i]
          pred_depth = pred_depths[i]
  
          pred_depth[pred_depth < args.min_depth] = args.min_depth
          pred_depth[pred_depth > args.max_depth] = args.max_depth
  
          if args.split == 'eigen':
              mask = np.logical_and(gt_depth > args.min_depth, gt_depth < args.max_depth)
  
  
              if args.garg_crop or args.eigen_crop:
                  gt_height, gt_width = gt_depth.shape
  
                  # crop used by Garg ECCV16
                  # if used on gt_size 370x1224 produces a crop of [-218, -3, 44, 1180]
                  if args.garg_crop:
                      crop = np.array([0.40810811 * gt_height,  0.99189189 * gt_height,
                                       0.03594771 * gt_width,   0.96405229 * gt_width]).astype(np.int32)
                  # crop we found by trial and error to reproduce Eigen NIPS14 results
                  elif args.eigen_crop:
                      crop = np.array([0.3324324 * gt_height,  0.91351351 * gt_height,
                                       0.0359477 * gt_width,   0.96405229 * gt_width]).astype(np.int32)
  
                  crop_mask = np.zeros(mask.shape)
                  crop_mask[crop[0]:crop[1],crop[2]:crop[3]] = 1
                  mask = np.logical_and(mask, crop_mask)
  
          if args.split == 'kitti':
              gt_disp = gt_disparities[i]
              mask = gt_disp > 0
              pred_disp = pred_disparities_resized[i]
  
              disp_diff = np.abs(gt_disp[mask] - pred_disp[mask])
              bad_pixels = np.logical_and(disp_diff >= 3, (disp_diff / gt_disp[mask]) >= 0.05)
              d1_all[i] = 100.0 * bad_pixels.sum() / mask.sum()
  
          abs_rel[i], sq_rel[i], rms[i], log_rms[i], a1[i], a2[i], a3[i] = compute_errors(gt_depth[mask], pred_depth[mask])
  
      print("{:>10}, {:>10}, {:>10}, {:>10}, {:>10}, {:>10}, {:>10}, {:>10}".format('abs_rel', 'sq_rel', 'rms', 'log_rms', 'd1_all', 'a1', 'a2', 'a3'))
      print("{:10.4f}, {:10.4f}, {:10.3f}, {:10.3f}, {:10.3f}, {:10.3f}, {:10.3f}, {:10.3f}".format(abs_rel.mean(), sq_rel.mean(), rms.mean(), log_rms.mean(), d1_all.mean(), a1.mean(), a2.mean(), a3.mean()))
  
  
  def evaluate_images(pred_disparities, test_files='./utils/filenames/eigen_test_files.txt', gt_path="/home/xyguo/dataset/kitti_png/"):
      # --split eigen
      # --garg_crop
      # --predicted_disp_path. /$1/disparities_pp.npy
      # --gt_path ~/dataset/kitti_png/
      args = parser.parse_args(["--split", "eigen", "--garg_crop", "--predicted_disp_path", None, "--gt_path", gt_path])
      # print("evaluate args: {}".format(args))
  
      if args.split == 'kitti':
          num_samples = 200
          assert False
  
          # gt_disparities = load_gt_disp_kitti(args.gt_path)
          # gt_depths, pred_depths, pred_disparities_resized = convert_disps_to_depths_kitti(gt_disparities,
          #                                                                                  pred_disparities)
      elif args.split == 'eigen':
          if isinstance(test_files, str):
              test_files = read_text_lines(test_files)
              num_samples = 697
          elif isinstance(test_files, list):
              num_samples = len(test_files)
              pass
          else:
              assert False
          assert pred_disparities.shape[0] == num_samples
  
          # get gt filenames, calib info, image sizes, image filenames, cam ids
          gt_files, gt_calib, im_sizes, im_files, cams = read_file_data(test_files, args.gt_path)
  
          # num_test = len(im_files)
          gt_depths = []
          pred_depths = []
          for t_id in range(num_samples):
              camera_id = cams[t_id]  # 2 is left, 3 is right
              depth = generate_depth_map(gt_calib[t_id], gt_files[t_id], im_sizes[t_id], camera_id, False, False)
              gt_depths.append(depth.astype(np.float32))
  
              disp_pred = cv2.resize(pred_disparities[t_id], (im_sizes[t_id][1], im_sizes[t_id][0]),
                                     interpolation=cv2.INTER_LINEAR)
              disp_pred = disp_pred * disp_pred.shape[1]
  
              # need to convert from disparity to depth
              focal_length, baseline = get_focal_length_baseline(gt_calib[t_id], camera_id)
              depth_pred = (baseline * focal_length) / disp_pred
              depth_pred[np.isinf(depth_pred)] = 0
  
              pred_depths.append(depth_pred)
      else:
          assert False, "invalid split name, either kitti or eigen"
  
      rms = np.zeros(num_samples, np.float32)
      log_rms = np.zeros(num_samples, np.float32)
      abs_rel = np.zeros(num_samples, np.float32)
      sq_rel = np.zeros(num_samples, np.float32)
      d1_all = np.zeros(num_samples, np.float32)
      a1 = np.zeros(num_samples, np.float32)
      a2 = np.zeros(num_samples, np.float32)
      a3 = np.zeros(num_samples, np.float32)
  
      for i in range(num_samples):
  
          gt_depth = gt_depths[i]
          pred_depth = pred_depths[i]
  
          pred_depth[pred_depth < args.min_depth] = args.min_depth
          pred_depth[pred_depth > args.max_depth] = args.max_depth
  
          if args.split == 'eigen':
              mask = np.logical_and(gt_depth > args.min_depth, gt_depth < args.max_depth)
  
              if args.garg_crop or args.eigen_crop:
                  gt_height, gt_width = gt_depth.shape
  
                  # crop used by Garg ECCV16
                  # if used on gt_size 370x1224 produces a crop of [-218, -3, 44, 1180]
                  if args.garg_crop:
                      crop = np.array([0.40810811 * gt_height, 0.99189189 * gt_height,
                                       0.03594771 * gt_width, 0.96405229 * gt_width]).astype(np.int32)
                  # crop we found by trial and error to reproduce Eigen NIPS14 results
                  elif args.eigen_crop:
                      crop = np.array([0.3324324 * gt_height, 0.91351351 * gt_height,
                                       0.0359477 * gt_width, 0.96405229 * gt_width]).astype(np.int32)
  
                  crop_mask = np.zeros(mask.shape)
                  crop_mask[crop[0]:crop[1], crop[2]:crop[3]] = 1
                  mask = np.logical_and(mask, crop_mask)
  
          # if args.split == 'kitti':
          #     gt_disp = gt_disparities[i]
          #     mask = gt_disp > 0
          #     pred_disp = pred_disparities_resized[i]
          #
          #     disp_diff = np.abs(gt_disp[mask] - pred_disp[mask])
          #     bad_pixels = np.logical_and(disp_diff >= 3, (disp_diff / gt_disp[mask]) >= 0.05)
          #     d1_all[i] = 100.0 * bad_pixels.sum() / mask.sum()
  
          abs_rel[i], sq_rel[i], rms[i], log_rms[i], a1[i], a2[i], a3[i] = compute_errors(gt_depth[mask],
                                                                                          pred_depth[mask])
  
      abs_rel_mean = abs_rel.mean()
      sq_rel_mean = sq_rel.mean()
      rms_mean = rms.mean()
      log_rms_mean = log_rms.mean()
      d1_all_mean = d1_all.mean()
      a1_mean = a1.mean()
      a2_mean = a2.mean()
      a3_mean = a3.mean()
      # print("tested {} images".format(num_samples))
      # print("{:>10}, {:>10}, {:>10}, {:>10}, {:>10}, {:>10}, {:>10}, {:>10}, {:>10}".format('num', 'abs_rel', 'sq_rel', 'rms', 'log_rms',
      #                                                                               'd1_all', 'a1', 'a2', 'a3'))
      # print("{:10d}, {:10.4f}, {:10.4f}, {:10.3f}, {:10.3f}, {:10.3f}, {:10.3f}, {:10.3f}, {:10.3f}".format(num_samples, abs_rel_mean,
      #                                                                                               sq_rel_mean,
      #                                                                                               rms_mean,
      #                                                                                               log_rms_mean,
      #                                                                                               d1_all_mean,
      #                                                                                               a1_mean, a2_mean,
      #                                                                                               a3_mean))
      return {"abs_rel": abs_rel_mean, "sq_rel": sq_rel_mean, "rms": rms_mean, "log_rms": log_rms_mean,
              "d1_all": d1_all_mean, "a1": a1_mean, "a2": a2_mean, "a3": a3_mean}
  
  
  def evaluate_images_abs(pred_disparities, test_files='./utils/filenames/eigen_test_files.txt', gt_path="/home/xyguo/dataset/kitti_raw/"):
      assert len(pred_disparities.shape) == 3
      return evaluate_images(pred_disparities / pred_disparities.shape[2], test_files, gt_path)
  
  
  def test_evaluate_image():
      args = parser.parse_args()
      pred_disparities = np.load(args.predicted_disp_path)
      evaluate_images(pred_disparities)
  
  if __name__ == '__main__':
      evaluate_command_line()
      # test_evaluate_image()
  