bhagwandas's picture
Create app.py
aae87aa verified
raw
history blame
2.14 kB
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):
# Convert image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Edge detection
edges = cv2.Canny(gray, 50, 150)
# Finding contours
contours, _ = cv2.findContours(edges, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# Calculate crack metrics
crack_lengths = [cv2.arcLength(cnt, True) for cnt in contours]
crack_widths = [cv2.boundingRect(cnt)[2] for cnt in contours]
return edges, crack_lengths, crack_widths
def main():
st.set_page_config(page_title='Structural Integrity Analyst', layout='wide')
st.title('πŸ—οΈ Structural Integrity Analyst')
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_lengths, crack_widths = analyze_crack(image)
st.subheader("Uploaded Image")
st.image(uploaded_file, caption="Uploaded Image", use_column_width=True)
# Display processed image
st.subheader("Processed Crack Detection")
fig, ax = plt.subplots()
ax.imshow(edges, cmap='gray')
ax.axis("off")
st.pyplot(fig)
# Data Analysis
data = pd.DataFrame({
"Crack Length (pixels)": crack_lengths,
"Crack Width (pixels)": crack_widths
})
st.subheader("Crack Metrics")
st.dataframe(data)
# Visualization
fig1 = px.histogram(data, x="Crack Length (pixels)", title="Crack Length Distribution", nbins=10)
fig2 = px.histogram(data, x="Crack Width (pixels)", title="Crack Width Distribution", nbins=10)
st.plotly_chart(fig1, use_container_width=True)
st.plotly_chart(fig2, use_container_width=True)
if __name__ == "__main__":
main()