Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -48,15 +48,21 @@ def parse_xml_points(xml_file):
|
|
| 48 |
points[pt_type] = (x, y)
|
| 49 |
return points
|
| 50 |
|
| 51 |
-
# ----------------
|
| 52 |
-
def
|
| 53 |
h, w = img.shape[:2]
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
return canvas
|
| 61 |
|
| 62 |
# ---------------- Main Function ----------------
|
|
@@ -81,46 +87,39 @@ def homography_all_detectors(flat_file, persp_file, json_file, xml_file):
|
|
| 81 |
kp1,kp2,good_matches = detect_and_match(flat_gray,persp_gray,method)
|
| 82 |
if kp1 is None or kp2 is None or len(good_matches)<4: continue
|
| 83 |
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
| 85 |
|
|
|
|
| 86 |
src_pts = np.float32([kp1[m.queryIdx].pt for m in good_matches]).reshape(-1,1,2)
|
| 87 |
dst_pts = np.float32([kp2[m.trainIdx].pt for m in good_matches]).reshape(-1,1,2)
|
| 88 |
H,_ = cv2.findHomography(src_pts,dst_pts,cv2.RANSAC,5.0)
|
| 89 |
if H is None: continue
|
| 90 |
|
| 91 |
-
# Homography ROI
|
| 92 |
roi_corners_flat = get_rotated_rect_corners(roi_x,roi_y,roi_w,roi_h,roi_rot_deg)
|
| 93 |
roi_corners_persp = cv2.perspectiveTransform(roi_corners_flat.reshape(-1,1,2),H).reshape(-1,2)
|
| 94 |
persp_roi = persp_img.copy()
|
| 95 |
cv2.polylines(persp_roi,[roi_corners_persp.astype(int)],True,(0,255,0),2)
|
| 96 |
for px,py in roi_corners_persp: cv2.circle(persp_roi,(int(px),int(py)),5,(255,0,0),-1)
|
| 97 |
|
| 98 |
-
# XML Ground-Truth overlay
|
| 99 |
xml_gt_img = persp_img.copy()
|
| 100 |
ordered_pts = ['TopLeft', 'TopRight', 'BottomRight', 'BottomLeft']
|
| 101 |
xml_polygon = [xml_points[pt] for pt in ordered_pts]
|
| 102 |
pts = np.array(xml_polygon, np.int32).reshape((-1,1,2))
|
| 103 |
cv2.polylines(xml_gt_img,[pts],isClosed=True,color=(255,0,0),thickness=3)
|
| 104 |
|
| 105 |
-
# Convert to RGB
|
| 106 |
-
flat_rgb = cv2.cvtColor(flat_img,cv2.COLOR_BGR2RGB)
|
| 107 |
-
match_rgb = cv2.cvtColor(match_img,cv2.COLOR_BGR2RGB)
|
| 108 |
-
roi_rgb = cv2.cvtColor(persp_roi,cv2.COLOR_BGR2RGB)
|
| 109 |
-
xml_rgb = cv2.cvtColor(xml_gt_img,cv2.COLOR_BGR2RGB)
|
| 110 |
-
|
| 111 |
-
# Determine max height and width
|
| 112 |
-
max_h = max(flat_rgb.shape[0], match_rgb.shape[0], roi_rgb.shape[0], xml_rgb.shape[0])
|
| 113 |
-
max_w = max(flat_rgb.shape[1], match_rgb.shape[1], roi_rgb.shape[1], xml_rgb.shape[1])
|
| 114 |
-
|
| 115 |
-
# Pad images
|
| 116 |
-
flat_pad = pad_to_size(flat_rgb, max_h, max_w)
|
| 117 |
-
match_pad = pad_to_size(match_rgb, max_h, max_w)
|
| 118 |
-
roi_pad = pad_to_size(roi_rgb, max_h, max_w)
|
| 119 |
-
xml_pad = pad_to_size(xml_rgb, max_h, max_w)
|
| 120 |
|
| 121 |
# Merge 2x2 grid
|
| 122 |
-
top = np.hstack([
|
| 123 |
-
bottom = np.hstack([
|
| 124 |
combined_grid = np.vstack([top, bottom])
|
| 125 |
|
| 126 |
# Save grid
|
|
@@ -152,7 +151,7 @@ iface = gr.Interface(
|
|
| 152 |
gr.File(label="Download AKAZE Result")
|
| 153 |
],
|
| 154 |
title="Homography ROI + Feature Matching + XML GT",
|
| 155 |
-
description="Flat + Perspective images with mockup.json & XML.
|
| 156 |
)
|
| 157 |
|
| 158 |
iface.launch()
|
|
|
|
| 48 |
points[pt_type] = (x, y)
|
| 49 |
return points
|
| 50 |
|
| 51 |
+
# ---------------- Fit-to-Box Helper ----------------
|
| 52 |
+
def fit_to_box(img, target_h=600, target_w=600):
|
| 53 |
h, w = img.shape[:2]
|
| 54 |
+
scale = min(target_w/w, target_h/h) # preserve aspect ratio
|
| 55 |
+
new_w, new_h = int(w*scale), int(h*scale)
|
| 56 |
+
resized = cv2.resize(img, (new_w, new_h))
|
| 57 |
+
|
| 58 |
+
# symmetric padding
|
| 59 |
+
top = (target_h - new_h) // 2
|
| 60 |
+
bottom = target_h - new_h - top
|
| 61 |
+
left = (target_w - new_w) // 2
|
| 62 |
+
right = target_w - new_w - left
|
| 63 |
+
|
| 64 |
+
canvas = np.ones((target_h, target_w, 3), dtype=np.uint8) * 255
|
| 65 |
+
canvas[top:top+new_h, left:left+new_w] = resized
|
| 66 |
return canvas
|
| 67 |
|
| 68 |
# ---------------- Main Function ----------------
|
|
|
|
| 87 |
kp1,kp2,good_matches = detect_and_match(flat_gray,persp_gray,method)
|
| 88 |
if kp1 is None or kp2 is None or len(good_matches)<4: continue
|
| 89 |
|
| 90 |
+
# ---------------- Match Image (aligned sizes)
|
| 91 |
+
flat_box = fit_to_box(flat_img, 600, 600)
|
| 92 |
+
persp_box = fit_to_box(persp_img, 600, 600)
|
| 93 |
+
match_img = cv2.drawMatches(flat_box,kp1,persp_box,kp2,good_matches,None,flags=2)
|
| 94 |
|
| 95 |
+
# ---------------- Homography ROI
|
| 96 |
src_pts = np.float32([kp1[m.queryIdx].pt for m in good_matches]).reshape(-1,1,2)
|
| 97 |
dst_pts = np.float32([kp2[m.trainIdx].pt for m in good_matches]).reshape(-1,1,2)
|
| 98 |
H,_ = cv2.findHomography(src_pts,dst_pts,cv2.RANSAC,5.0)
|
| 99 |
if H is None: continue
|
| 100 |
|
|
|
|
| 101 |
roi_corners_flat = get_rotated_rect_corners(roi_x,roi_y,roi_w,roi_h,roi_rot_deg)
|
| 102 |
roi_corners_persp = cv2.perspectiveTransform(roi_corners_flat.reshape(-1,1,2),H).reshape(-1,2)
|
| 103 |
persp_roi = persp_img.copy()
|
| 104 |
cv2.polylines(persp_roi,[roi_corners_persp.astype(int)],True,(0,255,0),2)
|
| 105 |
for px,py in roi_corners_persp: cv2.circle(persp_roi,(int(px),int(py)),5,(255,0,0),-1)
|
| 106 |
|
| 107 |
+
# ---------------- XML Ground-Truth overlay
|
| 108 |
xml_gt_img = persp_img.copy()
|
| 109 |
ordered_pts = ['TopLeft', 'TopRight', 'BottomRight', 'BottomLeft']
|
| 110 |
xml_polygon = [xml_points[pt] for pt in ordered_pts]
|
| 111 |
pts = np.array(xml_polygon, np.int32).reshape((-1,1,2))
|
| 112 |
cv2.polylines(xml_gt_img,[pts],isClosed=True,color=(255,0,0),thickness=3)
|
| 113 |
|
| 114 |
+
# Convert all to RGB & fit into boxes
|
| 115 |
+
flat_rgb = fit_to_box(cv2.cvtColor(flat_img,cv2.COLOR_BGR2RGB),600,600)
|
| 116 |
+
match_rgb = fit_to_box(cv2.cvtColor(match_img,cv2.COLOR_BGR2RGB),600,600)
|
| 117 |
+
roi_rgb = fit_to_box(cv2.cvtColor(persp_roi,cv2.COLOR_BGR2RGB),600,600)
|
| 118 |
+
xml_rgb = fit_to_box(cv2.cvtColor(xml_gt_img,cv2.COLOR_BGR2RGB),600,600)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
# Merge 2x2 grid
|
| 121 |
+
top = np.hstack([flat_rgb, match_rgb])
|
| 122 |
+
bottom = np.hstack([roi_rgb, xml_rgb])
|
| 123 |
combined_grid = np.vstack([top, bottom])
|
| 124 |
|
| 125 |
# Save grid
|
|
|
|
| 151 |
gr.File(label="Download AKAZE Result")
|
| 152 |
],
|
| 153 |
title="Homography ROI + Feature Matching + XML GT",
|
| 154 |
+
description="Flat + Perspective images with mockup.json & XML. Aspect ratio preserved, images centered in uniform boxes."
|
| 155 |
)
|
| 156 |
|
| 157 |
iface.launch()
|