Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -4,7 +4,6 @@ import json
|
|
4 |
import gradio as gr
|
5 |
import os
|
6 |
import xml.etree.ElementTree as ET
|
7 |
-
from lxml import etree
|
8 |
|
9 |
# ---------------- Helper functions ----------------
|
10 |
def get_rotated_rect_corners(x, y, w, h, rotation_deg):
|
@@ -40,37 +39,24 @@ def detect_and_match(img1_gray, img2_gray, method="SIFT", ratio_thresh=0.78):
|
|
40 |
def parse_xml_points(xml_file):
|
41 |
tree = ET.parse(xml_file)
|
42 |
root = tree.getroot()
|
43 |
-
points=[]
|
44 |
-
for pt_type in ["TopLeft","TopRight","BottomLeft","BottomRight"]:
|
45 |
-
elem=root.find(f".//point[@type='{pt_type}']")
|
46 |
-
points.append([float(elem.get("x")), float(elem.get("y"))])
|
47 |
-
return np.array(points,dtype=np.float32).reshape(-1,2)
|
48 |
-
|
49 |
-
def extract_four_points_from_xml(xml_path):
|
50 |
-
tree = etree.parse(xml_path)
|
51 |
-
root = tree.getroot()
|
52 |
transform = root.find('.//transform')
|
53 |
points = {}
|
54 |
for pt in transform.findall('.//point'):
|
55 |
pt_type = pt.attrib['type']
|
56 |
-
x =
|
57 |
-
y =
|
58 |
points[pt_type] = (x, y)
|
59 |
return points
|
60 |
|
61 |
-
def draw_polygon_overlay(img, points_dict):
|
62 |
-
ordered_points = ['TopLeft','TopRight','BottomRight','BottomLeft']
|
63 |
-
polygon = [points_dict[pt] for pt in ordered_points]
|
64 |
-
pts = np.array(polygon, np.int32).reshape((-1,1,2))
|
65 |
-
img_overlay = img.copy()
|
66 |
-
cv2.polylines(img_overlay, [pts], isClosed=True, color=(255,0,0), thickness=3)
|
67 |
-
return img_overlay
|
68 |
-
|
69 |
# ---------------- Padding Helper ----------------
|
70 |
def pad_to_size(img, target_h, target_w):
|
71 |
h, w = img.shape[:2]
|
|
|
|
|
|
|
|
|
72 |
canvas = np.ones((target_h, target_w,3), dtype=np.uint8)*255
|
73 |
-
canvas[:h, :w] = img
|
74 |
return canvas
|
75 |
|
76 |
# ---------------- Main Function ----------------
|
@@ -86,7 +72,6 @@ def homography_all_detectors(flat_file, persp_file, json_file, xml_file):
|
|
86 |
flat_gray = preprocess_gray_clahe(flat_img)
|
87 |
persp_gray = preprocess_gray_clahe(persp_img)
|
88 |
xml_points = parse_xml_points(xml_file.name)
|
89 |
-
xml_dict = extract_four_points_from_xml(xml_file.name)
|
90 |
|
91 |
methods = ["SIFT","ORB","BRISK","KAZE","AKAZE"]
|
92 |
gallery_paths = []
|
@@ -103,27 +88,31 @@ def homography_all_detectors(flat_file, persp_file, json_file, xml_file):
|
|
103 |
H,_ = cv2.findHomography(src_pts,dst_pts,cv2.RANSAC,5.0)
|
104 |
if H is None: continue
|
105 |
|
106 |
-
# Homography
|
107 |
roi_corners_flat = get_rotated_rect_corners(roi_x,roi_y,roi_w,roi_h,roi_rot_deg)
|
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 |
-
# XML
|
114 |
-
xml_gt_img =
|
|
|
|
|
|
|
|
|
115 |
|
116 |
# Convert to RGB
|
117 |
-
flat_rgb = cv2.cvtColor(flat_img,
|
118 |
-
match_rgb = cv2.cvtColor(match_img,
|
119 |
-
roi_rgb = cv2.cvtColor(persp_roi,
|
120 |
-
xml_rgb = cv2.cvtColor(xml_gt_img,
|
121 |
|
122 |
-
# Determine max height
|
123 |
max_h = max(flat_rgb.shape[0], match_rgb.shape[0], roi_rgb.shape[0], xml_rgb.shape[0])
|
124 |
max_w = max(flat_rgb.shape[1], match_rgb.shape[1], roi_rgb.shape[1], xml_rgb.shape[1])
|
125 |
|
126 |
-
# Pad
|
127 |
flat_pad = pad_to_size(flat_rgb, max_h, max_w)
|
128 |
match_pad = pad_to_size(match_rgb, max_h, max_w)
|
129 |
roi_pad = pad_to_size(roi_rgb, max_h, max_w)
|
@@ -134,10 +123,10 @@ def homography_all_detectors(flat_file, persp_file, json_file, xml_file):
|
|
134 |
bottom = np.hstack([roi_pad, xml_pad])
|
135 |
combined_grid = np.vstack([top, bottom])
|
136 |
|
137 |
-
# Save
|
138 |
base_name = os.path.splitext(os.path.basename(persp_file))[0]
|
139 |
file_name = f"{base_name}_{method.lower()}.png"
|
140 |
-
cv2.imwrite(file_name, cv2.cvtColor(combined_grid,
|
141 |
gallery_paths.append(file_name)
|
142 |
download_files.append(file_name)
|
143 |
|
@@ -162,7 +151,7 @@ iface = gr.Interface(
|
|
162 |
gr.File(label="Download AKAZE Result")
|
163 |
],
|
164 |
title="Homography ROI + Feature Matching + XML GT",
|
165 |
-
description="
|
166 |
)
|
167 |
|
168 |
iface.launch()
|
|
|
4 |
import gradio as gr
|
5 |
import os
|
6 |
import xml.etree.ElementTree as ET
|
|
|
7 |
|
8 |
# ---------------- Helper functions ----------------
|
9 |
def get_rotated_rect_corners(x, y, w, h, rotation_deg):
|
|
|
39 |
def parse_xml_points(xml_file):
|
40 |
tree = ET.parse(xml_file)
|
41 |
root = tree.getroot()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
transform = root.find('.//transform')
|
43 |
points = {}
|
44 |
for pt in transform.findall('.//point'):
|
45 |
pt_type = pt.attrib['type']
|
46 |
+
x = float(pt.attrib['x'])
|
47 |
+
y = float(pt.attrib['y'])
|
48 |
points[pt_type] = (x, y)
|
49 |
return points
|
50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
# ---------------- Padding Helper ----------------
|
52 |
def pad_to_size(img, target_h, target_w):
|
53 |
h, w = img.shape[:2]
|
54 |
+
top_pad = 0
|
55 |
+
left_pad = 0
|
56 |
+
bottom_pad = target_h - h
|
57 |
+
right_pad = target_w - w
|
58 |
canvas = np.ones((target_h, target_w,3), dtype=np.uint8)*255
|
59 |
+
canvas[top_pad:top_pad+h, left_pad:left_pad+w] = img
|
60 |
return canvas
|
61 |
|
62 |
# ---------------- Main Function ----------------
|
|
|
72 |
flat_gray = preprocess_gray_clahe(flat_img)
|
73 |
persp_gray = preprocess_gray_clahe(persp_img)
|
74 |
xml_points = parse_xml_points(xml_file.name)
|
|
|
75 |
|
76 |
methods = ["SIFT","ORB","BRISK","KAZE","AKAZE"]
|
77 |
gallery_paths = []
|
|
|
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)
|
|
|
123 |
bottom = np.hstack([roi_pad, xml_pad])
|
124 |
combined_grid = np.vstack([top, bottom])
|
125 |
|
126 |
+
# Save grid
|
127 |
base_name = os.path.splitext(os.path.basename(persp_file))[0]
|
128 |
file_name = f"{base_name}_{method.lower()}.png"
|
129 |
+
cv2.imwrite(file_name, cv2.cvtColor(combined_grid,cv2.COLOR_RGB2BGR))
|
130 |
gallery_paths.append(file_name)
|
131 |
download_files.append(file_name)
|
132 |
|
|
|
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. Original resolution maintained. Grid aligned with white padding."
|
155 |
)
|
156 |
|
157 |
iface.launch()
|