File size: 3,333 Bytes
43abac3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | """
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):
# Validate file paths upfront (before the ~30s model load)
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")
# --- Visualization Generation ---
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()
# Save a small report
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)
|