verify_denoise_ply.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import open3d as o3d
  2. import numpy as np
  3. import os
  4. def reconstruct_and_save_ply(npy_dir, output_ply_path):
  5. """
  6. Reads coord.npy, color.npy, and normal.npy from a directory,
  7. reconstructs a point cloud, and saves it as a .ply file.
  8. """
  9. print(f"Processing directory: {npy_dir}")
  10. # Define file paths
  11. coord_file = os.path.join(npy_dir, 'coord.npy')
  12. color_file = os.path.join(npy_dir, 'color.npy')
  13. normal_file = os.path.join(npy_dir, 'normal.npy')
  14. # Check if coordinate file exists (it's required)
  15. if not os.path.exists(coord_file):
  16. print(f" - Error: coord.npy not found in {npy_dir}. Skipping.")
  17. return
  18. # Load coordinates
  19. try:
  20. coords = np.load(coord_file)
  21. print(f" - Loaded {len(coords)} points from coord.npy.")
  22. except Exception as e:
  23. print(f" - Error loading coord.npy: {e}. Skipping.")
  24. return
  25. # Create point cloud object
  26. pcd = o3d.geometry.PointCloud()
  27. pcd.points = o3d.utility.Vector3dVector(coords)
  28. # Load colors if available
  29. if os.path.exists(color_file):
  30. try:
  31. colors = np.load(color_file)
  32. if len(colors) == len(coords):
  33. pcd.colors = o3d.utility.Vector3dVector(colors)
  34. print(" - Loaded colors.")
  35. else:
  36. print(" - Warning: Mismatch between point count and color count. Skipping colors.")
  37. except Exception as e:
  38. print(f" - Error loading color.npy: {e}.")
  39. else:
  40. print(" - info: color.npy not found.")
  41. # Load normals if available
  42. if os.path.exists(normal_file):
  43. try:
  44. normals = np.load(normal_file)
  45. if len(normals) == len(coords):
  46. pcd.normals = o3d.utility.Vector3dVector(normals)
  47. print(" - Loaded normals.")
  48. else:
  49. print(" - Warning: Mismatch between point count and normal count. Skipping normals.")
  50. except Exception as e:
  51. print(f" - Error loading normal.npy: {e}.")
  52. else:
  53. print(" - info: normal.npy not found.")
  54. # Save the reconstructed point cloud
  55. try:
  56. o3d.io.write_point_cloud(output_ply_path, pcd, write_ascii=True)
  57. print(f" - Successfully saved reconstructed point cloud to: {output_ply_path}")
  58. except Exception as e:
  59. print(f" - Error saving .ply file: {e}")
  60. if __name__ == '__main__':
  61. scenes_folder = "/media/gu/d54b9541-2b55-4c75-b059-3006d51983d53/lqc/Downloads/Scenes"
  62. scenes = os.listdir(scenes_folder)
  63. for scene_name in scenes:
  64. print(f"\nVerifying scene: {scene_name}...")
  65. scene_folder = os.path.join(scenes_folder, scene_name)
  66. npy_folder = os.path.join(scene_folder, "scene/val/process_data")
  67. # Directory where the .npy files are located
  68. process_folder = os.path.join(scene_folder, 'denoise_ply')
  69. if not os.path.exists(process_folder):
  70. os.makedirs(process_folder)
  71. # Path for the output .ply file
  72. output_ply = os.path.join(scene_folder, 'verified_cut_result.ply')
  73. if os.path.isdir(process_folder):
  74. reconstruct_and_save_ply(npy_folder, output_ply)
  75. else:
  76. print(f" - Process data folder not found: {process_folder}. Skipping scene.")