Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -59,7 +59,6 @@ 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 |
-
# ---------------- 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,11 +76,17 @@ def homography_all_detectors(flat_file, persp_file, json_file, xml_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 |
-
|
|
|
|
|
|
|
|
|
|
| 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,7 +100,7 @@ def homography_all_detectors(flat_file, persp_file, json_file, xml_file):
|
|
| 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
|
| 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,23 +108,18 @@ def homography_all_detectors(flat_file, persp_file, json_file, xml_file):
|
|
| 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(
|
| 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 |
-
#
|
| 112 |
-
max_h =
|
| 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([roi_pad, xml_pad])
|
| 124 |
combined_grid = np.vstack([top, bottom])
|
| 125 |
|
|
@@ -132,7 +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(
|
| 138 |
fn=homography_all_detectors,
|
|
|
|
| 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 |
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 |
+
# Resize/pad flat image to perspective image size
|
| 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 |
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 GT overlay
|
| 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 |
cv2.polylines(xml_gt_img,[pts],isClosed=True,color=(255,0,0),thickness=3)
|
| 109 |
|
| 110 |
# Convert to RGB
|
| 111 |
+
flat_rgb = cv2.cvtColor(flat_pad,cv2.COLOR_BGR2RGB)
|
| 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 |
+
# Pad all images to same size (optional here, already same)
|
| 117 |
+
max_h, max_w = target_h, target_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_rgb, match_rgb])
|
| 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 |
# ---------------- Gradio UI ----------------
|
| 137 |
iface = gr.Interface(
|
| 138 |
fn=homography_all_detectors,
|