Ayesha352 commited on
Commit
b6c2f72
·
verified ·
1 Parent(s): 6ba6b88

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -26
app.py CHANGED
@@ -4,6 +4,7 @@ import json
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):
@@ -45,25 +46,33 @@ def parse_xml_points(xml_file):
45
  points.append([float(elem.get("x")), float(elem.get("y"))])
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
- # ---------------- Draw XML Ground-Truth ----------------
60
- def draw_xml_gt(img, xml_points):
61
- for px,py in xml_points:
62
- cv2.circle(img,(int(px),int(py)),5,(0,0,255),-1)
63
- # Draw polygon
64
- cv2.polylines(img,[xml_points.astype(np.int32)],True,(255,0,0),2)
65
- return img
66
-
67
  # ---------------- Main Function ----------------
68
  def homography_all_detectors(flat_file, persp_file, json_file, xml_file):
69
  flat_img = cv2.imread(flat_file)
@@ -77,6 +86,7 @@ def homography_all_detectors(flat_file, persp_file, json_file, xml_file):
77
  flat_gray = preprocess_gray_clahe(flat_img)
78
  persp_gray = preprocess_gray_clahe(persp_img)
79
  xml_points = parse_xml_points(xml_file.name)
 
80
 
81
  methods = ["SIFT","ORB","BRISK","KAZE","AKAZE"]
82
  gallery_paths = []
@@ -93,7 +103,7 @@ def homography_all_detectors(flat_file, persp_file, json_file, xml_file):
93
  H,_ = cv2.findHomography(src_pts,dst_pts,cv2.RANSAC,5.0)
94
  if H is None: continue
95
 
96
- # ROI homography projection
97
  roi_corners_flat = get_rotated_rect_corners(roi_x,roi_y,roi_w,roi_h,roi_rot_deg)
98
  roi_corners_persp = cv2.perspectiveTransform(roi_corners_flat.reshape(-1,1,2),H).reshape(-1,2)
99
  persp_roi = persp_img.copy()
@@ -101,20 +111,19 @@ def homography_all_detectors(flat_file, persp_file, json_file, xml_file):
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
- xml_gt_img = draw_xml_gt(xml_gt_img, xml_points)
106
 
107
  # Convert to RGB
108
- flat_rgb = cv2.cvtColor(flat_img,cv2.COLOR_BGR2RGB)
109
- match_rgb = cv2.cvtColor(match_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
- # Determine max height and width for padding
114
  max_h = max(flat_rgb.shape[0], match_rgb.shape[0], roi_rgb.shape[0], xml_rgb.shape[0])
115
  max_w = max(flat_rgb.shape[1], match_rgb.shape[1], roi_rgb.shape[1], xml_rgb.shape[1])
116
 
117
- # Pad all images to same size
118
  flat_pad = pad_to_size(flat_rgb, max_h, max_w)
119
  match_pad = pad_to_size(match_rgb, max_h, max_w)
120
  roi_pad = pad_to_size(roi_rgb, max_h, max_w)
@@ -125,10 +134,10 @@ def homography_all_detectors(flat_file, persp_file, json_file, xml_file):
125
  bottom = np.hstack([roi_pad, xml_pad])
126
  combined_grid = np.vstack([top, bottom])
127
 
128
- # Save combined grid
129
  base_name = os.path.splitext(os.path.basename(persp_file))[0]
130
  file_name = f"{base_name}_{method.lower()}.png"
131
- cv2.imwrite(file_name, cv2.cvtColor(combined_grid,cv2.COLOR_RGB2BGR))
132
  gallery_paths.append(file_name)
133
  download_files.append(file_name)
134
 
@@ -152,8 +161,8 @@ iface = gr.Interface(
152
  gr.File(label="Download KAZE Result"),
153
  gr.File(label="Download AKAZE Result")
154
  ],
155
- title="Homography ROI Projection with Feature Matching & XML GT",
156
- description="Flat + Perspective images with mockup.json & XML. Original resolution maintained. Grid aligned with white padding. XML GT overlaid."
157
  )
158
 
159
  iface.launch()
 
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):
 
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 = int(float(pt.attrib['x']))
57
+ y = int(float(pt.attrib['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 ----------------
77
  def homography_all_detectors(flat_file, persp_file, json_file, xml_file):
78
  flat_img = cv2.imread(flat_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
  H,_ = cv2.findHomography(src_pts,dst_pts,cv2.RANSAC,5.0)
104
  if H is None: continue
105
 
106
+ # Homography-based ROI overlay
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()
 
111
  for px,py in roi_corners_persp: cv2.circle(persp_roi,(int(px),int(py)),5,(255,0,0),-1)
112
 
113
  # XML GT overlay
114
+ xml_gt_img = draw_polygon_overlay(persp_img, xml_dict)
 
115
 
116
  # Convert to RGB
117
+ flat_rgb = cv2.cvtColor(flat_img, cv2.COLOR_BGR2RGB)
118
+ match_rgb = cv2.cvtColor(match_img, cv2.COLOR_BGR2RGB)
119
+ roi_rgb = cv2.cvtColor(persp_roi, cv2.COLOR_BGR2RGB)
120
+ xml_rgb = cv2.cvtColor(xml_gt_img, cv2.COLOR_BGR2RGB)
121
 
122
+ # Determine max height/width
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 all images
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
  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, cv2.COLOR_RGB2BGR))
141
  gallery_paths.append(file_name)
142
  download_files.append(file_name)
143
 
 
161
  gr.File(label="Download KAZE Result"),
162
  gr.File(label="Download AKAZE Result")
163
  ],
164
+ title="Homography ROI + Feature Matching + XML GT",
165
+ description="Shows Flat, Feature-Matched, Homography ROI (green), and XML Ground-Truth (red) overlay in a 2x2 grid with same size."
166
  )
167
 
168
  iface.launch()