-
Notifications
You must be signed in to change notification settings - Fork 2
/
run.sh
632 lines (588 loc) · 26.6 KB
/
run.sh
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
#!/bin/bash
# Copyright (c) 2024, NVIDIA CORPORATION. All rights reserved.
#
# NVIDIA CORPORATION and its licensors retain all intellectual property
# and proprietary rights in and to this software, related documentation
# and any modifications thereto. Any use, reproduction, disclosure or
# distribution of this software and related documentation without an express
# license agreement from NVIDIA CORPORATION is strictly prohibited.
##################################################
# Load helper functions
##################################################
SOURCE_PATH=$(dirname "$BASH_SOURCE")
source $SOURCE_PATH/scripts/utils.sh
##################################################
# Loop over arguments and parse flags
##################################################
SCENE_PATH=""
for arg in "$@"; do
case $arg in
-h|--help)
echo "Usage: bash run.sh [SCENE_PATH] [OPTIONS]"
echo " SCENE_PATH: Path to scene directory (default: current directory)"
echo " OPTIONS:"
echo " --interactive: Run interactive tool to align canonical pose"
echo " --help: Print this help message"
exit 0
;;
--interactive)
IS_INTERACTIVE=1
;;
*)
# if SCENE_PATH is empty and arg is not an option, set SCENE_PATH
if [[ -z $SCENE_PATH ]] && [[ ! $arg == -* ]]; then
SCENE_PATH=$arg
# otherwise, print error
else
error "Unknown argument $arg. Use flag --help for usage."
fi
;;
esac
done
# If not scene is given, use the current directory
if [ -z $SCENE_PATH ]; then
SCENE_PATH="."
fi
if [ ! -d $SCENE_PATH ]; then
error "Unable to find scene directory `path $SCENE_PATH`. Exiting."
else
info "Processing scene `path $SCENE_PATH` ..."
fi
##################################################
# Redirect stdout and stderr to log file
##################################################
mkdir -p $SCENE_PATH/logs
LOG_FILE=$SCENE_PATH/logs/$(date +%Y-%m-%d_%H-%M-%S).log
info "Logging to `path $LOG_FILE` ..."
# exec > >(tee -i $LOG_FILE)
# exec > >(tee -i /dev/tty | sed -r 's/\x1b\[[0-9;]*m?//g' > $LOG_FILE)
exec > >(tee -i /dev/tty | stdbuf -oL sed -r 's/\x1b\[[0-9;]*m?//g' > $LOG_FILE)
# uses sed to remove ansi escape sequences before writing to log file
# and uses stdbuf to flush stdout/stderr when writing to log file
##################################################
# Load and print config variables
##################################################
header "Loading config variables"
source $SOURCE_PATH/config/default_config.sh
if [ ! -e $SCENE_PATH/config.sh ]; then
info "Unable to find scene config file `path $SCENE_PATH/config.sh`. Using defaults."
else
info "Loading config file `path $SCENE_PATH/config.sh` ..."
source $SCENE_PATH/config.sh
fi
config_vars=$(compgen -A variable | grep CONFIG_) # list all variables with CONFIG_ prefix
for var in $config_vars; do
cmd "$var=`eval echo \\$$var`"
done
echo
##################################################
# Extract frames from video as images
##################################################
header "Extracting frames from video"
image_files=($SCENE_PATH/images_scene/*.jpg)
if [ -e $image_files ]; then
info "Found existing frames in `path "$SCENE_PATH/images_scene/*.jpg"`. Skipping frame extraction."
elif [ ! -e $SCENE_PATH/$CONFIG_VIDEO_NAME ]; then
error "Unable to find video `path $SCENE_PATH/$CONFIG_VIDEO_NAME`. Exiting."
else
python $SOURCE_PATH/scripts/extract_sharpest_frames.py \
$SCENE_PATH/$CONFIG_VIDEO_NAME \
$SCENE_PATH/images_scene \
--min_sharpness_window_size -1 \
--max_frame_count $CONFIG_FRAME_COUNT \
--image_ext jpg ||
exit_with_error
fi
##################################################
# Generate COLMAP camera poses
##################################################
header "Generating camera poses via COLMAP"
image_files=($SCENE_PATH/images_scene/*.jpg)
for COLMAP_MATCHER in sequential exhaustive; do
if [ -e $SCENE_PATH/transforms_scene.json ]; then
info "Found existing `path $SCENE_PATH/transforms_scene.json`. Skipping COLMAP."
break
elif [ -e $SCENE_PATH/transforms_scene_$COLMAP_MATCHER.json ]; then
info "Found existing `path $SCENE_PATH/transforms_scene_$COLMAP_MATCHER.json`. Skipping $COLMAP_MATCHER matcher."
elif [ ! -e $CONFIG_NGP_PATH/scripts/colmap2nerf.py ]; then
error "Unable to find NGP script `path $CONFIG_NGP_PATH/scripts/colmap2nerf.py`. Exiting."
elif [ ! -e $image_files ]; then
error "Unable to find images in `path $SCENE_PATH/images_scene/*.jpg`. Exiting."
else
info "Running COLMAP with `path $COLMAP_MATCHER` matcher..."
if [ ! -d $SCENE_PATH/colmap_$COLMAP_MATCHER ]; then
mkdir $SCENE_PATH/colmap_$COLMAP_MATCHER
fi
(
cd $SCENE_PATH &&
python $CONFIG_NGP_PATH/scripts/colmap2nerf.py \
--run_colmap \
--images images_scene \
--out transforms_scene.json \
--overwrite \
--aabb_scale $CONFIG_AABB_SCALE \
--colmap_matcher $COLMAP_MATCHER \
--colmap_db colmap_$COLMAP_MATCHER/colmap.db \
--text colmap_$COLMAP_MATCHER/colmap_text
) &&
python $SOURCE_PATH/scripts/edit_transforms_file.py \
$SCENE_PATH/transforms_scene.json \
--sort_frames ||
exit_with_error
fi
# ensure that COLMAP succeeded by verifying that the number
# of occurences of "transform_matrix" in the transforms file
# is at least 90% of the number of images in images_scene/
num_transform_matrices=$(grep -c "transform_matrix" $SCENE_PATH/transforms_scene.json)
num_images=$(ls $SCENE_PATH/images_scene/ | wc -l)
if [ $num_transform_matrices -lt $((num_images * 9 / 10)) ]; then
# if matcher is sequential, try exhaustive
if [ $COLMAP_MATCHER == "sequential" ]; then
mv $SCENE_PATH/transforms_scene.json $SCENE_PATH/transforms_scene_sequential.json
export $COLMAP_MATCHER=exhaustive
info "COLMAP computed poses for only $num_transform_matrices/$num_images images with `path sequential` matcher."
info "Retrying with COLMAP matcher `path exhaustive`..."
# otherwise, exit
else
mv $SCENE_PATH/transforms_scene.json $SCENE_PATH/transforms_scene_exhaustive.json
error "COLMAP computed poses for only $num_transform_matrices/$num_images images with `path exhaustive` matcher. Exiting."
fi
else
info "COLMAP succeeded with matcher `path $CONFIG_COLMAP_MATCHER`."
python $SOURCE_PATH/scripts/edit_transforms_file.py \
$SCENE_PATH/transforms_scene.json \
--remove_omitted_frames_from_dir $SCENE_PATH/images_scene ||
exit_with_error
break
fi
done
##################################################
# Train NGP reconstruction of full scene
##################################################
header "Training NGP reconstruction of full scene"
if [[ -e $SCENE_PATH/nerf_scene.ingp ]]
then
info "Found existing `path $SCENE_PATH/nerf_scene.ingp`. Skipping scene NeRF."
else
python $CONFIG_NGP_PATH/scripts/run.py \
--scene $SCENE_PATH/transforms_scene.json \
--save_snapshot $SCENE_PATH/nerf_scene.ingp \
--n_steps $CONFIG_N_STEPS ||
exit_with_error
# TODO add flags for training latents and extrinsics (and export optimized poses)
# TODO fix video rendering and render a debug video
fi
##################################################
# Create masked training images and transforms file
##################################################
header "Creating masked images and transforms file"
raw_mask_files=($SCENE_PATH/$CONFIG_MASK_DIR/*.png)
masked_image_files=($SCENE_PATH/images_object/*.png)
if [ -e $masked_image_files ] && [ -e $SCENE_PATH/transforms_object.json ]; then
info "Found existing `path $SCENE_PATH/images_object/*.png` and" \
"`path $SCENE_PATH/transforms_object.json`. Skipping image masking."
elif [ ! -e $raw_mask_files ]; then
error "Unable to find masks in `path $SCENE_PATH/$CONFIG_MASK_DIR`. Exiting."
else
python $SOURCE_PATH/scripts/mask_images.py \
$SCENE_PATH/images_scene \
$SCENE_PATH/$CONFIG_MASK_DIR \
$SCENE_PATH/images_object \
--standardized_masks_dir $SCENE_PATH/masks_object &&
python $SOURCE_PATH/scripts/edit_transforms_file.py \
$SCENE_PATH/transforms_scene.json \
--output_file $SCENE_PATH/transforms_object.json \
--set_aabb_scale 1 \
--edit_frame_file_path "images_scene/" "images_object/" \
--edit_frame_file_path ".jpg" ".png" ||
exit_with_error
fi
##################################################
# Train NGP reconstruction of masked object
##################################################
header "Training NGP reconstruction of masked object"
if [[ -e $SCENE_PATH/nerf_object.ingp ]]
then
info "Found existing `path $SCENE_PATH/nerf_object.ingp`. Skipping object NeRF."
else
python $CONFIG_NGP_PATH/scripts/run.py \
--scene $SCENE_PATH/transforms_object.json \
--save_snapshot $SCENE_PATH/nerf_object.ingp \
--n_steps $CONFIG_N_STEPS ||
exit_with_error
# TODO add flags for training extrinsics & latents
# TODO fix video rendering and render a debug video
# TODO what is "andrewg_hack"?
fi
##################################################
# Export mesh from NGP object reconstruction
##################################################
header "Exporting rough, untextured mesh from NGP via marching cubes"
if [[ -e $SCENE_PATH/meshes/raw.ply ]]
then
info "Found existing `path $SCENE_PATH/meshes/raw.ply`. Skipping mesh export."
elif
[[ ! -e $SCENE_PATH/nerf_object.ingp ]]
then
error "Unable to find NGP snapshot `path $SCENE_PATH/nerf_object.ingp`. Exiting."
else
info "Exporting marching cubes mesh from NGP snapshot `path $SCENE_PATH/nerf_object.ingp` ..."
mkdir -p $SCENE_PATH/meshes &&
python $CONFIG_NGP_PATH/scripts/run.py \
--load_snapshot $SCENE_PATH/nerf_object.ingp \
--marching_cubes_res 256 \
--marching_cubes_density_thresh 1.0 \
--save_mesh $SCENE_PATH/meshes/raw.ply ||
exit_with_error
fi
##################################################
# Clean mesh
##################################################
header "Cleaning marching cubes mesh"
if [[ -e $SCENE_PATH/meshes/base.ply ]]
then
info "Found existing `path $SCENE_PATH/meshes/base.ply`. Skipping mesh cleaning."
elif
[[ ! -e $SCENE_PATH/meshes/raw.ply ]]
then
error "Unable to find marching cubes mesh `path $SCENE_PATH/meshes/raw.ply`. Exiting."
else
info "Cleaning mesh `path $SCENE_PATH/meshes/raw.ply` ..."
python $SOURCE_PATH/scripts/clean_mesh.py \
$SCENE_PATH/meshes/raw.ply \
$SCENE_PATH/meshes/base.ply ||
exit_with_error
fi
##################################################
# Generate raw BOP annotations (unscaled and not in canonical pose)
##################################################
header "Generating initial BOP annotations"
if [[ -e $SCENE_PATH/bop_raw/scene_gt_initial.json ]] && \
[[ -e $SCENE_PATH/bop_raw/scene_camera_initial.json ]]
then
info "Found existing initial BOP files in `path $SCENE_PATH/bop_raw`. Skipping."
elif
[[ ! -e $SCENE_PATH/transforms_scene.json ]]
then
error "Unable to find camera poses `path $SCENE_PATH/transforms_scene.json`. Exiting."
else
info "Generating initial BOP files in `path $SCENE_PATH/bop_raw` ..."
python $SOURCE_PATH/scripts/generate_gt_bop.py \
$SCENE_PATH/bop_raw \
--camera_poses_fn $SCENE_PATH/transforms_scene.json \
--bop_object_id $CONFIG_BOP_ID \
--output_fn_tag "_initial" ||
exit_with_error
fi
##################################################
# Export depth images from NGP scene reconstruction
##################################################
header "Exporting depth images from NGP scene reconstruction"
depth_files=($SCENE_PATH/depth_scene/*.png)
if [[ -e $depth_files ]]
then
info "Found existing depth images in `path "$SCENE_PATH/depth_scene/*.png"`. Skipping depth export."
elif
[[ ! -e $SCENE_PATH/nerf_scene.ingp ]]
then
error "Unable to find NGP snapshot `path $SCENE_PATH/nerf_scene.ingp`. Exiting."
else
info "Exporting depth images from NGP snapshot `path $SCENE_PATH/nerf_scene.ingp` to `path $SCENE_PATH/depth_scene` ..."
mkdir -p $SCENE_PATH/depth_scene &&
python $SOURCE_PATH/scripts/export_depth.py \
$SCENE_PATH/nerf_scene.ingp \
$SCENE_PATH/depth_scene \
--ngp_root $CONFIG_NGP_PATH \
--colorized_depth_dir $SCENE_PATH/depth_scene_color \
--clip_distance 10.0 ||
exit_with_error
fi
##################################################
# Apply texture to mesh using Open3D pipeline
##################################################
header "Generating textured mesh"
if [[ -e $SCENE_PATH/meshes/textured.ply ]] && \
[[ -e $SCENE_PATH/meshes/colored.ply ]]
then
info "Found existing `path $SCENE_PATH/meshes/textured.ply`. Skipping textured mesh."
elif [[ ! -e $SCENE_PATH/bop_raw/scene_gt_initial.json ]] || \
[[ ! -e $SCENE_PATH/bop_raw/scene_camera_initial.json ]] || \
[[ ! -e $SCENE_PATH/images_scene/ ]] || \
[[ ! -e $SCENE_PATH/depth_scene/ ]] || \
[[ ! -e $SCENE_PATH/masks_object ]]
then
error "Unable to find required input files. Exiting."
else
# generate Poisson mesh and apply texture
if [[ $CONFIG_GENERATE_POISSON_MESH == 1 ]]
then
info "Found config flag `path '$CONFIG_USE_POISSON_MESH == 1'`."
info "Generating textured poisson mesh and saving to `path $SCENE_PATH/meshes/textured.ply` ..."
python $SOURCE_PATH/scripts/o3d_mesh_pipeline.py \
$SCENE_PATH/meshes/textured.ply \
--colored_mesh_output_fn $SCENE_PATH/meshes/colored.ply \
--bop_pose_fn $SCENE_PATH/bop_raw/scene_gt_initial.json \
--bop_camera_fn $SCENE_PATH/bop_raw/scene_camera_initial.json \
--rgb_dir $SCENE_PATH/images_scene \
--depth_dir $SCENE_PATH/depth_scene \
--mask_dir $SCENE_PATH/masks_object \
--max_views 50 ||
exit_with_error
else
info "Found config flag `path '$CONFIG_USE_POISSON_MESH == 0'`."
info "Applying texture to existing mesh and saving to `path $SCENE_PATH/meshes/textured.ply` ..."
python $SOURCE_PATH/scripts/o3d_mesh_pipeline.py \
$SCENE_PATH/meshes/textured.ply \
--colored_mesh_output_fn $SCENE_PATH/meshes/colored.ply \
--subdivision_iterations 2 \
--texture_existing_mesh $SCENE_PATH/meshes/base.ply \
--bop_pose_fn $SCENE_PATH/bop_raw/scene_gt_initial.json \
--bop_camera_fn $SCENE_PATH/bop_raw/scene_camera_initial.json \
--rgb_dir $SCENE_PATH/images_scene \
--depth_dir $SCENE_PATH/depth_scene \
--mask_dir $SCENE_PATH/masks_object \
--max_views 50 ||
exit_with_error
# TODO can we downsample to the original vertices while keeping the same texture coordinates?
fi
fi
##################################################
# Prompt for canonical pose
##################################################
header "Setting canonical pose"
if [[ -e $SCENE_PATH/canonical_pose.json ]] && \
[[ -e $SCENE_PATH/meshes/canonical.ply ]]
then
info "Found existing `path $SCENE_PATH/canonical_pose.json` and `path $SCENE_PATH/meshes/canonical.ply`. Skipping."
elif [[ $IS_INTERACTIVE != 1 ]]
then
info "Unable to find existing canonical pose `path $SCENE_PATH/canonical_pose.json`."
error "Setting canonical pose requires interactive GUI tool. Use flag `path --interactive` to run. Exiting."
elif [[ ! -e $SCENE_PATH/meshes/textured.ply ]]
then
error "Unable to find textured mesh `path $SCENE_PATH/meshes/textured.ply`. Exiting."
else
info "Running interactive tool to set canonical pose ..."
python $SOURCE_PATH/scripts/manual_align.py \
$SCENE_PATH/meshes/colored.ply \
--output_mesh_fn $SCENE_PATH/meshes/canonical.ply \
--output_transform_fn $SCENE_PATH/canonical_pose.json ||
exit_with_error
fi
##################################################
# Prompt for GT scale
##################################################
header "Setting scale"
if [[ -e $SCENE_PATH/canonical_scale.json ]] && \
[[ -e $SCENE_PATH/meshes/scaled.ply ]]
then
info "Found existing `path $SCENE_PATH/canonical_scale.json` and `path $SCENE_PATH/meshes/scaled.ply`. Skipping."
elif [[ $CONFIG_SCALE_FACTOR != 0 ]]
then
info "Found config setting `path $CONFIG_SCALE_FACTOR`. Setting scale to $CONFIG_SCALE_FACTOR."
python $SOURCE_PATH/scripts/manual_scale.py \
$SCENE_PATH/meshes/canonical.ply \
$SCENE_PATH/meshes/scaled.ply \
$SCENE_PATH/canonical_scale.json \
--scale_factor $CONFIG_SCALE_FACTOR ||
exit_with_error
elif [[ $IS_INTERACTIVE != 1 ]]
then
info "Unable to find existing canonical scale `path $SCENE_PATH/canonical_scale.json`."
error "To set scale interactively, use flag `path --interactive` to run. Exiting."
elif [[ ! -e $SCENE_PATH/meshes/canonical.ply ]]
then
error "Unable to find textured mesh `path $SCENE_PATH/meshes/canonical.ply`. Exiting."
else
info "Running interactive tool to set canonical scale ..."
python $SOURCE_PATH/scripts/manual_scale.py \
$SCENE_PATH/meshes/canonical.ply \
$SCENE_PATH/meshes/scaled.ply \
$SCENE_PATH/canonical_scale.json ||
exit_with_error
fi
##################################################
# Apply scale and canonical pose to BOP annotations
##################################################
header "Applying canonical pose and scale"
if [[ -e $SCENE_PATH/bop_raw/scene_gt_canonical.json ]] && \
[[ -e $SCENE_PATH/bop_raw/scene_camera_canonical.json ]]
then
info "Found existing output files in `path $SCENE_PATH/bop_raw`. Skipping."
elif [[ ! -e $SCENE_PATH/bop_raw/scene_gt_initial.json ]] || \
[[ ! -e $SCENE_PATH/bop_raw/scene_camera_initial.json ]]
then
error "Unable to find required input files in `path $SCENE_PATH/bop_raw`. Exiting."
elif [[ ! -e $SCENE_PATH/canonical_pose.json ]]
then
error "Unable to find canonical pose `path $SCENE_PATH/canonical_pose.json`. Exiting."
elif [[ ! -e $SCENE_PATH/canonical_scale.json ]]
then
error "Unable to find canonical scale `path $SCENE_PATH/canonical_scale.json`. Exiting."
else
info "Applying canonical pose and scale to BOP annotations..."
python $SOURCE_PATH/scripts/apply_canonical_pose_and_scale.py \
--canonical_pose_fn $SCENE_PATH/canonical_pose.json \
--canonical_scale_fn $SCENE_PATH/canonical_scale.json \
--input_bop_gt_fn $SCENE_PATH/bop_raw/scene_gt_initial.json \
--input_bop_camera_fn $SCENE_PATH/bop_raw/scene_camera_initial.json \
--output_bop_gt_fn $SCENE_PATH/bop_raw/scene_gt_canonical.json \
--output_bop_camera_fn $SCENE_PATH/bop_raw/scene_camera_canonical.json ||
exit_with_error
fi
##################################################
# Prompt for alignment to reference
##################################################
if [[ $CONFIG_IS_REFERENCE == 1 ]]
then
header "Finalizing reference scene"
info "Found config flag `path '$CONFIG_IS_REFERENCE == 1'`."
if [[ -e $SCENE_PATH/bop_raw/scene_gt.json ]] &&
[[ -e $SCENE_PATH/bop_raw/scene_camera.json ]] &&
[[ -e $SCENE_PATH/meshes/reference.ply ]]
then
info "Found existing reference annotations and mesh. Skipping."
elif [[ ! -e $SCENE_PATH/bop_raw/scene_gt_canonical.json ]] || \
[[ ! -e $SCENE_PATH/bop_raw/scene_camera_canonical.json ]]
then
error "Unable to find required input files in `path $SCENE_PATH/bop_raw`. Exiting."
elif [[ ! -e $SCENE_PATH/meshes/scaled.ply ]]
then
error "Unable to find scaled mesh `path $SCENE_PATH/meshes/scaled.ply`. Exiting."
else
info "Copying canonical annotations to final annotations and scaled mesh to reference mesh ..."
cp $SCENE_PATH/bop_raw/scene_gt_canonical.json $SCENE_PATH/bop_raw/scene_gt.json &&
cp $SCENE_PATH/bop_raw/scene_camera_canonical.json $SCENE_PATH/bop_raw/scene_camera.json &&
cp $SCENE_PATH/meshes/scaled.ply $SCENE_PATH/meshes/reference.ply ||
exit_with_error
fi
else
header "Aligning to reference mesh"
info "Found config flag `path '$CONFIG_IS_REFERENCE != 1'`."
if [[ -e $SCENE_PATH/reference_alignment_pose.json ]] && \
[[ -e $SCENE_PATH/reference_alignment_scale.json ]] && \
[[ -e $SCENE_PATH/meshes/aligned_to_reference.ply ]]
then
info "Found existing `path $SCENE_PATH/reference_alignment_*.json`. Skipping."
elif [[ $IS_INTERACTIVE != 1 ]]
then
info "Unable to find existing `path $SCENE_PATH/reference_alignment_*.json`."
error "Aligning to reference requires interactive GUI tool. Use flag `path --interactive` to run. Exiting."
elif [[ ! -e $SCENE_PATH/meshes/scaled.ply ]]
then
error "Unable to find canonical scaled mesh `path $SCENE_PATH/meshes/scaled.ply`. Exiting."
elif [[ $CONFIG_REFERENCE_SCENE == "" ]]
then
error "CONFIG_REFERENCE_SCENE is not set. Exiting."
elif [[ ! -e $SCENE_PATH/$CONFIG_REFERENCE_SCENE/meshes/reference.ply ]]
then
error "Unable to find reference mesh `path $SCENE_PATH/$CONFIG_REFERENCE_SCENE/meshes/reference.ply`. Exiting."
else
info "Running interactive tool to align to reference mesh ..."
python $SOURCE_PATH/scripts/manual_align.py \
$SCENE_PATH/meshes/scaled.ply \
--already_in_canonical_pose \
--output_transform_fn $SCENE_PATH/reference_alignment_pose.json \
--output_scale_fn $SCENE_PATH/reference_alignment_scale.json \
--output_mesh_fn $SCENE_PATH/meshes/aligned_to_reference.ply \
--reference_mesh $SCENE_PATH/$CONFIG_REFERENCE_SCENE/meshes/reference.ply \
--allow_translation \
--allow_scaling &&
cp $SCENE_PATH/$CONFIG_REFERENCE_SCENE/meshes/reference.ply $SCENE_PATH/meshes/reference.ply ||
exit_with_error
fi
header "Applying reference pose alignment"
if [[ -e $SCENE_PATH/bop_raw/scene_gt.json ]] && \
[[ -e $SCENE_PATH/bop_raw/scene_camera.json ]]
then
info "Found existing output files in `path $SCENE_PATH/bop_raw`. Skipping."
elif [[ ! -e $SCENE_PATH/bop_raw/scene_gt_canonical.json ]] || \
[[ ! -e $SCENE_PATH/bop_raw/scene_camera_canonical.json ]]
then
error "Unable to find required input files in `path $SCENE_PATH/bop_raw`. Exiting."
elif [[ ! -e $SCENE_PATH/canonical_pose.json ]]
then
error "Unable to find canonical pose `path $SCENE_PATH/canonical_pose.json`. Exiting."
elif [[ ! -e $SCENE_PATH/canonical_scale.json ]]
then
error "Unable to find canonical scale `path $SCENE_PATH/canonical_scale.json`. Exiting."
else
info "Applying canonical pose and scale to BOP annotations..."
python $SOURCE_PATH/scripts/apply_canonical_pose_and_scale.py \
--canonical_pose_fn $SCENE_PATH/reference_alignment_pose.json \
--canonical_scale_fn $SCENE_PATH/reference_alignment_scale.json \
--input_bop_gt_fn $SCENE_PATH/bop_raw/scene_gt_canonical.json \
--input_bop_camera_fn $SCENE_PATH/bop_raw/scene_camera_canonical.json \
--output_bop_gt_fn $SCENE_PATH/bop_raw/scene_gt.json \
--output_bop_camera_fn $SCENE_PATH/bop_raw/scene_camera.json||
exit_with_error
fi
fi
##################################################
# Generate BOP masks
##################################################
header "Generating BOP masks"
mask_files=($SCENE_PATH/bop_raw/mask/*.png)
mask_visib_files=($SCENE_PATH/bop_raw/mask_visib/*.png)
if [[ -e $mask_files ]] && [[ -e $mask_visib_files ]]
then
info "Found existing BOP masks in `path $SCENE_PATH/bop_raw/mask` and `path $SCENE_PATH/bop_raw/mask_visib`. Skipping."
elif
[[ ! -e $SCENE_PATH/meshes/reference.ply ]] || \
[[ ! -e $SCENE_PATH/bop_raw/scene_gt.json ]] || \
[[ ! -e $SCENE_PATH/bop_raw/scene_camera.json ]] || \
[[ ! -e $SCENE_PATH/depth_scene/ ]]
then
error "Unable to find required input files for mask generation. Exiting."
else
info "Generating BOP masks in `path $SCENE_PATH/bop_raw` ..."
python $SOURCE_PATH/scripts/generate_bop_masks.py \
$SCENE_PATH/bop_raw \
--mask_types visible full \
--scene_depth_dir $SCENE_PATH/depth_scene \
--scene_gt_fn $SCENE_PATH/bop_raw/scene_gt_canonical.json \
--scene_camera_fn $SCENE_PATH/bop_raw/scene_camera_canonical.json \
--scene_mesh_fn $SCENE_PATH/meshes/scaled.ply \
--visible_tolerance $CONFIG_VISIBILITY_TOLERANCE ||
exit_with_error
fi
##################################################
# Assemble BOP annotations
##################################################
header "Assembling final BOP annotations"
info "Copying final BOP annotations to `path $SCENE_PATH/bop` ..."
if [[ -e $SCENE_PATH/bop/rgb ]] && \
[[ -e $SCENE_PATH/bop/depth_nerf ]] && \
[[ -e $SCENE_PATH/bop/mask ]] && \
[[ -e $SCENE_PATH/bop/mask_visib ]] && \
[[ -e $SCENE_PATH/bop/scene_gt.json ]] && \
[[ -e $SCENE_PATH/bop/scene_camera.json ]] && \
[[ -e $SCENE_PATH/bop/obj_$(printf "%06d" $CONFIG_BOP_ID).ply ]]
then
info "Found existing BOP annotations in `path $SCENE_PATH/bop`. Skipping."
elif [[ ! -e $SCENE_PATH/images_scene/ ]] || \
[[ ! -e $SCENE_PATH/depth_scene/ ]] || \
[[ ! -e $SCENE_PATH/bop_raw/mask/ ]] || \
[[ ! -e $SCENE_PATH/bop_raw/mask_visib/ ]] || \
[[ ! -e $SCENE_PATH/bop_raw/scene_gt.json ]] || \
[[ ! -e $SCENE_PATH/bop_raw/scene_camera.json ]] || \
[[ ! -e $SCENE_PATH/meshes/reference.ply ]]
then
error "Unable to find required input files for final BOP annotations. Exiting."
else
mkdir -p $SCENE_PATH/bop &&
cp -r $SCENE_PATH/images_scene $SCENE_PATH/bop/rgb &&
cp -r $SCENE_PATH/depth_scene $SCENE_PATH/bop/depth_nerf &&
cp -r $SCENE_PATH/bop_raw/mask $SCENE_PATH/bop/mask &&
cp -r $SCENE_PATH/bop_raw/mask_visib $SCENE_PATH/bop/mask_visib &&
cp $SCENE_PATH/bop_raw/scene_gt.json $SCENE_PATH/bop/scene_gt.json &&
cp $SCENE_PATH/bop_raw/scene_camera.json $SCENE_PATH/bop/scene_camera.json &&
cp $SCENE_PATH/meshes/reference.ply $SCENE_PATH/bop/obj_$(printf "%06d" $CONFIG_BOP_ID).ply ||
exit_with_error
fi
##################################################
# Done!
##################################################
header "Done!"
info "Final annotations are written to `path $SCENE_PATH/bop`."
exit 0