Shakir60 commited on
Commit
249fb68
·
verified ·
1 Parent(s): d6c46d4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +84 -81
app.py CHANGED
@@ -100,90 +100,93 @@ class ImageAnalyzer:
100
  ])
101
 
102
  def preprocess_image(self, image: Image.Image) -> Dict[str, Any]:
103
- """Enhanced image preprocessing with multiple analyses"""
104
- try:
105
- # Convert to RGB if necessary
106
- if image.mode != 'RGB':
107
- image = image.convert('RGB')
108
-
109
- # Basic image statistics
110
- img_array = np.array(image)
111
- stats = {
112
- "mean_brightness": np.mean(img_array),
113
- "std_brightness": np.std(img_array),
114
- "size": image.size,
115
- "aspect_ratio": image.size[0] / image.size[1]
116
- }
117
-
118
- # Edge detection for crack analysis
119
- gray = cv2.cvtColor(img_array, cv2.COLOR_RGB2GRAY)
120
- edges = cv2.Canny(gray, 100, 200)
121
- stats["edge_density"] = np.mean(edges > 0)
122
-
123
- # Color analysis for rust detection
124
- hsv = cv2.cvtColor(img_array, cv2.COLOR_RGB2HSV)
125
- rust_mask = cv2.inRange(hsv, np.array([0, 50, 50]), np.array([30, 255, 255]))
126
- stats["rust_percentage"] = np.mean(rust_mask > 0)
127
-
128
- # Transform for model
129
- model_input = self.transforms(image).unsqueeze(0).to(self.device)
130
-
131
- return {
132
- "model_input": model_input,
133
- "stats": stats,
134
- "edges": edges,
135
- "rust_mask": rust_mask
136
- }
137
- except Exception as e:
138
- logger.error(f"Preprocessing error: {e}")
139
- return None
140
 
141
- def detect_defects(self, image: Image.Image) -> Dict[str, Any]:
142
- """Enhanced defect detection with multiple analysis methods"""
143
- try:
144
- # Preprocess image
145
- proc_data = self.preprocess_image(image)
146
- if proc_data is None:
147
- return None
148
 
149
- # Model prediction
150
- with torch.no_grad():
151
- outputs = self.model(proc_data["model_input"])
152
-
153
- # Get probabilities
154
- probabilities = torch.nn.functional.softmax(outputs.logits, dim=1)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
155
 
156
- # Convert to dictionary
157
- defect_probs = {
158
- self.defect_classes[i]: float(probabilities[0][i])
159
- for i in range(len(self.defect_classes))
160
- }
161
-
162
- # Generate attention heatmap
163
- attention_weights = outputs.attentions[-1].mean(dim=1)[0] if hasattr(outputs, 'attentions') else None
164
- heatmap = self.generate_heatmap(attention_weights, image.size) if attention_weights is not None else None
165
-
166
- # Additional analysis based on image statistics
167
- additional_analysis = self.analyze_image_statistics(proc_data["stats"])
168
-
169
- # Combine all results
170
- result = {
171
- "defect_probabilities": defect_probs,
172
- "heatmap": heatmap,
173
- "image_statistics": proc_data["stats"],
174
- "additional_analysis": additional_analysis,
175
- "edge_detection": proc_data["edges"],
176
- "rust_detection": proc_data["rust_mask"],
177
- "timestamp": datetime.now().isoformat()
178
- }
179
-
180
- # Save to history
181
- self.history.append(result)
182
-
183
- return result
184
- except Exception as e:
185
- logger.error(f"Defect detection error: {e}")
186
- return None
 
 
 
 
187
 
188
  def analyze_image_statistics(self, stats: Dict) -> Dict[str, Any]:
189
  """Analyze image statistics for additional insights"""
 
100
  ])
101
 
102
  def preprocess_image(self, image: Image.Image) -> Dict[str, Any]:
103
+ """Enhanced image preprocessing with multiple analyses"""
104
+ try:
105
+ # Convert to RGB if necessary
106
+ if image.mode != 'RGB':
107
+ image = image.convert('RGB')
108
+
109
+ # Basic image statistics
110
+ img_array = np.array(image)
111
+ stats = {
112
+ "mean_brightness": np.mean(img_array),
113
+ "std_brightness": np.std(img_array),
114
+ "size": image.size,
115
+ "aspect_ratio": image.size[0] / image.size[1]
116
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
117
 
118
+ # Edge detection for crack analysis
119
+ gray = cv2.cvtColor(img_array, cv2.COLOR_RGB2GRAY)
120
+ edges = cv2.Canny(gray, 100, 200)
121
+ stats["edge_density"] = np.mean(edges > 0)
 
 
 
122
 
123
+ # Color analysis for rust detection
124
+ hsv = cv2.cvtColor(img_array, cv2.COLOR_RGB2HSV)
125
+ rust_mask = cv2.inRange(hsv, np.array([0, 50, 50]), np.array([30, 255, 255]))
126
+ stats["rust_percentage"] = np.mean(rust_mask > 0)
127
+
128
+ # Transform for model
129
+ model_input = self.transforms(image).unsqueeze(0).to(self.device)
130
+
131
+ return {
132
+ "model_input": model_input,
133
+ "stats": stats,
134
+ "edges": edges,
135
+ "rust_mask": rust_mask
136
+ }
137
+ except Exception as e:
138
+ logger.error(f"Preprocessing error: {e}")
139
+ return None
140
+
141
+
142
+ def detect_defects(self, image: Image.Image) -> Dict[str, Any]:
143
+ """Enhanced defect detection with multiple analysis methods"""
144
+ try:
145
+ # Preprocess image
146
+ proc_data = self.preprocess_image(image)
147
+ if proc_data is None:
148
+ logger.error("Image preprocessing failed.")
149
+ return None # Early return if preprocessing failed
150
+
151
+ # Model prediction
152
+ with torch.no_grad():
153
+ outputs = self.model(proc_data["model_input"])
154
 
155
+ # Get probabilities
156
+ probabilities = torch.nn.functional.softmax(outputs.logits, dim=1)
157
+
158
+ # Convert to dictionary
159
+ defect_probs = {
160
+ self.defect_classes[i]: float(probabilities[0][i])
161
+ for i in range(len(self.defect_classes))
162
+ }
163
+
164
+ # Generate attention heatmap
165
+ attention_weights = outputs.attentions[-1].mean(dim=1)[0] if hasattr(outputs, 'attentions') else None
166
+ heatmap = self.generate_heatmap(attention_weights, image.size) if attention_weights is not None else None
167
+
168
+ # Additional analysis based on image statistics
169
+ additional_analysis = self.analyze_image_statistics(proc_data["stats"])
170
+
171
+ # Combine all results
172
+ result = {
173
+ "defect_probabilities": defect_probs,
174
+ "heatmap": heatmap,
175
+ "image_statistics": proc_data["stats"],
176
+ "additional_analysis": additional_analysis,
177
+ "edge_detection": proc_data["edges"],
178
+ "rust_detection": proc_data["rust_mask"],
179
+ "timestamp": datetime.now().isoformat()
180
+ }
181
+
182
+ # Save to history
183
+ self.history.append(result)
184
+
185
+ return result
186
+ except Exception as e:
187
+ logger.error(f"Defect detection error: {e}")
188
+ return None
189
+
190
 
191
  def analyze_image_statistics(self, stats: Dict) -> Dict[str, Any]:
192
  """Analyze image statistics for additional insights"""