lyimo commited on
Commit
fbaa8e3
·
verified ·
1 Parent(s): 5944a16

Create part3.py

Browse files
Files changed (1) hide show
  1. part3.py +179 -0
part3.py ADDED
@@ -0,0 +1,179 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import cv2
3
+ from segment_anything import sam_model_registry, SamPredictor
4
+ import matplotlib.pyplot as plt
5
+ import io
6
+ from PIL import Image
7
+
8
+ class SAMAnalyzer:
9
+ def __init__(self, model_path="sam_vit_h_4b8939.pth"):
10
+ self.model_path = model_path
11
+ self.sam = None
12
+ self.predictor = None
13
+ self.initialize_sam()
14
+
15
+ def initialize_sam(self):
16
+ """Initialize SAM model"""
17
+ try:
18
+ self.sam = sam_model_registry["vit_h"](checkpoint=self.model_path)
19
+ self.predictor = SamPredictor(self.sam)
20
+ print("SAM model initialized successfully")
21
+ except Exception as e:
22
+ print(f"Error initializing SAM model: {e}")
23
+ raise
24
+
25
+ def process_image(self, image_data):
26
+ """Process uploaded image using SAM"""
27
+ try:
28
+ # Convert uploaded image to numpy array
29
+ if isinstance(image_data, (str, bytes)):
30
+ if isinstance(image_data, str):
31
+ image = cv2.imread(image_data)
32
+ else:
33
+ nparr = np.frombuffer(image_data, np.uint8)
34
+ image = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
35
+ image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
36
+ else:
37
+ image = np.array(Image.open(image_data))
38
+
39
+ # Segment farmland
40
+ print("Segmenting farmland...")
41
+ farmland_mask = self.segment_farmland(image)
42
+
43
+ # Calculate vegetation index
44
+ print("Calculating vegetation index...")
45
+ veg_index = self.calculate_vegetation_index(image, farmland_mask)
46
+
47
+ # Analyze health
48
+ print("Analyzing crop health...")
49
+ health_analysis = self.analyze_crop_health(veg_index, farmland_mask)
50
+
51
+ # Create visualization
52
+ print("Generating visualization...")
53
+ viz_plot = self.create_visualization(image, farmland_mask, veg_index)
54
+
55
+ return veg_index, health_analysis, viz_plot
56
+
57
+ except Exception as e:
58
+ print(f"Error processing image: {e}")
59
+ return None, None, None
60
+
61
+ def segment_farmland(self, image):
62
+ """Segment farmland using SAM2"""
63
+ self.predictor.set_image(image)
64
+
65
+ # Generate automatic mask proposals
66
+ h, w = image.shape[:2]
67
+ input_point = np.array([[w//2, h//2]])
68
+ input_label = np.array([1])
69
+
70
+ masks, scores, logits = self.predictor.predict(
71
+ point_coords=input_point,
72
+ point_labels=input_label,
73
+ multimask_output=True
74
+ )
75
+
76
+ # Select best mask
77
+ best_mask = masks[scores.argmax()]
78
+ return best_mask
79
+
80
+ def calculate_vegetation_index(self, image, mask):
81
+ """Calculate vegetation index using RGB"""
82
+ r, g, b = image[:,:,0], image[:,:,1], image[:,:,2]
83
+
84
+ numerator = (2 * g.astype(float) - r.astype(float) - b.astype(float))
85
+ denominator = (2 * g.astype(float) + r.astype(float) + b.astype(float))
86
+ denominator[denominator == 0] = 1e-10
87
+
88
+ veg_index = numerator / denominator
89
+ veg_index = (veg_index + 1) / 2
90
+ veg_index = veg_index * mask
91
+
92
+ return veg_index
93
+
94
+ def analyze_crop_health(self, veg_index, mask):
95
+ """Analyze crop health based on vegetation index"""
96
+ valid_pixels = veg_index[mask > 0]
97
+ if len(valid_pixels) == 0:
98
+ return {
99
+ 'average_index': 0,
100
+ 'health_distribution': {
101
+ 'low_vegetation': 0,
102
+ 'moderate_vegetation': 0,
103
+ 'high_vegetation': 0
104
+ },
105
+ 'overall_health': 'No vegetation detected'
106
+ }
107
+
108
+ avg_index = np.mean(valid_pixels)
109
+ health_categories = {
110
+ 'low_vegetation': np.sum((valid_pixels <= 0.3)) / len(valid_pixels),
111
+ 'moderate_vegetation': np.sum((valid_pixels > 0.3) & (valid_pixels <= 0.6)) / len(valid_pixels),
112
+ 'high_vegetation': np.sum((valid_pixels > 0.6)) / len(valid_pixels)
113
+ }
114
+
115
+ return {
116
+ 'average_index': avg_index,
117
+ 'health_distribution': health_categories,
118
+ 'overall_health': 'Healthy' if avg_index > 0.5 else 'Needs attention'
119
+ }
120
+
121
+ def create_visualization(self, image, mask, veg_index):
122
+ """Create visualization of results"""
123
+ fig = plt.figure(figsize=(15, 5))
124
+
125
+ # Original image with mask overlay
126
+ plt.subplot(131)
127
+ plt.imshow(image)
128
+ plt.imshow(mask, alpha=0.3, cmap='gray')
129
+ plt.title('Segmented Farmland')
130
+ plt.axis('off')
131
+
132
+ # Vegetation index heatmap
133
+ plt.subplot(132)
134
+ plt.imshow(veg_index, cmap='RdYlGn')
135
+ plt.colorbar(label='Vegetation Index')
136
+ plt.title('Vegetation Index')
137
+ plt.axis('off')
138
+
139
+ # Health classification
140
+ plt.subplot(133)
141
+ health_mask = np.zeros_like(veg_index)
142
+ health_mask[veg_index <= 0.3] = 1
143
+ health_mask[(veg_index > 0.3) & (veg_index <= 0.6)] = 2
144
+ health_mask[veg_index > 0.6] = 3
145
+ health_mask = health_mask * mask
146
+ plt.imshow(health_mask, cmap='viridis')
147
+ plt.colorbar(ticks=[1, 2, 3],
148
+ label='Vegetation Levels',
149
+ boundaries=np.arange(0.5, 4.5),
150
+ values=[1, 2, 3])
151
+ plt.title('Vegetation Levels')
152
+ plt.axis('off')
153
+
154
+ plt.tight_layout()
155
+
156
+ # Save plot to buffer
157
+ buf = io.BytesIO()
158
+ plt.savefig(buf, format='png', bbox_inches='tight')
159
+ buf.seek(0)
160
+ plt.close()
161
+
162
+ return buf
163
+
164
+ def format_analysis_text(self, health_analysis):
165
+ """Format health analysis results as text"""
166
+ return f"""
167
+ 🌿 Vegetation Analysis Results:
168
+
169
+ 📊 Average Vegetation Index: {health_analysis['average_index']:.2f}
170
+
171
+ 🌱 Vegetation Distribution:
172
+ • Low Vegetation: {health_analysis['health_distribution']['low_vegetation']*100:.1f}%
173
+ • Moderate Vegetation: {health_analysis['health_distribution']['moderate_vegetation']*100:.1f}%
174
+ • High Vegetation: {health_analysis['health_distribution']['high_vegetation']*100:.1f}%
175
+
176
+ 📋 Overall Health Status: {health_analysis['overall_health']}
177
+
178
+ Note: Analysis uses SAM2 for farmland segmentation
179
+ """