Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -78,11 +78,21 @@ def remap_keypoints_to_box(kps, orig_shape, target_h=600, target_w=600):
|
|
| 78 |
x, y = kp.pt
|
| 79 |
x_new = x * scale + left
|
| 80 |
y_new = y * scale + top
|
| 81 |
-
# (x, y, size, angle, response, octave, class_id)
|
| 82 |
kps_new.append(cv2.KeyPoint(x_new, y_new, max(1.0, kp.size * scale),
|
| 83 |
kp.angle, kp.response, kp.octave, kp.class_id))
|
| 84 |
return kps_new
|
| 85 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
# ---------------- Main Function ----------------
|
| 87 |
def homography_all_detectors(flat_file, persp_file, json_file, xml_file):
|
| 88 |
flat_img = cv2.imread(flat_file)
|
|
@@ -105,21 +115,14 @@ def homography_all_detectors(flat_file, persp_file, json_file, xml_file):
|
|
| 105 |
kp1,kp2,good_matches = detect_and_match(flat_gray,persp_gray,method)
|
| 106 |
if kp1 is None or kp2 is None or len(good_matches)<4: continue
|
| 107 |
|
| 108 |
-
# ---------------- Match Image (aligned sizes) ----------------
|
| 109 |
flat_box = fit_to_box(flat_img, 600, 600)
|
| 110 |
persp_box = fit_to_box(persp_img, 600, 600)
|
| 111 |
|
| 112 |
-
# NEW: remap original keypoints -> boxed coords (order preserved)
|
| 113 |
kp1_box = remap_keypoints_to_box(kp1, flat_img.shape, 600, 600)
|
| 114 |
kp2_box = remap_keypoints_to_box(kp2, persp_img.shape, 600, 600)
|
| 115 |
|
| 116 |
-
match_img = cv2.drawMatches(
|
| 117 |
-
flat_box, kp1_box,
|
| 118 |
-
persp_box, kp2_box,
|
| 119 |
-
good_matches, None, flags=2
|
| 120 |
-
)
|
| 121 |
|
| 122 |
-
# ---------------- Homography ROI (still uses original coords) ----------------
|
| 123 |
src_pts = np.float32([kp1[m.queryIdx].pt for m in good_matches]).reshape(-1,1,2)
|
| 124 |
dst_pts = np.float32([kp2[m.trainIdx].pt for m in good_matches]).reshape(-1,1,2)
|
| 125 |
H,_ = cv2.findHomography(src_pts,dst_pts,cv2.RANSAC,5.0)
|
|
@@ -131,25 +134,28 @@ def homography_all_detectors(flat_file, persp_file, json_file, xml_file):
|
|
| 131 |
cv2.polylines(persp_roi,[roi_corners_persp.astype(int)],True,(0,255,0),2)
|
| 132 |
for px,py in roi_corners_persp: cv2.circle(persp_roi,(int(px),int(py)),5,(255,0,0),-1)
|
| 133 |
|
| 134 |
-
# ---------------- XML Ground-Truth overlay ----------------
|
| 135 |
xml_gt_img = persp_img.copy()
|
| 136 |
ordered_pts = ['TopLeft', 'TopRight', 'BottomRight', 'BottomLeft']
|
| 137 |
xml_polygon = [xml_points[pt] for pt in ordered_pts]
|
| 138 |
pts = np.array(xml_polygon, np.int32).reshape((-1,1,2))
|
| 139 |
cv2.polylines(xml_gt_img,[pts],isClosed=True,color=(255,0,0),thickness=3)
|
| 140 |
|
| 141 |
-
# Convert all to RGB & fit into boxes
|
| 142 |
flat_rgb = fit_to_box(cv2.cvtColor(flat_img,cv2.COLOR_BGR2RGB),600,600)
|
| 143 |
match_rgb = fit_to_box(cv2.cvtColor(match_img,cv2.COLOR_BGR2RGB),600,600)
|
| 144 |
roi_rgb = fit_to_box(cv2.cvtColor(persp_roi,cv2.COLOR_BGR2RGB),600,600)
|
| 145 |
xml_rgb = fit_to_box(cv2.cvtColor(xml_gt_img,cv2.COLOR_BGR2RGB),600,600)
|
| 146 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
# Merge 2x2 grid
|
| 148 |
top = np.hstack([flat_rgb, match_rgb])
|
| 149 |
bottom = np.hstack([roi_rgb, xml_rgb])
|
| 150 |
combined_grid = np.vstack([top, bottom])
|
| 151 |
|
| 152 |
-
# Save grid
|
| 153 |
base_name = os.path.splitext(os.path.basename(persp_file))[0]
|
| 154 |
file_name = f"{base_name}_{method.lower()}.png"
|
| 155 |
cv2.imwrite(file_name, cv2.cvtColor(combined_grid,cv2.COLOR_RGB2BGR))
|
|
@@ -159,7 +165,6 @@ def homography_all_detectors(flat_file, persp_file, json_file, xml_file):
|
|
| 159 |
while len(download_files)<5: download_files.append(None)
|
| 160 |
return gallery_paths, download_files[0], download_files[1], download_files[2], download_files[3], download_files[4]
|
| 161 |
|
| 162 |
-
|
| 163 |
# ---------------- Gradio UI ----------------
|
| 164 |
iface = gr.Interface(
|
| 165 |
fn=homography_all_detectors,
|
|
@@ -178,7 +183,7 @@ iface = gr.Interface(
|
|
| 178 |
gr.File(label="Download AKAZE Result")
|
| 179 |
],
|
| 180 |
title="Homography ROI + Feature Matching + XML GT",
|
| 181 |
-
description="Flat + Perspective images with mockup.json & XML. Aspect ratio preserved, images centered in uniform boxes."
|
| 182 |
)
|
| 183 |
|
| 184 |
iface.launch()
|
|
|
|
| 78 |
x, y = kp.pt
|
| 79 |
x_new = x * scale + left
|
| 80 |
y_new = y * scale + top
|
|
|
|
| 81 |
kps_new.append(cv2.KeyPoint(x_new, y_new, max(1.0, kp.size * scale),
|
| 82 |
kp.angle, kp.response, kp.octave, kp.class_id))
|
| 83 |
return kps_new
|
| 84 |
|
| 85 |
+
# ---------------- Add Heading on Top ----------------
|
| 86 |
+
def add_heading(img, text):
|
| 87 |
+
# add white band on top
|
| 88 |
+
h, w = img.shape[:2]
|
| 89 |
+
band_h = 40
|
| 90 |
+
canvas = np.ones((h+band_h, w, 3), dtype=np.uint8) * 255
|
| 91 |
+
canvas[band_h:] = img
|
| 92 |
+
cv2.putText(canvas, text, (10, 30), cv2.FONT_HERSHEY_SIMPLEX,
|
| 93 |
+
1, (0,0,0), 2, cv2.LINE_AA)
|
| 94 |
+
return canvas
|
| 95 |
+
|
| 96 |
# ---------------- Main Function ----------------
|
| 97 |
def homography_all_detectors(flat_file, persp_file, json_file, xml_file):
|
| 98 |
flat_img = cv2.imread(flat_file)
|
|
|
|
| 115 |
kp1,kp2,good_matches = detect_and_match(flat_gray,persp_gray,method)
|
| 116 |
if kp1 is None or kp2 is None or len(good_matches)<4: continue
|
| 117 |
|
|
|
|
| 118 |
flat_box = fit_to_box(flat_img, 600, 600)
|
| 119 |
persp_box = fit_to_box(persp_img, 600, 600)
|
| 120 |
|
|
|
|
| 121 |
kp1_box = remap_keypoints_to_box(kp1, flat_img.shape, 600, 600)
|
| 122 |
kp2_box = remap_keypoints_to_box(kp2, persp_img.shape, 600, 600)
|
| 123 |
|
| 124 |
+
match_img = cv2.drawMatches(flat_box, kp1_box, persp_box, kp2_box, good_matches, None, flags=2)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
|
|
|
|
| 126 |
src_pts = np.float32([kp1[m.queryIdx].pt for m in good_matches]).reshape(-1,1,2)
|
| 127 |
dst_pts = np.float32([kp2[m.trainIdx].pt for m in good_matches]).reshape(-1,1,2)
|
| 128 |
H,_ = cv2.findHomography(src_pts,dst_pts,cv2.RANSAC,5.0)
|
|
|
|
| 134 |
cv2.polylines(persp_roi,[roi_corners_persp.astype(int)],True,(0,255,0),2)
|
| 135 |
for px,py in roi_corners_persp: cv2.circle(persp_roi,(int(px),int(py)),5,(255,0,0),-1)
|
| 136 |
|
|
|
|
| 137 |
xml_gt_img = persp_img.copy()
|
| 138 |
ordered_pts = ['TopLeft', 'TopRight', 'BottomRight', 'BottomLeft']
|
| 139 |
xml_polygon = [xml_points[pt] for pt in ordered_pts]
|
| 140 |
pts = np.array(xml_polygon, np.int32).reshape((-1,1,2))
|
| 141 |
cv2.polylines(xml_gt_img,[pts],isClosed=True,color=(255,0,0),thickness=3)
|
| 142 |
|
|
|
|
| 143 |
flat_rgb = fit_to_box(cv2.cvtColor(flat_img,cv2.COLOR_BGR2RGB),600,600)
|
| 144 |
match_rgb = fit_to_box(cv2.cvtColor(match_img,cv2.COLOR_BGR2RGB),600,600)
|
| 145 |
roi_rgb = fit_to_box(cv2.cvtColor(persp_roi,cv2.COLOR_BGR2RGB),600,600)
|
| 146 |
xml_rgb = fit_to_box(cv2.cvtColor(xml_gt_img,cv2.COLOR_BGR2RGB),600,600)
|
| 147 |
|
| 148 |
+
# Add headings
|
| 149 |
+
flat_rgb = add_heading(flat_rgb, "Flat Image")
|
| 150 |
+
match_rgb = add_heading(match_rgb, "Flat -> Perspective Feature Matching")
|
| 151 |
+
roi_rgb = add_heading(roi_rgb, "Perspective Image with Homography ROI")
|
| 152 |
+
xml_rgb = add_heading(xml_rgb, "Perspective GT ROI")
|
| 153 |
+
|
| 154 |
# Merge 2x2 grid
|
| 155 |
top = np.hstack([flat_rgb, match_rgb])
|
| 156 |
bottom = np.hstack([roi_rgb, xml_rgb])
|
| 157 |
combined_grid = np.vstack([top, bottom])
|
| 158 |
|
|
|
|
| 159 |
base_name = os.path.splitext(os.path.basename(persp_file))[0]
|
| 160 |
file_name = f"{base_name}_{method.lower()}.png"
|
| 161 |
cv2.imwrite(file_name, cv2.cvtColor(combined_grid,cv2.COLOR_RGB2BGR))
|
|
|
|
| 165 |
while len(download_files)<5: download_files.append(None)
|
| 166 |
return gallery_paths, download_files[0], download_files[1], download_files[2], download_files[3], download_files[4]
|
| 167 |
|
|
|
|
| 168 |
# ---------------- Gradio UI ----------------
|
| 169 |
iface = gr.Interface(
|
| 170 |
fn=homography_all_detectors,
|
|
|
|
| 183 |
gr.File(label="Download AKAZE Result")
|
| 184 |
],
|
| 185 |
title="Homography ROI + Feature Matching + XML GT",
|
| 186 |
+
description="Flat + Perspective images with mockup.json & XML. Aspect ratio preserved, images centered in uniform boxes, headings added."
|
| 187 |
)
|
| 188 |
|
| 189 |
iface.launch()
|