-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathmain.py
More file actions
150 lines (134 loc) · 5.85 KB
/
Copy pathmain.py
File metadata and controls
150 lines (134 loc) · 5.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/env python3
"""DynamicMap_Benchmark adapter for dynamic-object-removal (numpy-only).
Usage (from a cloned DynamicMap_Benchmark repo, after
``pip install "dynamic-object-removal>=0.5"``):
python main.py --data_dir /path/to/00 --algorithm fusion
python main.py --data_dir /path/to/00 --algorithm range
python main.py --data_dir /path/to/00 --algorithm scan_ratio
python main.py --data_dir /path/to/00 --algorithm temporal
Writes ``dor_<algorithm>_output.pcd`` into ``data_dir`` (cleaned accumulated map).
Run ``export_eval_pcd`` + ``evaluate_all.py`` in the benchmark repo for SA/DA/AA/HA.
"""
from __future__ import annotations
import argparse
from pathlib import Path
import numpy as np
try:
import dynamic_object_removal as core
except ImportError as exc:
raise SystemExit(
'Install first: pip install "dynamic-object-removal>=0.5"'
) from exc
def _load_sequence(pcd_dir: Path) -> tuple[np.ndarray, list[tuple[np.ndarray, np.ndarray]], list[tuple[int, int]]]:
scan_files = sorted(pcd_dir.glob("*.pcd"))
if not scan_files:
raise SystemExit(f"No scans in {pcd_dir}")
chunks: list[np.ndarray] = []
scans: list[tuple[np.ndarray, np.ndarray]] = []
slices: list[tuple[int, int]] = []
cursor = 0
for path in scan_files:
scan = core.load_pcd_scan(path)
# PCD defaults VIEWPOINT to identity, so a header at the default value and
# a scan genuinely captured at the world origin are indistinguishable —
# accept both as a zero origin.
origin = scan.viewpoint[:3] if scan.viewpoint is not None else np.zeros(3)
n = len(scan.points)
chunks.append(scan.points)
scans.append((scan.points, origin))
slices.append((cursor, cursor + n))
cursor += n
return np.concatenate(chunks, axis=0), scans, slices
def _clean(
algorithm: str,
acc_map: np.ndarray,
scans: list[tuple[np.ndarray, np.ndarray]],
slices: list[tuple[int, int]],
*,
h_res: float,
v_res: float,
min_see_through: int,
max_surface_hits: int,
voxel_size: float,
temporal_min_hits: int,
sr_min_votes: int | None,
fusion_workers: int,
) -> np.ndarray:
if algorithm == "fusion":
# OR-fusion of free-space carving, DUFOMap-style eroded voids, and
# scan-ratio votes — the library's highest-accuracy map cleaner.
_, keep = core.clean_map_by_fusion(acc_map, scans, workers=fusion_workers)
elif algorithm == "range":
ground_z = float(np.percentile(acc_map[:, 2], 2))
_, keep = core.clean_map_by_visibility(
acc_map,
scans,
h_res_deg=h_res,
v_res_deg=v_res,
min_see_through=min_see_through,
max_surface_hits=max_surface_hits,
ground_z=ground_z,
)
elif algorithm == "scan_ratio":
# min_votes=None: the library normalizes votes per point by the number of
# scans that revisit its polar column (majority rule).
_, keep = core.clean_map_by_scan_ratio(acc_map, scans, min_votes=sr_min_votes)
elif algorithm == "temporal":
keep = np.ones(len(acc_map), dtype=bool)
tfilter = core.TemporalConsistencyFilter(
voxel_size=voxel_size,
window_size=len(scans),
min_hits=temporal_min_hits,
)
# Two passes over the same scans: the first fills the hit counter
# (window_size == number of scans, so nothing is evicted), the second
# judges each scan against the full-sequence counts — re-inserting a
# scan evicts its own first-pass copy, leaving the counter unchanged.
for s, e in slices:
tfilter.filter(acc_map[s:e])
for s, e in slices:
_, keep_f = tfilter.filter(acc_map[s:e])
keep[s:e] = keep_f
else:
raise SystemExit(f"Unknown algorithm: {algorithm}")
return acc_map[keep]
def main() -> int:
parser = argparse.ArgumentParser(description="DynamicMap_Benchmark adapter (numpy-only).")
parser.add_argument("--data_dir", required=True, help="Sequence folder with pcd/ and gt_cloud.pcd.")
parser.add_argument("--algorithm", choices=["fusion", "range", "scan_ratio", "temporal"], default="fusion")
parser.add_argument("--h-res", type=float, default=1.0)
parser.add_argument("--v-res", type=float, default=1.0)
# 3/3 (vs the library's 2/2 default) is the setting behind the README range
# numbers, matching the upstream library's run_dynamicmap_benchmark.py.
parser.add_argument("--min-see-through", type=int, default=3)
parser.add_argument("--max-surface-hits", type=int, default=3)
parser.add_argument("--voxel-size", type=float, default=core.DEFAULT_TEMPORAL_VOXEL_SIZE)
parser.add_argument("--temporal-min-hits", type=int, default=2)
parser.add_argument("--sr-min-votes", type=int, default=None,
help="Fixed absolute vote threshold (default: majority of each point's column revisits).")
parser.add_argument("--fusion-workers", type=int, default=6,
help="Process pool size for the fusion carving channels.")
args = parser.parse_args()
data_dir = Path(args.data_dir)
pcd_dir = data_dir / "pcd"
acc_map, scans, slices = _load_sequence(pcd_dir)
cleaned = _clean(
args.algorithm,
acc_map,
scans,
slices,
h_res=args.h_res,
v_res=args.v_res,
min_see_through=args.min_see_through,
max_surface_hits=args.max_surface_hits,
voxel_size=args.voxel_size,
temporal_min_hits=args.temporal_min_hits,
sr_min_votes=args.sr_min_votes,
fusion_workers=args.fusion_workers,
)
out = data_dir / f"dor_{args.algorithm}_output.pcd"
core.save_points(out, cleaned, fmt="pcd")
print(f"Wrote {len(cleaned):,} points -> {out}")
return 0
if __name__ == "__main__":
raise SystemExit(main())