diml_to_interiornet.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import cv2
  2. import os
  3. import shutil
  4. import numpy as np
  5. def sample_to_interiornet():
  6. diml_path = "/nvme/datasets/diml_depth/scenes"
  7. hd7_path = "/nvme/datasets/diml_depth/HD7"
  8. depth_paths = [
  9. "/nvme/datasets/diml_depth/train/HR/11. Bedroom/depth_filled",
  10. "/nvme/datasets/diml_depth/train/HR/12. Livingroom/depth_filled"]
  11. depth_images = []
  12. for path in depth_paths:
  13. depth_images += [os.path.join(path, name) for name in os.listdir(path)
  14. if os.path.isfile(os.path.join(path, name))]
  15. scene_paths = [os.path.join(diml_path, name) for name in os.listdir(diml_path)
  16. if os.path.isdir(os.path.join(diml_path, name))]
  17. for scene_path in scene_paths:
  18. frame_paths = [os.path.join(scene_path, name) for name in os.listdir(scene_path)
  19. if os.path.isfile(os.path.join(scene_path, name))]
  20. new_frame_path = os.path.join(hd7_path, scene_path.split('/')[-1])
  21. os.mkdir(new_frame_path)
  22. os.mkdir(os.path.join(new_frame_path, "cam0"))
  23. os.mkdir(os.path.join(new_frame_path, "depth0"))
  24. os.mkdir(os.path.join(new_frame_path, "label0"))
  25. os.mkdir(os.path.join(new_frame_path, "cam0", "data"))
  26. os.mkdir(os.path.join(new_frame_path, "depth0", "data"))
  27. os.mkdir(os.path.join(new_frame_path, "label0", "data"))
  28. print(new_frame_path)
  29. for i, frame_path in enumerate(frame_paths):
  30. file_name = frame_path.split('/')[-1][:-6]
  31. img = cv2.imread(frame_path, cv2.IMREAD_UNCHANGED)
  32. print(file_name)
  33. depth_path = [path for path in depth_images if file_name in path][0]
  34. depth_img = cv2.imread(depth_path, cv2.IMREAD_UNCHANGED)
  35. img = cv2.resize(img, dsize=(img.shape[1] / 2, img.shape[0] / 2), interpolation=cv2.INTER_LINEAR)
  36. depth_img = cv2.resize(depth_img, dsize=(depth_img.shape[1] / 2, depth_img.shape[0] / 2),
  37. interpolation=cv2.INTER_LINEAR)
  38. label_img = depth_img.copy()
  39. label_img[:, :] = 3
  40. cv2.imwrite(os.path.join(new_frame_path, "cam0", "data", "{}.png".format(i)), img)
  41. cv2.imwrite(os.path.join(new_frame_path, "depth0", "data", "{}.png".format(i)), depth_img)
  42. cv2.imwrite(os.path.join(new_frame_path, "label0", "data", "{}_instance.png".format(i)), label_img)
  43. cv2.imwrite(os.path.join(new_frame_path, "label0", "data", "{}_nyu.png".format(i)), label_img)
  44. def full_to_interiornet():
  45. scene_file_path = "/nvme/datasets/diml_depth/scenes.txt"
  46. base_path = "/nvme/datasets/diml_depth/"
  47. out_path = "/nvme/datasets/diml_depth/HD7/"
  48. cam0_render = "/nvme/datasets/interiornet/3FO4IDEI1LAV_Bedroom/cam0.render"
  49. num_frames = 20
  50. shape = (672, 378)
  51. np.random.seed(123)
  52. with open(scene_file_path, 'r') as f:
  53. scene_lines = f.readlines()
  54. scene_lines = [sn.split('\n')[0] for sn in scene_lines]
  55. scene_paths = [os.path.join(base_path, sn.split('-')[0]) for sn in scene_lines]
  56. scene_ranges = [sn.split('-')[1] for sn in scene_lines]
  57. scene_ranges = [(int(rn[1:-1].split(':')[0]), int(rn[1:-1].split(':')[1])) for rn in scene_ranges]
  58. for i, scene_path in enumerate(scene_paths):
  59. file_list = []
  60. for j in range(scene_ranges[i][0], scene_ranges[i][1]+1):
  61. scene_path_col = os.path.join(scene_path, "{}/col".format(j))
  62. if os.path.exists(scene_path_col):
  63. file_list += [os.path.join(scene_path_col, dn) for dn in os.listdir(scene_path_col)]
  64. scene_count = len(os.listdir(out_path))
  65. scene_out_path = "{:02d}DIML_{}".format(scene_count + 1, scene_path.split('/')[-2].split(' ')[1])
  66. scene_out_path = os.path.join(out_path, scene_out_path)
  67. if os.path.exists(scene_out_path):
  68. shutil.rmtree(scene_out_path)
  69. os.mkdir(scene_out_path)
  70. os.mkdir(os.path.join(scene_out_path, "cam0"))
  71. os.mkdir(os.path.join(scene_out_path, "depth0"))
  72. os.mkdir(os.path.join(scene_out_path, "label0"))
  73. os.mkdir(os.path.join(scene_out_path, "cam0", "data"))
  74. os.mkdir(os.path.join(scene_out_path, "depth0", "data"))
  75. os.mkdir(os.path.join(scene_out_path, "label0", "data"))
  76. shutil.copyfile(cam0_render, os.path.join(scene_out_path, "cam0.render"))
  77. print(scene_out_path)
  78. frame_paths = np.random.choice(file_list, num_frames, False)
  79. for j, frame_path in enumerate(frame_paths):
  80. img = cv2.imread(frame_path, cv2.IMREAD_UNCHANGED)
  81. depth_path = frame_path.replace('/col/', '/up_png/')
  82. depth_path = depth_path.replace('_c.png', '_ud.png')
  83. depth_img = cv2.imread(depth_path, cv2.IMREAD_UNCHANGED)
  84. if depth_img is None:
  85. print(depth_path)
  86. exit()
  87. if img is None:
  88. print(frame_path)
  89. exit()
  90. img = cv2.resize(img, dsize=shape, interpolation=cv2.INTER_LINEAR)
  91. depth_img = cv2.resize(depth_img, dsize=shape,
  92. interpolation=cv2.INTER_LINEAR)
  93. label_img = depth_img.copy()
  94. label_img[:, :] = 3
  95. cv2.imwrite(os.path.join(scene_out_path, "cam0", "data", "{}.png".format(j)), img)
  96. cv2.imwrite(os.path.join(scene_out_path, "depth0", "data", "{}.png".format(j)), depth_img)
  97. cv2.imwrite(os.path.join(scene_out_path, "label0", "data", "{}_instance.png".format(j)), label_img)
  98. cv2.imwrite(os.path.join(scene_out_path, "label0", "data", "{}_nyu.png".format(j)), label_img)
  99. if __name__ == '__main__':
  100. full_to_interiornet()