Upload src/baseline_enhanced.py with huggingface_hub
Browse files- src/baseline_enhanced.py +58 -16
src/baseline_enhanced.py
CHANGED
|
@@ -119,6 +119,9 @@ def get_vertices_and_edges_enhanced(gest_seg_np, edge_th=15.0,
|
|
| 119 |
min_blob_area=4, max_blob_area=5000):
|
| 120 |
if not isinstance(gest_seg_np, np.ndarray):
|
| 121 |
gest_seg_np = np.array(gest_seg_np)
|
|
|
|
|
|
|
|
|
|
| 122 |
vertices, vertex_areas = [], []
|
| 123 |
for v_class, v_type in [('apex', 'apex'), ('eave_end_point', 'eave_end_point')]:
|
| 124 |
color = np.array(gestalt_color_mapping[v_class])
|
|
@@ -131,11 +134,11 @@ def get_vertices_and_edges_enhanced(gest_seg_np, edge_th=15.0,
|
|
| 131 |
if min_blob_area <= area <= max_blob_area:
|
| 132 |
vertices.append({"xy": centroids[i], "type": v_type})
|
| 133 |
vertex_areas.append(area)
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
for edge_class in
|
| 139 |
if edge_class not in gestalt_color_mapping:
|
| 140 |
continue
|
| 141 |
edge_color = np.array(gestalt_color_mapping[edge_class])
|
|
@@ -155,18 +158,57 @@ def get_vertices_and_edges_enhanced(gest_seg_np, edge_th=15.0,
|
|
| 155 |
proj = (xs - x0) * vx + (ys - y0) * vy
|
| 156 |
p1 = np.array([x0 + proj.min() * vx, y0 + proj.min() * vy])
|
| 157 |
p2 = np.array([x0 + proj.max() * vx, y0 + proj.max() * vy])
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
continue
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
return vertices, connections, edge_types
|
| 171 |
|
| 172 |
|
|
|
|
| 119 |
min_blob_area=4, max_blob_area=5000):
|
| 120 |
if not isinstance(gest_seg_np, np.ndarray):
|
| 121 |
gest_seg_np = np.array(gest_seg_np)
|
| 122 |
+
img_h, img_w = gest_seg_np.shape[:2]
|
| 123 |
+
|
| 124 |
+
# ββ Step 1: blob vertices (apex, eave_end_point colored regions) ββββββ
|
| 125 |
vertices, vertex_areas = [], []
|
| 126 |
for v_class, v_type in [('apex', 'apex'), ('eave_end_point', 'eave_end_point')]:
|
| 127 |
color = np.array(gestalt_color_mapping[v_class])
|
|
|
|
| 134 |
if min_blob_area <= area <= max_blob_area:
|
| 135 |
vertices.append({"xy": centroids[i], "type": v_type})
|
| 136 |
vertex_areas.append(area)
|
| 137 |
+
|
| 138 |
+
# ββ Step 2: fit lines to every edge-class region (collect for reuse) ββ
|
| 139 |
+
_EDGE_CLASSES = ['eave', 'ridge', 'rake', 'valley', 'hip', 'flashing', 'step_flashing']
|
| 140 |
+
fitted_lines = [] # (vx, vy, x0, y0, edge_class, p1, p2)
|
| 141 |
+
for edge_class in _EDGE_CLASSES:
|
| 142 |
if edge_class not in gestalt_color_mapping:
|
| 143 |
continue
|
| 144 |
edge_color = np.array(gestalt_color_mapping[edge_class])
|
|
|
|
| 158 |
proj = (xs - x0) * vx + (ys - y0) * vy
|
| 159 |
p1 = np.array([x0 + proj.min() * vx, y0 + proj.min() * vy])
|
| 160 |
p2 = np.array([x0 + proj.max() * vx, y0 + proj.max() * vy])
|
| 161 |
+
fitted_lines.append((float(vx), float(vy), float(x0), float(y0),
|
| 162 |
+
edge_class, p1, p2))
|
| 163 |
+
|
| 164 |
+
# ββ Step 3: line-intersection vertices (key improvement) βββββββββββββ
|
| 165 |
+
# Every crossing between two DIFFERENT edge-class lines is a candidate vertex.
|
| 166 |
+
# This catches corners that have no apex/eave_end_point blob annotation.
|
| 167 |
+
existing_xy = [v['xy'] for v in vertices]
|
| 168 |
+
for i in range(len(fitted_lines)):
|
| 169 |
+
vx1, vy1, x01, y01, ec1, _, _ = fitted_lines[i]
|
| 170 |
+
for j in range(i + 1, len(fitted_lines)):
|
| 171 |
+
vx2, vy2, x02, y02, ec2, _, _ = fitted_lines[j]
|
| 172 |
+
if ec1 == ec2:
|
| 173 |
+
continue # same-class lines rarely intersect at a meaningful corner
|
| 174 |
+
denom = vx1 * vy2 - vx2 * vy1
|
| 175 |
+
if abs(denom) < 1e-6:
|
| 176 |
+
continue # parallel
|
| 177 |
+
t = ((x02 - x01) * vy2 - (y02 - y01) * vx2) / denom
|
| 178 |
+
ix = x01 + t * vx1
|
| 179 |
+
iy = y01 + t * vy1
|
| 180 |
+
if not (3 <= ix < img_w - 3 and 3 <= iy < img_h - 3):
|
| 181 |
+
continue
|
| 182 |
+
ipt = np.array([ix, iy])
|
| 183 |
+
# Skip if too close to an already-known vertex (blob or prior intersection)
|
| 184 |
+
if existing_xy and min(np.linalg.norm(np.array(p) - ipt) for p in existing_xy) < edge_th:
|
| 185 |
continue
|
| 186 |
+
vertices.append({"xy": ipt, "type": "apex"})
|
| 187 |
+
vertex_areas.append(5)
|
| 188 |
+
existing_xy.append(ipt)
|
| 189 |
+
|
| 190 |
+
if len(vertices) < 2:
|
| 191 |
+
return vertices, [], []
|
| 192 |
+
|
| 193 |
+
# ββ Step 4: connect vertices using the fitted lines βββββββββββββββββββ
|
| 194 |
+
apex_pts = np.array([v['xy'] for v in vertices])
|
| 195 |
+
connections, edge_types = [], []
|
| 196 |
+
seen_edges: set = set()
|
| 197 |
+
for vx, vy, x0, y0, edge_class, p1, p2 in fitted_lines:
|
| 198 |
+
dists = np.array([_point_to_segment_dist(apex_pts[j], p1, p2)
|
| 199 |
+
for j in range(len(apex_pts))])
|
| 200 |
+
near = np.where(dists <= edge_th)[0]
|
| 201 |
+
if len(near) < 2:
|
| 202 |
+
continue
|
| 203 |
+
near_pts = apex_pts[near]
|
| 204 |
+
i1 = near[np.argmin(np.linalg.norm(near_pts - p1, axis=1))]
|
| 205 |
+
i2 = near[np.argmin(np.linalg.norm(near_pts - p2, axis=1))]
|
| 206 |
+
if i1 != i2:
|
| 207 |
+
ek = tuple(sorted((i1, i2)))
|
| 208 |
+
if ek not in seen_edges:
|
| 209 |
+
seen_edges.add(ek)
|
| 210 |
+
connections.append((ek[0], ek[1]))
|
| 211 |
+
edge_types.append(edge_class)
|
| 212 |
return vertices, connections, edge_types
|
| 213 |
|
| 214 |
|