Skip to content

Commit

Permalink
copy geo-location to stain overlay images work
Browse files Browse the repository at this point in the history
  • Loading branch information
cnpcshangbo committed Mar 1, 2024
1 parent 24e2db8 commit 3a924bb
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 36 deletions.
15 changes: 15 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
11 changes: 10 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,14 @@
"cSpell.words": [
"stainmask",
"stainoverlay"
]
],
"python.testing.unittestArgs": [
"-v",
"-s",
".",
"-p",
"test_*.py"
],
"python.testing.pytestEnabled": false,
"python.testing.unittestEnabled": true
}
10 changes: 5 additions & 5 deletions config.ini
Original file line number Diff line number Diff line change
@@ -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.

2 changes: 2 additions & 0 deletions copy_geo_exiftool.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/home/roboticslab/Developer/image2overlays/.conda/bin/python
import os
import subprocess
import json

Expand All @@ -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}",
Expand Down
35 changes: 11 additions & 24 deletions copy_geolocation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,25 @@
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) ...

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__":
Expand Down
9 changes: 5 additions & 4 deletions copy_geolocation_crack.py
Original file line number Diff line number Diff line change
Expand Up @@ -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...")
log_message("Copying geolocation info to crack overlay...")
19 changes: 19 additions & 0 deletions copy_geolocation_stain.py
Original file line number Diff line number Diff line change
@@ -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...")
7 changes: 5 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.")

Expand Down

0 comments on commit 3a924bb

Please sign in to comment.