Update app.py
Browse files
app.py
CHANGED
|
@@ -1,46 +1,71 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
from transformers import
|
| 3 |
from PIL import Image
|
| 4 |
import torch
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
}
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
def main():
|
| 23 |
st.title("Structural Damage Assessment")
|
| 24 |
|
|
|
|
|
|
|
| 25 |
uploaded_file = st.file_uploader("Upload structural image", type=['jpg', 'jpeg', 'png'])
|
| 26 |
|
| 27 |
if uploaded_file:
|
| 28 |
image = Image.open(uploaded_file)
|
| 29 |
-
st.image(image, caption="
|
| 30 |
|
| 31 |
-
with st.spinner("Analyzing..."):
|
| 32 |
-
|
| 33 |
|
| 34 |
-
for
|
| 35 |
-
confidence =
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
| 44 |
|
| 45 |
if __name__ == "__main__":
|
| 46 |
main()
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from transformers import ViTForImageClassification, ViTImageProcessor
|
| 3 |
from PIL import Image
|
| 4 |
import torch
|
| 5 |
|
| 6 |
+
# Custom labels for structural damage
|
| 7 |
+
DAMAGE_TYPES = {
|
| 8 |
+
'spalling': {
|
| 9 |
+
'remedies': ['Remove loose concrete', 'Clean exposed reinforcement', 'Apply repair mortar'],
|
| 10 |
+
'risk_level': 'High'
|
| 11 |
+
},
|
| 12 |
+
'reinforcement_corrosion': {
|
| 13 |
+
'remedies': ['Remove rust', 'Apply corrosion inhibitor', 'Repair concrete cover'],
|
| 14 |
+
'risk_level': 'Critical'
|
| 15 |
+
},
|
| 16 |
+
'structural_crack': {
|
| 17 |
+
'remedies': ['Monitor crack width', 'Epoxy injection', 'Structural reinforcement'],
|
| 18 |
+
'risk_level': 'Critical'
|
| 19 |
+
},
|
| 20 |
+
'dampness': {
|
| 21 |
+
'remedies': ['Identify water source', 'Fix waterproofing', 'Improve drainage'],
|
| 22 |
+
'risk_level': 'Medium'
|
| 23 |
+
}
|
| 24 |
}
|
| 25 |
|
| 26 |
+
@st.cache_resource
|
| 27 |
+
def load_model():
|
| 28 |
+
model = ViTForImageClassification.from_pretrained(
|
| 29 |
+
"nateraw/vit-base-patch16-224-finetuned-structural-damage",
|
| 30 |
+
num_labels=len(DAMAGE_TYPES),
|
| 31 |
+
id2label={i: label for i, label in enumerate(DAMAGE_TYPES.keys())},
|
| 32 |
+
label2id={label: i for i, label in enumerate(DAMAGE_TYPES.keys())}
|
| 33 |
+
)
|
| 34 |
+
processor = ViTImageProcessor.from_pretrained("google/vit-base-patch16-224")
|
| 35 |
+
return model, processor
|
| 36 |
+
|
| 37 |
+
def analyze_damage(image, model, processor):
|
| 38 |
+
inputs = processor(images=image, return_tensors="pt")
|
| 39 |
+
outputs = model(**inputs)
|
| 40 |
+
probs = torch.nn.functional.softmax(outputs.logits, dim=-1)
|
| 41 |
+
return probs[0]
|
| 42 |
|
| 43 |
def main():
|
| 44 |
st.title("Structural Damage Assessment")
|
| 45 |
|
| 46 |
+
model, processor = load_model()
|
| 47 |
+
|
| 48 |
uploaded_file = st.file_uploader("Upload structural image", type=['jpg', 'jpeg', 'png'])
|
| 49 |
|
| 50 |
if uploaded_file:
|
| 51 |
image = Image.open(uploaded_file)
|
| 52 |
+
st.image(image, caption="Analyzed Structure")
|
| 53 |
|
| 54 |
+
with st.spinner("Analyzing structural damage..."):
|
| 55 |
+
predictions = analyze_damage(image, model, processor)
|
| 56 |
|
| 57 |
+
for idx, (damage_type, conf) in enumerate(zip(DAMAGE_TYPES.keys(), predictions)):
|
| 58 |
+
confidence = float(conf) * 100
|
| 59 |
+
if confidence > 10: # Show only significant predictions
|
| 60 |
+
st.subheader(f"{damage_type.replace('_', ' ').title()}")
|
| 61 |
+
st.progress(confidence / 100)
|
| 62 |
+
st.write(f"Confidence: {confidence:.1f}%")
|
| 63 |
+
st.write(f"Risk Level: {DAMAGE_TYPES[damage_type]['risk_level']}")
|
| 64 |
+
|
| 65 |
+
st.write("Recommended Actions:")
|
| 66 |
+
for remedy in DAMAGE_TYPES[damage_type]['remedies']:
|
| 67 |
+
st.write(f"• {remedy}")
|
| 68 |
+
st.divider()
|
| 69 |
|
| 70 |
if __name__ == "__main__":
|
| 71 |
main()
|