Update app.py
Browse files
app.py
CHANGED
@@ -2,70 +2,104 @@ 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 |
-
|
10 |
-
|
11 |
-
},
|
12 |
-
'
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
'
|
17 |
-
'
|
18 |
-
'
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
'
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
}
|
25 |
|
26 |
@st.cache_resource
|
27 |
def load_model():
|
28 |
model = ViTForImageClassification.from_pretrained(
|
29 |
-
"
|
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
|
|
|
38 |
inputs = processor(images=image, return_tensors="pt")
|
|
|
|
|
|
|
|
|
39 |
outputs = model(**inputs)
|
40 |
-
probs = torch.nn.functional.softmax(outputs.logits, dim
|
41 |
-
return probs
|
42 |
|
43 |
def main():
|
44 |
-
st.title("Structural Damage Assessment")
|
45 |
-
|
|
|
46 |
model, processor = load_model()
|
47 |
|
48 |
-
uploaded_file = st.file_uploader("
|
49 |
|
50 |
if uploaded_file:
|
51 |
image = Image.open(uploaded_file)
|
52 |
-
st.image(image, caption="
|
53 |
|
54 |
with st.spinner("Analyzing structural damage..."):
|
55 |
predictions = analyze_damage(image, model, processor)
|
56 |
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
|
|
69 |
|
70 |
if __name__ == "__main__":
|
71 |
main()
|
|
|
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'},
|
10 |
+
2: {'name': 'structural_crack', 'risk': 'High'},
|
11 |
+
3: {'name': 'dampness', 'risk': 'Medium'},
|
12 |
+
4: {'name': 'no_damage', 'risk': 'Low'}
|
13 |
+
}
|
14 |
+
|
15 |
+
REMEDIES = {
|
16 |
+
'spalling': [
|
17 |
+
'Remove loose concrete',
|
18 |
+
'Clean exposed area',
|
19 |
+
'Apply repair mortar'
|
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
|
43 |
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 process_image(image, processor):
|
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("Choose an image", type=['jpg', 'jpeg', 'png'])
|
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 structural damage..."):
|
75 |
predictions = analyze_damage(image, model, processor)
|
76 |
|
77 |
+
col1, col2 = st.columns(2)
|
78 |
+
|
79 |
+
with col1:
|
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: {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]:
|
101 |
+
st.write(f"• {remedy}")
|
102 |
+
st.write("---")
|
103 |
|
104 |
if __name__ == "__main__":
|
105 |
main()
|