Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -46,17 +46,37 @@ def parse_xml_points(xml_file):
|
|
46 |
return np.array(points,dtype=np.float32).reshape(-1,2)
|
47 |
|
48 |
# ---------------- Padding Helper ----------------
|
49 |
-
def
|
50 |
h, w = img.shape[:2]
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
|
|
|
|
|
|
|
|
58 |
return canvas
|
59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
# ---------------- Main Function ----------------
|
61 |
def homography_all_detectors(flat_file, persp_file, json_file, xml_file):
|
62 |
flat_img = cv2.imread(flat_file)
|
@@ -102,22 +122,8 @@ def homography_all_detectors(flat_file, persp_file, json_file, xml_file):
|
|
102 |
roi_rgb = cv2.cvtColor(persp_roi,cv2.COLOR_BGR2RGB)
|
103 |
xml_rgb = cv2.cvtColor(xml_gt_img,cv2.COLOR_BGR2RGB)
|
104 |
|
105 |
-
|
106 |
-
max_h = max(flat_rgb.shape[0], match_rgb.shape[0], roi_rgb.shape[0], xml_rgb.shape[0])
|
107 |
-
max_w = max(flat_rgb.shape[1], match_rgb.shape[1], roi_rgb.shape[1], xml_rgb.shape[1])
|
108 |
-
|
109 |
-
# Pad all images to same size
|
110 |
-
flat_pad = pad_to_size(flat_rgb, max_h, max_w)
|
111 |
-
match_pad = pad_to_size(match_rgb, max_h, max_w)
|
112 |
-
roi_pad = pad_to_size(roi_rgb, max_h, max_w)
|
113 |
-
xml_pad = pad_to_size(xml_rgb, max_h, max_w)
|
114 |
-
|
115 |
-
# Merge 2x2 grid
|
116 |
-
top = np.hstack([flat_pad, match_pad])
|
117 |
-
bottom = np.hstack([roi_pad, xml_pad])
|
118 |
-
combined_grid = np.vstack([top, bottom])
|
119 |
|
120 |
-
# Save combined grid
|
121 |
base_name = os.path.splitext(os.path.basename(persp_file))[0]
|
122 |
file_name = f"{base_name}_{method.lower()}.png"
|
123 |
cv2.imwrite(file_name, cv2.cvtColor(combined_grid,cv2.COLOR_RGB2BGR))
|
|
|
46 |
return np.array(points,dtype=np.float32).reshape(-1,2)
|
47 |
|
48 |
# ---------------- Padding Helper ----------------
|
49 |
+
def pad_to_match(img, target_h=None, target_w=None):
|
50 |
h, w = img.shape[:2]
|
51 |
+
pad_top, pad_bottom, pad_left, pad_right = 0,0,0,0
|
52 |
+
if target_h is not None and h < target_h:
|
53 |
+
diff = target_h - h
|
54 |
+
pad_top = diff // 2
|
55 |
+
pad_bottom = diff - pad_top
|
56 |
+
if target_w is not None and w < target_w:
|
57 |
+
diff = target_w - w
|
58 |
+
pad_left = diff // 2
|
59 |
+
pad_right = diff - pad_left
|
60 |
+
canvas = np.ones((h+pad_top+pad_bottom, w+pad_left+pad_right,3), dtype=np.uint8)*255
|
61 |
+
canvas[pad_top:pad_top+h, pad_left:pad_left+w] = img
|
62 |
return canvas
|
63 |
|
64 |
+
def merge_2x2_grid(images):
|
65 |
+
top_h = max(images[0].shape[0], images[1].shape[0])
|
66 |
+
bottom_h = max(images[2].shape[0], images[3].shape[0])
|
67 |
+
left_w = max(images[0].shape[1], images[2].shape[1])
|
68 |
+
right_w = max(images[1].shape[1], images[3].shape[1])
|
69 |
+
|
70 |
+
flat_pad = pad_to_match(images[0], target_h=top_h, target_w=left_w)
|
71 |
+
match_pad = pad_to_match(images[1], target_h=top_h, target_w=right_w)
|
72 |
+
roi_pad = pad_to_match(images[2], target_h=bottom_h, target_w=left_w)
|
73 |
+
xml_pad = pad_to_match(images[3], target_h=bottom_h, target_w=right_w)
|
74 |
+
|
75 |
+
top_row = np.hstack([flat_pad, match_pad])
|
76 |
+
bottom_row = np.hstack([roi_pad, xml_pad])
|
77 |
+
grid = np.vstack([top_row, bottom_row])
|
78 |
+
return grid
|
79 |
+
|
80 |
# ---------------- Main Function ----------------
|
81 |
def homography_all_detectors(flat_file, persp_file, json_file, xml_file):
|
82 |
flat_img = cv2.imread(flat_file)
|
|
|
122 |
roi_rgb = cv2.cvtColor(persp_roi,cv2.COLOR_BGR2RGB)
|
123 |
xml_rgb = cv2.cvtColor(xml_gt_img,cv2.COLOR_BGR2RGB)
|
124 |
|
125 |
+
combined_grid = merge_2x2_grid([flat_rgb, match_rgb, roi_rgb, xml_rgb])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
126 |
|
|
|
127 |
base_name = os.path.splitext(os.path.basename(persp_file))[0]
|
128 |
file_name = f"{base_name}_{method.lower()}.png"
|
129 |
cv2.imwrite(file_name, cv2.cvtColor(combined_grid,cv2.COLOR_RGB2BGR))
|