From 3c0d4ac09350732a5a61eb8ae545d547b4ea5a77 Mon Sep 17 00:00:00 2001 From: Luke Lesh Date: Fri, 9 Apr 2021 00:25:41 +0000 Subject: [PATCH 1/4] FEATURE: added script to move sweeps to correct folder --- .../scripts/correct_sweeps.py | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 src/universal_devkit/scripts/correct_sweeps.py diff --git a/src/universal_devkit/scripts/correct_sweeps.py b/src/universal_devkit/scripts/correct_sweeps.py new file mode 100644 index 0000000..cb8df53 --- /dev/null +++ b/src/universal_devkit/scripts/correct_sweeps.py @@ -0,0 +1,45 @@ +""" +Correct timestamps of already annotated files. + +$ python correct_sweeps.py -i input_directory +""" + +import argparse +import os +import shutil + +from universal_devkit.utils.utils import get_closest_match + + +def correct_sweeps(input_dir): + annotation_dir = os.path.join(input_dir, "samples", "LIDAR_TOP", "annotations") + camera_dir = os.path.join(input_dir, "samples", "CAMERA_FRONT") + sweeps_pcd_dir = os.path.join(input_dir, "sweeps", "LIDAR_TOP") + sweeps_image_dir = os.path.join(input_dir, "sweeps", "CAMERA_FRONT") + camera_images = [] + for filename in os.listdir(camera_dir): + filename_no_extension, extension = os.path.splitext(filename) + if extension == ".png": + camera_images.append(int(filename_no_extension)) + camera_images.sort() + for filename in os.listdir(annotation_dir): + if os.path.getsize(os.path.join(annotation_dir, filename)) == 2: + filename_no_extension, extension = os.path.splitext(filename) + pcd_file = os.path.join( + input_dir, "samples", "LIDAR_TOP", filename_no_extension + ) + shutil.move(pcd_file, sweeps_pcd_dir) + filename_no_extension_no_pcd, extension = os.path.splitext( + filename_no_extension + ) + image = get_closest_match(camera_images, int(filename_no_extension_no_pcd)) + image_file = os.path.join(camera_dir, str(image) + ".png") + shutil.move(image_file, sweeps_image_dir) + + +if __name__ == "__main__": + # Construct the argument parser and parse the arguments + ap = argparse.ArgumentParser() + ap.add_argument("-i", "--input", type=str, help="The input directory") + args = vars(ap.parse_args()) + correct_sweeps(args["input"]) From 590d50e78afbc9fa7d9d8775c991ea43a2b7b423 Mon Sep 17 00:00:00 2001 From: Eric Wiener Date: Fri, 9 Apr 2021 15:14:32 -0400 Subject: [PATCH 2/4] FEAT: correct_sweeps now deleting empty annotation file Also converted loop to get all camera images from using os.listdir to using glob. --- .../scripts/correct_sweeps.py | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/universal_devkit/scripts/correct_sweeps.py b/src/universal_devkit/scripts/correct_sweeps.py index cb8df53..19a0dc8 100644 --- a/src/universal_devkit/scripts/correct_sweeps.py +++ b/src/universal_devkit/scripts/correct_sweeps.py @@ -1,5 +1,6 @@ """ -Correct timestamps of already annotated files. +Move all keyframes with empty annotations into the sweeps directory. +Will also move corresponding images and delete empty annotation files. $ python correct_sweeps.py -i input_directory """ @@ -7,6 +8,8 @@ import argparse import os import shutil +from glob import glob +from pathlib import Path from universal_devkit.utils.utils import get_closest_match @@ -16,12 +19,12 @@ def correct_sweeps(input_dir): camera_dir = os.path.join(input_dir, "samples", "CAMERA_FRONT") sweeps_pcd_dir = os.path.join(input_dir, "sweeps", "LIDAR_TOP") sweeps_image_dir = os.path.join(input_dir, "sweeps", "CAMERA_FRONT") - camera_images = [] - for filename in os.listdir(camera_dir): - filename_no_extension, extension = os.path.splitext(filename) - if extension == ".png": - camera_images.append(int(filename_no_extension)) + + camera_images = [ + int(Path(filepath).stem) for filepath in glob("{}/*.png".format(camera_dir)) + ] camera_images.sort() + for filename in os.listdir(annotation_dir): if os.path.getsize(os.path.join(annotation_dir, filename)) == 2: filename_no_extension, extension = os.path.splitext(filename) @@ -36,10 +39,15 @@ def correct_sweeps(input_dir): image_file = os.path.join(camera_dir, str(image) + ".png") shutil.move(image_file, sweeps_image_dir) + # Remove the empty annotation file + os.remove(os.path.join(annotation_dir, filename)) + if __name__ == "__main__": # Construct the argument parser and parse the arguments ap = argparse.ArgumentParser() - ap.add_argument("-i", "--input", type=str, help="The input directory") + ap.add_argument( + "-i", "--input", type=str, required=True, help="The input directory" + ) args = vars(ap.parse_args()) correct_sweeps(args["input"]) From 531f690997f98bc9994cedb95c74614ba0fb2cb5 Mon Sep 17 00:00:00 2001 From: Luke Lesh Date: Fri, 16 Apr 2021 01:46:19 +0000 Subject: [PATCH 3/4] FIX: correctly moves all images needed to the sweeps folder --- .../scripts/correct_sweeps.py | 29 +++++++++++++------ 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/src/universal_devkit/scripts/correct_sweeps.py b/src/universal_devkit/scripts/correct_sweeps.py index 19a0dc8..3a7489d 100644 --- a/src/universal_devkit/scripts/correct_sweeps.py +++ b/src/universal_devkit/scripts/correct_sweeps.py @@ -23,24 +23,35 @@ def correct_sweeps(input_dir): camera_images = [ int(Path(filepath).stem) for filepath in glob("{}/*.png".format(camera_dir)) ] - camera_images.sort() + + pcd_sweeps = [] + pcd_samples = [] for filename in os.listdir(annotation_dir): + filename_no_extension, extension = os.path.splitext(filename) + filename_no_extension_no_pcd, extension = os.path.splitext( + filename_no_extension + ) if os.path.getsize(os.path.join(annotation_dir, filename)) == 2: - filename_no_extension, extension = os.path.splitext(filename) pcd_file = os.path.join( input_dir, "samples", "LIDAR_TOP", filename_no_extension ) shutil.move(pcd_file, sweeps_pcd_dir) - filename_no_extension_no_pcd, extension = os.path.splitext( - filename_no_extension - ) - image = get_closest_match(camera_images, int(filename_no_extension_no_pcd)) - image_file = os.path.join(camera_dir, str(image) + ".png") - shutil.move(image_file, sweeps_image_dir) - + pcd_sweeps.append(int(filename_no_extension_no_pcd)) # Remove the empty annotation file os.remove(os.path.join(annotation_dir, filename)) + else: + pcd_samples.append(int(filename_no_extension_no_pcd)) + + pcd_sweeps.sort() + pcd_samples.sort() + + for image in camera_images: + closest_sweep = get_closest_match(pcd_sweeps, image) + closest_sample = get_closest_match(pcd_samples, image) + if abs(closest_sweep - image) < abs(closest_sample - image): + image_file = os.path.join(camera_dir, str(image) + ".png") + shutil.move(image_file, sweeps_image_dir) if __name__ == "__main__": From 0605a242ed67f11692a43386b19a93a435371765 Mon Sep 17 00:00:00 2001 From: Luke Lesh Date: Sat, 17 Apr 2021 01:56:36 +0000 Subject: [PATCH 4/4] FEAT: added second script for 2 folders --- .../scripts/correct_sweeps_2_folders.py | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 src/universal_devkit/scripts/correct_sweeps_2_folders.py diff --git a/src/universal_devkit/scripts/correct_sweeps_2_folders.py b/src/universal_devkit/scripts/correct_sweeps_2_folders.py new file mode 100644 index 0000000..b52f00a --- /dev/null +++ b/src/universal_devkit/scripts/correct_sweeps_2_folders.py @@ -0,0 +1,68 @@ +""" +Move all keyframes with empty annotations into the sweeps directory. +Will also move corresponding images and delete empty annotation files. + +$ python correct_sweeps.py -i input_directory +""" + +import argparse +import os +import shutil +from glob import glob +from pathlib import Path + +from universal_devkit.utils.utils import get_closest_match + + +def correct_sweeps(input_dir): + annotation_dir = os.path.join(input_dir, "samples", "LIDAR_TOP", "annotations") + camera_dir1 = os.path.join(input_dir, "samples", "CAMERA_FRONT_RIGHT") + camera_dir2 = os.path.join(input_dir, "samples", "CAMERA_FRONT_LEFT") + sweeps_pcd_dir = os.path.join(input_dir, "sweeps", "LIDAR_TOP") + sweeps_image_dir1 = os.path.join(input_dir, "sweeps", "CAMERA_FRONT_RIGHT") + sweeps_image_dir2 = os.path.join(input_dir, "sweeps", "CAMERA_FRONT_LEFT") + + camera_images = [ + int(Path(filepath).stem) for filepath in glob("{}/*.png".format(camera_dir1)) + ] + + pcd_sweeps = [] + pcd_samples = [] + + for filename in os.listdir(annotation_dir): + filename_no_extension, extension = os.path.splitext(filename) + filename_no_extension_no_pcd, extension = os.path.splitext( + filename_no_extension + ) + if os.path.getsize(os.path.join(annotation_dir, filename)) == 2: + pcd_file = os.path.join( + input_dir, "samples", "LIDAR_TOP", filename_no_extension + ) + shutil.move(pcd_file, sweeps_pcd_dir) + pcd_sweeps.append(int(filename_no_extension_no_pcd)) + # Remove the empty annotation file + os.remove(os.path.join(annotation_dir, filename)) + else: + pcd_samples.append(int(filename_no_extension_no_pcd)) + + pcd_sweeps.sort() + pcd_samples.sort() + + for image in camera_images: + closest_sweep = get_closest_match(pcd_sweeps, image) + closest_sample = get_closest_match(pcd_samples, image) + if abs(closest_sweep - image) < abs(closest_sample - image): + image_file1 = os.path.join(camera_dir1, str(image) + ".png") + shutil.move(image_file1, sweeps_image_dir1) + image_file2 = os.path.join(camera_dir2, str(image) + ".png") + shutil.move(image_file2, sweeps_image_dir2) + + +if __name__ == "__main__": + # Construct the argument parser and parse the arguments + ap = argparse.ArgumentParser() + ap.add_argument( + "-i", "--input", type=str, required=True, help="The input directory" + ) + args = vars(ap.parse_args()) + correct_sweeps(args["input"])