| """ |
| EcoPulse: Region Comparison Tool |
| Compares greenery percentages between two different satellite images and generates a visual report. |
| """ |
| import argparse |
| import os |
| import cv2 |
| import numpy as np |
| import matplotlib.pyplot as plt |
| import pandas as pd |
| from src.pipeline import EcoPulsePipeline |
| from src.visualization import create_greenery_overlay |
|
|
| def compare_regions(config_path, image_a_path, image_b_path): |
| |
| for label, path in [("image_a", image_a_path), ("image_b", image_b_path)]: |
| if not os.path.exists(path): |
| raise FileNotFoundError(f"{label} not found: {path}") |
|
|
| print("=" * 60) |
| print(" EcoPulse: Region Comparison") |
| print("=" * 60) |
|
|
| pipeline = EcoPulsePipeline(config_path) |
| |
| print(f"\nProcessing Area A: {os.path.basename(image_a_path)}...") |
| img_a, results_a = pipeline.process_image(image_a_path) |
| |
| print(f"Processing Area B: {os.path.basename(image_b_path)}...") |
| img_b, results_b = pipeline.process_image(image_b_path) |
| |
| pct_a = results_a['greenery_percentage'] |
| pct_b = results_b['greenery_percentage'] |
| |
| diff = pct_a - pct_b |
| more_green = "Area A" if diff > 0 else "Area B" |
| |
| print(f"\n{'='*60}") |
| print(f" COMPARISON SUMMARY") |
| print(f"{'='*60}") |
| print(f" Area A Greenery: {pct_a:.2f}%") |
| print(f" Area B Greenery: {pct_b:.2f}%") |
| print(f" Difference: {abs(diff):.2f}%") |
| print(f" Result: {more_green} is more vegetated.") |
| print(f"{'='*60}\n") |
| |
| |
| output_dir = pipeline.config['paths']['output_figures'] |
| os.makedirs(output_dir, exist_ok=True) |
| |
| comp_a, _ = create_greenery_overlay(img_a, results_a['mask_classifications']) |
| comp_b, _ = create_greenery_overlay(img_b, results_b['mask_classifications']) |
| |
| fig, axes = plt.subplots(1, 2, figsize=(16, 8)) |
| |
| axes[0].imshow(comp_a) |
| axes[0].set_title(f"Area A: {pct_a:.1f}% Greenery", fontsize=16) |
| axes[0].axis('off') |
| |
| axes[1].imshow(comp_b) |
| axes[1].set_title(f"Area B: {pct_b:.1f}% Greenery", fontsize=16) |
| axes[1].axis('off') |
| |
| fig.suptitle(f"EcoPulse Region Comparison\n{more_green} is more vegetated by {abs(diff):.1f}%", fontsize=20) |
| plt.tight_layout() |
| |
| vis_path = os.path.join(output_dir, "region_comparison.png") |
| plt.savefig(vis_path, bbox_inches='tight', dpi=150) |
| plt.close() |
| |
| |
| report_path = os.path.join(output_dir, "region_comparison_report.csv") |
| df = pd.DataFrame({ |
| 'Region': ['Area A', 'Area B'], |
| 'Image': [image_a_path, image_b_path], |
| 'Greenery_Percentage': [pct_a, pct_b] |
| }) |
| df.to_csv(report_path, index=False) |
| |
| print(f"Visual Report saved to: {vis_path}") |
| print(f"Data Report saved to: {report_path}") |
|
|
| if __name__ == "__main__": |
| parser = argparse.ArgumentParser(description="Compare greenery between two images") |
| parser.add_argument("--config", default="config/config.yaml", help="Path to config") |
| parser.add_argument("--image_a", required=True, help="Path to first image") |
| parser.add_argument("--image_b", required=True, help="Path to second image") |
| args = parser.parse_args() |
| |
| compare_regions(args.config, args.image_a, args.image_b) |
|
|