From 1e71e2bcac08f08805d92694bba551834cae2441 Mon Sep 17 00:00:00 2001 From: Bo Shang Date: Thu, 30 May 2024 21:19:32 -0400 Subject: [PATCH] Export Filtered Overlay Scripts --- export_filtered_overlay_png/Readme.md | 7 +++ .../copy_and_resize.py | 46 ++++++++++++++++ .../copy_and_resize_parallel.py | 53 +++++++++++++++++++ .../export_filtered_overlay_png.py | 35 ++++++++++++ .../export_nn_filtered_mask.py | 35 ++++++++++++ .../export_solid_filtered_overlay.py | 41 ++++++++++++++ 6 files changed, 217 insertions(+) create mode 100644 export_filtered_overlay_png/Readme.md create mode 100644 export_filtered_overlay_png/copy_and_resize.py create mode 100644 export_filtered_overlay_png/copy_and_resize_parallel.py create mode 100644 export_filtered_overlay_png/export_filtered_overlay_png.py create mode 100644 export_filtered_overlay_png/export_nn_filtered_mask.py create mode 100644 export_filtered_overlay_png/export_solid_filtered_overlay.py diff --git a/export_filtered_overlay_png/Readme.md b/export_filtered_overlay_png/Readme.md new file mode 100644 index 0000000..3409ec9 --- /dev/null +++ b/export_filtered_overlay_png/Readme.md @@ -0,0 +1,7 @@ +# Export Filtered Overlay Scripts +Those scripts are used for exporting the filtered overlay. The filtered overlay images are created by the script "crack2curve.py". +- "export_filtered_overlay_png.py": export the filtered overlay png, but the result image size is too large. +- "copy_and_resize.py": resize "filtered_overlay.png" from the curve folder and place the results into nn_filtered_crack_overlay folder; +- "copy_and_resize_parallel.py": this file used parallel processing to make the processing faster. +- "export_nn_filtered_mask.py": export the filtered mask, those masks may be used by the front end. +- "export_solid_filtered_overlay.py": export the solid filtered overlay. Those files are used by the WebODM for 3D reconstruction. \ No newline at end of file diff --git a/export_filtered_overlay_png/copy_and_resize.py b/export_filtered_overlay_png/copy_and_resize.py new file mode 100644 index 0000000..09dc64a --- /dev/null +++ b/export_filtered_overlay_png/copy_and_resize.py @@ -0,0 +1,46 @@ +import os +import shutil +from PIL import Image + +def copy_and_resize(input_folder, output_folder, new_width=800): + """Copies and resizes filtered_overlay.png images from subfolders in the input folder to the output folder. + + Args: + input_folder (str): Path to the folder containing subfolders with images. + output_folder (str): Path to the destination folder for the resized images. + new_width (int, optional): The desired width for the resized images (default is 800 pixels). + """ + + os.makedirs(output_folder, exist_ok=True) + + for subfolder in os.listdir(input_folder): + subfolder_path = os.path.join(input_folder, subfolder) + + if os.path.isdir(subfolder_path): + filtered_overlay_path = os.path.join(subfolder_path, "filtered_overlay.png") + + if os.path.exists(filtered_overlay_path): + new_filename = os.path.join(output_folder, subfolder) + + # Open the image + img = Image.open(filtered_overlay_path) + + # Calculate the new height to maintain aspect ratio + width, height = img.size + new_height = int(new_width * height / width) + + # Resize the image + resized_img = img.resize((new_width, new_height), Image.Resampling.LANCZOS) + + # Save the resized image + resized_img.save(new_filename) + print(f"Copied and resized {filtered_overlay_path} to {new_filename}") + else: + print(f"Warning: filtered_overlay.png not found in {subfolder_path}") + +# --- (Use the same input/output paths as before) --- +# Get the input and output folder paths +input_folder = "/home/roboticslab/Downloads/OneDrive_2024-02-03/NYCSpan8-9/raw/images_out/2024-04-26_07-29-38/curve" +output_folder = "/home/roboticslab/Downloads/OneDrive_2024-02-03/NYCSpan8-9/raw/images_out/2024-04-26_07-29-38/nn_filtered_crack_overlay" + +copy_and_resize(input_folder, output_folder) diff --git a/export_filtered_overlay_png/copy_and_resize_parallel.py b/export_filtered_overlay_png/copy_and_resize_parallel.py new file mode 100644 index 0000000..81a04b4 --- /dev/null +++ b/export_filtered_overlay_png/copy_and_resize_parallel.py @@ -0,0 +1,53 @@ +import os +import shutil +from PIL import Image +from multiprocessing import Pool + +def process_image(args): + """Processes a single image: copies and resizes it. + + Args: + args (tuple): A tuple containing (subfolder_path, output_folder, new_width) + """ + + subfolder_path, output_folder, new_width = args # Unpack the arguments + filtered_overlay_path = os.path.join(subfolder_path, "filtered_overlay.png") + + if os.path.exists(filtered_overlay_path): + new_filename = os.path.join(output_folder, os.path.basename(subfolder_path)) + + img = Image.open(filtered_overlay_path) + width, height = img.size + new_height = int(new_width * height / width) + + resized_img = img.resize((new_width, new_height), Image.Resampling.LANCZOS) + resized_img.save(new_filename) + print(f"Copied and resized {filtered_overlay_path} to {new_filename}") + else: + print(f"Warning: filtered_overlay.png not found in {subfolder_path}") + +def copy_and_resize_parallel(input_folder, output_folder, new_width=800): + """Copies and resizes images in parallel using multiprocessing. + + Args: + input_folder (str): Path to the folder containing subfolders with images. + output_folder (str): Path to the destination folder for the resized images. + new_width (int, optional): The desired width for the resized images (default is 800 pixels). + """ + + os.makedirs(output_folder, exist_ok=True) + + # Prepare arguments for each image (subfolder) + args = [(os.path.join(input_folder, subfolder), output_folder, new_width) + for subfolder in os.listdir(input_folder) if os.path.isdir(os.path.join(input_folder, subfolder))] + + # Use a Pool of workers to process images in parallel + with Pool() as pool: # Automatically uses the optimal number of processes based on your CPU cores + pool.map(process_image, args) + + +# --- (Use the same input/output paths as before) --- +input_folder = "/home/roboticslab/Downloads/OneDrive_2024-02-03/NYCSpan8-9/raw/images_out/2024-04-26_07-29-38/curve" +output_folder = "/home/roboticslab/Downloads/OneDrive_2024-02-03/NYCSpan8-9/raw/images_out/2024-04-26_07-29-38/nn_filtered_crack_overlay" + +copy_and_resize_parallel(input_folder, output_folder) diff --git a/export_filtered_overlay_png/export_filtered_overlay_png.py b/export_filtered_overlay_png/export_filtered_overlay_png.py new file mode 100644 index 0000000..8e8dba6 --- /dev/null +++ b/export_filtered_overlay_png/export_filtered_overlay_png.py @@ -0,0 +1,35 @@ +import os +import shutil + +def copy_filtered_overlays(input_folder, output_folder): + """Copies filtered_overlay.png images from subfolders in the input folder to the output folder. + + Args: + input_folder (str): Path to the folder containing subfolders with images. + output_folder (str): Path to the destination folder for the filtered overlay images. + """ + + os.makedirs(output_folder, exist_ok=True) # Create output folder if it doesn't exist + + for subfolder in os.listdir(input_folder): + subfolder_path = os.path.join(input_folder, subfolder) + + # Check if the item is a directory (we're expecting folders like S1074591.png) + if os.path.isdir(subfolder_path): + filtered_overlay_path = os.path.join(subfolder_path, "filtered_overlay.png") + + # Check if filtered_overlay.png exists in the subfolder + if os.path.exists(filtered_overlay_path): + # Construct the new filename in the output folder (using the subfolder name) + new_filename = os.path.join(output_folder, subfolder) + shutil.copy2(filtered_overlay_path, new_filename) # Copy with metadata + print(f"Copied {filtered_overlay_path} to {new_filename}") + else: + print(f"Warning: filtered_overlay.png not found in {subfolder_path}") + +# Get the input and output folder paths +input_folder = "/home/roboticslab/Downloads/OneDrive_2024-02-03/NYCSpan8-9/raw/images_out/2024-04-26_07-29-38/curve" +output_folder = "/home/roboticslab/Downloads/OneDrive_2024-02-03/NYCSpan8-9/raw/images_out/2024-04-26_07-29-38/nn_filtered_crack_overlay" + +# Call the function to copy the images +copy_filtered_overlays(input_folder, output_folder) diff --git a/export_filtered_overlay_png/export_nn_filtered_mask.py b/export_filtered_overlay_png/export_nn_filtered_mask.py new file mode 100644 index 0000000..29380b0 --- /dev/null +++ b/export_filtered_overlay_png/export_nn_filtered_mask.py @@ -0,0 +1,35 @@ +import os +import shutil + +def copy_filtered_overlays(input_folder, output_folder): + """Copies filtered_overlay.png images from subfolders in the input folder to the output folder. + + Args: + input_folder (str): Path to the folder containing subfolders with images. + output_folder (str): Path to the destination folder for the filtered overlay images. + """ + + os.makedirs(output_folder, exist_ok=True) # Create output folder if it doesn't exist + + for subfolder in os.listdir(input_folder): + subfolder_path = os.path.join(input_folder, subfolder) + + # Check if the item is a directory (we're expecting folders like S1074591.png) + if os.path.isdir(subfolder_path): + filtered_overlay_path = os.path.join(subfolder_path, "filtered_mask.png") + + # Check if filtered_overlay.png exists in the subfolder + if os.path.exists(filtered_overlay_path): + # Construct the new filename in the output folder (using the subfolder name) + new_filename = os.path.join(output_folder, subfolder) + shutil.copy2(filtered_overlay_path, new_filename) # Copy with metadata + print(f"Copied {filtered_overlay_path} to {new_filename}") + else: + print(f"Warning: filtered_overlay.png not found in {subfolder_path}") + +# Get the input and output folder paths +input_folder = "/home/roboticslab/Downloads/OneDrive_2024-02-03/NYCSpan8-9/raw/images_out/2024-04-26_07-29-38/curve" +output_folder = "/home/roboticslab/Downloads/OneDrive_2024-02-03/NYCSpan8-9/raw/images_out/2024-04-26_07-29-38/nn_filtered_crack_mask" + +# Call the function to copy the images +copy_filtered_overlays(input_folder, output_folder) diff --git a/export_filtered_overlay_png/export_solid_filtered_overlay.py b/export_filtered_overlay_png/export_solid_filtered_overlay.py new file mode 100644 index 0000000..9f358c5 --- /dev/null +++ b/export_filtered_overlay_png/export_solid_filtered_overlay.py @@ -0,0 +1,41 @@ +import os +import shutil + + +def copy_filtered_overlays(input_folder, output_folder): + """Copies filtered_overlay.png images from subfolders in the input folder to the output folder. + + Args: + input_folder (str): Path to the folder containing subfolders with images. + output_folder (str): Path to the destination folder for the filtered overlay images. + """ + + os.makedirs( + output_folder, exist_ok=True + ) # Create output folder if it doesn't exist + + for subfolder in os.listdir(input_folder): + subfolder_path = os.path.join(input_folder, subfolder) + + # Check if the item is a directory (we're expecting folders like S1074591.png) + if os.path.isdir(subfolder_path): + filtered_overlay_path = os.path.join( + subfolder_path, "solid_filtered_overlay.png" + ) + + # Check if filtered_overlay.png exists in the subfolder + if os.path.exists(filtered_overlay_path): + # Construct the new filename in the output folder (using the subfolder name) + new_filename = os.path.join(output_folder, subfolder) + shutil.copy2(filtered_overlay_path, new_filename) # Copy with metadata + print(f"Copied {filtered_overlay_path} to {new_filename}") + else: + print(f"Warning: filtered_overlay.png not found in {subfolder_path}") + + +# Get the input and output folder paths +input_folder = "/home/roboticslab/Downloads/OneDrive_2024-02-03/NYCSpan8-9/raw/images_out/2024-04-26_07-29-38/curve" +output_folder = "/home/roboticslab/Downloads/OneDrive_2024-02-03/NYCSpan8-9/raw/images_out/2024-04-26_07-29-38/nn_solidfiltered_crack_overlay" + +# Call the function to copy the images +copy_filtered_overlays(input_folder, output_folder)