Skip to content

Commit 776e10b

Browse files
Manuel Lopez Antequerafacebook-github-bot
authored andcommitted
Method to load a pointcloud (#755)
Summary: Pull Request resolved: #755 Reviewed By: mwerlberger Differential Revision: D28412702 fbshipit-source-id: 963af5be3387942a904fbaa30e918b3ecfaafc4c
1 parent b08e2b7 commit 776e10b

2 files changed

Lines changed: 35 additions & 0 deletions

File tree

opensfm/dataset.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1193,6 +1193,12 @@ def depthmap_file(self, image: str, suffix: str) -> str:
11931193
def point_cloud_file(self, filename: str = "merged.ply") -> str:
11941194
return os.path.join(self._depthmap_path(), filename)
11951195

1196+
def load_point_cloud(
1197+
self, filename: str = "merged.ply"
1198+
) -> Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray]:
1199+
with self.io_handler.open(self.point_cloud_file(filename), "r") as fp:
1200+
return io.point_cloud_from_ply(fp)
1201+
11961202
def save_point_cloud(
11971203
self,
11981204
points: np.ndarray,

opensfm/io.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -964,6 +964,35 @@ def reconstruction_to_ply(
964964
return points_to_ply_string(vertices, point_num_views)
965965

966966

967+
def point_cloud_from_ply(
968+
fp: t.TextIO,
969+
) -> t.Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray]:
970+
"""Load point cloud from a PLY file."""
971+
all_lines = fp.read().splitlines()
972+
start = all_lines.index("end_header") + 1
973+
lines = all_lines[start:]
974+
n = len(lines)
975+
976+
points = np.zeros((n, 3), dtype=np.float32)
977+
normals = np.zeros((n, 3), dtype=np.float32)
978+
colors = np.zeros((n, 3), dtype=np.uint8)
979+
labels = np.zeros((n,), dtype=np.uint8)
980+
detections = np.zeros((n,), dtype=np.uint8)
981+
982+
for i, row in enumerate(lines):
983+
words = row.split()
984+
label = int(words[9])
985+
points[i] = list(map(float, words[0:3]))
986+
normals[i] = list(map(float, words[3:6]))
987+
colors[i] = list(map(int, words[6:9]))
988+
labels[i] = label
989+
if len(words) == 11:
990+
detection = int(words[10])
991+
detections[i] = detection
992+
993+
return points, normals, colors, labels, detections
994+
995+
967996
def point_cloud_to_ply(
968997
points: np.ndarray,
969998
normals: np.ndarray,

0 commit comments

Comments
 (0)