File size: 14,125 Bytes
80585bc
 
 
 
 
 
 
 
aabfb0a
80585bc
 
aabfb0a
 
 
 
 
 
80585bc
aabfb0a
 
 
80585bc
aabfb0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80585bc
aabfb0a
80585bc
aabfb0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d6c46d4
aabfb0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e7c0374
 
 
 
 
 
 
 
 
 
 
 
 
249fb68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e7c0374
249fb68
 
 
aabfb0a
 
249fb68
 
 
 
 
 
 
 
 
 
 
aabfb0a
249fb68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aabfb0a
 
 
 
0bc09c2
aabfb0a
 
 
 
 
0bc09c2
aabfb0a
 
 
0bc09c2
aabfb0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ed38ea6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aabfb0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80585bc
 
 
 
 
 
aabfb0a
 
 
 
 
 
 
 
80585bc
aabfb0a
80585bc
aabfb0a
80585bc
 
 
 
aabfb0a
80585bc
aabfb0a
80585bc
aabfb0a
 
 
80585bc
aabfb0a
 
 
 
 
 
 
 
 
 
 
 
 
80585bc
aabfb0a
 
 
 
 
 
 
 
 
 
 
 
 
80585bc
aabfb0a
 
 
80585bc
aabfb0a
 
80585bc
aabfb0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0bc09c2
aabfb0a
 
 
 
0bc09c2
aabfb0a
 
 
80585bc
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
import streamlit as st
import os
from groq import Groq
from transformers import ViTForImageClassification, ViTImageProcessor
from sentence_transformers import SentenceTransformer
from PIL import Image
import torch
import numpy as np
from typing import List, Dict, Tuple, Optional, Any
import faiss
import json
import torchvision.transforms.functional as TF
from torchvision import transforms
import cv2
import pandas as pd
from datetime import datetime
import logging

# Setup logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

class ConfigManager:
    """Manages configuration settings for the application"""
    DEFAULT_CONFIG = {
        "model_settings": {
            "vit_model": "google/vit-base-patch16-224",
            "sentence_transformer": "all-MiniLM-L6-v2",
            "groq_model": "llama-3.3-70b-versatile"
        },
        "analysis_settings": {
            "confidence_threshold": 0.5,
            "max_defects": 3,
            "heatmap_intensity": 0.7
        },
        "rag_settings": {
            "num_relevant_docs": 3,
            "similarity_threshold": 0.75
        }
    }

    @staticmethod
    def load_config():
        """Load configuration with fallback to defaults"""
        try:
            if os.path.exists('config.json'):
                with open('config.json', 'r') as f:
                    config = json.load(f)
                return {**ConfigManager.DEFAULT_CONFIG, **config}
        except Exception as e:
            logger.warning(f"Error loading config: {e}")
        return ConfigManager.DEFAULT_CONFIG

config = ConfigManager.load_config()

class ImageAnalyzer:
    def __init__(self):
        self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
        self.config = config["model_settings"]
        self.analysis_config = config["analysis_settings"]
        self.defect_classes = [
            "spalling", "reinforcement_corrosion", "structural_cracks",
            "water_damage", "surface_deterioration", "alkali_silica_reaction",
            "concrete_delamination", "honeycomb", "scaling",
            "efflorescence", "joint_deterioration", "carbonation"
        ]
        self.initialize_models()
        self.history = []

    def initialize_models(self):
        """Initialize all required models"""
        try:
            # Initialize ViT model
            self.model = ViTForImageClassification.from_pretrained(
                self.config["vit_model"],
                num_labels=len(self.defect_classes),
                ignore_mismatched_sizes=True
            ).to(self.device)
            
            # Initialize image processor
            self.processor = ViTImageProcessor.from_pretrained(self.config["vit_model"])
            
            # Initialize transformations pipeline
            self.transforms = self._setup_transforms()
            
            return True
        except Exception as e:
            logger.error(f"Model initialization error: {e}")
            return False

    def _setup_transforms(self):
        """Setup image transformation pipeline"""
        return transforms.Compose([
            transforms.Resize((224, 224)),
            transforms.ToTensor(),
            transforms.Normalize(mean=[0.485, 0.456, 0.406], 
                              std=[0.229, 0.224, 0.225]),
            transforms.RandomAdjustSharpness(2),
            transforms.ColorJitter(brightness=0.2, contrast=0.2)
        ])

    def preprocess_image(self, image: Image.Image) -> Dict[str, Any]:
        """Enhanced image preprocessing with multiple analyses"""
        try:
            # Convert to RGB if necessary
            if image.mode != 'RGB':
                image = image.convert('RGB')
                # Basic image statistics
                img_array = np.array(image)
                stats = {
                    "mean_brightness": np.mean(img_array),
                    "std_brightness": np.std(img_array),
                    "size": image.size,
                    "aspect_ratio": image.size[0] / image.size[1]
                }

        # Edge detection for crack analysis
        gray = cv2.cvtColor(img_array, cv2.COLOR_RGB2GRAY)
        edges = cv2.Canny(gray, 100, 200)
        stats["edge_density"] = np.mean(edges > 0)

        # Color analysis for rust detection
        hsv = cv2.cvtColor(img_array, cv2.COLOR_RGB2HSV)
        rust_mask = cv2.inRange(hsv, np.array([0, 50, 50]), np.array([30, 255, 255]))
        stats["rust_percentage"] = np.mean(rust_mask > 0)

        # Transform for model
        model_input = self.transforms(image).unsqueeze(0).to(self.device)

        return {
            "model_input": model_input,
            "stats": stats,
            "edges": edges,
            "rust_mask": rust_mask
        }
except Exception as e:
        logger.error(f"Preprocessing error: {e}")
        return None


    def detect_defects(self, image: Image.Image) -> Dict[str, Any]:
    """Enhanced defect detection with multiple analysis methods"""
    try:
        # Preprocess image
        proc_data = self.preprocess_image(image)
        if proc_data is None:
            logger.error("Image preprocessing failed.")
            return None  # Early return if preprocessing failed

        # Model prediction
        with torch.no_grad():
            outputs = self.model(proc_data["model_input"])
            
        # Get probabilities
        probabilities = torch.nn.functional.softmax(outputs.logits, dim=1)
        
        # Convert to dictionary
        defect_probs = {
            self.defect_classes[i]: float(probabilities[0][i])
            for i in range(len(self.defect_classes))
        }

        # Generate attention heatmap
        attention_weights = outputs.attentions[-1].mean(dim=1)[0] if hasattr(outputs, 'attentions') else None
        heatmap = self.generate_heatmap(attention_weights, image.size) if attention_weights is not None else None

        # Additional analysis based on image statistics
        additional_analysis = self.analyze_image_statistics(proc_data["stats"])

        # Combine all results
        result = {
            "defect_probabilities": defect_probs,
            "heatmap": heatmap,
            "image_statistics": proc_data["stats"],
            "additional_analysis": additional_analysis,
            "edge_detection": proc_data["edges"],
            "rust_detection": proc_data["rust_mask"],
            "timestamp": datetime.now().isoformat()
        }

        # Save to history
        self.history.append(result)

        return result
    except Exception as e:
        logger.error(f"Defect detection error: {e}")
        return None


    def analyze_image_statistics(self, stats: Dict) -> Dict[str, Any]:
        """Analyze image statistics for additional insights"""
        analysis = {}
        
        # Brightness analysis
        if stats["mean_brightness"] < 50:
            analysis["lighting_condition"] = "Poor lighting - may affect accuracy"
        elif stats["mean_brightness"] > 200:
            analysis["lighting_condition"] = "Overexposed - may affect accuracy"
        
        # Edge density analysis
        if stats["edge_density"] > 0.1:
            analysis["crack_likelihood"] = "High crack probability based on edge detection"
        
        # Rust analysis
        if stats["rust_percentage"] > 0.05:
            analysis["corrosion_indicator"] = "Possible corrosion detected"

        return analysis

    def generate_heatmap(self, attention_weights: torch.Tensor, image_size: Tuple[int, int]) -> np.ndarray:
        """Generate enhanced attention heatmap"""
        try:
            if attention_weights is None:
                return None

            # Process attention weights
            heatmap = attention_weights.cpu().numpy()
            heatmap = cv2.resize(heatmap, image_size)
            
            # Enhanced normalization
            heatmap = np.maximum(heatmap, 0)
            heatmap = (heatmap - heatmap.min()) / (heatmap.max() - heatmap.min() + 1e-8)
            
            # Apply gamma correction
            gamma = self.analysis_config["heatmap_intensity"]
            heatmap = np.power(heatmap, gamma)
            
            # Apply colormap
            heatmap = (heatmap * 255).astype(np.uint8)
            heatmap = cv2.applyColorMap(heatmap, cv2.COLORMAP_JET)
            
            return heatmap
        except Exception as e:
            logger.error(f"Heatmap generation error: {e}")
            return None
class RAGSystem:
    """Basic RAG System for storing and retrieving documents."""
    def __init__(self):
        self.embedding_model = SentenceTransformer(config["model_settings"]["sentence_transformer"])
        self.vector_store = faiss.IndexFlatL2(384)  # 384-dim for MiniLM embeddings
        self.knowledge_base = []

    def add_documents(self, docs: List[str]):
        """Add documents to the vector store."""
        embeddings = self.embedding_model.encode(docs)
        self.vector_store.add(np.array(embeddings).astype('float32'))
        for doc in docs:
            self.knowledge_base.append({"text": doc})

    def search(self, query: str, k: int = 3):
        """Retrieve similar documents for the query."""
        query_embedding = self.embedding_model.encode([query])
        D, I = self.vector_store.search(np.array(query_embedding).astype('float32'), k)
        return [self.knowledge_base[i]["text"] for i in I[0]]


class EnhancedRAGSystem(RAGSystem):
    """Enhanced RAG system with additional features"""
    def __init__(self):
        super().__init__()
        self.config = config["rag_settings"]
        self.query_history = []

    def get_relevant_context(self, query: str, k: int = None) -> str:
        """Enhanced context retrieval with debugging info"""
        if k is None:
            k = self.config["num_relevant_docs"]

        # Log query
        self.query_history.append({
            "timestamp": datetime.now().isoformat(),
            "query": query
        })

        # Generate query embedding
        query_embedding = self.embedding_model.encode([query])
        
        # Search for similar documents
        D, I = self.vector_store.search(np.array(query_embedding).astype('float32'), k)
        
        # Filter by similarity threshold
        relevant_docs = [
            self.knowledge_base[i]["text"] 
            for i, dist in zip(I[0], D[0]) 
            if dist < self.config["similarity_threshold"]
        ]
        
        return "\n\n".join(relevant_docs)

def main():
    st.set_page_config(
        page_title="Enhanced Construction Defect Analyzer",
        page_icon="🏗️",
        layout="wide"
    )

    st.title("🏗️ Advanced Construction Defect Analysis System")

    # Initialize systems
    if 'rag_system' not in st.session_state:
        st.session_state.rag_system = EnhancedRAGSystem()
    if 'image_analyzer' not in st.session_state:
        st.session_state.image_analyzer = ImageAnalyzer()

    # Sidebar for settings and history
    with st.sidebar:
        st.header("Settings & History")
        show_debug = st.checkbox("Show Debug Information")
        confidence_threshold = st.slider(
            "Confidence Threshold",
            min_value=0.0,
            max_value=1.0,
            value=config["analysis_settings"]["confidence_threshold"]
        )
        
        if st.button("View Analysis History"):
            st.write("Recent Analyses:", st.session_state.image_analyzer.history[-5:])

    # Main interface
    col1, col2 = st.columns([2, 3])

    with col1:
        uploaded_file = st.file_uploader(
            "Upload a construction image",
            type=['jpg', 'jpeg', 'png']
        )
        
        user_query = st.text_input(
            "Ask a question about construction defects:",
            help="Enter your question about specific defects or general construction issues"
        )

    with col2:
        if uploaded_file:
            image = Image.open(uploaded_file)
            
            # Create tabs for different views
            tabs = st.tabs(["Original", "Analysis", "Details"])
            
            with tabs[0]:
                st.image(image, caption="Uploaded Image")
            
            with tabs[1]:
                with st.spinner("Analyzing image..."):
                    results = st.session_state.image_analyzer.detect_defects(image)
                    
                    if results:
                        # Show defect probabilities
                        defect_probs = results["defect_probabilities"]
                        significant_defects = {
                            k: v for k, v in defect_probs.items() 
                            if v > confidence_threshold
                        }
                        
                        if significant_defects:
                            st.subheader("Detected Defects")
                            fig = plt.figure(figsize=(10, 6))
                            plt.barh(list(significant_defects.keys()),
                                   list(significant_defects.values()))
                            st.pyplot(fig)
                        
                        # Show heatmap
                        if results["heatmap"] is not None:
                            st.image(results["heatmap"], caption="Defect Attention Map")

            with tabs[2]:
                if results:
                    st.json(results["additional_analysis"])
                    if show_debug:
                        st.json(results["image_statistics"])

        if user_query:
            with st.spinner("Processing query..."):
                context = st.session_state.rag_system.get_relevant_context(user_query)
                response = get_groq_response(user_query, context)
                
                st.subheader("AI Assistant Response")
                st.write(response)
                
                if show_debug:
                    st.subheader("Retrieved Context")
                    st.text(context)

if __name__ == "__main__":
    main()