Spaces:
Sleeping
Sleeping
Commit ·
8d484eb
1
Parent(s): 3d4e00e
Add vegetation and structural sub-classification for detected changes
Browse files- app/detection_engine.py +370 -22
- app/main.py +2 -0
- static/js/app.js +2 -0
- templates/index.html +3 -2
app/detection_engine.py
CHANGED
|
@@ -446,23 +446,26 @@ def visualize_changes(img1, img2, change_mask, regions=None):
|
|
| 446 |
x, y, w, h = r["bbox"]
|
| 447 |
cv2.rectangle(overlay_uint8, (x, y), (x + w, y + h), (255, 255, 255), 1)
|
| 448 |
|
| 449 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 450 |
stories = r.get("estimated_stories")
|
| 451 |
stage = r.get("construction_stage")
|
| 452 |
-
if stories is not None
|
| 453 |
-
parts
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
|
| 457 |
-
|
| 458 |
label = " | ".join(parts)
|
| 459 |
font = cv2.FONT_HERSHEY_SIMPLEX
|
| 460 |
-
font_scale = max(0.
|
| 461 |
thickness = 1
|
| 462 |
(tw, th), _ = cv2.getTextSize(label, font, font_scale, thickness)
|
| 463 |
lx = x
|
| 464 |
ly = max(th + 4, y - 6)
|
| 465 |
-
# Background rectangle for readability
|
| 466 |
cv2.rectangle(overlay_uint8, (lx, ly - th - 4), (lx + tw + 6, ly + 2),
|
| 467 |
(0, 0, 0), cv2.FILLED)
|
| 468 |
cv2.putText(overlay_uint8, label, (lx + 3, ly - 2), font,
|
|
@@ -781,7 +784,336 @@ def classify_with_ensemble(image_region, bbox, num_sub=4):
|
|
| 781 |
|
| 782 |
|
| 783 |
# ---------------------------------------------------------------------------
|
| 784 |
-
# 11.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 785 |
# ---------------------------------------------------------------------------
|
| 786 |
|
| 787 |
_BUILDING_TYPES = {"New Construction/Building", "Demolition/Clearing"}
|
|
@@ -1005,7 +1337,7 @@ def analyze_building_3d(before_img, after_img, region, features):
|
|
| 1005 |
|
| 1006 |
|
| 1007 |
# ---------------------------------------------------------------------------
|
| 1008 |
-
#
|
| 1009 |
# ---------------------------------------------------------------------------
|
| 1010 |
|
| 1011 |
def analyze_change_regions(change_mask, image, min_area=200, use_ensemble=True,
|
|
@@ -1046,21 +1378,37 @@ def analyze_change_regions(change_mask, image, min_area=200, use_ensemble=True,
|
|
| 1046 |
"center": (int(cx), int(cy)),
|
| 1047 |
"object_type": object_type,
|
| 1048 |
"confidence": confidence,
|
|
|
|
|
|
|
| 1049 |
"estimated_stories": None,
|
| 1050 |
"estimated_height_m": None,
|
| 1051 |
"construction_stage": None,
|
| 1052 |
}
|
| 1053 |
|
| 1054 |
-
# 3D analysis
|
| 1055 |
-
if
|
| 1056 |
-
|
| 1057 |
-
|
| 1058 |
-
|
| 1059 |
-
|
| 1060 |
-
|
| 1061 |
-
|
| 1062 |
-
|
| 1063 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1064 |
|
| 1065 |
change_regions.append(region)
|
| 1066 |
|
|
@@ -1069,7 +1417,7 @@ def analyze_change_regions(change_mask, image, min_area=200, use_ensemble=True,
|
|
| 1069 |
|
| 1070 |
|
| 1071 |
# ---------------------------------------------------------------------------
|
| 1072 |
-
#
|
| 1073 |
# ---------------------------------------------------------------------------
|
| 1074 |
|
| 1075 |
def run_detection(before_pil, after_pil, method="AI-Based Deep Learning",
|
|
|
|
| 446 |
x, y, w, h = r["bbox"]
|
| 447 |
cv2.rectangle(overlay_uint8, (x, y), (x + w, y + h), (255, 255, 255), 1)
|
| 448 |
|
| 449 |
+
# Build annotation label from sub-type and 3D info
|
| 450 |
+
parts = []
|
| 451 |
+
sub = r.get("sub_type")
|
| 452 |
+
if sub:
|
| 453 |
+
parts.append(sub)
|
| 454 |
stories = r.get("estimated_stories")
|
| 455 |
stage = r.get("construction_stage")
|
| 456 |
+
if stories is not None:
|
| 457 |
+
parts.append(f"{stories}F")
|
| 458 |
+
if stage and stage != "Unknown":
|
| 459 |
+
parts.append(stage)
|
| 460 |
+
|
| 461 |
+
if parts:
|
| 462 |
label = " | ".join(parts)
|
| 463 |
font = cv2.FONT_HERSHEY_SIMPLEX
|
| 464 |
+
font_scale = max(0.30, min(0.50, w / 220))
|
| 465 |
thickness = 1
|
| 466 |
(tw, th), _ = cv2.getTextSize(label, font, font_scale, thickness)
|
| 467 |
lx = x
|
| 468 |
ly = max(th + 4, y - 6)
|
|
|
|
| 469 |
cv2.rectangle(overlay_uint8, (lx, ly - th - 4), (lx + tw + 6, ly + 2),
|
| 470 |
(0, 0, 0), cv2.FILLED)
|
| 471 |
cv2.putText(overlay_uint8, label, (lx + 3, ly - 2), font,
|
|
|
|
| 784 |
|
| 785 |
|
| 786 |
# ---------------------------------------------------------------------------
|
| 787 |
+
# 11. Vegetation sub-classification
|
| 788 |
+
# ---------------------------------------------------------------------------
|
| 789 |
+
|
| 790 |
+
_VEGETATION_TYPES = {"Vegetation Change"}
|
| 791 |
+
|
| 792 |
+
|
| 793 |
+
def _compute_region_greenness(crop):
|
| 794 |
+
"""Return (ndvi, green_ratio, mean_saturation) for an RGB crop."""
|
| 795 |
+
if crop.size == 0 or crop.shape[0] < 2 or crop.shape[1] < 2:
|
| 796 |
+
return 0.0, 0.0, 0.0
|
| 797 |
+
mean_rgb = np.mean(crop, axis=(0, 1)).astype(np.float64)
|
| 798 |
+
total = np.sum(mean_rgb) + 1e-6
|
| 799 |
+
green_ratio = mean_rgb[1] / total
|
| 800 |
+
ndvi = (mean_rgb[1] - mean_rgb[0]) / (mean_rgb[1] + mean_rgb[0] + 1e-6)
|
| 801 |
+
hsv = cv2.cvtColor(crop, cv2.COLOR_RGB2HSV)
|
| 802 |
+
mean_sat = float(np.mean(hsv[:, :, 1]))
|
| 803 |
+
return float(ndvi), float(green_ratio), mean_sat
|
| 804 |
+
|
| 805 |
+
|
| 806 |
+
def _compute_texture_regularity(gray_crop):
|
| 807 |
+
"""Measure how regular/grid-like the texture is (low entropy = regular crops)."""
|
| 808 |
+
if gray_crop.size == 0 or gray_crop.shape[0] < 3 or gray_crop.shape[1] < 3:
|
| 809 |
+
return 3.0
|
| 810 |
+
gx = cv2.Sobel(gray_crop.astype(np.float32), cv2.CV_64F, 1, 0, ksize=3)
|
| 811 |
+
gy = cv2.Sobel(gray_crop.astype(np.float32), cv2.CV_64F, 0, 1, ksize=3)
|
| 812 |
+
angles = np.arctan2(gy, gx + 1e-8)
|
| 813 |
+
hist, _ = np.histogram(angles, bins=8, range=(-np.pi, np.pi))
|
| 814 |
+
hist = hist / (hist.sum() + 1e-8)
|
| 815 |
+
entropy = -np.sum(hist[hist > 0] * np.log2(hist[hist > 0] + 1e-10))
|
| 816 |
+
return float(entropy)
|
| 817 |
+
|
| 818 |
+
|
| 819 |
+
def classify_vegetation_subtype(before_img, after_img, bbox):
|
| 820 |
+
"""
|
| 821 |
+
Compare before/after crops to determine vegetation change sub-type.
|
| 822 |
+
Returns (subtype_name, confidence).
|
| 823 |
+
"""
|
| 824 |
+
x, y, w, h = bbox
|
| 825 |
+
pad = 5
|
| 826 |
+
y1, y2 = max(0, y - pad), min(before_img.shape[0], y + h + pad)
|
| 827 |
+
x1, x2 = max(0, x - pad), min(before_img.shape[1], x + w + pad)
|
| 828 |
+
|
| 829 |
+
before_crop = before_img[y1:y2, x1:x2]
|
| 830 |
+
after_crop = after_img[y1:y2, x1:x2]
|
| 831 |
+
|
| 832 |
+
if before_crop.size == 0 or after_crop.size == 0:
|
| 833 |
+
return "Vegetation Change", 0.3
|
| 834 |
+
|
| 835 |
+
ndvi_b, green_b, sat_b = _compute_region_greenness(before_crop)
|
| 836 |
+
ndvi_a, green_a, sat_a = _compute_region_greenness(after_crop)
|
| 837 |
+
|
| 838 |
+
gray_b = cv2.cvtColor(before_crop, cv2.COLOR_RGB2GRAY)
|
| 839 |
+
gray_a = cv2.cvtColor(after_crop, cv2.COLOR_RGB2GRAY)
|
| 840 |
+
tex_entropy_b = _compute_texture_regularity(gray_b)
|
| 841 |
+
tex_entropy_a = _compute_texture_regularity(gray_a)
|
| 842 |
+
|
| 843 |
+
brightness_b = float(np.mean(gray_b))
|
| 844 |
+
brightness_a = float(np.mean(gray_a))
|
| 845 |
+
|
| 846 |
+
ndvi_delta = ndvi_a - ndvi_b
|
| 847 |
+
green_delta = green_a - green_b
|
| 848 |
+
sat_delta = sat_a - sat_b
|
| 849 |
+
|
| 850 |
+
scores = {
|
| 851 |
+
"Deforestation/Tree Removal": 0.0,
|
| 852 |
+
"New Vegetation/Growth": 0.0,
|
| 853 |
+
"Crop/Agricultural Change": 0.0,
|
| 854 |
+
"Vegetation Health Decline": 0.0,
|
| 855 |
+
"Seasonal Variation": 0.0,
|
| 856 |
+
}
|
| 857 |
+
|
| 858 |
+
# --- Deforestation: was green, now not green ---
|
| 859 |
+
if ndvi_b > 0.08 and ndvi_delta < -0.06:
|
| 860 |
+
scores["Deforestation/Tree Removal"] += 0.30
|
| 861 |
+
if green_b > 0.36 and green_delta < -0.03:
|
| 862 |
+
scores["Deforestation/Tree Removal"] += 0.20
|
| 863 |
+
if brightness_a > brightness_b + 10:
|
| 864 |
+
scores["Deforestation/Tree Removal"] += 0.15
|
| 865 |
+
if sat_delta < -15:
|
| 866 |
+
scores["Deforestation/Tree Removal"] += 0.15
|
| 867 |
+
if tex_entropy_a < tex_entropy_b - 0.3:
|
| 868 |
+
scores["Deforestation/Tree Removal"] += 0.10
|
| 869 |
+
|
| 870 |
+
# --- New Vegetation/Growth: was bare, now green ---
|
| 871 |
+
if ndvi_a > 0.08 and ndvi_delta > 0.06:
|
| 872 |
+
scores["New Vegetation/Growth"] += 0.30
|
| 873 |
+
if green_a > 0.36 and green_delta > 0.03:
|
| 874 |
+
scores["New Vegetation/Growth"] += 0.20
|
| 875 |
+
if sat_delta > 15:
|
| 876 |
+
scores["New Vegetation/Growth"] += 0.15
|
| 877 |
+
if brightness_a < brightness_b - 5:
|
| 878 |
+
scores["New Vegetation/Growth"] += 0.10
|
| 879 |
+
if tex_entropy_a > tex_entropy_b + 0.2:
|
| 880 |
+
scores["New Vegetation/Growth"] += 0.10
|
| 881 |
+
|
| 882 |
+
# --- Crop/Agricultural Change: regular texture patterns, moderate color shift ---
|
| 883 |
+
is_regular = tex_entropy_b < 2.5 or tex_entropy_a < 2.5
|
| 884 |
+
if is_regular:
|
| 885 |
+
scores["Crop/Agricultural Change"] += 0.25
|
| 886 |
+
if 0.03 < abs(ndvi_delta) < 0.12:
|
| 887 |
+
scores["Crop/Agricultural Change"] += 0.20
|
| 888 |
+
if sat_b > 35 and sat_a > 35:
|
| 889 |
+
scores["Crop/Agricultural Change"] += 0.15
|
| 890 |
+
if abs(green_delta) < 0.04 and abs(ndvi_delta) > 0.02:
|
| 891 |
+
scores["Crop/Agricultural Change"] += 0.15
|
| 892 |
+
area = w * h
|
| 893 |
+
if area > 3000:
|
| 894 |
+
scores["Crop/Agricultural Change"] += 0.10
|
| 895 |
+
|
| 896 |
+
# --- Vegetation Health Decline: still green but browning ---
|
| 897 |
+
if ndvi_b > 0.05 and ndvi_a > 0.02 and ndvi_delta < -0.03:
|
| 898 |
+
scores["Vegetation Health Decline"] += 0.25
|
| 899 |
+
if green_b > 0.34 and green_a > 0.30 and green_delta < -0.02:
|
| 900 |
+
scores["Vegetation Health Decline"] += 0.20
|
| 901 |
+
if -20 < sat_delta < -3:
|
| 902 |
+
scores["Vegetation Health Decline"] += 0.20
|
| 903 |
+
if abs(brightness_a - brightness_b) < 15:
|
| 904 |
+
scores["Vegetation Health Decline"] += 0.10
|
| 905 |
+
|
| 906 |
+
# --- Seasonal Variation: mild shift in color/texture, both sides green ---
|
| 907 |
+
if ndvi_b > 0.04 and ndvi_a > 0.04 and abs(ndvi_delta) < 0.05:
|
| 908 |
+
scores["Seasonal Variation"] += 0.25
|
| 909 |
+
if abs(green_delta) < 0.03:
|
| 910 |
+
scores["Seasonal Variation"] += 0.20
|
| 911 |
+
if abs(sat_delta) < 12:
|
| 912 |
+
scores["Seasonal Variation"] += 0.15
|
| 913 |
+
if abs(brightness_a - brightness_b) < 12:
|
| 914 |
+
scores["Seasonal Variation"] += 0.15
|
| 915 |
+
|
| 916 |
+
best = max(scores, key=scores.get)
|
| 917 |
+
conf = scores[best]
|
| 918 |
+
if conf < 0.25:
|
| 919 |
+
return "Vegetation Change", 0.3
|
| 920 |
+
return best, min(conf, 1.0)
|
| 921 |
+
|
| 922 |
+
|
| 923 |
+
# ---------------------------------------------------------------------------
|
| 924 |
+
# 12. Structural change sub-classification
|
| 925 |
+
# ---------------------------------------------------------------------------
|
| 926 |
+
|
| 927 |
+
_STRUCTURAL_TYPES = {"New Construction/Building", "Demolition/Clearing",
|
| 928 |
+
"Road/Pavement Change"}
|
| 929 |
+
|
| 930 |
+
|
| 931 |
+
def _region_has_structure(crop):
|
| 932 |
+
"""Heuristic: does this crop contain building-like structure (edges + regularity)?"""
|
| 933 |
+
if crop.size == 0 or crop.shape[0] < 3 or crop.shape[1] < 3:
|
| 934 |
+
return False, 0.0, 0.0
|
| 935 |
+
gray = cv2.cvtColor(crop, cv2.COLOR_RGB2GRAY).astype(np.float32)
|
| 936 |
+
gx = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=3)
|
| 937 |
+
gy = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=3)
|
| 938 |
+
edge_density = float(np.mean(np.sqrt(gx**2 + gy**2)))
|
| 939 |
+
angles = np.arctan2(gy, gx + 1e-8)
|
| 940 |
+
hist, _ = np.histogram(angles, bins=8, range=(-np.pi, np.pi))
|
| 941 |
+
hist = hist / (hist.sum() + 1e-8)
|
| 942 |
+
entropy = -np.sum(hist[hist > 0] * np.log2(hist[hist > 0] + 1e-10))
|
| 943 |
+
has_structure = edge_density > 25 and entropy < 2.8
|
| 944 |
+
return has_structure, edge_density, entropy
|
| 945 |
+
|
| 946 |
+
|
| 947 |
+
def classify_structural_subtype(before_img, after_img, bbox, main_type):
|
| 948 |
+
"""
|
| 949 |
+
Compare before/after crops to determine structural change sub-type.
|
| 950 |
+
Returns (subtype_name, confidence).
|
| 951 |
+
"""
|
| 952 |
+
x, y, w, h = bbox
|
| 953 |
+
pad = 5
|
| 954 |
+
y1, y2 = max(0, y - pad), min(before_img.shape[0], y + h + pad)
|
| 955 |
+
x1, x2 = max(0, x - pad), min(before_img.shape[1], x + w + pad)
|
| 956 |
+
|
| 957 |
+
before_crop = before_img[y1:y2, x1:x2]
|
| 958 |
+
after_crop = after_img[y1:y2, x1:x2]
|
| 959 |
+
|
| 960 |
+
if before_crop.size == 0 or after_crop.size == 0:
|
| 961 |
+
return main_type, 0.3
|
| 962 |
+
|
| 963 |
+
struct_b, edge_b, ent_b = _region_has_structure(before_crop)
|
| 964 |
+
struct_a, edge_a, ent_a = _region_has_structure(after_crop)
|
| 965 |
+
|
| 966 |
+
gray_b = cv2.cvtColor(before_crop, cv2.COLOR_RGB2GRAY)
|
| 967 |
+
gray_a = cv2.cvtColor(after_crop, cv2.COLOR_RGB2GRAY)
|
| 968 |
+
brightness_b = float(np.mean(gray_b))
|
| 969 |
+
brightness_a = float(np.mean(gray_a))
|
| 970 |
+
texture_b = float(np.std(gray_b))
|
| 971 |
+
texture_a = float(np.std(gray_a))
|
| 972 |
+
|
| 973 |
+
hsv_b = cv2.cvtColor(before_crop, cv2.COLOR_RGB2HSV)
|
| 974 |
+
hsv_a = cv2.cvtColor(after_crop, cv2.COLOR_RGB2HSV)
|
| 975 |
+
sat_b = float(np.mean(hsv_b[:, :, 1]))
|
| 976 |
+
sat_a = float(np.mean(hsv_a[:, :, 1]))
|
| 977 |
+
|
| 978 |
+
# Check greenness to detect cleared-to-green or green-to-built transitions
|
| 979 |
+
mean_rgb_b = np.mean(before_crop, axis=(0, 1))
|
| 980 |
+
mean_rgb_a = np.mean(after_crop, axis=(0, 1))
|
| 981 |
+
ndvi_b = (mean_rgb_b[1] - mean_rgb_b[0]) / (mean_rgb_b[1] + mean_rgb_b[0] + 1e-6)
|
| 982 |
+
ndvi_a = (mean_rgb_a[1] - mean_rgb_a[0]) / (mean_rgb_a[1] + mean_rgb_a[0] + 1e-6)
|
| 983 |
+
|
| 984 |
+
area = w * h
|
| 985 |
+
|
| 986 |
+
if main_type == "Road/Pavement Change":
|
| 987 |
+
return _classify_road_subtype(
|
| 988 |
+
struct_b, struct_a, edge_b, edge_a, brightness_b, brightness_a,
|
| 989 |
+
texture_b, texture_a, area, w, h
|
| 990 |
+
)
|
| 991 |
+
|
| 992 |
+
scores = {
|
| 993 |
+
"New Building": 0.0,
|
| 994 |
+
"Building Expansion": 0.0,
|
| 995 |
+
"Renovation/Modification": 0.0,
|
| 996 |
+
"Partial Demolition": 0.0,
|
| 997 |
+
"Full Demolition": 0.0,
|
| 998 |
+
"Infrastructure Change": 0.0,
|
| 999 |
+
}
|
| 1000 |
+
|
| 1001 |
+
# --- New Building: before had no structure, after does ---
|
| 1002 |
+
if not struct_b and struct_a:
|
| 1003 |
+
scores["New Building"] += 0.35
|
| 1004 |
+
if edge_a > edge_b + 15:
|
| 1005 |
+
scores["New Building"] += 0.15
|
| 1006 |
+
if ent_a < ent_b - 0.3:
|
| 1007 |
+
scores["New Building"] += 0.10
|
| 1008 |
+
if ndvi_b > 0.05 and ndvi_a < 0.03:
|
| 1009 |
+
scores["New Building"] += 0.10
|
| 1010 |
+
if sat_a < sat_b - 10:
|
| 1011 |
+
scores["New Building"] += 0.10
|
| 1012 |
+
|
| 1013 |
+
# --- Building Expansion: both have structure but after has more ---
|
| 1014 |
+
if struct_b and struct_a:
|
| 1015 |
+
scores["Building Expansion"] += 0.15
|
| 1016 |
+
if struct_b and edge_a > edge_b * 1.2:
|
| 1017 |
+
scores["Building Expansion"] += 0.20
|
| 1018 |
+
if struct_b and texture_a > texture_b + 5:
|
| 1019 |
+
scores["Building Expansion"] += 0.15
|
| 1020 |
+
if abs(ent_a - ent_b) < 0.5 and edge_a > edge_b:
|
| 1021 |
+
scores["Building Expansion"] += 0.15
|
| 1022 |
+
|
| 1023 |
+
# --- Renovation/Modification: both have structure, similar density but different appearance ---
|
| 1024 |
+
if struct_b and struct_a:
|
| 1025 |
+
scores["Renovation/Modification"] += 0.15
|
| 1026 |
+
if abs(edge_a - edge_b) < 12:
|
| 1027 |
+
scores["Renovation/Modification"] += 0.15
|
| 1028 |
+
if abs(brightness_a - brightness_b) > 8:
|
| 1029 |
+
scores["Renovation/Modification"] += 0.20
|
| 1030 |
+
if abs(sat_a - sat_b) > 10:
|
| 1031 |
+
scores["Renovation/Modification"] += 0.15
|
| 1032 |
+
if abs(texture_a - texture_b) < 10:
|
| 1033 |
+
scores["Renovation/Modification"] += 0.10
|
| 1034 |
+
|
| 1035 |
+
# --- Partial Demolition: before had structure, after has less ---
|
| 1036 |
+
if struct_b and edge_a < edge_b * 0.7:
|
| 1037 |
+
scores["Partial Demolition"] += 0.25
|
| 1038 |
+
if struct_b and ent_a > ent_b + 0.3:
|
| 1039 |
+
scores["Partial Demolition"] += 0.15
|
| 1040 |
+
if texture_a > texture_b + 8:
|
| 1041 |
+
scores["Partial Demolition"] += 0.15
|
| 1042 |
+
if brightness_a > brightness_b + 10:
|
| 1043 |
+
scores["Partial Demolition"] += 0.10
|
| 1044 |
+
|
| 1045 |
+
# --- Full Demolition: before had structure, after is bare/empty ---
|
| 1046 |
+
if struct_b and not struct_a:
|
| 1047 |
+
scores["Full Demolition"] += 0.35
|
| 1048 |
+
if edge_b > 30 and edge_a < 20:
|
| 1049 |
+
scores["Full Demolition"] += 0.15
|
| 1050 |
+
if texture_b > 25 and texture_a < 20:
|
| 1051 |
+
scores["Full Demolition"] += 0.15
|
| 1052 |
+
if brightness_a > brightness_b + 15:
|
| 1053 |
+
scores["Full Demolition"] += 0.10
|
| 1054 |
+
|
| 1055 |
+
# --- Infrastructure Change: elongated shape, high edge regularity ---
|
| 1056 |
+
aspect = max(w, h) / max(min(w, h), 1)
|
| 1057 |
+
if aspect > 3.0:
|
| 1058 |
+
scores["Infrastructure Change"] += 0.25
|
| 1059 |
+
if ent_a < 2.0 or ent_b < 2.0:
|
| 1060 |
+
scores["Infrastructure Change"] += 0.15
|
| 1061 |
+
if area > 2000 and aspect > 2.5:
|
| 1062 |
+
scores["Infrastructure Change"] += 0.15
|
| 1063 |
+
|
| 1064 |
+
best = max(scores, key=scores.get)
|
| 1065 |
+
conf = scores[best]
|
| 1066 |
+
if conf < 0.25:
|
| 1067 |
+
return main_type, 0.3
|
| 1068 |
+
return best, min(conf, 1.0)
|
| 1069 |
+
|
| 1070 |
+
|
| 1071 |
+
def _classify_road_subtype(struct_b, struct_a, edge_b, edge_a,
|
| 1072 |
+
brightness_b, brightness_a, texture_b, texture_a,
|
| 1073 |
+
area, w, h):
|
| 1074 |
+
"""Sub-classify road/pavement changes."""
|
| 1075 |
+
scores = {
|
| 1076 |
+
"New Road/Pavement": 0.0,
|
| 1077 |
+
"Road Widening": 0.0,
|
| 1078 |
+
"Road Resurfacing": 0.0,
|
| 1079 |
+
"Road Deterioration": 0.0,
|
| 1080 |
+
}
|
| 1081 |
+
|
| 1082 |
+
if not struct_b and struct_a:
|
| 1083 |
+
scores["New Road/Pavement"] += 0.30
|
| 1084 |
+
if edge_a > edge_b + 10:
|
| 1085 |
+
scores["New Road/Pavement"] += 0.20
|
| 1086 |
+
if brightness_a < brightness_b:
|
| 1087 |
+
scores["New Road/Pavement"] += 0.15
|
| 1088 |
+
|
| 1089 |
+
if struct_b and struct_a and edge_a > edge_b * 1.15:
|
| 1090 |
+
scores["Road Widening"] += 0.30
|
| 1091 |
+
if area > 2000:
|
| 1092 |
+
scores["Road Widening"] += 0.15
|
| 1093 |
+
|
| 1094 |
+
if struct_b and struct_a and abs(edge_a - edge_b) < 10:
|
| 1095 |
+
scores["Road Resurfacing"] += 0.20
|
| 1096 |
+
if abs(brightness_a - brightness_b) > 12:
|
| 1097 |
+
scores["Road Resurfacing"] += 0.25
|
| 1098 |
+
if abs(texture_a - texture_b) < 8:
|
| 1099 |
+
scores["Road Resurfacing"] += 0.15
|
| 1100 |
+
|
| 1101 |
+
if texture_a > texture_b + 10:
|
| 1102 |
+
scores["Road Deterioration"] += 0.25
|
| 1103 |
+
if edge_a < edge_b - 5:
|
| 1104 |
+
scores["Road Deterioration"] += 0.20
|
| 1105 |
+
if brightness_a > brightness_b + 8:
|
| 1106 |
+
scores["Road Deterioration"] += 0.15
|
| 1107 |
+
|
| 1108 |
+
best = max(scores, key=scores.get)
|
| 1109 |
+
conf = scores[best]
|
| 1110 |
+
if conf < 0.25:
|
| 1111 |
+
return "Road/Pavement Change", 0.3
|
| 1112 |
+
return best, min(conf, 1.0)
|
| 1113 |
+
|
| 1114 |
+
|
| 1115 |
+
# ---------------------------------------------------------------------------
|
| 1116 |
+
# 13. 3D Building Analysis — height estimation + construction stage
|
| 1117 |
# ---------------------------------------------------------------------------
|
| 1118 |
|
| 1119 |
_BUILDING_TYPES = {"New Construction/Building", "Demolition/Clearing"}
|
|
|
|
| 1337 |
|
| 1338 |
|
| 1339 |
# ---------------------------------------------------------------------------
|
| 1340 |
+
# 14. Region analysis
|
| 1341 |
# ---------------------------------------------------------------------------
|
| 1342 |
|
| 1343 |
def analyze_change_regions(change_mask, image, min_area=200, use_ensemble=True,
|
|
|
|
| 1378 |
"center": (int(cx), int(cy)),
|
| 1379 |
"object_type": object_type,
|
| 1380 |
"confidence": confidence,
|
| 1381 |
+
"sub_type": None,
|
| 1382 |
+
"sub_type_confidence": None,
|
| 1383 |
"estimated_stories": None,
|
| 1384 |
"estimated_height_m": None,
|
| 1385 |
"construction_stage": None,
|
| 1386 |
}
|
| 1387 |
|
| 1388 |
+
# Sub-classification and 3D analysis require before image
|
| 1389 |
+
if before_img is not None:
|
| 1390 |
+
if object_type in _VEGETATION_TYPES:
|
| 1391 |
+
sub, sub_conf = classify_vegetation_subtype(
|
| 1392 |
+
before_img, image, (x, y, w, h))
|
| 1393 |
+
region["sub_type"] = sub
|
| 1394 |
+
region["sub_type_confidence"] = sub_conf
|
| 1395 |
+
|
| 1396 |
+
elif object_type in _STRUCTURAL_TYPES:
|
| 1397 |
+
sub, sub_conf = classify_structural_subtype(
|
| 1398 |
+
before_img, image, (x, y, w, h), object_type)
|
| 1399 |
+
region["sub_type"] = sub
|
| 1400 |
+
region["sub_type_confidence"] = sub_conf
|
| 1401 |
+
|
| 1402 |
+
# 3D analysis for building/construction regions
|
| 1403 |
+
if object_type in _BUILDING_TYPES:
|
| 1404 |
+
pad = 5
|
| 1405 |
+
ry1 = max(0, y - pad)
|
| 1406 |
+
ry2 = min(image.shape[0], y + h + pad)
|
| 1407 |
+
rx1 = max(0, x - pad)
|
| 1408 |
+
rx2 = min(image.shape[1], x + w + pad)
|
| 1409 |
+
crop = image[ry1:ry2, rx1:rx2]
|
| 1410 |
+
feats = extract_advanced_features(crop) if crop.size > 0 else None
|
| 1411 |
+
analyze_building_3d(before_img, image, region, feats)
|
| 1412 |
|
| 1413 |
change_regions.append(region)
|
| 1414 |
|
|
|
|
| 1417 |
|
| 1418 |
|
| 1419 |
# ---------------------------------------------------------------------------
|
| 1420 |
+
# 15. Main pipeline
|
| 1421 |
# ---------------------------------------------------------------------------
|
| 1422 |
|
| 1423 |
def run_detection(before_pil, after_pil, method="AI-Based Deep Learning",
|
app/main.py
CHANGED
|
@@ -205,6 +205,8 @@ async def detect(
|
|
| 205 |
"bbox": {"x": int(r["bbox"][0]), "y": int(r["bbox"][1]), "w": int(r["bbox"][2]), "h": int(r["bbox"][3])},
|
| 206 |
"objectType": str(r["object_type"]),
|
| 207 |
"confidence": float(r["confidence"]),
|
|
|
|
|
|
|
| 208 |
"estimatedStories": r.get("estimated_stories"),
|
| 209 |
"estimatedHeightM": float(r["estimated_height_m"]) if r.get("estimated_height_m") is not None else None,
|
| 210 |
"constructionStage": r.get("construction_stage"),
|
|
|
|
| 205 |
"bbox": {"x": int(r["bbox"][0]), "y": int(r["bbox"][1]), "w": int(r["bbox"][2]), "h": int(r["bbox"][3])},
|
| 206 |
"objectType": str(r["object_type"]),
|
| 207 |
"confidence": float(r["confidence"]),
|
| 208 |
+
"subType": r.get("sub_type"),
|
| 209 |
+
"subTypeConfidence": float(r["sub_type_confidence"]) if r.get("sub_type_confidence") is not None else None,
|
| 210 |
"estimatedStories": r.get("estimated_stories"),
|
| 211 |
"estimatedHeightM": float(r["estimated_height_m"]) if r.get("estimated_height_m") is not None else None,
|
| 212 |
"constructionStage": r.get("construction_stage"),
|
static/js/app.js
CHANGED
|
@@ -262,12 +262,14 @@ function showResult(data) {
|
|
| 262 |
tbody.innerHTML = '';
|
| 263 |
(data.regions || []).slice(0, 50).forEach((r) => {
|
| 264 |
const tr = document.createElement('tr');
|
|
|
|
| 265 |
const stories = r.estimatedStories != null ? r.estimatedStories : '—';
|
| 266 |
const height = r.estimatedHeightM != null ? r.estimatedHeightM + ' m' : '—';
|
| 267 |
const stage = r.constructionStage && r.constructionStage !== 'Unknown' ? r.constructionStage : '—';
|
| 268 |
tr.innerHTML = `
|
| 269 |
<td>${r.id}</td>
|
| 270 |
<td>${r.objectType}</td>
|
|
|
|
| 271 |
<td>${(r.confidence * 100).toFixed(1)}%</td>
|
| 272 |
<td>${r.area.toLocaleString()}</td>
|
| 273 |
<td>${stories}</td>
|
|
|
|
| 262 |
tbody.innerHTML = '';
|
| 263 |
(data.regions || []).slice(0, 50).forEach((r) => {
|
| 264 |
const tr = document.createElement('tr');
|
| 265 |
+
const subType = r.subType || '—';
|
| 266 |
const stories = r.estimatedStories != null ? r.estimatedStories : '—';
|
| 267 |
const height = r.estimatedHeightM != null ? r.estimatedHeightM + ' m' : '—';
|
| 268 |
const stage = r.constructionStage && r.constructionStage !== 'Unknown' ? r.constructionStage : '—';
|
| 269 |
tr.innerHTML = `
|
| 270 |
<td>${r.id}</td>
|
| 271 |
<td>${r.objectType}</td>
|
| 272 |
+
<td>${subType}</td>
|
| 273 |
<td>${(r.confidence * 100).toFixed(1)}%</td>
|
| 274 |
<td>${r.area.toLocaleString()}</td>
|
| 275 |
<td>${stories}</td>
|
templates/index.html
CHANGED
|
@@ -250,7 +250,8 @@
|
|
| 250 |
<thead>
|
| 251 |
<tr>
|
| 252 |
<th>#</th>
|
| 253 |
-
<th>
|
|
|
|
| 254 |
<th>Confidence</th>
|
| 255 |
<th>Area (px)</th>
|
| 256 |
<th>Stories</th>
|
|
@@ -291,6 +292,6 @@
|
|
| 291 |
</div>
|
| 292 |
</div>
|
| 293 |
|
| 294 |
-
<script src="/static/js/app.js?v=
|
| 295 |
</body>
|
| 296 |
</html>
|
|
|
|
| 250 |
<thead>
|
| 251 |
<tr>
|
| 252 |
<th>#</th>
|
| 253 |
+
<th>Change Type</th>
|
| 254 |
+
<th>Sub-Type</th>
|
| 255 |
<th>Confidence</th>
|
| 256 |
<th>Area (px)</th>
|
| 257 |
<th>Stories</th>
|
|
|
|
| 292 |
</div>
|
| 293 |
</div>
|
| 294 |
|
| 295 |
+
<script src="/static/js/app.js?v=12"></script>
|
| 296 |
</body>
|
| 297 |
</html>
|