|
import streamlit as st |
|
import cv2 |
|
import numpy as np |
|
import matplotlib.pyplot as plt |
|
import pandas as pd |
|
import plotly.express as px |
|
from PIL import Image |
|
|
|
def analyze_crack(image): |
|
|
|
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) |
|
|
|
|
|
edges = cv2.Canny(gray, 50, 150) |
|
|
|
|
|
contours, _ = cv2.findContours(edges, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) |
|
|
|
|
|
crack_data = [] |
|
for cnt in contours: |
|
length = cv2.arcLength(cnt, True) |
|
x, y, w, h = cv2.boundingRect(cnt) |
|
width = w |
|
severity = classify_crack(length, width) |
|
crack_data.append({"Length": length, "Width": width, "Severity": severity, "X": x, "Y": y}) |
|
|
|
return edges, crack_data |
|
|
|
def classify_crack(length, width): |
|
if length > 150 or width > 20: |
|
return "π΄ Major" |
|
elif length > 80 or width > 10: |
|
return "π Moderate" |
|
else: |
|
return "π’ Minor" |
|
|
|
def generate_description(severity): |
|
if "Major" in severity: |
|
return "π¨ This crack is classified as **Major**, indicating significant structural distress. Immediate intervention is required." |
|
elif "Moderate" in severity: |
|
return "β οΈ This crack is classified as **Moderate**. Monitoring and remedial measures should be considered." |
|
else: |
|
return "β
This crack is **Minor** and likely due to surface shrinkage or thermal expansion. Periodic monitoring is recommended." |
|
|
|
def determine_structure_safety(crack_data): |
|
if not crack_data: |
|
return "β
Structure is Safe", "green" |
|
|
|
major_count = sum(1 for crack in crack_data if "Major" in crack["Severity"]) |
|
moderate_count = sum(1 for crack in crack_data if "Moderate" in crack["Severity"]) |
|
|
|
if major_count > 0 or moderate_count > 3: |
|
return "π¨ Structure is NOT Safe", "red" |
|
|
|
return "β οΈ Structure Needs Monitoring", "orange" |
|
|
|
def main(): |
|
st.set_page_config(page_title='ποΈ Structural Integrity Analyst', layout='wide', initial_sidebar_state='expanded') |
|
|
|
st.markdown("<h1 style='text-align: center; color: #003366;'>ποΈ Structural Integrity Analyst</h1>", unsafe_allow_html=True) |
|
|
|
st.sidebar.header("π Upload Crack Image") |
|
uploaded_file = st.sidebar.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"]) |
|
|
|
if uploaded_file is not None: |
|
image = Image.open(uploaded_file) |
|
image = np.array(image) |
|
|
|
edges, crack_data = analyze_crack(image) |
|
|
|
|
|
structure_status, status_color = determine_structure_safety(crack_data) |
|
st.markdown(f"<h2 style='text-align: center; color: {status_color};'>{structure_status}</h2>", unsafe_allow_html=True) |
|
|
|
|
|
col1, col2 = st.columns(2) |
|
|
|
with col1: |
|
st.subheader("πΈ Uploaded Image") |
|
st.image(uploaded_file, caption="Uploaded Image", use_column_width=True) |
|
|
|
with col2: |
|
st.subheader("π Processed Crack Detection") |
|
fig, ax = plt.subplots() |
|
ax.imshow(edges, cmap='gray') |
|
ax.axis("off") |
|
st.pyplot(fig) |
|
|
|
|
|
data = pd.DataFrame(crack_data) |
|
|
|
if not data.empty: |
|
st.subheader("π Crack Metrics & Classification") |
|
st.dataframe(data.style.applymap(lambda val: 'background-color: #FFDDC1' if 'Major' in str(val) else ('background-color: #FFF3CD' if 'Moderate' in str(val) else 'background-color: #D4EDDA'))) |
|
|
|
|
|
st.subheader("π Crack Analysis & Recommendations") |
|
for _, row in data.iterrows(): |
|
st.markdown(f"**Crack at (X: {row['X']}, Y: {row['Y']})** - {generate_description(row['Severity'])}") |
|
|
|
|
|
fig1 = px.histogram(data, x="Length", color="Severity", title="π Crack Length Distribution", nbins=10, color_discrete_map={"π΄ Major": "red", "π Moderate": "orange", "π’ Minor": "green"}) |
|
fig2 = px.histogram(data, x="Width", color="Severity", title="π Crack Width Distribution", nbins=10, color_discrete_map={"π΄ Major": "red", "π Moderate": "orange", "π’ Minor": "green"}) |
|
|
|
st.plotly_chart(fig1, use_container_width=True) |
|
st.plotly_chart(fig2, use_container_width=True) |
|
|
|
if __name__ == "__main__": |
|
main() |
|
|