Ayesha352 commited on
Commit
da68729
·
verified ·
1 Parent(s): e4aeb4c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -22
app.py CHANGED
@@ -5,7 +5,6 @@ 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):
10
  rot_rad = np.deg2rad(rotation_deg)
11
  cos_r, sin_r = np.cos(rot_rad), np.sin(rot_rad)
@@ -45,7 +44,6 @@ 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
- # ---------------- Main Function ----------------
49
  def homography_all_detectors(flat_file, persp_file, json_file, xml_file):
50
  flat_img = cv2.imread(flat_file)
51
  persp_img = cv2.imread(persp_file)
@@ -60,17 +58,15 @@ def homography_all_detectors(flat_file, persp_file, json_file, xml_file):
60
  xml_points = parse_xml_points(xml_file.name)
61
 
62
  methods = ["SIFT","ORB","BRISK","KAZE","AKAZE"]
63
- gallery_images = []
64
  download_files = []
65
 
66
  for method in methods:
67
  kp1,kp2,good_matches = detect_and_match(flat_gray,persp_gray,method)
68
  if kp1 is None or kp2 is None or len(good_matches)<4: continue
69
 
70
- # Feature matching image
71
  match_img = cv2.drawMatches(flat_img,kp1,persp_img,kp2,good_matches,None,flags=2)
72
 
73
- # Homography & ROI mapping
74
  src_pts = np.float32([kp1[m.queryIdx].pt for m in good_matches]).reshape(-1,1,2)
75
  dst_pts = np.float32([kp2[m.trainIdx].pt for m in good_matches]).reshape(-1,1,2)
76
  H,_ = cv2.findHomography(src_pts,dst_pts,cv2.RANSAC,5.0)
@@ -82,34 +78,25 @@ def homography_all_detectors(flat_file, persp_file, json_file, xml_file):
82
  cv2.polylines(persp_roi,[roi_corners_persp.astype(int)],True,(0,255,0),2)
83
  for px,py in roi_corners_persp: cv2.circle(persp_roi,(int(px),int(py)),5,(255,0,0),-1)
84
 
85
- # XML Ground truth on perspective
86
  xml_gt_img = persp_img.copy()
87
  xml_mapped = cv2.perspectiveTransform(xml_points.reshape(-1,1,2),H).reshape(-1,2)
88
  for px,py in xml_mapped: cv2.circle(xml_gt_img,(int(px),int(py)),5,(0,0,255),-1)
89
 
90
- # Convert to RGB (original size kept)
91
- flat_rgb = cv2.cvtColor(flat_img,cv2.COLOR_BGR2RGB)
92
- match_rgb = cv2.cvtColor(match_img,cv2.COLOR_BGR2RGB)
93
- roi_rgb = cv2.cvtColor(persp_roi,cv2.COLOR_BGR2RGB)
94
- xml_rgb = cv2.cvtColor(xml_gt_img,cv2.COLOR_BGR2RGB)
95
-
96
- # Merge 2x2 grid keeping original resolutions
97
- top = np.hstack([flat_rgb, match_rgb])
98
- bottom = np.hstack([roi_rgb, xml_rgb])
99
  combined_grid = np.vstack([top, bottom])
100
 
101
- gallery_images.append((combined_grid,f"{method} Detector"))
102
-
103
- # Save combined grid for download
104
  base_name = os.path.splitext(os.path.basename(persp_file))[0]
105
  file_name = f"{base_name}_{method.lower()}.png"
106
- cv2.imwrite(file_name, cv2.cvtColor(combined_grid,cv2.COLOR_RGB2BGR))
 
107
  download_files.append(file_name)
108
 
109
  while len(download_files)<5: download_files.append(None)
110
- return [gallery_images]+download_files[:5]
111
 
112
- # ---------------- Gradio UI ----------------
113
  iface = gr.Interface(
114
  fn=homography_all_detectors,
115
  inputs=[
@@ -127,7 +114,7 @@ iface = gr.Interface(
127
  gr.File(label="Download AKAZE Result")
128
  ],
129
  title="Homography ROI Projection with Feature Matching & XML GT",
130
- description="Flat + Perspective images with mockup.json & XML. Shows 4 views per detector: Flat, Feature Matches, ROI Projection, XML Ground Truth. Original resolution kept."
131
  )
132
 
133
  iface.launch()
 
5
  import os
6
  import xml.etree.ElementTree as ET
7
 
 
8
  def get_rotated_rect_corners(x, y, w, h, rotation_deg):
9
  rot_rad = np.deg2rad(rotation_deg)
10
  cos_r, sin_r = np.cos(rot_rad), np.sin(rot_rad)
 
44
  points.append([float(elem.get("x")), float(elem.get("y"))])
45
  return np.array(points,dtype=np.float32).reshape(-1,2)
46
 
 
47
  def homography_all_detectors(flat_file, persp_file, json_file, xml_file):
48
  flat_img = cv2.imread(flat_file)
49
  persp_img = cv2.imread(persp_file)
 
58
  xml_points = parse_xml_points(xml_file.name)
59
 
60
  methods = ["SIFT","ORB","BRISK","KAZE","AKAZE"]
61
+ gallery_paths = []
62
  download_files = []
63
 
64
  for method in methods:
65
  kp1,kp2,good_matches = detect_and_match(flat_gray,persp_gray,method)
66
  if kp1 is None or kp2 is None or len(good_matches)<4: continue
67
 
 
68
  match_img = cv2.drawMatches(flat_img,kp1,persp_img,kp2,good_matches,None,flags=2)
69
 
 
70
  src_pts = np.float32([kp1[m.queryIdx].pt for m in good_matches]).reshape(-1,1,2)
71
  dst_pts = np.float32([kp2[m.trainIdx].pt for m in good_matches]).reshape(-1,1,2)
72
  H,_ = cv2.findHomography(src_pts,dst_pts,cv2.RANSAC,5.0)
 
78
  cv2.polylines(persp_roi,[roi_corners_persp.astype(int)],True,(0,255,0),2)
79
  for px,py in roi_corners_persp: cv2.circle(persp_roi,(int(px),int(py)),5,(255,0,0),-1)
80
 
 
81
  xml_gt_img = persp_img.copy()
82
  xml_mapped = cv2.perspectiveTransform(xml_points.reshape(-1,1,2),H).reshape(-1,2)
83
  for px,py in xml_mapped: cv2.circle(xml_gt_img,(int(px),int(py)),5,(0,0,255),-1)
84
 
85
+ # Merge 2x2 grid (original size)
86
+ top = np.hstack([flat_img, match_img])
87
+ bottom = np.hstack([persp_roi, xml_gt_img])
 
 
 
 
 
 
88
  combined_grid = np.vstack([top, bottom])
89
 
90
+ # Save combined grid
 
 
91
  base_name = os.path.splitext(os.path.basename(persp_file))[0]
92
  file_name = f"{base_name}_{method.lower()}.png"
93
+ cv2.imwrite(file_name, combined_grid)
94
+ gallery_paths.append(file_name)
95
  download_files.append(file_name)
96
 
97
  while len(download_files)<5: download_files.append(None)
98
+ return gallery_paths, download_files[0], download_files[1], download_files[2], download_files[3], download_files[4]
99
 
 
100
  iface = gr.Interface(
101
  fn=homography_all_detectors,
102
  inputs=[
 
114
  gr.File(label="Download AKAZE Result")
115
  ],
116
  title="Homography ROI Projection with Feature Matching & XML GT",
117
+ description="Flat + Perspective images with mockup.json & XML. Shows 4 views per detector. Original resolution kept."
118
  )
119
 
120
  iface.launch()