Ayesha352 commited on
Commit
388fe9d
·
verified ·
1 Parent(s): 28c9121

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -77
app.py CHANGED
@@ -65,26 +65,8 @@ def fit_to_box(img, target_h=600, target_w=600):
65
  canvas[top:top+new_h, left:left+new_w] = resized
66
  return canvas
67
 
68
- # ---------------- NEW: Remap keypoints to boxed image coords ----------------
69
- def remap_keypoints_to_box(kps, orig_shape, target_h=600, target_w=600):
70
- h, w = orig_shape[:2]
71
- scale = min(target_w / w, target_h / h)
72
- new_w, new_h = int(w * scale), int(h * scale)
73
- top = (target_h - new_h) // 2
74
- left = (target_w - new_w) // 2
75
-
76
- kps_new = []
77
- for kp in kps:
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
@@ -94,7 +76,7 @@ def add_heading(img, text):
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)
99
  persp_img = cv2.imread(persp_file)
100
  mockup = json.load(open(json_file.name))
@@ -107,67 +89,51 @@ def homography_all_detectors(flat_file, persp_file, json_file, xml_file):
107
  persp_gray = preprocess_gray_clahe(persp_img)
108
  xml_points = parse_xml_points(xml_file.name)
109
 
110
- methods = ["SIFT","ORB","BRISK","KAZE","AKAZE"]
111
- gallery_paths = []
112
- download_files = []
113
-
114
- for method in methods:
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)
129
- if H is None: continue
130
 
131
- roi_corners_flat = get_rotated_rect_corners(roi_x,roi_y,roi_w,roi_h,roi_rot_deg)
132
- roi_corners_persp = cv2.perspectiveTransform(roi_corners_flat.reshape(-1,1,2),H).reshape(-1,2)
133
- persp_roi = persp_img.copy()
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))
162
- gallery_paths.append(file_name)
163
- download_files.append(file_name)
164
 
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,
171
  inputs=[
172
  gr.Image(label="Upload Flat Image",type="filepath"),
173
  gr.Image(label="Upload Perspective Image",type="filepath"),
@@ -175,15 +141,11 @@ iface = gr.Interface(
175
  gr.File(label="Upload XML file",file_types=[".xml"])
176
  ],
177
  outputs=[
178
- gr.Gallery(label="Results per Detector",show_label=True),
179
- gr.File(label="Download SIFT Result"),
180
- gr.File(label="Download ORB Result"),
181
- gr.File(label="Download BRISK Result"),
182
- gr.File(label="Download KAZE Result"),
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()
 
65
  canvas[top:top+new_h, left:left+new_w] = resized
66
  return canvas
67
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68
  # ---------------- Add Heading on Top ----------------
69
  def add_heading(img, text):
 
70
  h, w = img.shape[:2]
71
  band_h = 40
72
  canvas = np.ones((h+band_h, w, 3), dtype=np.uint8) * 255
 
76
  return canvas
77
 
78
  # ---------------- Main Function ----------------
79
+ def homography_show_three(flat_file, persp_file, json_file, xml_file):
80
  flat_img = cv2.imread(flat_file)
81
  persp_img = cv2.imread(persp_file)
82
  mockup = json.load(open(json_file.name))
 
89
  persp_gray = preprocess_gray_clahe(persp_img)
90
  xml_points = parse_xml_points(xml_file.name)
91
 
92
+ # --- SIFT (only to get Homography) ---
93
+ kp1,kp2,good_matches = detect_and_match(flat_gray,persp_gray,"SIFT")
94
+ if kp1 is None or kp2 is None or len(good_matches)<4:
95
+ return ["Not enough matches found"], None, None, None
 
 
 
 
 
 
 
 
 
 
 
96
 
97
+ src_pts = np.float32([kp1[m.queryIdx].pt for m in good_matches]).reshape(-1,1,2)
98
+ dst_pts = np.float32([kp2[m.trainIdx].pt for m in good_matches]).reshape(-1,1,2)
99
+ H,_ = cv2.findHomography(src_pts,dst_pts,cv2.RANSAC,5.0)
 
100
 
101
+ # --- 1. Flat image with ROI from JSON ---
102
+ flat_with_roi = flat_img.copy()
103
+ roi_corners_flat = get_rotated_rect_corners(roi_x,roi_y,roi_w,roi_h,roi_rot_deg)
104
+ cv2.polylines(flat_with_roi,[roi_corners_flat.astype(int)],True,(0,255,0),2)
105
+ for px,py in roi_corners_flat: cv2.circle(flat_with_roi,(int(px),int(py)),5,(255,0,0),-1)
106
 
107
+ # --- 2. Perspective image with Homography ROI ---
108
+ roi_corners_persp = cv2.perspectiveTransform(roi_corners_flat.reshape(-1,1,2),H).reshape(-1,2)
109
+ persp_roi = persp_img.copy()
110
+ cv2.polylines(persp_roi,[roi_corners_persp.astype(int)],True,(0,255,0),2)
111
+ for px,py in roi_corners_persp: cv2.circle(persp_roi,(int(px),int(py)),5,(255,0,0),-1)
112
 
113
+ # --- 3. Perspective image with Ground Truth ROI from XML ---
114
+ xml_gt_img = persp_img.copy()
115
+ ordered_pts = ['TopLeft', 'TopRight', 'BottomRight', 'BottomLeft']
116
+ xml_polygon = [xml_points[pt] for pt in ordered_pts]
117
+ pts = np.array(xml_polygon, np.int32).reshape((-1,1,2))
118
+ cv2.polylines(xml_gt_img,[pts],isClosed=True,color=(255,0,0),thickness=3)
119
 
120
+ # Resize + Headings
121
+ flat_rgb = add_heading(fit_to_box(cv2.cvtColor(flat_with_roi,cv2.COLOR_BGR2RGB),600,600), "Flat Image with JSON ROI")
122
+ roi_rgb = add_heading(fit_to_box(cv2.cvtColor(persp_roi,cv2.COLOR_BGR2RGB),600,600), "Perspective Image with Homography ROI")
123
+ xml_rgb = add_heading(fit_to_box(cv2.cvtColor(xml_gt_img,cv2.COLOR_BGR2RGB),600,600), "Perspective Image with GT ROI")
 
124
 
125
+ # Side-by-side 1 row
126
+ combined = np.hstack([flat_rgb, roi_rgb, xml_rgb])
 
 
127
 
128
+ base_name = os.path.splitext(os.path.basename(persp_file))[0]
129
+ file_name = f"{base_name}_result.png"
130
+ cv2.imwrite(file_name, cv2.cvtColor(combined,cv2.COLOR_RGB2BGR))
 
 
131
 
132
+ return [file_name], file_name, None, None, None
 
133
 
134
  # ---------------- Gradio UI ----------------
135
  iface = gr.Interface(
136
+ fn=homography_show_three,
137
  inputs=[
138
  gr.Image(label="Upload Flat Image",type="filepath"),
139
  gr.Image(label="Upload Perspective Image",type="filepath"),
 
141
  gr.File(label="Upload XML file",file_types=[".xml"])
142
  ],
143
  outputs=[
144
+ gr.Gallery(label="Final Result",show_label=True),
145
+ gr.File(label="Download Result")
 
 
 
 
146
  ],
147
+ title="Homography ROI vs GT ROI",
148
+ description="Shows Flat image with JSON ROI, Perspective with Homography ROI, and Perspective with GT ROI in one row."
149
  )
150
 
151
  iface.launch()