DynamicPacific commited on
Commit
dbdc03c
·
1 Parent(s): 961360f

Revert to original GeoTIFF-only implementation

Browse files

- Removed multi-format image support (PNG, JPG, etc.)
- Restored original process_geotiff_file function
- Restored GeoTIFF-only file input
- Restored original advanced_extraction.py implementation
- Back to simple, working GeoTIFF tree detection

Files changed (2) hide show
  1. app.py +25 -45
  2. utils/advanced_extraction.py +1 -1
app.py CHANGED
@@ -154,59 +154,44 @@ def create_split_view_map(geojson_data, bounds):
154
  m = folium.Map(location=[40.7, -74.0], zoom_start=10)
155
  return m
156
 
157
- def process_image_file(image_file):
158
- """Process uploaded image file for tree detection."""
159
- if image_file is None:
160
- return None, "Please upload an image file or use the example file"
161
 
162
  try:
163
  # Create unique ID
164
  unique_id = str(uuid.uuid4().hex)[:8]
165
 
166
  # Handle file upload
167
- if hasattr(image_file, 'name'):
168
- filename = os.path.basename(image_file.name)
169
  else:
170
- filename = os.path.basename(image_file)
171
 
172
  # Save uploaded file
173
- image_path = os.path.join(TEMP_DIRS['uploads'], f"{unique_id}_{filename}")
174
 
175
- if hasattr(image_file, 'read'):
176
- file_content = image_file.read()
177
- with open(image_path, "wb") as f:
178
  f.write(file_content)
179
  else:
180
- shutil.copy(image_file, image_path)
181
 
182
- logger.info(f"File saved to {image_path}")
183
 
184
- # Check if it's a GeoTIFF file for advanced processing
185
- if filename.lower().endswith(('.tif', '.tiff')):
186
- # Use advanced extraction for GeoTIFF files
187
- from utils.advanced_extraction import extract_features_from_geotiff
188
-
189
- logger.info("Extracting tree features from GeoTIFF...")
190
- geojson_data = extract_features_from_geotiff(image_path, TEMP_DIRS['processed'], "trees")
191
- else:
192
- # Use general image processing for other formats
193
- from utils.geospatial import process_image_to_geojson
194
- from utils.image_processing import process_image
195
-
196
- logger.info("Processing regular image for tree detection...")
197
- processed_image_path = process_image(image_path, TEMP_DIRS['processed'])
198
- geojson_data = process_image_to_geojson(processed_image_path, feature_type="trees", original_file_path=image_path)
199
 
200
  if not geojson_data or not geojson_data.get('features'):
201
  return None, "No trees detected in the image"
202
 
203
  # Get bounds and create map
204
- if filename.lower().endswith(('.tif', '.tiff')):
205
- bounds = get_bounds_from_geotiff(image_path)
206
- else:
207
- # For regular images, use default bounds or extract from metadata
208
- bounds = get_bounds_from_geotiff(image_path) # This will use defaults for non-GeoTIFF files
209
-
210
  map_obj = create_split_view_map(geojson_data, bounds)
211
 
212
  if map_obj:
@@ -304,7 +289,7 @@ def create_gradio_interface():
304
  gr.HTML("""
305
  <div style="text-align: center; padding: 20px; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); border-radius: 10px; margin-bottom: 20px;">
306
  <h1 style="color: white; margin: 0; font-size: 2.5em;">🌲 ForestAI</h1>
307
- <p style="color: white; margin: 10px 0 0 0; font-size: 1.2em;">Tree Detection from Satellite & Aerial Imagery</p>
308
  </div>
309
  """)
310
 
@@ -313,8 +298,8 @@ def create_gradio_interface():
313
  gr.Markdown("### Upload GeoTIFF File")
314
 
315
  file_input = gr.File(
316
- label="Select Image File",
317
- file_types=[".tif", ".tiff", ".png", ".jpg", ".jpeg", ".bmp", ".gif"],
318
  type="filepath"
319
  )
320
 
@@ -359,7 +344,7 @@ def create_gradio_interface():
359
  display:flex; align-items:center; justify-content:center;
360
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);">
361
  <div style="text-align:center; color:#666;">
362
- <h3>🌲 Upload an image file or use example to see detected trees</h3>
363
  <p>Interactive map will appear here</p>
364
  </div>
365
  </div>
@@ -369,7 +354,7 @@ def create_gradio_interface():
369
 
370
  # Event handlers
371
  analyze_btn.click(
372
- fn=process_image_file,
373
  inputs=[file_input],
374
  outputs=[map_output, status_output],
375
  show_progress=True
@@ -385,16 +370,11 @@ def create_gradio_interface():
385
  # Simple instructions
386
  gr.Markdown("""
387
  ### How to Use:
388
- 1. **Upload** an image file (GeoTIFF, PNG, JPG, etc.) OR click "Use Example File" to try with the included sample
389
  2. **Click** "Detect Trees" to analyze your uploaded image
390
  3. **Explore** the interactive map with detected tree areas
391
  4. **Use** the split-view slider to compare base map and satellite imagery
392
 
393
- ### Supported Formats:
394
- - **GeoTIFF (.tif, .tiff)**: Best for satellite imagery with geographic data
395
- - **Regular Images (.png, .jpg, .jpeg, .bmp, .gif)**: For general image analysis
396
- - **Processing**: GeoTIFF files use advanced NDVI analysis, other formats use general image processing
397
-
398
  ### Map Controls:
399
  - **Split View**: Drag the vertical slider to compare layers
400
  - **Zoom**: Scroll to zoom in/out, drag to pan
 
154
  m = folium.Map(location=[40.7, -74.0], zoom_start=10)
155
  return m
156
 
157
+ def process_geotiff_file(geotiff_file):
158
+ """Process uploaded GeoTIFF file for tree detection."""
159
+ if geotiff_file is None:
160
+ return None, "Please upload a GeoTIFF file or use the example file"
161
 
162
  try:
163
  # Create unique ID
164
  unique_id = str(uuid.uuid4().hex)[:8]
165
 
166
  # Handle file upload
167
+ if hasattr(geotiff_file, 'name'):
168
+ filename = os.path.basename(geotiff_file.name)
169
  else:
170
+ filename = os.path.basename(geotiff_file)
171
 
172
  # Save uploaded file
173
+ geotiff_path = os.path.join(TEMP_DIRS['uploads'], f"{unique_id}_{filename}")
174
 
175
+ if hasattr(geotiff_file, 'read'):
176
+ file_content = geotiff_file.read()
177
+ with open(geotiff_path, "wb") as f:
178
  f.write(file_content)
179
  else:
180
+ shutil.copy(geotiff_file, geotiff_path)
181
 
182
+ logger.info(f"File saved to {geotiff_path}")
183
 
184
+ # Import and extract features
185
+ from utils.advanced_extraction import extract_features_from_geotiff
186
+
187
+ logger.info("Extracting tree features...")
188
+ geojson_data = extract_features_from_geotiff(geotiff_path, TEMP_DIRS['processed'], "trees")
 
 
 
 
 
 
 
 
 
 
189
 
190
  if not geojson_data or not geojson_data.get('features'):
191
  return None, "No trees detected in the image"
192
 
193
  # Get bounds and create map
194
+ bounds = get_bounds_from_geotiff(geotiff_path)
 
 
 
 
 
195
  map_obj = create_split_view_map(geojson_data, bounds)
196
 
197
  if map_obj:
 
289
  gr.HTML("""
290
  <div style="text-align: center; padding: 20px; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); border-radius: 10px; margin-bottom: 20px;">
291
  <h1 style="color: white; margin: 0; font-size: 2.5em;">🌲 ForestAI</h1>
292
+ <p style="color: white; margin: 10px 0 0 0; font-size: 1.2em;">Tree Detection from Satellite Imagery</p>
293
  </div>
294
  """)
295
 
 
298
  gr.Markdown("### Upload GeoTIFF File")
299
 
300
  file_input = gr.File(
301
+ label="Select GeoTIFF File",
302
+ file_types=[".tif", ".tiff"],
303
  type="filepath"
304
  )
305
 
 
344
  display:flex; align-items:center; justify-content:center;
345
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);">
346
  <div style="text-align:center; color:#666;">
347
+ <h3>🌲 Upload a GeoTIFF file or use example to see detected trees</h3>
348
  <p>Interactive map will appear here</p>
349
  </div>
350
  </div>
 
354
 
355
  # Event handlers
356
  analyze_btn.click(
357
+ fn=process_geotiff_file,
358
  inputs=[file_input],
359
  outputs=[map_output, status_output],
360
  show_progress=True
 
370
  # Simple instructions
371
  gr.Markdown("""
372
  ### How to Use:
373
+ 1. **Upload** a GeoTIFF satellite image file OR click "Use Example File" to try with the included sample
374
  2. **Click** "Detect Trees" to analyze your uploaded image
375
  3. **Explore** the interactive map with detected tree areas
376
  4. **Use** the split-view slider to compare base map and satellite imagery
377
 
 
 
 
 
 
378
  ### Map Controls:
379
  - **Split View**: Drag the vertical slider to compare layers
380
  - **Zoom**: Scroll to zoom in/out, drag to pan
utils/advanced_extraction.py CHANGED
@@ -83,4 +83,4 @@ def extract_features_from_geotiff(image_path, output_folder, feature_type="trees
83
 
84
  except Exception as e:
85
  logging.error(f"Error extracting features: {str(e)}")
86
- return {"type": "FeatureCollection", "features": []}
 
83
 
84
  except Exception as e:
85
  logging.error(f"Error extracting features: {str(e)}")
86
+ return {"type": "FeatureCollection", "features": []}