las2ply.py 1.92 KB
Newer Older
1
import laspy
2
3
4
5
6
7
8
9
10
from pyntcloud import PyntCloud
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
from path import Path
import numpy as np


parser = ArgumentParser(description='Convert las cloud to ply along with centroid',
                        formatter_class=ArgumentDefaultsHelpFormatter)

Clement Pinard's avatar
Clement Pinard committed
11
parser.add_argument('las', type=Path,
12
                    help='path to video folder root')
Clement Pinard's avatar
Clement Pinard committed
13
14
15
16
17
18
19
20
21
22
23
parser.add_argument('--output_folder', metavar='PATH',
                    default=None, type=Path,
                    help="where to save ply file and txt centroid")
parser.add_argument('--verbose', '-v', action='store_true')


def load_and_convert(input_file, output_folder, verbose=False):
    output_folder.makedirs_p()
    ply_path = output_folder / input_file.namebase + '.ply'
    txt_path = output_folder / input_file.namebase + '_centroid.txt'
    file_type = input_file.ext[1:].upper()
24
25
26
27
28
29
30
31
    if file_type == "LAS":
        offset = np.array(laspy.file.File(input_file, mode="r").header.offset)
    else:
        offset = np.zeros(3)
    cloud = PyntCloud.from_file(input_file)
    xyz = cloud.points[['x', 'y', 'z']]
    cloud.points = xyz + offset

Clement Pinard's avatar
Clement Pinard committed
32
33
34
35
36
37
38
39
40
    if verbose:
        print("{} file with {:,} points "
              "(centroid : [{:.2f}, {:.2f}, {:.2f}] in km) "
              "successfully loaded".format(file_type,
                                           len((cloud.points)),
                                           *(cloud.centroid/1000)))

    output_centroid = cloud.centroid
    np.savetxt(txt_path, output_centroid)
41
42
43
44

    cloud.points -= cloud.centroid

    cloud.to_file(ply_path)
Clement Pinard's avatar
Clement Pinard committed
45
46
47
    if verbose:
        print("saved shifted cloud to {}, centroid to {}".format(ply_path, txt_path))
    return ply_path, output_centroid
48
49
50


if __name__ == '__main__':
Clement Pinard's avatar
Clement Pinard committed
51
52
53
54
    args = parser.parse_args()
    if args.output_folder is None:
        args.output_folder = args.las_path.parent
    load_and_convert(args.las_path, args.output_folder, args.verbose)