cli_utils.py 15.4 KB
Newer Older
Clément Pinard's avatar
Clément Pinard committed
1
2
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
from path import Path
3
import numpy as np
Clément Pinard's avatar
Clément Pinard committed
4
5


6
def add_main_options(parser):
Clément Pinard's avatar
Clément Pinard committed
7
8
9
10
11
12
13
14
15
16
    main_parser = parser.add_argument_group("Main options")
    main_parser.add_argument('--input_folder', metavar='PATH', default=Path("."), type=Path,
                             help="Folder with LAS point cloud, videos, and images")
    main_parser.add_argument('--workspace', metavar='PATH', default=Path("."),
                             help='path to workspace where COLMAP operations will be done', type=Path)
    main_parser.add_argument('--raw_output_folder', metavar='PATH', default=Path("."),
                             help='path to output folder : must be big !', type=Path)
    main_parser.add_argument('--converted_output_folder', metavar='PATH', default=Path("."),
                             help='path to output folder : must be big !', type=Path)

Clément Pinard's avatar
Clément Pinard committed
17
18
    main_parser.add_argument('--skip_step', metavar="N", nargs="*", default=[], type=int,
                             help='Skip selected steps')
Clément Pinard's avatar
Clément Pinard committed
19
20
    main_parser.add_argument('--begin_step', metavar="N", type=int, default=None)
    main_parser.add_argument('--show_steps', action="store_true")
Clément Pinard's avatar
Clément Pinard committed
21
    main_parser.add_argument('--add_new_videos', action="store_true",
22
                             help="If selected, will skip first 6 steps to directly register videos without mapping")
Clément Pinard's avatar
Clément Pinard committed
23
    main_parser.add_argument('--generate_groundtruth_for_individual_images', '--gt_images', action="store_true",
24
                             help="If selected, will generate Ground truth for individual images as well as videos")
Clément Pinard's avatar
Clément Pinard committed
25
    main_parser.add_argument('--save_space', action="store_true")
Clément Pinard's avatar
Clément Pinard committed
26
    main_parser.add_argument('-v', '--verbose', action="count", default=0)
Clément Pinard's avatar
Clément Pinard committed
27
28
29
30
31
    main_parser.add_argument('--resume_work', action="store_true",
                             help='If selected, will try to skip video aready localized, and ground truth already generated')
    main_parser.add_argument('--inspect_dataset', action="store_true",
                             help='If selected, will open a window to inspect the dataset. '
                                  'See https://github.com/ETH3D/dataset-pipeline#dataset-inspection')
Clément Pinard's avatar
Clément Pinard committed
32
33
34
35
36
37
    main_parser.add_argument('--vid_ext', nargs='+', default=[".mp4", ".MP4"],
                             help='Video extensions to scrape from input folder')
    main_parser.add_argument('--pic_ext', nargs='+', default=[".jpg", ".JPG", ".png", ".PNG"],
                             help='Image extensions to scrape from input folder')
    main_parser.add_argument('--raw_ext', nargs='+', default=[".ARW", ".NEF", ".DNG"],
                             help='Raw Image extensions to scrape from input folder')
Clément Pinard's avatar
Clément Pinard committed
38

39
40
41

def add_pcp_options(parser):
    pcp_parser = parser.add_argument_group("PointCloud preparation")
Clément Pinard's avatar
Clément Pinard committed
42
43
44
45
    pcp_parser.add_argument("--pointcloud_resolution", default=None, type=float,
                            help='If set, will subsample the Lidar point clouds at the chosen resolution')
    pcp_parser.add_argument("--SOR", default=[10, 6], nargs=2, type=float,
                            help="Satistical Outlier Removal parameters : Number of nearest neighbours, max relative distance to standard deviation")
Clément Pinard's avatar
Clément Pinard committed
46
47
    pcp_parser.add_argument('--registration_method', choices=["simple", "eth3d", "interactive"], default="simple",
                            help='Method used for point cloud registration. See README, Manual step by step : step 11')
Clément Pinard's avatar
Clément Pinard committed
48

49
50

def add_ve_options(parser):
Clément Pinard's avatar
Clément Pinard committed
51
52
    ve_parser = parser.add_argument_group("Video extractor")
    ve_parser.add_argument('--total_frames', default=500, type=int)
Clément Pinard's avatar
Clément Pinard committed
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
    ve_parser.add_argument('--orientation_weight', default=1, type=float,
                           help="Weight applied to orientation during optimal sample. "
                           "Higher means two pictures with same location but different "
                           "orientation will be considered farer apart")
    ve_parser.add_argument('--resolution_weight', default=1, type=float,
                           help="same as orientation, but with image size")
    ve_parser.add_argument('--num_neighbours', default=10, type=int,
                           help='Number of frame shared between subsequent chunks')
    ve_parser.add_argument('--system', default="epsg:2154",
                           help='coordinates system used for GPS, should be the same as the LAS files used')
    ve_parser.add_argument('--lowfps', default=1, type=int,
                           help="framerate at which videos will be scanned WITH reconstruction")
    ve_parser.add_argument('--max_sequence_length', default=4000, type=int,
                           help='Number max of frames for a chunk. '
                           'This is for RAM purpose, as loading feature matches of thousands of frames can take up GBs of RAM')
68
69
    ve_parser.add_argument('--include_lowfps_thorough', action='store_true',
                           help="if selected, will include videos frames at lowfps for thorough scan (longer)")
Clément Pinard's avatar
Clément Pinard committed
70
71
72
    ve_parser.add_argument('--generic_model', default='OPENCV',
                           help='COLMAP model for generic videos. Same zoom level assumed throughout the whole video. '
                           'See https://colmap.github.io/cameras.html')
73
74
75
76
    ve_parser.add_argument('--full_metadata', default=None,
                           help='where to save all concatenated metadata in a file that will be used to add new videos afterward. '
                                'If not set, will save at the root of workspace')

Clément Pinard's avatar
Clément Pinard committed
77

78
def add_exec_options(parser):
Clément Pinard's avatar
Clément Pinard committed
79
80
81
    exec_parser = parser.add_argument_group("Executable files")
    exec_parser.add_argument('--log', default=None, type=Path)
    exec_parser.add_argument('--nw', default="native-wrapper.sh", type=Path,
Clément Pinard's avatar
Clément Pinard committed
82
                             help="native-wrapper.sh file location (see Anafi SDK documentation)")
Clément Pinard's avatar
Clément Pinard committed
83
84
85
86
87
88
89
    exec_parser.add_argument("--colmap", default="colmap", type=Path,
                             help="colmap exec file location")
    exec_parser.add_argument("--eth3d", default="../dataset-pipeline/build",
                             type=Path, help="ETH3D detaset pipeline exec files folder location")
    exec_parser.add_argument("--ffmpeg", default="ffmpeg", type=Path)
    exec_parser.add_argument("--pcl_util", default="pcl_util/build", type=Path)

90
91

def add_pm_options(parser):
Clément Pinard's avatar
Clément Pinard committed
92
    pm_parser = parser.add_argument_group("Photogrammetry")
Clément Pinard's avatar
Clément Pinard committed
93
    pm_parser.add_argument('--max_num_matches', default=32768, type=int, help="max number of matches, lower it if you get GPU memory error")
Clément Pinard's avatar
Clément Pinard committed
94
95
96
    pm_parser.add_argument('--match_method', default='vocab_tree', choices=['vocab_tree', 'exhaustive'],
                           help='Match method for first thorough photogrammetry, '
                                'see https://colmap.github.io/tutorial.html#feature-matching-and-geometric-verification')
Clément Pinard's avatar
Clément Pinard committed
97
98
99
100
    pm_parser.add_argument('--vocab_tree', type=Path, default="vocab_tree_flickr100K_words256K.bin")
    pm_parser.add_argument('--triangulate', action="store_true")
    pm_parser.add_argument('--multiple_models', action='store_true', help='If selected, will let colmap mapper do multiple models.'
                                                                          'The biggest one will then be chosen')
Clément Pinard's avatar
Clément Pinard committed
101
102
103
104
105
106
107
    pm_parser.add_argument('--more_sift_features', action="store_true",
                           help="If selected, will activate the COLMAP options ` SiftExtraction.domain_size_pooling` "
                                " and `--SiftExtraction.estimate_affine_shape` during feature extraction. Be careful, "
                                "this does not use GPU and is thus very slow. More info : "
                                "https://colmap.github.io/faq.html#increase-number-of-matches-sparse-3d-points")
    pm_parser.add_argument('--filter_models', action="store_true",
                           help="If selected, will filter video localization to smooth trajectory")
Clément Pinard's avatar
Clément Pinard committed
108
109
    pm_parser.add_argument('--stereo_min_depth', type=float, default=0.1, help="Min depth for PatchMatch Stereo")
    pm_parser.add_argument('--stereo_max_depth', type=float, default=100, help="Max depth for PatchMatch Stereo")
Clément Pinard's avatar
Clément Pinard committed
110

111
112

def add_om_options(parser):
Clément Pinard's avatar
Clément Pinard committed
113
    om_parser = parser.add_argument_group("Occlusion Mesh")
Clément Pinard's avatar
Clément Pinard committed
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
    om_parser.add_argument('--normals_method', default="radius", choices=["radius", "neighbours"],
                           help='Method used for normal computation between radius and nearest neighbours')
    om_parser.add_argument('--normals_radius', default=0.2, type=float,
                           help='If radius method for normals, radius within which other points will be considered neighbours')
    om_parser.add_argument('--normals_neighbours', default=8, type=int,
                           help='If nearest neighbours method chosen, number of neighbours to consider.'
                                'Could be very close or very far points, but has a constant complexity')
    om_parser.add_argument('--mesh_resolution', default=0.2, type=float,
                           help='Mesh resolution for occlusion in meters. Higher means more coarse. (in meters)')
    om_parser.add_argument('--splats', action='store_true',
                           help='If selected, will create splats for points in the cloud that are far from the occlusion mesh')
    om_parser.add_argument('--splat_threshold', default=0.1, type=float,
                           help='Distance from occlusion mesh at which a splat will be created for a particular point (in meters)')
    om_parser.add_argument('--max_splat_size', default=None, type=float,
                           help='Splat size is defined by mean istance from its neighbours. You can define a max splat size for '
                                'isolated points which otherwise would make a very large useless splat. '
                                'If not set, will be `2.5*splat_threshold`.')
131
132
133


def add_gt_options(parser):
Clément Pinard's avatar
Clément Pinard committed
134
135
136
    gt_parser = parser.add_argument_group("Ground Truth Creator")
    gt_parser.add_argument('--max_occlusion_depth', default=250, type=float,
                           help='max depth for occlusion. Everything further will not be considered at infinity')
137
138
    gt_parser.add_argument('--eth3d_splat_radius', default=0.01, type=float,
                           help='see splat radius for ETH3D')
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165


def set_full_argparser():
    parser = ArgumentParser(description='Main pipeline, from LIDAR pictures and videos to GT depth enabled videos',
                            formatter_class=ArgumentDefaultsHelpFormatter)

    add_main_options(parser)
    add_pcp_options(parser)
    add_ve_options(parser)
    add_exec_options(parser)
    add_pm_options(parser)
    add_om_options(parser)
    add_gt_options(parser)
    return parser


def set_full_argparser_no_lidar():
    parser = ArgumentParser(description='Main pipeline, from pictures and videos to GT depth enabled videos, '
                                        'using only COLMAP (No lidar)',
                            formatter_class=ArgumentDefaultsHelpFormatter)
    add_main_options(parser)
    add_ve_options(parser)
    add_exec_options(parser)
    add_pm_options(parser)
    add_om_options(parser)
    add_gt_options(parser)

Clément Pinard's avatar
Clément Pinard committed
166
167
168
169
170
    nl_parser = parser.add_argument_group("Main pipeline no Lidar specific options")
    nl_parser.add_argument("--SOR", default=[10, 6], nargs=2, type=float,
                           help="Satistical Outlier Removal parameters : Number of nearest neighbours, "
                                "max relative distance to standard deviation. "
                                "This will be used for filtering dense reconstruction")
171
172
173
174
175
176
177
178
179
180
181
182
183
184
    return parser


def set_new_images_arparser():
    parser = ArgumentParser(description='Additional pipeline, to add new pictures to an already existing workspace, '
                                        'The main pipeline must be finished before launching this script, '
                                        ' at least until reconstruction alignment',
                            formatter_class=ArgumentDefaultsHelpFormatter)
    add_main_options(parser)
    add_exec_options(parser)
    add_pm_options(parser)
    add_om_options(parser)
    add_gt_options(parser)

Clément Pinard's avatar
Clément Pinard committed
185
186
187
188
189
190
191
192
193
194
195
196
    ni_parser = parser.add_argument_group("Add new pictures specific options")

    ni_parser.add_argument("--map_new_images", action="store_true",
                           help="if selected, will replace the 'omage_registrator' step with a full mapping step")
    ni_parser.add_argument("--bundle_adjuster_steps", default=100, type=int,
                           help="number of iteration for bundle adjustor after image registration")
    ni_parser.add_argument("--rebuild_occlusion_mesh", action="store_true",
                           help="If selected, will rebuild a new dense point cloud and deauney mesh. "
                                "Useful when new images see new parts of the model")
    ni_parser.add_argument('--generic_model', default='OPENCV',
                           help='COLMAP model for added pictures. Same zoom level assumed throughout whole folders. '
                           'See https://colmap.github.io/cameras.html')
Clément Pinard's avatar
Clément Pinard committed
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
    return parser


def print_step(step_number, step_name):
    print("\n\n=================")
    print("Step {}".format(step_number))
    print(step_name)
    print("=================")


global_steps = ["Point Cloud Preparation",
                "Pictures preparation",
                "Extracting Videos and selecting optimal frames for a thorough scan",
                "First thorough photogrammetry",
                "Alignment of photogrammetric reconstruction with GPS",
                "Video localization with respect to reconstruction",
                "Full reconstruction point cloud densificitation",
                "Alignment of photogrammetric reconstruction with Lidar point cloud",
                "Occlusion Mesh computing",
                "Ground Truth creation"]

per_vid_steps_1 = ["Full video extraction",
                   "Sky mask generation",
                   "Complete photogrammetry with video at 1 fps",
                   "Localizing remaining frames",
                   "Re-Alignment of triangulated points with Lidar point cloud"]
per_vid_steps_2 = ["Creating Ground truth data",
Clément Pinard's avatar
Clément Pinard committed
224
                   "Create video with GT visualization and Convert to KITTI format"]
Clément Pinard's avatar
Clément Pinard committed
225
226
227
228
229
230
231
232
233
234
235
236
237
238


def print_workflow():
    print("Global steps :")
    for i, s in enumerate(global_steps):
        print("{}) {}".format(i+1, s))
        if i == 4:
            print("\tPer video:")
            for i, s in enumerate(per_vid_steps_1):
                print("\t{}) {}".format(i+1, s))

    print("\tPer video:")
    for i, s in enumerate(per_vid_steps_2):
        print("\t{}) {}".format(i+1, s))
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256


def get_matrix(path):
    if path.isfile():
        '''Note : We use the inverse matrix here, because in general, it's easier to register the reconstructed model into the lidar one,
        as the reconstructed will have less points, but in the end we need the matrix to apply to the lidar point to be aligned
        with the camera positions (ie the inverse)'''
        return np.linalg.inv(np.fromfile(path, sep=" ").reshape(4, 4))
    else:
        print("Error, no registration matrix can be found")
        print("Ensure that your registration matrix was saved under the name {}".format(path))
        decision = None
        while decision not in ["y", "n", ""]:
            decision = input("retry ? [Y/n]")
        if decision.lower() in ["y", ""]:
            return get_matrix(path)
        elif decision.lower() == "n":
            return np.eye(4)