another verssion from gemini
Browse files
app.py
CHANGED
|
@@ -3,11 +3,43 @@ import numpy as np
|
|
| 3 |
import pandas as pd
|
| 4 |
from PIL import Image
|
| 5 |
import logging
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
# Your helper imports and tensorflow models are assumed to be in the same directory.
|
| 8 |
-
# Ensure 'clustering.py' and 'utils.py' are present in your HuggingFace Space.
|
| 9 |
-
import clustering
|
| 10 |
-
import utils
|
| 11 |
from tensorflow import keras
|
| 12 |
|
| 13 |
# --- Basic Setup ---
|
|
@@ -18,20 +50,32 @@ IMAGE_PATH = "classified_damage_sites.png"
|
|
| 18 |
CSV_PATH = "classified_damage_sites.csv"
|
| 19 |
|
| 20 |
# Load models once at startup to improve performance
|
|
|
|
|
|
|
|
|
|
| 21 |
try:
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
except Exception as e:
|
| 25 |
logging.error(f"Error loading models: {e}")
|
| 26 |
-
#
|
| 27 |
-
#
|
| 28 |
-
raise
|
| 29 |
|
| 30 |
damage_classes = {3: "Martensite", 2: "Interface", 0: "Notch", 1: "Shadowing"}
|
| 31 |
model1_windowsize = [250, 250]
|
| 32 |
model2_windowsize = [100, 100]
|
| 33 |
|
| 34 |
-
|
| 35 |
# --- Core Processing Function (Your original logic) ---
|
| 36 |
def damage_classification(SEM_image, image_threshold, model1_threshold, model2_threshold):
|
| 37 |
"""
|
|
@@ -39,41 +83,54 @@ def damage_classification(SEM_image, image_threshold, model1_threshold, model2_t
|
|
| 39 |
It returns the classified image and paths to the output files.
|
| 40 |
"""
|
| 41 |
if SEM_image is None:
|
| 42 |
-
# This error will be displayed nicely in the Gradio interface
|
| 43 |
raise gr.Error("Please upload an SEM Image before running classification.")
|
| 44 |
|
|
|
|
|
|
|
|
|
|
| 45 |
damage_sites = {}
|
|
|
|
| 46 |
# Step 1: Clustering to find damage centroids
|
|
|
|
| 47 |
all_centroids = clustering.get_centroids(
|
| 48 |
SEM_image,
|
| 49 |
image_threshold=image_threshold,
|
| 50 |
fill_holes=True,
|
| 51 |
filter_close_centroids=True,
|
| 52 |
)
|
|
|
|
| 53 |
for c in all_centroids:
|
| 54 |
damage_sites[(c[0], c[1])] = "Not Classified"
|
| 55 |
|
| 56 |
# Step 2: Model 1 to identify inclusions
|
| 57 |
if len(all_centroids) > 0:
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
# Step 3: Model 2 to classify remaining damage types
|
| 66 |
centroids_model2 = [list(k) for k, v in damage_sites.items() if v == "Not Classified"]
|
| 67 |
if centroids_model2:
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
# Step 4: Draw boxes on image and save output image
|
| 79 |
# The utils.show_boxes function is assumed to return a PIL Image object
|
|
@@ -84,8 +141,11 @@ def damage_classification(SEM_image, image_threshold, model1_threshold, model2_t
|
|
| 84 |
df = pd.DataFrame(data, columns=["x", "y", "damage_type"])
|
| 85 |
df.to_csv(CSV_PATH, index=False)
|
| 86 |
|
| 87 |
-
|
|
|
|
|
|
|
| 88 |
|
|
|
|
| 89 |
|
| 90 |
# --- Gradio Interface Definition ---
|
| 91 |
with gr.Blocks() as app:
|
|
@@ -99,28 +159,39 @@ with gr.Blocks() as app:
|
|
| 99 |
model1_threshold_input = gr.Number(value=0.7, label="Inclusion Model Certainty (0-1)")
|
| 100 |
model2_threshold_input = gr.Number(value=0.5, label="Damage Model Certainty (0-1)")
|
| 101 |
classify_btn = gr.Button("Run Classification", variant="primary")
|
| 102 |
-
|
| 103 |
with gr.Column(scale=2):
|
| 104 |
output_image = gr.Image(label="Classified Image")
|
| 105 |
# Initialize DownloadButtons as hidden. They will become visible after a successful run.
|
| 106 |
-
|
| 107 |
-
|
|
|
|
| 108 |
|
| 109 |
# This wrapper function handles the UI updates, which is the robust way to use Gradio.
|
| 110 |
def run_classification_and_update_ui(sem_image, cluster_thresh, m1_thresh, m2_thresh):
|
| 111 |
"""
|
| 112 |
Calls the core logic and then returns updates for the Gradio UI components.
|
| 113 |
"""
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
|
| 125 |
# Connect the button's click event to the wrapper function
|
| 126 |
classify_btn.click(
|
|
@@ -139,4 +210,4 @@ with gr.Blocks() as app:
|
|
| 139 |
)
|
| 140 |
|
| 141 |
if __name__ == "__main__":
|
| 142 |
-
app.launch()
|
|
|
|
| 3 |
import pandas as pd
|
| 4 |
from PIL import Image
|
| 5 |
import logging
|
| 6 |
+
import os # Import os for path checks
|
| 7 |
+
|
| 8 |
+
# Placeholder imports for clustering and utils.
|
| 9 |
+
# In a real scenario, these files (clustering.py, utils.py)
|
| 10 |
+
# would contain your actual implementation.
|
| 11 |
+
try:
|
| 12 |
+
import clustering
|
| 13 |
+
import utils
|
| 14 |
+
except ImportError as e:
|
| 15 |
+
logging.error(f"Error importing helper modules: {e}. Using dummy functions.")
|
| 16 |
+
# Define dummy functions if imports fail, to allow the app to launch.
|
| 17 |
+
class DummyClustering:
|
| 18 |
+
def get_centroids(self, *args, **kwargs):
|
| 19 |
+
logging.warning("Using dummy get_centroids. Provide actual clustering.py.")
|
| 20 |
+
# Return some dummy centroids for demonstration
|
| 21 |
+
# In a real scenario, you might want to raise an error or return an empty list
|
| 22 |
+
# if clustering is critical for app functionality.
|
| 23 |
+
return [(100, 100), (200, 200)]
|
| 24 |
+
|
| 25 |
+
class DummyUtils:
|
| 26 |
+
def prepare_classifier_input(self, *args, **kwargs):
|
| 27 |
+
logging.warning("Using dummy prepare_classifier_input. Provide actual utils.py.")
|
| 28 |
+
# Return dummy data for model input
|
| 29 |
+
return np.zeros((1, 250, 250, 3)) # Example shape, adjust as per your model input
|
| 30 |
+
|
| 31 |
+
def show_boxes(self, image, damage_sites, save_image=False, image_path=None):
|
| 32 |
+
logging.warning("Using dummy show_boxes. Provide actual utils.py.")
|
| 33 |
+
# Return the original image for dummy display
|
| 34 |
+
# In a real app, this would draw boxes
|
| 35 |
+
if image is None:
|
| 36 |
+
return Image.new('RGB', (400, 400), color = 'red') # Placeholder if no image provided
|
| 37 |
+
return image
|
| 38 |
+
|
| 39 |
+
clustering = DummyClustering()
|
| 40 |
+
utils = DummyUtils()
|
| 41 |
+
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
from tensorflow import keras
|
| 44 |
|
| 45 |
# --- Basic Setup ---
|
|
|
|
| 50 |
CSV_PATH = "classified_damage_sites.csv"
|
| 51 |
|
| 52 |
# Load models once at startup to improve performance
|
| 53 |
+
model1 = None
|
| 54 |
+
model2 = None
|
| 55 |
+
|
| 56 |
try:
|
| 57 |
+
# Check if model files exist before attempting to load
|
| 58 |
+
if os.path.exists('rwthmaterials_dp800_network1_inclusion.h5'):
|
| 59 |
+
model1 = keras.models.load_model('rwthmaterials_dp800_network1_inclusion.h5')
|
| 60 |
+
logging.info("Model 1 loaded successfully.")
|
| 61 |
+
else:
|
| 62 |
+
logging.warning("Model 1 (rwthmaterials_dp800_network1_inclusion.h5) not found. Classification results may be inaccurate.")
|
| 63 |
+
|
| 64 |
+
if os.path.exists('rwthmaterials_dp800_network2_damage.h5'):
|
| 65 |
+
model2 = keras.models.load_model('rwthmaterials_dp800_network2_damage.h5')
|
| 66 |
+
logging.info("Model 2 loaded successfully.")
|
| 67 |
+
else:
|
| 68 |
+
logging.warning("Model 2 (rwthmaterials_dp800_network2_damage.h5) not found. Classification results may be inaccurate.")
|
| 69 |
+
|
| 70 |
except Exception as e:
|
| 71 |
logging.error(f"Error loading models: {e}")
|
| 72 |
+
# Models are set to None, and warnings/errors are logged.
|
| 73 |
+
# The app will still attempt to launch.
|
|
|
|
| 74 |
|
| 75 |
damage_classes = {3: "Martensite", 2: "Interface", 0: "Notch", 1: "Shadowing"}
|
| 76 |
model1_windowsize = [250, 250]
|
| 77 |
model2_windowsize = [100, 100]
|
| 78 |
|
|
|
|
| 79 |
# --- Core Processing Function (Your original logic) ---
|
| 80 |
def damage_classification(SEM_image, image_threshold, model1_threshold, model2_threshold):
|
| 81 |
"""
|
|
|
|
| 83 |
It returns the classified image and paths to the output files.
|
| 84 |
"""
|
| 85 |
if SEM_image is None:
|
|
|
|
| 86 |
raise gr.Error("Please upload an SEM Image before running classification.")
|
| 87 |
|
| 88 |
+
if model1 is None or model2 is None:
|
| 89 |
+
raise gr.Error("Models not loaded. Please ensure model files are present and valid.")
|
| 90 |
+
|
| 91 |
damage_sites = {}
|
| 92 |
+
|
| 93 |
# Step 1: Clustering to find damage centroids
|
| 94 |
+
# Ensure clustering.get_centroids handles the case of no centroids found
|
| 95 |
all_centroids = clustering.get_centroids(
|
| 96 |
SEM_image,
|
| 97 |
image_threshold=image_threshold,
|
| 98 |
fill_holes=True,
|
| 99 |
filter_close_centroids=True,
|
| 100 |
)
|
| 101 |
+
|
| 102 |
for c in all_centroids:
|
| 103 |
damage_sites[(c[0], c[1])] = "Not Classified"
|
| 104 |
|
| 105 |
# Step 2: Model 1 to identify inclusions
|
| 106 |
if len(all_centroids) > 0:
|
| 107 |
+
try:
|
| 108 |
+
images_model1 = utils.prepare_classifier_input(SEM_image, all_centroids, window_size=model1_windowsize)
|
| 109 |
+
y1_pred = model1.predict(np.asarray(images_model1, dtype=float))
|
| 110 |
+
inclusions = np.where(y1_pred[:, 0] > model1_threshold)[0]
|
| 111 |
+
for idx in inclusions:
|
| 112 |
+
coord = all_centroids[idx]
|
| 113 |
+
damage_sites[(coord[0], coord[1])] = "Inclusion"
|
| 114 |
+
except Exception as e:
|
| 115 |
+
logging.error(f"Error during Model 1 prediction: {e}")
|
| 116 |
|
| 117 |
# Step 3: Model 2 to classify remaining damage types
|
| 118 |
centroids_model2 = [list(k) for k, v in damage_sites.items() if v == "Not Classified"]
|
| 119 |
if centroids_model2:
|
| 120 |
+
try:
|
| 121 |
+
images_model2 = utils.prepare_classifier_input(SEM_image, centroids_model2, window_size=model2_windowsize)
|
| 122 |
+
y2_pred = model2.predict(np.asarray(images_model2, dtype=float))
|
| 123 |
+
# Adjust the thresholding for damage_index to handle potential empty results
|
| 124 |
+
damage_index = np.asarray(y2_pred > model2_threshold).nonzero()
|
| 125 |
+
|
| 126 |
+
for i in range(len(damage_index[0])):
|
| 127 |
+
sample_idx = damage_index[0][i]
|
| 128 |
+
class_idx = damage_index[1][i]
|
| 129 |
+
label = damage_classes.get(class_idx, "Unknown")
|
| 130 |
+
coord = centroids_model2[sample_idx]
|
| 131 |
+
damage_sites[(coord[0], coord[1])] = label
|
| 132 |
+
except Exception as e:
|
| 133 |
+
logging.error(f"Error during Model 2 prediction: {e}")
|
| 134 |
|
| 135 |
# Step 4: Draw boxes on image and save output image
|
| 136 |
# The utils.show_boxes function is assumed to return a PIL Image object
|
|
|
|
| 141 |
df = pd.DataFrame(data, columns=["x", "y", "damage_type"])
|
| 142 |
df.to_csv(CSV_PATH, index=False)
|
| 143 |
|
| 144 |
+
# Log file paths to ensure they are correct
|
| 145 |
+
logging.info(f"Generated Image Path: {IMAGE_PATH}")
|
| 146 |
+
logging.info(f"Generated CSV Path: {CSV_PATH}")
|
| 147 |
|
| 148 |
+
return image_with_boxes, IMAGE_PATH, CSV_PATH
|
| 149 |
|
| 150 |
# --- Gradio Interface Definition ---
|
| 151 |
with gr.Blocks() as app:
|
|
|
|
| 159 |
model1_threshold_input = gr.Number(value=0.7, label="Inclusion Model Certainty (0-1)")
|
| 160 |
model2_threshold_input = gr.Number(value=0.5, label="Damage Model Certainty (0-1)")
|
| 161 |
classify_btn = gr.Button("Run Classification", variant="primary")
|
|
|
|
| 162 |
with gr.Column(scale=2):
|
| 163 |
output_image = gr.Image(label="Classified Image")
|
| 164 |
# Initialize DownloadButtons as hidden. They will become visible after a successful run.
|
| 165 |
+
# Explicitly setting value=None to be safe, though visible=False should imply it.
|
| 166 |
+
download_image_btn = gr.DownloadButton(label="Download Image", value=None, visible=False)
|
| 167 |
+
download_csv_btn = gr.DownloadButton(label="Download CSV", value=None, visible=False)
|
| 168 |
|
| 169 |
# This wrapper function handles the UI updates, which is the robust way to use Gradio.
|
| 170 |
def run_classification_and_update_ui(sem_image, cluster_thresh, m1_thresh, m2_thresh):
|
| 171 |
"""
|
| 172 |
Calls the core logic and then returns updates for the Gradio UI components.
|
| 173 |
"""
|
| 174 |
+
try:
|
| 175 |
+
# Call the main processing function
|
| 176 |
+
classified_img, img_path, csv_path = damage_classification(sem_image, cluster_thresh, m1_thresh, m2_thresh)
|
| 177 |
+
|
| 178 |
+
# Return the results in the correct order to update the output components.
|
| 179 |
+
# Use gr.update to change properties of a component, like visibility and value.
|
| 180 |
+
return (
|
| 181 |
+
classified_img,
|
| 182 |
+
gr.update(value=img_path, visible=True),
|
| 183 |
+
gr.update(value=csv_path, visible=True)
|
| 184 |
+
)
|
| 185 |
+
except Exception as e:
|
| 186 |
+
# Catch any error during classification and display it gracefully
|
| 187 |
+
logging.error(f"Error during classification: {e}")
|
| 188 |
+
gr.Warning(f"An error occurred: {e}")
|
| 189 |
+
# Keep download buttons hidden on error and clear image
|
| 190 |
+
return (
|
| 191 |
+
None, # Clear the image on error
|
| 192 |
+
gr.update(visible=False),
|
| 193 |
+
gr.update(visible=False)
|
| 194 |
+
)
|
| 195 |
|
| 196 |
# Connect the button's click event to the wrapper function
|
| 197 |
classify_btn.click(
|
|
|
|
| 210 |
)
|
| 211 |
|
| 212 |
if __name__ == "__main__":
|
| 213 |
+
app.launch()
|