Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -45,25 +45,25 @@ def parse_xml_points(xml_file):
|
|
| 45 |
points.append([float(elem.get("x")), float(elem.get("y"))])
|
| 46 |
return np.array(points,dtype=np.float32).reshape(-1,2)
|
| 47 |
|
| 48 |
-
# ---------------- Padding
|
| 49 |
def pad_to_size(img, target_h, target_w):
|
| 50 |
h, w = img.shape[:2]
|
| 51 |
-
top_pad = 0
|
| 52 |
-
left_pad = 0
|
| 53 |
-
bottom_pad = target_h - h
|
| 54 |
-
right_pad = target_w - w
|
| 55 |
canvas = np.ones((target_h, target_w,3), dtype=np.uint8)*255
|
| 56 |
-
canvas[
|
| 57 |
return canvas
|
| 58 |
|
| 59 |
-
# ---------------- Resize feature-match to original reference size ----------------
|
| 60 |
def match_img_to_reference(match_img, ref_h, ref_w):
|
| 61 |
h, w = match_img.shape[:2]
|
| 62 |
-
scale =
|
| 63 |
-
new_w
|
|
|
|
| 64 |
resized = cv2.resize(match_img, (new_w,new_h))
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
# ---------------- Main Function ----------------
|
| 69 |
def homography_all_detectors(flat_file, persp_file, json_file, xml_file):
|
|
@@ -110,22 +110,26 @@ def homography_all_detectors(flat_file, persp_file, json_file, xml_file):
|
|
| 110 |
roi_rgb = cv2.cvtColor(persp_roi,cv2.COLOR_BGR2RGB)
|
| 111 |
xml_rgb = cv2.cvtColor(xml_gt_img,cv2.COLOR_BGR2RGB)
|
| 112 |
|
| 113 |
-
# Resize
|
| 114 |
-
match_rgb = match_img_to_reference(cv2.cvtColor(match_img, cv2.COLOR_BGR2RGB),
|
|
|
|
| 115 |
|
| 116 |
-
# Determine max height and width for grid
|
| 117 |
max_h = max(flat_rgb.shape[0], match_rgb.shape[0], roi_rgb.shape[0], xml_rgb.shape[0])
|
| 118 |
max_w = max(flat_rgb.shape[1], match_rgb.shape[1], roi_rgb.shape[1], xml_rgb.shape[1])
|
| 119 |
|
|
|
|
| 120 |
flat_pad = pad_to_size(flat_rgb, max_h, max_w)
|
|
|
|
| 121 |
roi_pad = pad_to_size(roi_rgb, max_h, max_w)
|
| 122 |
xml_pad = pad_to_size(xml_rgb, max_h, max_w)
|
| 123 |
|
| 124 |
# Merge 2x2 grid
|
| 125 |
-
top = np.hstack([flat_pad,
|
| 126 |
bottom = np.hstack([roi_pad, xml_pad])
|
| 127 |
combined_grid = np.vstack([top, bottom])
|
| 128 |
|
|
|
|
| 129 |
base_name = os.path.splitext(os.path.basename(persp_file))[0]
|
| 130 |
file_name = f"{base_name}_{method.lower()}.png"
|
| 131 |
cv2.imwrite(file_name, cv2.cvtColor(combined_grid,cv2.COLOR_RGB2BGR))
|
|
@@ -135,6 +139,7 @@ def homography_all_detectors(flat_file, persp_file, json_file, xml_file):
|
|
| 135 |
while len(download_files)<5: download_files.append(None)
|
| 136 |
return gallery_paths, download_files[0], download_files[1], download_files[2], download_files[3], download_files[4]
|
| 137 |
|
|
|
|
| 138 |
iface = gr.Interface(
|
| 139 |
fn=homography_all_detectors,
|
| 140 |
inputs=[
|
|
@@ -152,7 +157,7 @@ iface = gr.Interface(
|
|
| 152 |
gr.File(label="Download AKAZE Result")
|
| 153 |
],
|
| 154 |
title="Homography ROI Projection with Feature Matching & XML GT",
|
| 155 |
-
description="Flat + Perspective images with mockup.json & XML.
|
| 156 |
)
|
| 157 |
|
| 158 |
iface.launch()
|
|
|
|
| 45 |
points.append([float(elem.get("x")), float(elem.get("y"))])
|
| 46 |
return np.array(points,dtype=np.float32).reshape(-1,2)
|
| 47 |
|
| 48 |
+
# ---------------- Padding Helpers ----------------
|
| 49 |
def pad_to_size(img, target_h, target_w):
|
| 50 |
h, w = img.shape[:2]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
canvas = np.ones((target_h, target_w,3), dtype=np.uint8)*255
|
| 52 |
+
canvas[:h,:w] = img
|
| 53 |
return canvas
|
| 54 |
|
|
|
|
| 55 |
def match_img_to_reference(match_img, ref_h, ref_w):
|
| 56 |
h, w = match_img.shape[:2]
|
| 57 |
+
scale = ref_w / w
|
| 58 |
+
new_w = ref_w
|
| 59 |
+
new_h = int(h * scale)
|
| 60 |
resized = cv2.resize(match_img, (new_w,new_h))
|
| 61 |
+
if new_h < ref_h:
|
| 62 |
+
canvas = np.ones((ref_h, ref_w,3), dtype=np.uint8)*255
|
| 63 |
+
canvas[:new_h,:new_w] = resized
|
| 64 |
+
return canvas
|
| 65 |
+
else:
|
| 66 |
+
return resized
|
| 67 |
|
| 68 |
# ---------------- Main Function ----------------
|
| 69 |
def homography_all_detectors(flat_file, persp_file, json_file, xml_file):
|
|
|
|
| 110 |
roi_rgb = cv2.cvtColor(persp_roi,cv2.COLOR_BGR2RGB)
|
| 111 |
xml_rgb = cv2.cvtColor(xml_gt_img,cv2.COLOR_BGR2RGB)
|
| 112 |
|
| 113 |
+
# Resize match_img to flat image width, maintain aspect ratio
|
| 114 |
+
match_rgb = match_img_to_reference(cv2.cvtColor(match_img, cv2.COLOR_BGR2RGB),
|
| 115 |
+
flat_rgb.shape[0], flat_rgb.shape[1])
|
| 116 |
|
| 117 |
+
# Determine max height and width for grid
|
| 118 |
max_h = max(flat_rgb.shape[0], match_rgb.shape[0], roi_rgb.shape[0], xml_rgb.shape[0])
|
| 119 |
max_w = max(flat_rgb.shape[1], match_rgb.shape[1], roi_rgb.shape[1], xml_rgb.shape[1])
|
| 120 |
|
| 121 |
+
# Pad all images
|
| 122 |
flat_pad = pad_to_size(flat_rgb, max_h, max_w)
|
| 123 |
+
match_pad = pad_to_size(match_rgb, max_h, max_w)
|
| 124 |
roi_pad = pad_to_size(roi_rgb, max_h, max_w)
|
| 125 |
xml_pad = pad_to_size(xml_rgb, max_h, max_w)
|
| 126 |
|
| 127 |
# Merge 2x2 grid
|
| 128 |
+
top = np.hstack([flat_pad, match_pad])
|
| 129 |
bottom = np.hstack([roi_pad, xml_pad])
|
| 130 |
combined_grid = np.vstack([top, bottom])
|
| 131 |
|
| 132 |
+
# Save combined grid
|
| 133 |
base_name = os.path.splitext(os.path.basename(persp_file))[0]
|
| 134 |
file_name = f"{base_name}_{method.lower()}.png"
|
| 135 |
cv2.imwrite(file_name, cv2.cvtColor(combined_grid,cv2.COLOR_RGB2BGR))
|
|
|
|
| 139 |
while len(download_files)<5: download_files.append(None)
|
| 140 |
return gallery_paths, download_files[0], download_files[1], download_files[2], download_files[3], download_files[4]
|
| 141 |
|
| 142 |
+
# ---------------- Gradio UI ----------------
|
| 143 |
iface = gr.Interface(
|
| 144 |
fn=homography_all_detectors,
|
| 145 |
inputs=[
|
|
|
|
| 157 |
gr.File(label="Download AKAZE Result")
|
| 158 |
],
|
| 159 |
title="Homography ROI Projection with Feature Matching & XML GT",
|
| 160 |
+
description="Flat + Perspective images with mockup.json & XML. Original resolution maintained. Grid aligned with white padding."
|
| 161 |
)
|
| 162 |
|
| 163 |
iface.launch()
|