Ayesha352 commited on
Commit
bf80eca
·
verified ·
1 Parent(s): 2a8ccae

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -36
app.py CHANGED
@@ -3,7 +3,7 @@ import numpy as np
3
  import json
4
  import gradio as gr
5
 
6
- # Helper functions remain same
7
  def get_rotated_rect_corners(x, y, w, h, rotation_deg):
8
  rot_rad = np.deg2rad(rotation_deg)
9
  cos_r = np.cos(rot_rad)
@@ -67,11 +67,16 @@ def detect_and_match(img1_gray, img2_gray, method="SIFT", ratio_thresh=0.78):
67
  good.append(m)
68
  return kp1, kp2, good
69
 
70
- # Main function
71
- def homography_demo(flat_file, persp_file, json_file):
72
- flat_img = cv2.imread(flat_file.name)
73
- persp_img = cv2.imread(persp_file.name)
74
- mockup = json.load(open(json_file.name))
 
 
 
 
 
75
 
76
  roi_data = mockup["printAreas"][0]["position"]
77
  roi_x = roi_data["x"]
@@ -80,13 +85,14 @@ def homography_demo(flat_file, persp_file, json_file):
80
  roi_h = mockup["printAreas"][0]["height"]
81
  roi_rot_deg = mockup["printAreas"][0]["rotation"]
82
 
83
- flat_gray = preprocess_gray_clahe(flat_img)
84
- persp_gray = preprocess_gray_clahe(persp_img)
85
 
86
- methods = ["SIFT", "ORB", "BRISK", "KAZE", "AKAZE"]
87
- outputs = []
 
88
 
89
- for method in methods:
90
  kp1, kp2, good_matches = detect_and_match(flat_gray, persp_gray, method=method)
91
  if kp1 is None or kp2 is None or len(good_matches) < 4:
92
  continue
@@ -101,32 +107,38 @@ def homography_demo(flat_file, persp_file, json_file):
101
  roi_corners_flat = get_rotated_rect_corners(roi_x, roi_y, roi_w, roi_h, roi_rot_deg)
102
  roi_corners_persp = cv2.perspectiveTransform(roi_corners_flat.reshape(-1,1,2), H).reshape(-1,2)
103
 
104
- persp_debug = persp_img.copy()
105
  cv2.polylines(persp_debug, [roi_corners_persp.astype(int)], True, (0,255,0), 2)
106
  for (px, py) in roi_corners_persp:
107
  cv2.circle(persp_debug, (int(px), int(py)), 5, (255,0,0), -1)
108
 
109
- outputs.append((f"{method} Result", cv2.cvtColor(persp_debug, cv2.COLOR_BGR2RGB)))
110
-
111
- return outputs
112
-
113
- # Gradio UI
114
- with gr.Blocks() as demo:
115
- gr.Markdown("## Homography ROI Demo with Multiple Feature Detectors")
116
- with gr.Row():
117
- flat_input = gr.File(label="Upload Flat Image", file_types=[".jpg",".png",".jpeg"])
118
- persp_input = gr.File(label="Upload Perspective Image", file_types=[".jpg",".png",".jpeg"])
119
- json_input = gr.File(label="Upload mockup.json", file_types=[".json"])
120
- with gr.Row():
121
- flat_preview = gr.Image(type="filepath", label="Flat Image Preview", height=200)
122
- persp_preview = gr.Image(type="filepath", label="Perspective Image Preview", height=200)
123
- output_gallery = gr.Gallery(label="Perspective ROI Results", show_label=True, columns=2, height=600)
124
- run_btn = gr.Button("Run Homography")
125
-
126
- # Link previews
127
- flat_input.change(lambda f: f.name if f else None, inputs=flat_input, outputs=flat_preview)
128
- persp_input.change(lambda f: f.name if f else None, inputs=persp_input, outputs=persp_preview)
129
-
130
- run_btn.click(homography_demo, inputs=[flat_input, persp_input, json_input], outputs=output_gallery)
131
-
132
- demo.launch()
 
 
 
 
 
 
 
3
  import json
4
  import gradio as gr
5
 
6
+ # ---------------- Your Original Functions (Unchanged) ---------------- #
7
  def get_rotated_rect_corners(x, y, w, h, rotation_deg):
8
  rot_rad = np.deg2rad(rotation_deg)
9
  cos_r = np.cos(rot_rad)
 
67
  good.append(m)
68
  return kp1, kp2, good
69
 
70
+ # ---------------- Processing Function for Gradio ---------------- #
71
+ def homography_all_detectors(flat_img, persp_img, json_file):
72
+ if flat_img is None or persp_img is None:
73
+ return [None] * 6
74
+
75
+ flat_bgr = cv2.cvtColor(flat_img, cv2.COLOR_RGB2BGR)
76
+ persp_bgr = cv2.cvtColor(persp_img, cv2.COLOR_RGB2BGR)
77
+
78
+ with open(json_file.name, 'r') as f:
79
+ mockup = json.load(f)
80
 
81
  roi_data = mockup["printAreas"][0]["position"]
82
  roi_x = roi_data["x"]
 
85
  roi_h = mockup["printAreas"][0]["height"]
86
  roi_rot_deg = mockup["printAreas"][0]["rotation"]
87
 
88
+ flat_gray = preprocess_gray_clahe(flat_bgr)
89
+ persp_gray = preprocess_gray_clahe(persp_bgr)
90
 
91
+ detectors = ["SIFT", "ORB", "BRISK", "KAZE", "AKAZE"]
92
+ gallery_images = []
93
+ download_files = [None] * 5
94
 
95
+ for i, method in enumerate(detectors):
96
  kp1, kp2, good_matches = detect_and_match(flat_gray, persp_gray, method=method)
97
  if kp1 is None or kp2 is None or len(good_matches) < 4:
98
  continue
 
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
 
110
+ persp_debug = persp_bgr.copy()
111
  cv2.polylines(persp_debug, [roi_corners_persp.astype(int)], True, (0,255,0), 2)
112
  for (px, py) in roi_corners_persp:
113
  cv2.circle(persp_debug, (int(px), int(py)), 5, (255,0,0), -1)
114
 
115
+ result_rgb = cv2.cvtColor(persp_debug, cv2.COLOR_BGR2RGB)
116
+ file_name = f"result_{method.lower()}.png"
117
+ cv2.imwrite(file_name, result_rgb[:, :, ::-1]) # save as BGR
118
+
119
+ gallery_images.append((f"{method} Result", result_rgb))
120
+ download_files[i] = file_name
121
+
122
+ return [gallery_images] + download_files
123
+
124
+ # ---------------- Gradio Interface ---------------- #
125
+ iface = gr.Interface(
126
+ fn=homography_all_detectors,
127
+ inputs=[
128
+ gr.Image(type="numpy", label="Image 1 (Flat)"),
129
+ gr.Image(type="numpy", label="Image 2 (Perspective)"),
130
+ gr.File(type="filepath", label="JSON File")
131
+ ],
132
+ outputs=[
133
+ gr.Gallery(label="Results"),
134
+ gr.File(label="Download SIFT Result"),
135
+ gr.File(label="Download ORB Result"),
136
+ gr.File(label="Download BRISK Result"),
137
+ gr.File(label="Download KAZE Result"),
138
+ gr.File(label="Download AKAZE Result")
139
+ ],
140
+ title="Homography ROI Projection with Multiple Feature Detectors",
141
+ description="Upload a flat image, a perspective image, and the JSON file. The system will compute homography with SIFT, ORB, BRISK, KAZE, and AKAZE, project the bounding box, and allow result download."
142
+ )
143
+
144
+ iface.launch()