Ayesha352 commited on
Commit
00c2dbc
·
verified ·
1 Parent(s): 86185b2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -31
app.py CHANGED
@@ -46,36 +46,24 @@ def parse_xml_points(xml_file):
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):
@@ -118,11 +106,25 @@ def homography_all_detectors(flat_file, persp_file, json_file, xml_file):
118
 
119
  # Convert to RGB
120
  flat_rgb = cv2.cvtColor(flat_img,cv2.COLOR_BGR2RGB)
121
- match_rgb = cv2.cvtColor(match_img,cv2.COLOR_BGR2RGB)
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"
@@ -133,7 +135,6 @@ def homography_all_detectors(flat_file, persp_file, json_file, xml_file):
133
  while len(download_files)<5: download_files.append(None)
134
  return gallery_paths, download_files[0], download_files[1], download_files[2], download_files[3], download_files[4]
135
 
136
- # ---------------- Gradio UI ----------------
137
  iface = gr.Interface(
138
  fn=homography_all_detectors,
139
  inputs=[
@@ -151,7 +152,7 @@ iface = gr.Interface(
151
  gr.File(label="Download AKAZE Result")
152
  ],
153
  title="Homography ROI Projection with Feature Matching & XML GT",
154
- description="Flat + Perspective images with mockup.json & XML. Original resolution maintained. Grid aligned with white padding."
155
  )
156
 
157
  iface.launch()
 
46
  return np.array(points,dtype=np.float32).reshape(-1,2)
47
 
48
  # ---------------- Padding Helper ----------------
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[top_pad:top_pad+h, left_pad:left_pad+w] = img
 
 
 
 
 
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 = min(ref_w/w, ref_h/h)
63
+ new_w, new_h = int(w*scale), int(h*scale)
64
+ resized = cv2.resize(match_img, (new_w,new_h))
65
+ padded = pad_to_size(resized, ref_h, ref_w)
66
+ return padded
 
 
 
 
 
 
 
67
 
68
  # ---------------- Main Function ----------------
69
  def homography_all_detectors(flat_file, persp_file, json_file, xml_file):
 
106
 
107
  # Convert to RGB
108
  flat_rgb = cv2.cvtColor(flat_img,cv2.COLOR_BGR2RGB)
109
+ persp_rgb = cv2.cvtColor(persp_img,cv2.COLOR_BGR2RGB)
110
  roi_rgb = cv2.cvtColor(persp_roi,cv2.COLOR_BGR2RGB)
111
  xml_rgb = cv2.cvtColor(xml_gt_img,cv2.COLOR_BGR2RGB)
112
 
113
+ # Resize feature-match image to match original flat/perspective
114
+ match_rgb = match_img_to_reference(cv2.cvtColor(match_img, cv2.COLOR_BGR2RGB), flat_rgb.shape[0], flat_rgb.shape[1])
115
+
116
+ # Determine max height and width for grid (all images now same)
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, match_rgb])
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"
 
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
  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. Feature-match aligned with original images using white padding."
156
  )
157
 
158
  iface.launch()