Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -232,23 +232,42 @@ def match_left_to_right(left_boxes, right_boxes):
|
|
| 232 |
|
| 233 |
def visualize(img, left, right, mapping):
|
| 234 |
vis = img.copy()
|
|
|
|
|
|
|
| 235 |
for i, tb in enumerate(left):
|
| 236 |
-
x,y,w,h = tb
|
| 237 |
-
cv2.rectangle(vis, (x,y), (x+w,y+h), (255,0,0), 2)
|
| 238 |
-
cv2.putText(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 239 |
for j, rb in enumerate(right):
|
| 240 |
-
|
| 241 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 242 |
for i, tb in enumerate(left):
|
| 243 |
key = f"test_box_{i+1}"
|
| 244 |
tx, ty, tw, th = tb
|
| 245 |
-
tcx, tcy = int(tx + tw/2), int(ty + th/2)
|
| 246 |
-
for rb in mapping
|
| 247 |
-
rcx = int(rb["x"] + rb["w"]/2)
|
| 248 |
-
rcy = int(rb["y"] + rb["h"]/2)
|
| 249 |
-
cv2.line(vis, (tcx,tcy), (rcx,rcy), (0,0,255), 1)
|
|
|
|
|
|
|
| 250 |
vis_rgb = cv2.cvtColor(vis, cv2.COLOR_BGR2RGB)
|
| 251 |
return vis_rgb
|
|
|
|
| 252 |
def keep_one_box_per_row(rects, reference_rects=None, row_tol=ROW_TOL):
|
| 253 |
"""
|
| 254 |
Keep only one representative box per row.
|
|
|
|
| 232 |
|
| 233 |
def visualize(img, left, right, mapping):
|
| 234 |
vis = img.copy()
|
| 235 |
+
|
| 236 |
+
# --- Draw left (test) boxes ---
|
| 237 |
for i, tb in enumerate(left):
|
| 238 |
+
x, y, w, h = tb
|
| 239 |
+
cv2.rectangle(vis, (x, y), (x + w, y + h), (255, 0, 0), 2)
|
| 240 |
+
cv2.putText(
|
| 241 |
+
vis, f"T{i+1}", (x, y - 5),
|
| 242 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.45, (255, 0, 0), 1
|
| 243 |
+
)
|
| 244 |
+
|
| 245 |
+
# --- Draw right (reference) boxes (skip synthetic ones) ---
|
| 246 |
for j, rb in enumerate(right):
|
| 247 |
+
if rb.get('synthetic', False):
|
| 248 |
+
continue # skip synthetic boxes to avoid double-drawing
|
| 249 |
+
color = (255, 0, 255) # magenta for right boxes
|
| 250 |
+
cv2.rectangle(
|
| 251 |
+
vis,
|
| 252 |
+
(rb['x'], rb['y']),
|
| 253 |
+
(rb['x'] + rb['w'], rb['y'] + rb['h']),
|
| 254 |
+
color,
|
| 255 |
+
1
|
| 256 |
+
)
|
| 257 |
+
# --- Draw red matching lines ---
|
| 258 |
for i, tb in enumerate(left):
|
| 259 |
key = f"test_box_{i+1}"
|
| 260 |
tx, ty, tw, th = tb
|
| 261 |
+
tcx, tcy = int(tx + tw / 2), int(ty + th / 2)
|
| 262 |
+
for rb in mapping.get(key, {}).get("matched_refs", []):
|
| 263 |
+
rcx = int(rb["x"] + rb["w"] / 2)
|
| 264 |
+
rcy = int(rb["y"] + rb["h"] / 2)
|
| 265 |
+
cv2.line(vis, (tcx, tcy), (rcx, rcy), (0, 0, 255), 1)
|
| 266 |
+
|
| 267 |
+
# Convert BGR → RGB for Gradio display
|
| 268 |
vis_rgb = cv2.cvtColor(vis, cv2.COLOR_BGR2RGB)
|
| 269 |
return vis_rgb
|
| 270 |
+
|
| 271 |
def keep_one_box_per_row(rects, reference_rects=None, row_tol=ROW_TOL):
|
| 272 |
"""
|
| 273 |
Keep only one representative box per row.
|