From 3a924bbf36dd8ab407d86434ed757e5cb93df95c Mon Sep 17 00:00:00 2001 From: Bo Shang Date: Thu, 29 Feb 2024 21:24:07 -0500 Subject: [PATCH] copy geo-location to stain overlay images work --- .vscode/launch.json | 15 +++++++++++++++ .vscode/settings.json | 11 ++++++++++- config.ini | 10 +++++----- copy_geo_exiftool.py | 2 ++ copy_geolocation.py | 35 +++++++++++------------------------ copy_geolocation_crack.py | 9 +++++---- copy_geolocation_stain.py | 19 +++++++++++++++++++ main.py | 7 +++++-- 8 files changed, 72 insertions(+), 36 deletions(-) create mode 100644 .vscode/launch.json create mode 100644 copy_geolocation_stain.py diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..6b76b4f --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,15 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Python Debugger: Current File", + "type": "debugpy", + "request": "launch", + "program": "${file}", + "console": "integratedTerminal" + } + ] +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json index 1e75118..4827440 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -2,5 +2,14 @@ "cSpell.words": [ "stainmask", "stainoverlay" - ] + ], + "python.testing.unittestArgs": [ + "-v", + "-s", + ".", + "-p", + "test_*.py" + ], + "python.testing.pytestEnabled": false, + "python.testing.unittestEnabled": true } \ No newline at end of file diff --git a/config.ini b/config.ini index e25b153..a5fa492 100644 --- a/config.ini +++ b/config.ini @@ -1,25 +1,25 @@ [Settings] image_path = /home/roboticslab/Downloads/2d_up_9_to_8_test/images -output_directory = /home/roboticslab/Downloads/2d_up_9_to_8_test/images_out/2024-02-28_21-23-17 +output_directory = /home/roboticslab/Downloads/2d_up_9_to_8_test/images_out/2024-02-26_23-06-36 output_dir_message = output_directory is auto-generated. [CrackSegmentation] config = config_bo.json model = saved/UperNet/01-17_12-55/best_model.pth -mask_directory = /home/roboticslab/Downloads/2d_up_9_to_8_test/images_out/2024-02-28_21-23-17/crackmask +mask_directory = /home/roboticslab/Downloads/2d_up_9_to_8_test/images_out/2024-02-26_23-06-36/crackmask mask_dir_message = mask_directory is auto-generated. [StainSegmentation] config = config_stain.json model = saved/StainNet/2024-02-26_00-05/best_model.pth -mask_directory = /home/roboticslab/Downloads/2d_up_9_to_8_test/images_out/2024-02-28_21-23-17/stainmask +mask_directory = /home/roboticslab/Downloads/2d_up_9_to_8_test/images_out/2024-02-26_23-06-36/stainmask mask_dir_message = mask_directory is auto-generated. [CrackOverlay] -overlay_directory = /home/roboticslab/Downloads/2d_up_9_to_8_test/images_out/2024-02-28_21-23-17/crackoverlay/images +overlay_directory = /home/roboticslab/Downloads/2d_up_9_to_8_test/images_out/2024-02-26_23-06-36/crackoverlay/images overlay_dir_message = overlay_directory is auto-generated. [StainOverlay] -overlay_directory = /home/roboticslab/Downloads/2d_up_9_to_8_test/images_out/2024-02-28_21-23-17/stainoverlay/images +overlay_directory = /home/roboticslab/Downloads/2d_up_9_to_8_test/images_out/2024-02-26_23-06-36/stainoverlay/images overlay_dir_message = overlay_directory is auto-generated. diff --git a/copy_geo_exiftool.py b/copy_geo_exiftool.py index 2fa9a01..03f114c 100644 --- a/copy_geo_exiftool.py +++ b/copy_geo_exiftool.py @@ -1,4 +1,5 @@ #!/home/roboticslab/Developer/image2overlays/.conda/bin/python +import os import subprocess import json @@ -16,6 +17,7 @@ def get_geolocation_with_exiftool(image_path): def write_geolocation_with_exiftool(dest_image, latitude, longitude, altitude): command = [ "exiftool", + "-overwrite_original", # Modify in-place f"-GPSLatitude={latitude}", f"-GPSLongitude={longitude}", f"-GPSAltitude={altitude}", diff --git a/copy_geolocation.py b/copy_geolocation.py index 2d06488..b4acd83 100644 --- a/copy_geolocation.py +++ b/copy_geolocation.py @@ -2,29 +2,7 @@ import argparse import piexif from PIL import Image - - -def process_single_image(raw_image_path, mask_image_path): - """Copies geolocation (EXIF) data from a raw image to a corresponding mask image. - - Args: - raw_image_path (str): Path to the raw image file. - mask_image_path (str): Path to the mask image file. - """ - - if os.path.exists(mask_image_path): - try: - with Image.open(raw_image_path) as raw_image: - exif_data = piexif.load(raw_image.info.get("exif", {})) - - # Handle cases where no EXIF data exists - if exif_data: - with Image.open(mask_image_path) as mask_image: - mask_image.save(mask_image_path, "JPEG", exif=piexif.dump(exif_data)) - print(f"Geolocation copied to: {mask_image_path}") - - except (piexif.InvalidImageDataError, OSError) as e: - print(f"Error processing images: {e}") +from copy_geo_exiftool import process_single_image def main(raw_images_dir=None, mask_images_dir=None): # ... (argument parsing - same as before) ... @@ -32,8 +10,17 @@ def main(raw_images_dir=None, mask_images_dir=None): for raw_image_name in os.listdir(raw_images_dir): raw_image_path = os.path.join(raw_images_dir, raw_image_name) # ... (construct mask_image_path - same as before) ... + # Assuming mask images have a similar naming convention + mask_image_name = raw_image_name.replace( + "raw", "mask" + ) # Example transformation + mask_image_path = os.path.join(mask_images_dir, mask_image_name) + if os.path.exists(mask_image_path): + process_single_image(raw_image_path, mask_image_path) + else: + print("Mask image not found. Skipping to the next image.") + # raise FileNotFoundError("Mask image not found at specified path.") - process_single_image(raw_image_path, mask_image_path) if __name__ == "__main__": diff --git a/copy_geolocation_crack.py b/copy_geolocation_crack.py index 8123df4..8135bd9 100644 --- a/copy_geolocation_crack.py +++ b/copy_geolocation_crack.py @@ -11,13 +11,14 @@ # Extract image_path and mask_directory from the config file image_path = config['Settings']['image_path'] # Assuming you want to use CrackSegmentation's mask_directory for this example -mask_directory = config['CrackSegmentation']['mask_directory'] - +# mask_directory = config['CrackSegmentation']['mask_directory'] +overlay_directory = config['CrackOverlay']['overlay_directory'] # The path to your copy_geolocation.py script copy_geolocation_script_path = 'copy_geolocation.py' # Call copy_geolocation.py with the extracted paths # subprocess.run(['python', copy_geolocation_script_path, image_path, mask_directory], check=True) -call_in_conda_env(f"python {copy_geolocation_script_path} {image_path} {mask_directory}", "/home/roboticslab/Developer/image2overlays/.conda") +# call_in_conda_env(f"python {copy_geolocation_script_path} {image_path} {mask_directory}", "/home/roboticslab/Developer/image2overlays/.conda") +call_in_conda_env(f"python {copy_geolocation_script_path} {image_path} {overlay_directory}", "/home/roboticslab/Developer/image2overlays/.conda") -log_message("Copying geolocation info to stain overlay...") \ No newline at end of file +log_message("Copying geolocation info to crack overlay...") \ No newline at end of file diff --git a/copy_geolocation_stain.py b/copy_geolocation_stain.py new file mode 100644 index 0000000..1ee9790 --- /dev/null +++ b/copy_geolocation_stain.py @@ -0,0 +1,19 @@ +#!~/anaconda3/bin/python +#Copy geolocation data to stain overlay images +import configparser +from main import log_message, call_in_conda_env + +# Load the configuration file +config = configparser.ConfigParser() +config.read('config.ini') + +# Extract image_path and overlay_directory from the config file +image_path = config['Settings']['image_path'] +overlay_directory = config['StainOverlay']['overlay_directory'] +# The path to your copy_geolocation.py script +copy_geolocation_script_path = 'copy_geolocation.py' + +# Call copy_geolocation.py with the extracted paths +call_in_conda_env(f"python {copy_geolocation_script_path} {image_path} {overlay_directory}", "/home/roboticslab/Developer/image2overlays/.conda") + +log_message("Copying geolocation info to stain overlay...") \ No newline at end of file diff --git a/main.py b/main.py index 69e3179..1099f0e 100644 --- a/main.py +++ b/main.py @@ -41,6 +41,9 @@ def main(): log_message("Running crack overlay...") call_in_conda_env("python crackoverlay.py") + + log_message("Copying geolocation info to crack overlay...") + call_in_conda_env("python copy_geolocation_crack.py") if process_stain: # Run stain related processing @@ -50,8 +53,8 @@ def main(): log_message("Running stain overlay...") call_in_conda_env("python stainoverlay.py") - log_message("Copying geolocation info to crack overlay...") - call_in_conda_env("python copy_geolocation_crack.py") + log_message("Copying geolocation info to stain overlay...") + call_in_conda_env("python copy_geolocation_stain.py") log_message("Script sequence completed.")