diml_to_interiornet_2.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. def full_to_interiornet():
  2. scene_file_path = "/nvme/datasets/diml_depth/scenes.txt"
  3. base_path = "/nvme/datasets/diml_depth/"
  4. out_path = "/nvme/datasets/diml_depth/HD7/"
  5. cam0_render = "/nvme/datasets/interiornet/3FO4IDEI1LAV_Bedroom/cam0.render"
  6. num_frames = 20
  7. shape = (672, 378)
  8. np.random.seed(123)
  9. with open(scene_file_path, 'r') as f:
  10. scene_lines = f.readlines()
  11. scene_lines = [sn.split('\n')[0] for sn in scene_lines]
  12. scene_paths = [os.path.join(base_path, sn.split('-')[0]) for sn in scene_lines]
  13. scene_ranges = [sn.split('-')[1] for sn in scene_lines]
  14. scene_ranges = [(int(rn[1:-1].split(':')[0]), int(rn[1:-1].split(':')[1])) for rn in scene_ranges]
  15. for i, scene_path in enumerate(scene_paths):
  16. file_list = []
  17. for j in range(scene_ranges[i][0], scene_ranges[i][1]+1):
  18. scene_path_col = os.path.join(scene_path, "{}/col".format(j))
  19. if os.path.exists(scene_path_col):
  20. file_list += [os.path.join(scene_path_col, dn) for dn in os.listdir(scene_path_col)]
  21. scene_count = len(os.listdir(out_path))
  22. scene_out_path = "{:02d}DIML_{}".format(scene_count + 1, scene_path.split('/')[-2].split(' ')[1])
  23. scene_out_path = os.path.join(out_path, scene_out_path)
  24. if os.path.exists(scene_out_path):
  25. shutil.rmtree(scene_out_path)
  26. os.mkdir(scene_out_path)
  27. os.mkdir(os.path.join(scene_out_path, "cam0"))
  28. os.mkdir(os.path.join(scene_out_path, "depth0"))
  29. os.mkdir(os.path.join(scene_out_path, "label0"))
  30. os.mkdir(os.path.join(scene_out_path, "cam0", "data"))
  31. os.mkdir(os.path.join(scene_out_path, "depth0", "data"))
  32. os.mkdir(os.path.join(scene_out_path, "label0", "data"))
  33. shutil.copyfile(cam0_render, os.path.join(scene_out_path, "cam0.render"))
  34. print(scene_out_path)
  35. frame_paths = np.random.choice(file_list, num_frames, False)
  36. for j, frame_path in enumerate(frame_paths):
  37. img = cv2.imread(frame_path, cv2.IMREAD_UNCHANGED)
  38. depth_path = frame_path.replace('/col/', '/up_png/')
  39. depth_path = depth_path.replace('_c.png', '_ud.png')
  40. depth_img = cv2.imread(depth_path, cv2.IMREAD_UNCHANGED)
  41. if depth_img is None:
  42. print(depth_path)
  43. exit()
  44. if img is None:
  45. print(frame_path)
  46. exit()
  47. img = cv2.resize(img, dsize=shape, interpolation=cv2.INTER_LINEAR)
  48. depth_img = cv2.resize(depth_img, dsize=shape,
  49. interpolation=cv2.INTER_LINEAR)
  50. label_img = depth_img.copy()
  51. label_img[:, :] = 3
  52. cv2.imwrite(os.path.join(scene_out_path, "cam0", "data", "{}.png".format(j)), img)
  53. cv2.imwrite(os.path.join(scene_out_path, "depth0", "data", "{}.png".format(j)), depth_img)
  54. cv2.imwrite(os.path.join(scene_out_path, "label0", "data", "{}_instance.png".format(j)), label_img)
  55. cv2.imwrite(os.path.join(scene_out_path, "label0", "data", "{}_nyu.png".format(j)), label_img)
  56. if __name__ == '__main__':
  57. full_to_interiornet()