-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcrack2curve.py
130 lines (97 loc) · 4.76 KB
/
crack2curve.py
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
# This script is to parse the ini file to:
# - get the path for crack_spall mask file
# - get the path for 'curve' folder and make sure it exist
# - extract only the crack mask
# - convert this crack mask into curve and straight and save curves into the 'curve' folder
import os
import configparser
from pathlib import Path
from DirectoryImageMaskProcessor_1 import DirectoryImageMaskProcessor
from DirectoryImageMaskProcessor_2curve import (
DirectoryImageMaskProcessor_2curve,
)
class CrackMaskProcessor:
def __init__(self, config_file):
self.config_file = config_file
self.raw_directory = None
self.mask_directory = None
self.config_param = None
self.model = None
self._parse_config()
def _parse_config(self):
config = configparser.ConfigParser()
config.read(self.config_file)
self.raw_directory = config["Settings"]["image_path"]
self.mask_directory = config["CrackSegmentation"]["mask_directory"]
self.config_param = config["CrackSegmentation"]["config"]
self.model = config["CrackSegmentation"]["model"]
print(f"Configurations loaded:")
print(f"RAW_DIR={self.raw_directory}")
print(f"MASK_DIR={self.mask_directory}")
print(f"CONFIG={self.config_param}")
print(f"MODEL={self.model}")
def ensure_directories_exist(self):
if not os.path.exists(self.mask_directory):
os.makedirs(self.mask_directory)
print(f"Ensured existence of directory: {self.mask_directory}")
def get_raw_directory_path(self):
return self.raw_directory
def get_crack_spall_mask_path(self):
return self.mask_directory
def get_red_crack_mask_path(self):
return self.mask_directory.replace("crackmask", "red_crack_masks")
# def get_and_ensure_3masks_directory(self):
# # Replace 'crackmask' with '3masks' in the mask_directory path
# masks_directory = self.mask_directory.replace("crackmask", "3masks")
# # Ensure the '3masks' directory exists
# if not os.path.exists(masks_directory):
# os.makedirs(masks_directory)
# print(f"Ensured existence of directory: {masks_directory}")
# return masks_directory
def get_and_ensure_curve_directory(self):
# Replace 'crackmask' with 'curve' in the mask_directory path
masks_directory = self.mask_directory.replace("crackmask", "curve")
# Ensure the 'curve' directory exists
if not os.path.exists(masks_directory):
os.makedirs(masks_directory)
print(f"Ensured existence of directory: {masks_directory}")
return masks_directory
# Main part of the script
def extract_crack(crack_spall_mask_path):
input_directory = crack_spall_mask_path
processor_extract_crack = DirectoryImageMaskProcessor(input_directory)
processor_extract_crack.process_directory()
print(
f"Processing complete. Check the parent directory of '{input_directory}' for the results."
)
if __name__ == "__main__":
script_dir = Path(__file__).parent.absolute()
config_ini_path = script_dir / "config.ini"
print(f"Config INI Path: {config_ini_path}")
processor = CrackMaskProcessor(config_ini_path)
processor.ensure_directories_exist()
raw_directory_path = processor.get_raw_directory_path()
crack_spall_mask_path = processor.get_crack_spall_mask_path()
print(f"Crack/Spall Mask Path: {crack_spall_mask_path}")
# Extracting red crack images into a seperate file
# extract_crack(crack_spall_mask_path)
# Get red_crack_masks directory path
red_crack_mask_path = processor.get_red_crack_mask_path()
print(f"red_crack_mask Path: {red_crack_mask_path}")
# Before processing the directory, ensure it exists
os.makedirs(red_crack_mask_path, exist_ok=True)
# Check if the directory is empty
if not os.listdir(red_crack_mask_path):
print(f"Warning: The directory '{red_crack_mask_path}' is empty.")
print("Please ensure that the 'crack23directions.py' has been run and generated the red crack mask images.")
exit(1)
# List the files in the directory
files = [f for f in os.listdir(red_crack_mask_path) if f.lower().endswith(('.png', '.jpg', '.jpeg', '.tif', '.tiff'))]
print(f"Found {len(files)} image files in {red_crack_mask_path}")
if not files:
print("No image files found. Please check the file extensions and ensure the images are present.")
exit(1)
curve_directory_path = processor.get_and_ensure_curve_directory()
processor_extract_3 = DirectoryImageMaskProcessor_2curve(raw_directory_path, red_crack_mask_path)
processor_extract_3.process_directory()
print(f"Processing complete. Check the directory '{curve_directory_path}' for the results.")