Update app.py
Browse files
app.py
CHANGED
@@ -2,8 +2,8 @@ import streamlit as st
|
|
2 |
from transformers import ViTForImageClassification, ViTImageProcessor
|
3 |
from PIL import Image
|
4 |
import torch
|
5 |
-
import numpy as np
|
6 |
|
|
|
7 |
DAMAGE_TYPES = {
|
8 |
0: {'name': 'spalling', 'risk': 'High'},
|
9 |
1: {'name': 'reinforcement_corrosion', 'risk': 'Critical'},
|
@@ -13,30 +13,11 @@ DAMAGE_TYPES = {
|
|
13 |
}
|
14 |
|
15 |
REMEDIES = {
|
16 |
-
'spalling': [
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
]
|
21 |
-
'reinforcement_corrosion': [
|
22 |
-
'Remove rust',
|
23 |
-
'Apply corrosion inhibitor',
|
24 |
-
'Repair concrete cover'
|
25 |
-
],
|
26 |
-
'structural_crack': [
|
27 |
-
'Measure crack width',
|
28 |
-
'Epoxy injection',
|
29 |
-
'Monitor progression'
|
30 |
-
],
|
31 |
-
'dampness': [
|
32 |
-
'Identify water source',
|
33 |
-
'Improve drainage',
|
34 |
-
'Apply waterproofing'
|
35 |
-
],
|
36 |
-
'no_damage': [
|
37 |
-
'Regular maintenance',
|
38 |
-
'Periodic inspection'
|
39 |
-
]
|
40 |
}
|
41 |
|
42 |
@st.cache_resource
|
@@ -44,34 +25,30 @@ def load_model():
|
|
44 |
model = ViTForImageClassification.from_pretrained(
|
45 |
"google/vit-base-patch16-224",
|
46 |
num_labels=len(DAMAGE_TYPES),
|
|
|
47 |
)
|
48 |
processor = ViTImageProcessor.from_pretrained("google/vit-base-patch16-224")
|
49 |
return model, processor
|
50 |
|
51 |
-
def
|
52 |
image = image.convert('RGB')
|
53 |
inputs = processor(images=image, return_tensors="pt")
|
54 |
-
return inputs
|
55 |
-
|
56 |
-
def analyze_damage(image, model, processor):
|
57 |
-
inputs = process_image(image, processor)
|
58 |
outputs = model(**inputs)
|
59 |
probs = torch.nn.functional.softmax(outputs.logits, dim=1)[0]
|
60 |
return probs
|
61 |
|
62 |
def main():
|
63 |
st.title("Structural Damage Assessment Tool")
|
64 |
-
st.write("Upload an image of building structure for damage analysis")
|
65 |
|
66 |
model, processor = load_model()
|
67 |
|
68 |
-
uploaded_file = st.file_uploader("
|
69 |
|
70 |
if uploaded_file:
|
71 |
image = Image.open(uploaded_file)
|
72 |
st.image(image, caption="Uploaded Structure", use_column_width=True)
|
73 |
|
74 |
-
with st.spinner("Analyzing
|
75 |
predictions = analyze_damage(image, model, processor)
|
76 |
|
77 |
col1, col2 = st.columns(2)
|
@@ -80,21 +57,18 @@ def main():
|
|
80 |
st.subheader("Damage Assessment")
|
81 |
for idx, prob in enumerate(predictions):
|
82 |
damage_type = DAMAGE_TYPES[idx]['name']
|
83 |
-
risk_level = DAMAGE_TYPES[idx]['risk']
|
84 |
confidence = float(prob) * 100
|
85 |
-
|
86 |
if confidence > 15:
|
87 |
st.write(f"**{damage_type.replace('_', ' ').title()}**")
|
88 |
st.progress(confidence / 100)
|
89 |
st.write(f"Confidence: {confidence:.1f}%")
|
90 |
-
st.write(f"Risk Level: {
|
91 |
|
92 |
with col2:
|
93 |
st.subheader("Recommended Actions")
|
94 |
for idx, prob in enumerate(predictions):
|
95 |
damage_type = DAMAGE_TYPES[idx]['name']
|
96 |
confidence = float(prob) * 100
|
97 |
-
|
98 |
if confidence > 15:
|
99 |
st.write(f"**For {damage_type.replace('_', ' ').title()}:**")
|
100 |
for remedy in REMEDIES[damage_type]:
|
|
|
2 |
from transformers import ViTForImageClassification, ViTImageProcessor
|
3 |
from PIL import Image
|
4 |
import torch
|
|
|
5 |
|
6 |
+
# Define damage types and remedies
|
7 |
DAMAGE_TYPES = {
|
8 |
0: {'name': 'spalling', 'risk': 'High'},
|
9 |
1: {'name': 'reinforcement_corrosion', 'risk': 'Critical'},
|
|
|
13 |
}
|
14 |
|
15 |
REMEDIES = {
|
16 |
+
'spalling': ['Remove loose concrete', 'Clean exposed area', 'Apply repair mortar'],
|
17 |
+
'reinforcement_corrosion': ['Remove rust', 'Apply corrosion inhibitor', 'Repair concrete cover'],
|
18 |
+
'structural_crack': ['Measure crack width', 'Epoxy injection', 'Monitor progression'],
|
19 |
+
'dampness': ['Identify water source', 'Improve drainage', 'Apply waterproofing'],
|
20 |
+
'no_damage': ['Regular maintenance', 'Periodic inspection']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
}
|
22 |
|
23 |
@st.cache_resource
|
|
|
25 |
model = ViTForImageClassification.from_pretrained(
|
26 |
"google/vit-base-patch16-224",
|
27 |
num_labels=len(DAMAGE_TYPES),
|
28 |
+
ignore_mismatched_sizes=True
|
29 |
)
|
30 |
processor = ViTImageProcessor.from_pretrained("google/vit-base-patch16-224")
|
31 |
return model, processor
|
32 |
|
33 |
+
def analyze_damage(image, model, processor):
|
34 |
image = image.convert('RGB')
|
35 |
inputs = processor(images=image, return_tensors="pt")
|
|
|
|
|
|
|
|
|
36 |
outputs = model(**inputs)
|
37 |
probs = torch.nn.functional.softmax(outputs.logits, dim=1)[0]
|
38 |
return probs
|
39 |
|
40 |
def main():
|
41 |
st.title("Structural Damage Assessment Tool")
|
|
|
42 |
|
43 |
model, processor = load_model()
|
44 |
|
45 |
+
uploaded_file = st.file_uploader("Upload structural image", type=['jpg', 'jpeg', 'png'])
|
46 |
|
47 |
if uploaded_file:
|
48 |
image = Image.open(uploaded_file)
|
49 |
st.image(image, caption="Uploaded Structure", use_column_width=True)
|
50 |
|
51 |
+
with st.spinner("Analyzing..."):
|
52 |
predictions = analyze_damage(image, model, processor)
|
53 |
|
54 |
col1, col2 = st.columns(2)
|
|
|
57 |
st.subheader("Damage Assessment")
|
58 |
for idx, prob in enumerate(predictions):
|
59 |
damage_type = DAMAGE_TYPES[idx]['name']
|
|
|
60 |
confidence = float(prob) * 100
|
|
|
61 |
if confidence > 15:
|
62 |
st.write(f"**{damage_type.replace('_', ' ').title()}**")
|
63 |
st.progress(confidence / 100)
|
64 |
st.write(f"Confidence: {confidence:.1f}%")
|
65 |
+
st.write(f"Risk Level: {DAMAGE_TYPES[idx]['risk']}")
|
66 |
|
67 |
with col2:
|
68 |
st.subheader("Recommended Actions")
|
69 |
for idx, prob in enumerate(predictions):
|
70 |
damage_type = DAMAGE_TYPES[idx]['name']
|
71 |
confidence = float(prob) * 100
|
|
|
72 |
if confidence > 15:
|
73 |
st.write(f"**For {damage_type.replace('_', ' ').title()}:**")
|
74 |
for remedy in REMEDIES[damage_type]:
|