|
|
import os |
|
|
import argparse |
|
|
import json |
|
|
import numpy as np |
|
|
import pprint |
|
|
import time |
|
|
import multiprocessing as mp |
|
|
from functools import partial |
|
|
|
|
|
from plyfile import PlyData |
|
|
from tqdm import tqdm |
|
|
import torch |
|
|
|
|
|
dict_align_matrix ={} |
|
|
|
|
|
def process_per_scan(scan_id, scan_dir): |
|
|
|
|
|
global dict_align_matrix |
|
|
|
|
|
|
|
|
with open(os.path.join(scan_dir, scan_id, '%s_vh_clean_2.ply'%(scan_id)), 'rb') as f: |
|
|
plydata = PlyData.read(f) |
|
|
points = np.array([list(x) for x in plydata.elements[0]]) |
|
|
coords = np.ascontiguousarray(points[:, :3]) |
|
|
colors = np.ascontiguousarray(points[:, 3:6]) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
align_matrix = np.eye(4) |
|
|
with open(os.path.join(scan_dir, scan_id, '%s.txt'%(scan_id)), 'r') as f: |
|
|
for line in f: |
|
|
if line.startswith('axisAlignment'): |
|
|
align_matrix = np.array([float(x) for x in line.strip().split()[-16:]]).astype(np.float32).reshape(4, 4) |
|
|
break |
|
|
|
|
|
pts = np.ones((coords.shape[0], 4), dtype=coords.dtype) |
|
|
pts[:, 0:3] = coords |
|
|
coords = np.dot(pts, align_matrix.transpose())[:, :3] |
|
|
dict_align_matrix[scan_id] = align_matrix.tolist() |
|
|
|
|
|
assert (np.sum(np.isnan(coords)) == 0) |
|
|
|
|
|
|
|
|
def parse_args(): |
|
|
parser = argparse.ArgumentParser() |
|
|
|
|
|
parser.add_argument('--scannet_dir', required=True, type=str, |
|
|
help='the path to the downloaded ScanNet scans') |
|
|
|
|
|
|
|
|
parser.add_argument('--num_workers', default=-1, type=int, |
|
|
help='the number of processes, -1 means use the available max') |
|
|
parser.add_argument('--apply_global_alignment', default=True, action='store_true', |
|
|
help='rotate/translate entire scan globally to aligned it with other scans') |
|
|
args = parser.parse_args() |
|
|
|
|
|
|
|
|
args_string = pprint.pformat(vars(args)) |
|
|
print(args_string) |
|
|
|
|
|
return args |
|
|
|
|
|
|
|
|
def main(): |
|
|
args = parse_args() |
|
|
|
|
|
|
|
|
for split in ['scans']: |
|
|
scannet_dir = os.path.join(args.scannet_dir, split) |
|
|
|
|
|
scan_ids = os.listdir(scannet_dir) |
|
|
scan_ids.sort() |
|
|
print(split, '%d scans' % (len(scan_ids))) |
|
|
|
|
|
for scan_id in tqdm(scan_ids): |
|
|
process_per_scan(scan_id=scan_id,scan_dir=scannet_dir) |
|
|
|
|
|
global dict_align_matrix |
|
|
json.dump(dict_align_matrix,open("scannet_align_matrix.json","w")) |
|
|
|
|
|
if __name__ == '__main__': |
|
|
main() |
|
|
|