jbilcke-hf HF Staff commited on
Commit
52b0587
·
verified ·
1 Parent(s): de80228

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -10
app.py CHANGED
@@ -115,7 +115,7 @@ class GPUSatelliteModelGenerator:
115
  road_mask = cp.zeros((height * width,), dtype=bool)
116
  water_mask = cp.zeros((height * width,), dtype=bool)
117
 
118
- # Vectorized color matching
119
  for ref_hsv in self.shadow_colors_hsv:
120
  shadow_mask |= self.gpu_color_distance_hsv(hsv_pixels.T, ref_hsv, self.shadow_tolerance)
121
 
@@ -125,25 +125,54 @@ class GPUSatelliteModelGenerator:
125
  for ref_hsv in self.water_colors_hsv:
126
  water_mask |= self.gpu_color_distance_hsv(hsv_pixels.T, ref_hsv, self.water_tolerance)
127
 
128
- # Apply masks
129
- output_flat = output.reshape(-1, 3)
130
- output_flat[shadow_mask] = self.colors['black']
131
- output_flat[water_mask] = self.colors['blue']
132
- output_flat[road_mask] = self.colors['gray']
133
-
134
- # Vegetation and building detection
135
  h, s, v = hsv_pixels.T
136
  h = h * 2 # Convert to 0-360 range
137
  s = s / 255
138
  v = v / 255
139
 
 
140
  vegetation_mask = (h >= 40) & (h <= 150) & (s >= 0.15)
141
- building_mask = ~(shadow_mask | water_mask | road_mask | vegetation_mask)
142
 
 
 
 
 
 
 
 
 
143
  output_flat[vegetation_mask] = self.colors['green']
 
144
  output_flat[building_mask] = self.colors['white']
145
 
146
- return output.reshape(height, width, 3)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
147
 
148
  def estimate_heights_gpu(self, img, segmented):
149
  """GPU-accelerated height estimation"""
 
115
  road_mask = cp.zeros((height * width,), dtype=bool)
116
  water_mask = cp.zeros((height * width,), dtype=bool)
117
 
118
+ # Vectorized color matching for predefined colors
119
  for ref_hsv in self.shadow_colors_hsv:
120
  shadow_mask |= self.gpu_color_distance_hsv(hsv_pixels.T, ref_hsv, self.shadow_tolerance)
121
 
 
125
  for ref_hsv in self.water_colors_hsv:
126
  water_mask |= self.gpu_color_distance_hsv(hsv_pixels.T, ref_hsv, self.water_tolerance)
127
 
128
+ # Convert HSV to normalized values for vegetation and terrain detection
 
 
 
 
 
 
129
  h, s, v = hsv_pixels.T
130
  h = h * 2 # Convert to 0-360 range
131
  s = s / 255
132
  v = v / 255
133
 
134
+ # Create masks for vegetation and terrain
135
  vegetation_mask = (h >= 40) & (h <= 150) & (s >= 0.15)
136
+ terrain_mask = (h >= 10) & (h <= 30) & (s >= 0.15)
137
 
138
+ # Building mask (everything that's not another category)
139
+ building_mask = ~(shadow_mask | water_mask | road_mask | vegetation_mask | terrain_mask)
140
+
141
+ # Apply all masks to create final output
142
+ output_flat = output.reshape(-1, 3)
143
+ output_flat[shadow_mask] = self.colors['black']
144
+ output_flat[water_mask] = self.colors['blue']
145
+ output_flat[road_mask] = self.colors['gray']
146
  output_flat[vegetation_mask] = self.colors['green']
147
+ output_flat[terrain_mask] = self.colors['brown']
148
  output_flat[building_mask] = self.colors['white']
149
 
150
+ # Reshape back to image dimensions
151
+ segmented = output.reshape(height, width, 3)
152
+
153
+ # Handle isolated building pixels using GPU operations
154
+ kernel = cp.ones((3, 3), dtype=bool)
155
+ kernel[1, 1] = False
156
+
157
+ # For each color except white (buildings)
158
+ for color_name, color_value in self.colors.items():
159
+ if cp.array_equal(color_value, self.colors['white']):
160
+ continue
161
+
162
+ # Create mask for current color
163
+ color_mask = cp.all(segmented == color_value, axis=2)
164
+
165
+ # Dilate the mask
166
+ dilated = binary_dilation(color_mask, structure=kernel)
167
+
168
+ # Find building pixels that are mostly surrounded by this color
169
+ building_pixels = cp.all(segmented == self.colors['white'], axis=2)
170
+ surrounded = dilated & building_pixels
171
+
172
+ # Update those pixels to the current color
173
+ segmented[surrounded] = color_value
174
+
175
+ return segmented
176
 
177
  def estimate_heights_gpu(self, img, segmented):
178
  """GPU-accelerated height estimation"""