Shakir60 commited on
Commit
4f207d0
·
verified ·
1 Parent(s): d865cee

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -27
app.py CHANGED
@@ -1,46 +1,71 @@
1
  import streamlit as st
2
- from transformers import pipeline, AutoModelForImageClassification, AutoImageProcessor
3
  from PIL import Image
4
  import torch
5
 
6
- # Initialize models
7
- model_name = "microsoft/resnet-50"
8
- image_processor = AutoImageProcessor.from_pretrained(model_name)
9
- model = AutoModelForImageClassification.from_pretrained(model_name)
10
- classifier = pipeline("image-classification", model=model, image_processor=image_processor)
11
-
12
- damage_types = {
13
- 'spalling': ['Remove loose material', 'Apply repair mortar', 'Seal surface'],
14
- 'cracking': ['Measure crack width', 'Install crack monitors', 'Apply sealant'],
15
- 'corrosion': ['Clean exposed steel', 'Apply rust converter', 'Paint protective coating'],
16
- 'dampness': ['Locate water source', 'Fix drainage issues', 'Apply waterproofing']
 
 
 
 
 
 
 
17
  }
18
 
19
- def analyze_image(image):
20
- return classifier(image)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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="Uploaded Image")
30
 
31
- with st.spinner("Analyzing..."):
32
- results = analyze_image(image)
33
 
34
- for result in results:
35
- confidence = result['score'] * 100
36
- st.progress(confidence / 100)
37
- st.write(f"{result['label']}: {confidence:.1f}%")
38
-
39
- for damage_type, remedies in damage_types.items():
40
- if damage_type in result['label'].lower():
41
- st.subheader("Recommended Actions:")
42
- for remedy in remedies:
43
- st.write(f"• {remedy}")
 
 
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()