update versions
Browse files- app.py +4 -3
- requirements.txt +103 -102
- utils.py +82 -108
app.py
CHANGED
|
@@ -139,11 +139,12 @@ with gr.Blocks() as app:
|
|
| 139 |
gr.Markdown('The models used in this app are based on the following papers:')
|
| 140 |
gr.Markdown('Kusche, C., Reclik, T., Freund, M., Al-Samman, T., Kerzel, U., & Korte-Kerzel, S. (2019). Large-area, high-resolution characterisation and classification of damage mechanisms in dual-phase steel using deep learning. PloS one, 14(5), e0216493. [Link](https://doi.org/10.1371/journal.pone.0216493)')
|
| 141 |
#gr.Markdown('Medghalchi, S., Kusche, C. F., Karimi, E., Kerzel, U., & Korte-Kerzel, S. (2020). Damage analysis in dual-phase steel using deep learning: transfer from uniaxial to biaxial straining conditions by image data augmentation. Jom, 72, 4420-4430. [Link](https://link.springer.com/article/10.1007/s11837-020-04404-0)')
|
| 142 |
-
gr.Markdown('Setareh Medghalchi, Ehsan Karimi, Sang-Hyeok Lee, Benjamin Berkels, Ulrich Kerzel, Sandra Korte-Kerzel, Three-dimensional characterisation of deformation-induced damage in dual phase steel using deep learning, Materials & Design, Volume 232, 2023, 112108, ISSN 0264-1275, [link] (https://doi.org/10.1016/j.matdes.2023.112108
|
| 143 |
gr.Markdown('Original data and code, including the network weights, can be found at Zenodo [link](https://zenodo.org/records/8065752)')
|
| 144 |
|
| 145 |
-
image_input = gr.Image()
|
| 146 |
-
|
|
|
|
| 147 |
cluster_threshold_input = gr.Number(label='Cluster Threshold', value = 20,
|
| 148 |
info='Grayscale value at which a pixel is attributed to a potential damage site')
|
| 149 |
model1_threshold_input = gr.Number(label='Model 1 Threshold', value = 0.7, info='Threshold for the model identifying inclusions')
|
|
|
|
| 139 |
gr.Markdown('The models used in this app are based on the following papers:')
|
| 140 |
gr.Markdown('Kusche, C., Reclik, T., Freund, M., Al-Samman, T., Kerzel, U., & Korte-Kerzel, S. (2019). Large-area, high-resolution characterisation and classification of damage mechanisms in dual-phase steel using deep learning. PloS one, 14(5), e0216493. [Link](https://doi.org/10.1371/journal.pone.0216493)')
|
| 141 |
#gr.Markdown('Medghalchi, S., Kusche, C. F., Karimi, E., Kerzel, U., & Korte-Kerzel, S. (2020). Damage analysis in dual-phase steel using deep learning: transfer from uniaxial to biaxial straining conditions by image data augmentation. Jom, 72, 4420-4430. [Link](https://link.springer.com/article/10.1007/s11837-020-04404-0)')
|
| 142 |
+
gr.Markdown('Setareh Medghalchi, Ehsan Karimi, Sang-Hyeok Lee, Benjamin Berkels, Ulrich Kerzel, Sandra Korte-Kerzel, Three-dimensional characterisation of deformation-induced damage in dual phase steel using deep learning, Materials & Design, Volume 232, 2023, 112108, ISSN 0264-1275, [link] (https://doi.org/10.1016/j.matdes.2023.112108')
|
| 143 |
gr.Markdown('Original data and code, including the network weights, can be found at Zenodo [link](https://zenodo.org/records/8065752)')
|
| 144 |
|
| 145 |
+
#image_input = gr.Image(value='data/X4-Aligned_cropped_upperleft_small.png', label='Example SEM Image (DP800 steel)',)
|
| 146 |
+
image_input = gr.Image()
|
| 147 |
+
with gr.Row():
|
| 148 |
cluster_threshold_input = gr.Number(label='Cluster Threshold', value = 20,
|
| 149 |
info='Grayscale value at which a pixel is attributed to a potential damage site')
|
| 150 |
model1_threshold_input = gr.Number(label='Model 1 Threshold', value = 0.7, info='Threshold for the model identifying inclusions')
|
requirements.txt
CHANGED
|
@@ -1,102 +1,103 @@
|
|
| 1 |
-
absl-py==2.
|
| 2 |
-
aiofiles==
|
| 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 |
-
tensorflow==
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
tqdm==4.
|
| 94 |
-
typer
|
| 95 |
-
typing-extensions==4.
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
|
|
|
|
|
| 1 |
+
absl-py==2.3.0 ; python_version >= "3.11"
|
| 2 |
+
aiofiles==24.1.0 ; python_version >= "3.11"
|
| 3 |
+
annotated-types==0.7.0 ; python_version >= "3.11"
|
| 4 |
+
anyio==4.9.0 ; python_version >= "3.11"
|
| 5 |
+
astunparse==1.6.3 ; python_version >= "3.11"
|
| 6 |
+
audioop-lts==0.2.1 ; python_version >= "3.13"
|
| 7 |
+
certifi==2025.6.15 ; python_version >= "3.11"
|
| 8 |
+
charset-normalizer==3.4.2 ; python_version >= "3.11"
|
| 9 |
+
click==8.2.1 ; python_version >= "3.11" and sys_platform != "emscripten"
|
| 10 |
+
colorama==0.4.6 ; python_version >= "3.11" and platform_system == "Windows"
|
| 11 |
+
contourpy==1.3.2 ; python_version >= "3.11"
|
| 12 |
+
cycler==0.12.1 ; python_version >= "3.11"
|
| 13 |
+
fastapi==0.115.13 ; python_version >= "3.11"
|
| 14 |
+
ffmpy==0.6.0 ; python_version >= "3.11"
|
| 15 |
+
filelock==3.18.0 ; python_version >= "3.11"
|
| 16 |
+
flatbuffers==25.2.10 ; python_version >= "3.11"
|
| 17 |
+
fonttools==4.58.4 ; python_version >= "3.11"
|
| 18 |
+
fsspec==2025.5.1 ; python_version >= "3.11"
|
| 19 |
+
gast==0.6.0 ; python_version >= "3.11"
|
| 20 |
+
google-pasta==0.2.0 ; python_version >= "3.11"
|
| 21 |
+
gradio-client==1.10.3 ; python_version >= "3.11"
|
| 22 |
+
gradio==5.34.2 ; python_version >= "3.11"
|
| 23 |
+
groovy==0.1.2 ; python_version >= "3.11"
|
| 24 |
+
grpcio==1.73.0 ; python_version >= "3.11"
|
| 25 |
+
h11==0.16.0 ; python_version >= "3.11"
|
| 26 |
+
h5py==3.14.0 ; python_version >= "3.11"
|
| 27 |
+
hf-xet==1.1.5 ; python_version >= "3.11" and (platform_machine == "x86_64" or platform_machine == "amd64" or platform_machine == "arm64" or platform_machine == "aarch64")
|
| 28 |
+
httpcore==1.0.9 ; python_version >= "3.11"
|
| 29 |
+
httpx==0.28.1 ; python_version >= "3.11"
|
| 30 |
+
huggingface-hub==0.33.0 ; python_version >= "3.11"
|
| 31 |
+
idna==3.10 ; python_version >= "3.11"
|
| 32 |
+
imageio==2.37.0 ; python_version >= "3.11"
|
| 33 |
+
jinja2==3.1.6 ; python_version >= "3.11"
|
| 34 |
+
joblib==1.5.1 ; python_version >= "3.11"
|
| 35 |
+
keras==3.10.0 ; python_version >= "3.11"
|
| 36 |
+
kiwisolver==1.4.8 ; python_version >= "3.11"
|
| 37 |
+
libclang==18.1.1 ; python_version >= "3.11"
|
| 38 |
+
markdown-it-py==3.0.0 ; python_version >= "3.11"
|
| 39 |
+
markdown==3.8.2 ; python_version >= "3.11"
|
| 40 |
+
markupsafe==3.0.2 ; python_version >= "3.11"
|
| 41 |
+
matplotlib==3.10.3 ; python_version >= "3.11"
|
| 42 |
+
mdurl==0.1.2 ; python_version >= "3.11"
|
| 43 |
+
ml-dtypes==0.5.1 ; python_version >= "3.11"
|
| 44 |
+
namex==0.1.0 ; python_version >= "3.11"
|
| 45 |
+
numpy==2.1.3 ; python_version >= "3.11"
|
| 46 |
+
nvidia-cublas-cu12==12.5.3.2 ; python_version >= "3.11"
|
| 47 |
+
nvidia-cuda-cupti-cu12==12.5.82 ; python_version >= "3.11"
|
| 48 |
+
nvidia-cuda-nvcc-cu12==12.5.82 ; python_version >= "3.11"
|
| 49 |
+
nvidia-cuda-nvrtc-cu12==12.5.82 ; python_version >= "3.11"
|
| 50 |
+
nvidia-cuda-runtime-cu12==12.5.82 ; python_version >= "3.11"
|
| 51 |
+
nvidia-cudnn-cu12==9.3.0.75 ; python_version >= "3.11"
|
| 52 |
+
nvidia-cufft-cu12==11.2.3.61 ; python_version >= "3.11"
|
| 53 |
+
nvidia-curand-cu12==10.3.6.82 ; python_version >= "3.11"
|
| 54 |
+
nvidia-cusolver-cu12==11.6.3.83 ; python_version >= "3.11"
|
| 55 |
+
nvidia-cusparse-cu12==12.5.1.3 ; python_version >= "3.11"
|
| 56 |
+
nvidia-nccl-cu12==2.23.4 ; python_version >= "3.11"
|
| 57 |
+
nvidia-nvjitlink-cu12==12.5.82 ; python_version >= "3.11"
|
| 58 |
+
opt-einsum==3.4.0 ; python_version >= "3.11"
|
| 59 |
+
optree==0.16.0 ; python_version >= "3.11"
|
| 60 |
+
orjson==3.10.18 ; python_version >= "3.11"
|
| 61 |
+
packaging==25.0 ; python_version >= "3.11"
|
| 62 |
+
pandas==2.3.0 ; python_version >= "3.11"
|
| 63 |
+
pillow==11.2.1 ; python_version >= "3.11"
|
| 64 |
+
protobuf==5.29.5 ; python_version >= "3.11"
|
| 65 |
+
pydantic-core==2.33.2 ; python_version >= "3.11"
|
| 66 |
+
pydantic==2.11.7 ; python_version >= "3.11"
|
| 67 |
+
pydub==0.25.1 ; python_version >= "3.11"
|
| 68 |
+
pygments==2.19.2 ; python_version >= "3.11"
|
| 69 |
+
pyparsing==3.2.3 ; python_version >= "3.11"
|
| 70 |
+
python-dateutil==2.9.0.post0 ; python_version >= "3.11"
|
| 71 |
+
python-multipart==0.0.20 ; python_version >= "3.11"
|
| 72 |
+
pytz==2025.2 ; python_version >= "3.11"
|
| 73 |
+
pyyaml==6.0.2 ; python_version >= "3.11"
|
| 74 |
+
requests==2.32.4 ; python_version >= "3.11"
|
| 75 |
+
rich==14.0.0 ; python_version >= "3.11"
|
| 76 |
+
ruff==0.12.0 ; python_version >= "3.11" and sys_platform != "emscripten"
|
| 77 |
+
safehttpx==0.1.6 ; python_version >= "3.11"
|
| 78 |
+
scikit-learn==1.7.0 ; python_version >= "3.11"
|
| 79 |
+
scipy==1.16.0 ; python_version >= "3.11"
|
| 80 |
+
semantic-version==2.10.0 ; python_version >= "3.11"
|
| 81 |
+
setuptools==80.9.0 ; python_version >= "3.11"
|
| 82 |
+
shellingham==1.5.4 ; python_version >= "3.11" and sys_platform != "emscripten"
|
| 83 |
+
six==1.17.0 ; python_version >= "3.11"
|
| 84 |
+
sniffio==1.3.1 ; python_version >= "3.11"
|
| 85 |
+
starlette==0.46.2 ; python_version >= "3.11"
|
| 86 |
+
tensorboard-data-server==0.7.2 ; python_version >= "3.11"
|
| 87 |
+
tensorboard==2.19.0 ; python_version >= "3.11"
|
| 88 |
+
tensorflow-io-gcs-filesystem==0.37.1 ; python_version == "3.11"
|
| 89 |
+
tensorflow==2.19.0 ; python_version >= "3.11"
|
| 90 |
+
termcolor==3.1.0 ; python_version >= "3.11"
|
| 91 |
+
threadpoolctl==3.6.0 ; python_version >= "3.11"
|
| 92 |
+
tomlkit==0.13.3 ; python_version >= "3.11"
|
| 93 |
+
tqdm==4.67.1 ; python_version >= "3.11"
|
| 94 |
+
typer==0.16.0 ; python_version >= "3.11" and sys_platform != "emscripten"
|
| 95 |
+
typing-extensions==4.14.0 ; python_version >= "3.11"
|
| 96 |
+
typing-inspection==0.4.1 ; python_version >= "3.11"
|
| 97 |
+
tzdata==2025.2 ; python_version >= "3.11"
|
| 98 |
+
urllib3==2.5.0 ; python_version >= "3.11"
|
| 99 |
+
uvicorn==0.34.3 ; python_version >= "3.11" and sys_platform != "emscripten"
|
| 100 |
+
websockets==15.0.1 ; python_version >= "3.11"
|
| 101 |
+
werkzeug==3.1.3 ; python_version >= "3.11"
|
| 102 |
+
wheel==0.45.1 ; python_version >= "3.11"
|
| 103 |
+
wrapt==1.17.2 ; python_version >= "3.11"
|
utils.py
CHANGED
|
@@ -51,133 +51,107 @@ def show_boxes(image : np.ndarray, damage_sites : dict, box_size = [250,250],
|
|
| 51 |
"""
|
| 52 |
|
| 53 |
_, ax = plt.subplots(1)
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
plt.yticks([])
|
| 58 |
|
| 59 |
for key, label in damage_sites.items():
|
| 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 |
-
ax.legend(handles=legend_elements,bbox_to_anchor=(1.04, 1), loc="upper left")
|
| 94 |
-
|
| 95 |
-
if save_image:
|
| 96 |
-
plt.savefig(image_path,dpi=1200,bbox_inches='tight' )
|
| 97 |
-
|
| 98 |
-
canvas = plt.gca().figure.canvas
|
| 99 |
canvas.draw()
|
| 100 |
-
data = np.frombuffer(canvas.tostring_rgb(), dtype=np.uint8)
|
| 101 |
-
image = data.reshape(canvas.get_width_height()[::-1] + (3,))
|
| 102 |
-
|
| 103 |
-
plt.show()
|
| 104 |
|
|
|
|
|
|
|
|
|
|
| 105 |
|
|
|
|
| 106 |
|
| 107 |
-
return
|
| 108 |
|
| 109 |
|
| 110 |
###
|
| 111 |
### cut out small images from panorama, append colour information
|
| 112 |
###
|
| 113 |
-
def prepare_classifier_input(panorama
|
| 114 |
-
"""Create a list of smaller images from the SEM panoramic image.
|
| 115 |
-
The neural networks expect images of a given size that are centered around a single damage site candiates.
|
| 116 |
-
For each centroid (from the clustering step before), we cut out a smaller image from the panorama of the size
|
| 117 |
-
expected by the classfier network.
|
| 118 |
-
Since the networks expect colour images, we repeat the gray-scale image 3 times for a given candiate site.
|
| 119 |
-
|
| 120 |
-
Args:
|
| 121 |
-
panorama (np.ndarray): SEM input image
|
| 122 |
-
centroids (list): list of centroids for the damage site candidates
|
| 123 |
-
window_size (list, optional): Size of the image expected by the neural network later. Defaults to [250,250].
|
| 124 |
-
|
| 125 |
-
Returns:
|
| 126 |
-
list: List of "colour" images cut out from the SEM panorama, one per damage site candidate
|
| 127 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
|
| 129 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
|
| 131 |
-
|
| 132 |
-
|
| 133 |
images = []
|
| 134 |
|
| 135 |
-
for
|
| 136 |
-
x1 = int(
|
| 137 |
-
y1 = int(
|
| 138 |
-
x2 =
|
| 139 |
-
y2 =
|
| 140 |
-
|
| 141 |
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
##
|
| 146 |
-
if x1<0:
|
| 147 |
-
x1 = 0
|
| 148 |
-
x2 = window_size[0]
|
| 149 |
-
if x2>= panorama_shape[0]:
|
| 150 |
-
x1 = panorama_shape[0] - window_size[0]
|
| 151 |
-
x2 = panorama_shape[0]
|
| 152 |
-
if y1<0:
|
| 153 |
-
y1 = 0
|
| 154 |
-
y2 = window_size[1]
|
| 155 |
-
if y2>= panorama_shape[1]:
|
| 156 |
-
y1 = panorama_shape[1] - window_size[1]
|
| 157 |
-
y2 = panorama_shape[1]
|
| 158 |
-
|
| 159 |
-
# we now need to create the image path from the panoramic image that corresponds to the
|
| 160 |
-
# centroid, with the size determined by the window_size.
|
| 161 |
-
# First, we create an empty container with np.zeros()
|
| 162 |
-
tmp_img = np.zeros((window_size[1], window_size[0],1), dtype=float)
|
| 163 |
-
|
| 164 |
-
# Then we copy over the patch of the panomaric image.
|
| 165 |
-
# The later classfier expects colour images, i.e. 3 colour channels for RGB
|
| 166 |
-
# Since we use gray-scale images, we only have one colour information, so we add the image to the first colour channel
|
| 167 |
-
tmp_img[:,:,0] = panorama[x1:x2,y1:y2,0]
|
| 168 |
-
|
| 169 |
-
# rescale the colour values
|
| 170 |
-
tmp_img = tmp_img*2./255. - 1.
|
| 171 |
-
|
| 172 |
-
# The classifier expects colour images, i.e. 3 colour channels.
|
| 173 |
-
# We "fake" this by repeating the same gray-scale information 3 times, once per colour channel
|
| 174 |
-
tmp_img_colour = np.repeat(tmp_img,3, axis=2) #3
|
| 175 |
-
|
| 176 |
-
images.append(tmp_img_colour)
|
| 177 |
-
|
| 178 |
|
| 179 |
-
|
|
|
|
|
|
|
| 180 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 181 |
|
| 182 |
|
| 183 |
|
|
|
|
| 51 |
"""
|
| 52 |
|
| 53 |
_, ax = plt.subplots(1)
|
| 54 |
+
ax.imshow(image, cmap='gray') # show image on correct axis
|
| 55 |
+
ax.set_xticks([])
|
| 56 |
+
ax.set_yticks([])
|
|
|
|
| 57 |
|
| 58 |
for key, label in damage_sites.items():
|
| 59 |
+
position = [key[0], key[1]]
|
| 60 |
+
edgecolor = {
|
| 61 |
+
'Inclusion': 'b',
|
| 62 |
+
'Interface': 'g',
|
| 63 |
+
'Martensite': 'r',
|
| 64 |
+
'Notch': 'y',
|
| 65 |
+
'Shadowing': 'm'
|
| 66 |
+
}.get(label, 'k') # default: black
|
| 67 |
+
|
| 68 |
+
rect = patches.Rectangle((position[1] - box_size[1] / 2., position[0] - box_size[0] / 2),
|
| 69 |
+
box_size[1], box_size[0],
|
| 70 |
+
linewidth=1, edgecolor=edgecolor, facecolor='none')
|
| 71 |
+
ax.add_patch(rect)
|
| 72 |
+
|
| 73 |
+
legend_elements = [
|
| 74 |
+
Line2D([0], [0], color='b', lw=4, label='Inclusion'),
|
| 75 |
+
Line2D([0], [0], color='g', lw=4, label='Interface'),
|
| 76 |
+
Line2D([0], [0], color='r', lw=4, label='Martensite'),
|
| 77 |
+
Line2D([0], [0], color='y', lw=4, label='Notch'),
|
| 78 |
+
Line2D([0], [0], color='m', lw=4, label='Shadow'),
|
| 79 |
+
Line2D([0], [0], color='k', lw=4, label='Not Classified')
|
| 80 |
+
]
|
| 81 |
+
ax.legend(handles=legend_elements, bbox_to_anchor=(1.04, 1), loc="upper left")
|
| 82 |
+
|
| 83 |
+
fig = ax.figure
|
| 84 |
+
fig.tight_layout(pad=0)
|
| 85 |
+
|
| 86 |
+
if save_image and image_path:
|
| 87 |
+
fig.savefig(image_path, dpi=1200, bbox_inches='tight')
|
| 88 |
+
|
| 89 |
+
canvas = fig.canvas
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
canvas.draw()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
+
data = np.frombuffer(canvas.buffer_rgba(), dtype=np.uint8).reshape(
|
| 93 |
+
canvas.get_width_height()[::-1] + (4,))
|
| 94 |
+
data = data[:, :, :3] # RGB only
|
| 95 |
|
| 96 |
+
plt.close(fig)
|
| 97 |
|
| 98 |
+
return data
|
| 99 |
|
| 100 |
|
| 101 |
###
|
| 102 |
### cut out small images from panorama, append colour information
|
| 103 |
###
|
| 104 |
+
def prepare_classifier_input(panorama: np.ndarray, centroids: list, window_size=[250, 250]) -> list:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
"""
|
| 106 |
+
Extracts square image patches centered at each given centroid from a grayscale panoramic SEM image.
|
| 107 |
+
|
| 108 |
+
Each extracted patch is resized to the specified window size and converted into a 3-channel (RGB-like)
|
| 109 |
+
normalized image suitable for use with classification neural networks that expect color input.
|
| 110 |
|
| 111 |
+
Parameters
|
| 112 |
+
----------
|
| 113 |
+
panorama : np.ndarray
|
| 114 |
+
Input SEM image. Should be a 2D array (H, W) or a 3D array (H, W, 1) representing grayscale data.
|
| 115 |
+
|
| 116 |
+
centroids : list of [int, int]
|
| 117 |
+
List of (y, x) coordinates marking the centers of regions of interest. These are typically damage sites
|
| 118 |
+
identified in preprocessing (e.g., clustering).
|
| 119 |
+
|
| 120 |
+
window_size : list of int, optional
|
| 121 |
+
Size [height, width] of each extracted image patch. Defaults to [250, 250].
|
| 122 |
+
|
| 123 |
+
Returns
|
| 124 |
+
-------
|
| 125 |
+
list of np.ndarray
|
| 126 |
+
List of extracted and normalized 3-channel image patches, each with shape (height, width, 3). Only
|
| 127 |
+
centroids that allow full window extraction within image bounds are used.
|
| 128 |
+
"""
|
| 129 |
+
if panorama.ndim == 2:
|
| 130 |
+
panorama = np.expand_dims(panorama, axis=-1) # (H, W, 1)
|
| 131 |
|
| 132 |
+
H, W, _ = panorama.shape
|
| 133 |
+
win_h, win_w = window_size
|
| 134 |
images = []
|
| 135 |
|
| 136 |
+
for (cy, cx) in centroids:
|
| 137 |
+
x1 = int(cx - win_w / 2)
|
| 138 |
+
y1 = int(cy - win_h / 2)
|
| 139 |
+
x2 = x1 + win_w
|
| 140 |
+
y2 = y1 + win_h
|
|
|
|
| 141 |
|
| 142 |
+
# Skip if patch would go out of bounds
|
| 143 |
+
if x1 < 0 or y1 < 0 or x2 > W or y2 > H:
|
| 144 |
+
continue
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
|
| 146 |
+
# Extract and normalize patch
|
| 147 |
+
patch = panorama[y1:y2, x1:x2, 0].astype(np.float32)
|
| 148 |
+
patch = patch * 2. / 255. - 1.
|
| 149 |
|
| 150 |
+
# Replicate grayscale channel to simulate RGB
|
| 151 |
+
patch_color = np.repeat(patch[:, :, np.newaxis], 3, axis=2)
|
| 152 |
+
images.append(patch_color)
|
| 153 |
+
|
| 154 |
+
return images
|
| 155 |
|
| 156 |
|
| 157 |
|