Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -59,6 +59,7 @@ def pad_to_size(img, target_h, target_w):
|
|
59 |
canvas[top_pad:top_pad+h, left_pad:left_pad+w] = img
|
60 |
return canvas
|
61 |
|
|
|
62 |
def homography_all_detectors(flat_file, persp_file, json_file, xml_file):
|
63 |
flat_img = cv2.imread(flat_file)
|
64 |
persp_img = cv2.imread(persp_file)
|
@@ -76,17 +77,11 @@ def homography_all_detectors(flat_file, persp_file, json_file, xml_file):
|
|
76 |
gallery_paths = []
|
77 |
download_files = []
|
78 |
|
79 |
-
# Perspective image size
|
80 |
-
target_h, target_w = persp_img.shape[:2]
|
81 |
-
|
82 |
for method in methods:
|
83 |
kp1,kp2,good_matches = detect_and_match(flat_gray,persp_gray,method)
|
84 |
if kp1 is None or kp2 is None or len(good_matches)<4: continue
|
85 |
|
86 |
-
|
87 |
-
flat_pad = pad_to_size(flat_img, target_h, target_w)
|
88 |
-
|
89 |
-
match_img = cv2.drawMatches(flat_pad,kp1,persp_img,kp2,good_matches,None,flags=2)
|
90 |
|
91 |
src_pts = np.float32([kp1[m.queryIdx].pt for m in good_matches]).reshape(-1,1,2)
|
92 |
dst_pts = np.float32([kp2[m.trainIdx].pt for m in good_matches]).reshape(-1,1,2)
|
@@ -100,7 +95,7 @@ def homography_all_detectors(flat_file, persp_file, json_file, xml_file):
|
|
100 |
cv2.polylines(persp_roi,[roi_corners_persp.astype(int)],True,(0,255,0),2)
|
101 |
for px,py in roi_corners_persp: cv2.circle(persp_roi,(int(px),int(py)),5,(255,0,0),-1)
|
102 |
|
103 |
-
# XML
|
104 |
xml_gt_img = persp_img.copy()
|
105 |
ordered_pts = ['TopLeft', 'TopRight', 'BottomRight', 'BottomLeft']
|
106 |
xml_polygon = [xml_points[pt] for pt in ordered_pts]
|
@@ -108,18 +103,23 @@ def homography_all_detectors(flat_file, persp_file, json_file, xml_file):
|
|
108 |
cv2.polylines(xml_gt_img,[pts],isClosed=True,color=(255,0,0),thickness=3)
|
109 |
|
110 |
# Convert to RGB
|
111 |
-
flat_rgb = cv2.cvtColor(
|
112 |
match_rgb = cv2.cvtColor(match_img,cv2.COLOR_BGR2RGB)
|
113 |
roi_rgb = cv2.cvtColor(persp_roi,cv2.COLOR_BGR2RGB)
|
114 |
xml_rgb = cv2.cvtColor(xml_gt_img,cv2.COLOR_BGR2RGB)
|
115 |
|
116 |
-
#
|
117 |
-
max_h
|
|
|
|
|
|
|
|
|
|
|
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([roi_pad, xml_pad])
|
124 |
combined_grid = np.vstack([top, bottom])
|
125 |
|
@@ -132,6 +132,7 @@ def homography_all_detectors(flat_file, persp_file, json_file, xml_file):
|
|
132 |
|
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(
|
|
|
59 |
canvas[top_pad:top_pad+h, left_pad:left_pad+w] = img
|
60 |
return canvas
|
61 |
|
62 |
+
# ---------------- Main Function ----------------
|
63 |
def homography_all_detectors(flat_file, persp_file, json_file, xml_file):
|
64 |
flat_img = cv2.imread(flat_file)
|
65 |
persp_img = cv2.imread(persp_file)
|
|
|
77 |
gallery_paths = []
|
78 |
download_files = []
|
79 |
|
|
|
|
|
|
|
80 |
for method in methods:
|
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 |
+
match_img = cv2.drawMatches(flat_img,kp1,persp_img,kp2,good_matches,None,flags=2)
|
|
|
|
|
|
|
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)
|
|
|
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]
|
|
|
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([flat_pad, match_pad])
|
123 |
bottom = np.hstack([roi_pad, xml_pad])
|
124 |
combined_grid = np.vstack([top, bottom])
|
125 |
|
|
|
132 |
|
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 |
|
137 |
# ---------------- Gradio UI ----------------
|
138 |
iface = gr.Interface(
|