| |
| """ |
| preprocess_step3_ptv3.py |
| ========================= |
| NEST3D Pre-processing Step 3: Convert corrected PLY files to Pointcept format. |
| |
| Input: <data-dir>/sampleXXX/sampleXXX_corrected.ply |
| <split-dir>/{train,val,test}.txt (one sample ID per line) |
| Output: <out-dir>/train|val|test/sampleXXX/{coord.npy, color.npy, segment.npy} |
| |
| Format: |
| coord.npy : (N, 3) float32 - XYZ coordinates, centered on the scene |
| centroid and normalized to the unit |
| sphere (max radius = 1) |
| color.npy : (N, 3) float32 - RGB colors scaled to [0, 1] |
| segment.npy : (N,) int32 - semantic labels: 0=grass, 1=tree, 2=nest, |
| -1=ignore |
| |
| Semantic labels (0/1/2) are preserved as-is from the corrected PLY files; |
| the ignore label is remapped from 255 (CloudCompare convention) to -1 |
| (Pointcept convention). |
| |
| The train/val/test split is defined by train.txt, val.txt, and test.txt |
| (one sample ID per line) and is not hardcoded here -- this script simply |
| reads and applies whatever split those files define. This keeps the split |
| definition in one authoritative place. |
| |
| Usage: |
| python preprocess_step3_ptv3.py \\ |
| --data-dir /path/to/reconstructions \\ |
| --split-dir /path/to/split/txt/files \\ |
| --out-dir /path/to/output |
| |
| Author: NEST3D team |
| """ |
|
|
| import argparse |
| import numpy as np |
| from pathlib import Path |
| from plyfile import PlyData |
|
|
| |
| def load_split(split_dir, split_name): |
| path = split_dir / f"{split_name}.txt" |
| with open(path) as f: |
| samples = [line.strip() for line in f if line.strip()] |
| return samples |
|
|
| def load_ply(path): |
| ply = PlyData.read(str(path)) |
| v = ply["vertex"] |
| xyz = np.stack([v["x"], v["y"], v["z"]], axis=1).astype(np.float32) |
| rgb = np.stack([v["red"], v["green"], v["blue"]], axis=1).astype(np.float32) / 255.0 |
| lbl = np.array(v["scalar_Classification"], dtype=np.float32).astype(np.int32) |
| return xyz, rgb, lbl |
|
|
| def unit_sphere(xyz): |
| centroid = xyz.mean(axis=0) |
| xyz = xyz - centroid |
| max_radius = np.sqrt((xyz**2).sum(axis=1)).max() |
| if max_radius > 0: |
| xyz = xyz / max_radius |
| return xyz.astype(np.float32) |
|
|
| def remap_ignore(lbl): |
| """Only remap 255 -> -1 (ignore). Keep 0, 1, 2 as-is.""" |
| out = lbl.copy() |
| out[lbl == 255] = -1 |
| return out.astype(np.int32) |
|
|
| def save_sample(xyz, rgb, lbl, out_dir): |
| out_dir.mkdir(parents=True, exist_ok=True) |
| np.save(str(out_dir / "coord.npy"), xyz) |
| np.save(str(out_dir / "color.npy"), rgb) |
| np.save(str(out_dir / "segment.npy"), lbl) |
|
|
| |
| def main(): |
| parser = argparse.ArgumentParser(description="NEST3D Step 3: convert to Pointcept format") |
| parser.add_argument("--data-dir", type=Path, default=Path("./reconstructions"), |
| help="Path to reconstructions/ containing sampleXXX_corrected.ply files") |
| parser.add_argument("--split-dir", type=Path, default=Path("."), |
| help="Path to the directory containing train.txt, val.txt, test.txt") |
| parser.add_argument("--out-dir", type=Path, default=Path("./nest"), |
| help="Output directory for the Pointcept-format data") |
| args = parser.parse_args() |
|
|
| recon_dir = args.data_dir |
| out_dir = args.out_dir |
|
|
| all_splits = [(name, load_split(args.split_dir, name)) for name in ["train", "val", "test"]] |
|
|
| for split, samples in all_splits: |
| (out_dir / split).mkdir(parents=True, exist_ok=True) |
| print(f"\n=== {split.upper()} ({len(samples)} samples) ===") |
| for sample_id in samples: |
| sample_out_dir = out_dir / split / sample_id |
| ply_path = recon_dir / sample_id / f"{sample_id}_corrected.ply" |
|
|
| if all((sample_out_dir / f).exists() for f in ["coord.npy","color.npy","segment.npy"]): |
| print(f" [SKIP] {sample_id}") |
| continue |
|
|
| if not ply_path.exists(): |
| print(f" [MISSING] {sample_id}: {ply_path.name} not found") |
| continue |
|
|
| print(f" [PROC] {sample_id} ...", end=" ", flush=True) |
| xyz, rgb, lbl = load_ply(ply_path) |
| lbl = remap_ignore(lbl) |
| xyz = unit_sphere(xyz) |
|
|
| n_total = len(lbl) |
| n_grass = int((lbl==0).sum()) |
| n_tree = int((lbl==1).sum()) |
| n_nest = int((lbl==2).sum()) |
| n_ign = int((lbl==-1).sum()) |
|
|
| save_sample(xyz, rgb, lbl, sample_out_dir) |
| print(f"n={n_total:,} | grass={100*n_grass/n_total:.1f}% " |
| f"tree={100*n_tree/n_total:.1f}% nest={100*n_nest/n_total:.1f}% " |
| f"ignore={100*n_ign/n_total:.1f}%") |
|
|
| print(f"\nDone! Output: {out_dir}") |
| for split, samples in all_splits: |
| print(f" {split}: {len(samples)} samples") |
|
|
| if __name__ == "__main__": |
| main() |
|
|