Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import cv2
|
| 3 |
+
import numpy as np
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
+
import pandas as pd
|
| 6 |
+
import plotly.express as px
|
| 7 |
+
from PIL import Image
|
| 8 |
+
|
| 9 |
+
def analyze_crack(image):
|
| 10 |
+
# Convert image to grayscale
|
| 11 |
+
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 12 |
+
|
| 13 |
+
# Edge detection
|
| 14 |
+
edges = cv2.Canny(gray, 50, 150)
|
| 15 |
+
|
| 16 |
+
# Finding contours
|
| 17 |
+
contours, _ = cv2.findContours(edges, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
|
| 18 |
+
|
| 19 |
+
# Calculate crack metrics
|
| 20 |
+
crack_lengths = [cv2.arcLength(cnt, True) for cnt in contours]
|
| 21 |
+
crack_widths = [cv2.boundingRect(cnt)[2] for cnt in contours]
|
| 22 |
+
|
| 23 |
+
return edges, crack_lengths, crack_widths
|
| 24 |
+
|
| 25 |
+
def main():
|
| 26 |
+
st.set_page_config(page_title='Structural Integrity Analyst', layout='wide')
|
| 27 |
+
st.title('🏗️ Structural Integrity Analyst')
|
| 28 |
+
|
| 29 |
+
st.sidebar.header("Upload Crack Image")
|
| 30 |
+
uploaded_file = st.sidebar.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
| 31 |
+
|
| 32 |
+
if uploaded_file is not None:
|
| 33 |
+
image = Image.open(uploaded_file)
|
| 34 |
+
image = np.array(image)
|
| 35 |
+
|
| 36 |
+
edges, crack_lengths, crack_widths = analyze_crack(image)
|
| 37 |
+
|
| 38 |
+
st.subheader("Uploaded Image")
|
| 39 |
+
st.image(uploaded_file, caption="Uploaded Image", use_column_width=True)
|
| 40 |
+
|
| 41 |
+
# Display processed image
|
| 42 |
+
st.subheader("Processed Crack Detection")
|
| 43 |
+
fig, ax = plt.subplots()
|
| 44 |
+
ax.imshow(edges, cmap='gray')
|
| 45 |
+
ax.axis("off")
|
| 46 |
+
st.pyplot(fig)
|
| 47 |
+
|
| 48 |
+
# Data Analysis
|
| 49 |
+
data = pd.DataFrame({
|
| 50 |
+
"Crack Length (pixels)": crack_lengths,
|
| 51 |
+
"Crack Width (pixels)": crack_widths
|
| 52 |
+
})
|
| 53 |
+
|
| 54 |
+
st.subheader("Crack Metrics")
|
| 55 |
+
st.dataframe(data)
|
| 56 |
+
|
| 57 |
+
# Visualization
|
| 58 |
+
fig1 = px.histogram(data, x="Crack Length (pixels)", title="Crack Length Distribution", nbins=10)
|
| 59 |
+
fig2 = px.histogram(data, x="Crack Width (pixels)", title="Crack Width Distribution", nbins=10)
|
| 60 |
+
|
| 61 |
+
st.plotly_chart(fig1, use_container_width=True)
|
| 62 |
+
st.plotly_chart(fig2, use_container_width=True)
|
| 63 |
+
|
| 64 |
+
if __name__ == "__main__":
|
| 65 |
+
main()
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
|