Update part3.py
Browse files
part3.py
CHANGED
@@ -110,7 +110,7 @@ class SAMAnalyzer:
|
|
110 |
raise
|
111 |
|
112 |
def analyze_crop_health(self, veg_index, mask):
|
113 |
-
"""Analyze crop health based on vegetation index"""
|
114 |
try:
|
115 |
valid_pixels = veg_index[mask > 0]
|
116 |
if len(valid_pixels) == 0:
|
@@ -121,9 +121,15 @@ class SAMAnalyzer:
|
|
121 |
'moderate_vegetation': 0,
|
122 |
'high_vegetation': 0
|
123 |
},
|
124 |
-
'overall_health': 'No vegetation detected'
|
|
|
|
|
|
|
125 |
}
|
126 |
|
|
|
|
|
|
|
127 |
avg_index = np.mean(valid_pixels)
|
128 |
health_categories = {
|
129 |
'low_vegetation': np.sum((valid_pixels <= 0.3)) / len(valid_pixels),
|
@@ -131,18 +137,158 @@ class SAMAnalyzer:
|
|
131 |
'high_vegetation': np.sum((valid_pixels > 0.6)) / len(valid_pixels)
|
132 |
}
|
133 |
|
134 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
135 |
|
136 |
return {
|
137 |
'average_index': avg_index,
|
138 |
'health_distribution': health_categories,
|
139 |
-
'overall_health':
|
|
|
|
|
|
|
|
|
140 |
}
|
141 |
|
142 |
except Exception as e:
|
143 |
print(f"Error analyzing crop health: {e}")
|
144 |
raise
|
145 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
146 |
def create_visualization(self, image, mask, veg_index):
|
147 |
"""Create visualization of results"""
|
148 |
try:
|
@@ -188,4 +334,32 @@ class SAMAnalyzer:
|
|
188 |
|
189 |
except Exception as e:
|
190 |
print(f"Error creating visualization: {e}")
|
191 |
-
raise
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
110 |
raise
|
111 |
|
112 |
def analyze_crop_health(self, veg_index, mask):
|
113 |
+
"""Analyze crop health and insurance risk based on vegetation index"""
|
114 |
try:
|
115 |
valid_pixels = veg_index[mask > 0]
|
116 |
if len(valid_pixels) == 0:
|
|
|
121 |
'moderate_vegetation': 0,
|
122 |
'high_vegetation': 0
|
123 |
},
|
124 |
+
'overall_health': 'No vegetation detected',
|
125 |
+
'farm_size': 0,
|
126 |
+
'insurance_risk': 'Very High',
|
127 |
+
'insurance_recommendations': 'Cannot assess insurance without vegetation data'
|
128 |
}
|
129 |
|
130 |
+
# Calculate farm size (approximate)
|
131 |
+
farm_size = np.sum(mask) * 0.0001 # Convert pixels to hectares (approximate)
|
132 |
+
|
133 |
avg_index = np.mean(valid_pixels)
|
134 |
health_categories = {
|
135 |
'low_vegetation': np.sum((valid_pixels <= 0.3)) / len(valid_pixels),
|
|
|
137 |
'high_vegetation': np.sum((valid_pixels > 0.6)) / len(valid_pixels)
|
138 |
}
|
139 |
|
140 |
+
# Calculate vegetation uniformity
|
141 |
+
vegetation_uniformity = np.std(valid_pixels)
|
142 |
+
|
143 |
+
# Determine insurance risk level
|
144 |
+
insurance_risk = self.calculate_insurance_risk(
|
145 |
+
avg_index,
|
146 |
+
health_categories,
|
147 |
+
vegetation_uniformity,
|
148 |
+
farm_size
|
149 |
+
)
|
150 |
+
|
151 |
+
# Get insurance recommendations
|
152 |
+
insurance_recommendations = self.get_insurance_recommendations(
|
153 |
+
insurance_risk,
|
154 |
+
health_categories,
|
155 |
+
farm_size
|
156 |
+
)
|
157 |
|
158 |
return {
|
159 |
'average_index': avg_index,
|
160 |
'health_distribution': health_categories,
|
161 |
+
'overall_health': 'Healthy' if avg_index > 0.5 else 'Needs attention',
|
162 |
+
'farm_size': farm_size,
|
163 |
+
'vegetation_uniformity': vegetation_uniformity,
|
164 |
+
'insurance_risk': insurance_risk,
|
165 |
+
'insurance_recommendations': insurance_recommendations
|
166 |
}
|
167 |
|
168 |
except Exception as e:
|
169 |
print(f"Error analyzing crop health: {e}")
|
170 |
raise
|
171 |
|
172 |
+
def calculate_insurance_risk(self, avg_index, health_distribution, uniformity, farm_size):
|
173 |
+
"""Calculate insurance risk level based on vegetation analysis"""
|
174 |
+
risk_score = 0
|
175 |
+
|
176 |
+
# Vegetation health risk (0-40 points)
|
177 |
+
if avg_index >= 0.6:
|
178 |
+
risk_score += 40
|
179 |
+
elif avg_index >= 0.4:
|
180 |
+
risk_score += 25
|
181 |
+
elif avg_index >= 0.2:
|
182 |
+
risk_score += 10
|
183 |
+
|
184 |
+
# Vegetation distribution risk (0-30 points)
|
185 |
+
if health_distribution['high_vegetation'] > 0.6:
|
186 |
+
risk_score += 30
|
187 |
+
elif health_distribution['high_vegetation'] > 0.4:
|
188 |
+
risk_score += 20
|
189 |
+
elif health_distribution['moderate_vegetation'] > 0.5:
|
190 |
+
risk_score += 15
|
191 |
+
|
192 |
+
# Uniformity risk (0-20 points)
|
193 |
+
if uniformity < 0.1:
|
194 |
+
risk_score += 20
|
195 |
+
elif uniformity < 0.2:
|
196 |
+
risk_score += 15
|
197 |
+
elif uniformity < 0.3:
|
198 |
+
risk_score += 10
|
199 |
+
|
200 |
+
# Farm size risk (0-10 points)
|
201 |
+
if farm_size > 10: # Large farm
|
202 |
+
risk_score += 10
|
203 |
+
elif farm_size > 5: # Medium farm
|
204 |
+
risk_score += 7
|
205 |
+
elif farm_size > 2: # Small farm
|
206 |
+
risk_score += 5
|
207 |
+
|
208 |
+
# Determine risk level
|
209 |
+
if risk_score >= 80:
|
210 |
+
return "Low"
|
211 |
+
elif risk_score >= 60:
|
212 |
+
return "Moderate"
|
213 |
+
elif risk_score >= 40:
|
214 |
+
return "High"
|
215 |
+
else:
|
216 |
+
return "Very High"
|
217 |
+
|
218 |
+
def get_insurance_recommendations(self, risk_level, health_distribution, farm_size):
|
219 |
+
"""Get detailed insurance recommendations based on analysis"""
|
220 |
+
base_recommendations = {
|
221 |
+
"Low": {
|
222 |
+
"policy_type": "Standard Coverage",
|
223 |
+
"premium_level": "Lower premiums likely",
|
224 |
+
"coverage_options": [
|
225 |
+
"β’ Basic crop insurance",
|
226 |
+
"β’ Optional revenue protection",
|
227 |
+
"β’ Minimal deductible options",
|
228 |
+
"β’ Standard natural disaster coverage"
|
229 |
+
],
|
230 |
+
"additional_notes": "Farm shows good health indicators; standard coverage should be sufficient"
|
231 |
+
},
|
232 |
+
"Moderate": {
|
233 |
+
"policy_type": "Enhanced Coverage",
|
234 |
+
"premium_level": "Moderate premiums",
|
235 |
+
"coverage_options": [
|
236 |
+
"β’ Enhanced crop insurance",
|
237 |
+
"β’ Revenue protection recommended",
|
238 |
+
"β’ Weather index insurance",
|
239 |
+
"β’ Moderate deductible options",
|
240 |
+
"β’ Extended natural disaster coverage"
|
241 |
+
],
|
242 |
+
"additional_notes": "Consider additional coverage for specific risks"
|
243 |
+
},
|
244 |
+
"High": {
|
245 |
+
"policy_type": "Comprehensive Coverage",
|
246 |
+
"premium_level": "Higher premiums likely",
|
247 |
+
"coverage_options": [
|
248 |
+
"β’ Comprehensive crop insurance",
|
249 |
+
"β’ Multi-peril crop insurance recommended",
|
250 |
+
"β’ Weather index insurance strongly advised",
|
251 |
+
"β’ Consider higher coverage limits",
|
252 |
+
"β’ Full natural disaster coverage",
|
253 |
+
"β’ Supplemental coverage option (SCO)"
|
254 |
+
],
|
255 |
+
"additional_notes": "Risk mitigation strategies should be implemented alongside insurance"
|
256 |
+
},
|
257 |
+
"Very High": {
|
258 |
+
"policy_type": "Maximum Coverage",
|
259 |
+
"premium_level": "Significant premiums",
|
260 |
+
"coverage_options": [
|
261 |
+
"β’ Maximum coverage crop insurance",
|
262 |
+
"β’ Multi-peril crop insurance required",
|
263 |
+
"β’ Weather index insurance essential",
|
264 |
+
"β’ Additional risk management tools needed",
|
265 |
+
"β’ Maximum natural disaster coverage",
|
266 |
+
"β’ Enhanced coverage option (ECO)",
|
267 |
+
"β’ Consider crop diversification insurance"
|
268 |
+
],
|
269 |
+
"additional_notes": "Immediate risk mitigation actions recommended before planting"
|
270 |
+
}
|
271 |
+
}
|
272 |
+
|
273 |
+
# Adjust recommendations based on farm size
|
274 |
+
size_category = "small" if farm_size < 5 else "medium" if farm_size < 10 else "large"
|
275 |
+
|
276 |
+
recommendations = base_recommendations[risk_level]
|
277 |
+
|
278 |
+
# Add size-specific recommendations
|
279 |
+
if size_category == "small":
|
280 |
+
recommendations["coverage_options"].append("β’ Consider cooperative insurance options")
|
281 |
+
recommendations["coverage_options"].append("β’ Micro-insurance options available")
|
282 |
+
elif size_category == "medium":
|
283 |
+
recommendations["coverage_options"].append("β’ Consider split coverage options")
|
284 |
+
recommendations["coverage_options"].append("β’ Zone-based coverage recommended")
|
285 |
+
else:
|
286 |
+
recommendations["coverage_options"].append("β’ Consider zone-based coverage options")
|
287 |
+
recommendations["coverage_options"].append("β’ Enterprise unit structure recommended")
|
288 |
+
recommendations["coverage_options"].append("β’ Custom risk management solutions")
|
289 |
+
|
290 |
+
return recommendations
|
291 |
+
|
292 |
def create_visualization(self, image, mask, veg_index):
|
293 |
"""Create visualization of results"""
|
294 |
try:
|
|
|
334 |
|
335 |
except Exception as e:
|
336 |
print(f"Error creating visualization: {e}")
|
337 |
+
raise
|
338 |
+
|
339 |
+
def format_insurance_analysis(self, health_analysis):
|
340 |
+
"""Format insurance analysis results as text"""
|
341 |
+
try:
|
342 |
+
farm_size = health_analysis['farm_size']
|
343 |
+
risk_level = health_analysis['insurance_risk']
|
344 |
+
recommendations = health_analysis['insurance_recommendations']
|
345 |
+
|
346 |
+
return f"""
|
347 |
+
ποΈ Farm Analysis:
|
348 |
+
β’ Approximate Size: {farm_size:.1f} hectares
|
349 |
+
β’ Vegetation Uniformity: {health_analysis['vegetation_uniformity']:.2f}
|
350 |
+
|
351 |
+
π― Insurance Risk Level: {risk_level}
|
352 |
+
|
353 |
+
π‘ Recommended Insurance Strategy:
|
354 |
+
β’ Policy Type: {recommendations['policy_type']}
|
355 |
+
β’ Premium Level: {recommendations['premium_level']}
|
356 |
+
|
357 |
+
π Recommended Coverage Options:
|
358 |
+
{chr(10).join(recommendations['coverage_options'])}
|
359 |
+
|
360 |
+
π Additional Notes:
|
361 |
+
{recommendations['additional_notes']}
|
362 |
+
"""
|
363 |
+
except Exception as e:
|
364 |
+
print(f"Error formatting insurance analysis: {e}")
|
365 |
+
return "Error generating insurance recommendations"
|