| import streamlit as st
|
| import cv2
|
| import numpy as np
|
| from tensorflow.keras.models import load_model
|
| from PIL import Image
|
| import matplotlib.pyplot as plt
|
|
|
|
|
| @st.cache_resource
|
| def load_unet_model():
|
| model = load_model('unet_crack_detection.h5')
|
| return model
|
|
|
|
|
| def process_and_predict(image, model):
|
|
|
| img_gray = np.array(image.convert("L"))
|
|
|
|
|
| target_size = (256, 256)
|
| img_resized = cv2.resize(img_gray, target_size)
|
|
|
|
|
| img_resized = img_resized / 255.0
|
| img_resized = np.expand_dims(img_resized, axis=-1)
|
| img_resized = np.expand_dims(img_resized, axis=0)
|
|
|
|
|
| predicted_mask = model.predict(img_resized)
|
| predicted_mask = predicted_mask[0, :, :, 0]
|
|
|
|
|
| img_original_resized = cv2.resize(np.array(image), target_size)
|
| overlay = img_original_resized.copy()
|
| overlay[predicted_mask > 0.5] = [255, 0, 255]
|
|
|
|
|
| alpha = 1
|
| superimposed_img = cv2.addWeighted(img_original_resized, 1 - alpha, overlay, alpha, 0)
|
|
|
| return img_original_resized, predicted_mask, superimposed_img
|
|
|
|
|
| st.title("Concrete Crack Detection App")
|
| st.write("Upload an image, and the app will use a CNN model to predict and overlay the segmentation mask.")
|
|
|
|
|
| model = load_unet_model()
|
|
|
|
|
| uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
|
|
| if uploaded_file is not None:
|
|
|
| image = Image.open(uploaded_file)
|
|
|
|
|
| st.subheader("Original Image")
|
| st.image(image)
|
|
|
|
|
| img_original, predicted_mask, superimposed_img = process_and_predict(image, model)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| st.subheader("Superimposed Image with Mask")
|
| superimposed_img_rgb = cv2.cvtColor(superimposed_img, cv2.COLOR_BGR2RGB)
|
| st.image(superimposed_img_rgb)
|
|
|