Ayesha352 commited on
Commit
4c48aab
·
verified ·
1 Parent(s): c797e1a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -63
app.py CHANGED
@@ -71,7 +71,7 @@ def detect_and_match(img1_gray, img2_gray, method="SIFT", ratio_thresh=0.78):
71
  good.append(m)
72
  return kp1, kp2, good
73
 
74
- # ---------------- Main Homography Function (unchanged logic) ----------------
75
  def homography_all_detectors(flat_file, persp_file, json_file):
76
  flat_img = cv2.imread(flat_file.name)
77
  persp_img = cv2.imread(persp_file.name)
@@ -122,70 +122,30 @@ def homography_all_detectors(flat_file, persp_file, json_file):
122
  gallery_images.append((result_rgb, f"{method} Result"))
123
  download_files.append(file_name)
124
 
125
- # pad to 5 download slots
126
  while len(download_files) < 5:
127
  download_files.append(None)
128
 
129
- # return: gallery + 5 files
130
  return [gallery_images] + download_files[:5]
131
 
132
- # ---------------- Preview in the SAME Gallery (on upload) ----------------
133
- def preview_uploaded_gallery(flat_file, persp_file):
134
- """
135
- Returns a gallery list that shows the uploaded images with captions,
136
- without running homography. Used on upload change events.
137
- """
138
- items = []
139
- if flat_file is not None:
140
- flat_img = cv2.imread(flat_file.name)
141
- if flat_img is not None:
142
- items.append((cv2.cvtColor(flat_img, cv2.COLOR_BGR2RGB), "Flat (Uploaded)"))
143
- if persp_file is not None:def get_rotated_rect_corners(x, y, w, h, rotation_deg):
144
- r
145
- persp_img = cv2.imread(persp_file.name)
146
- if persp_img is not None:
147
- items.append((cv2.cvtColor(persp_img, cv2.COLOR_BGR2RGB), "Perspective (Uploaded)"))
148
- return items
149
-
150
- # ---------------- Gradio UI (keeps the look of previous interface) ----------------
151
- with gr.Blocks() as demo:
152
- gr.Markdown("# Homography ROI Projection with Multiple Feature Detectors")
153
- gr.Markdown("Upload flat & perspective images plus `mockup.json`. The gallery shows your uploads immediately, and detector results after you click **Run**. Download each result separately.")
154
-
155
- with gr.Row():
156
- flat_input = gr.File(label="Upload Flat Image", file_types=[".jpg", ".png", ".jpeg"])
157
- persp_input = gr.File(label="Upload Perspective Image", file_types=[".jpg", ".png", ".jpeg"])
158
- json_input = gr.File(label="Upload mockup.json", file_types=[".json"])
159
-
160
- # Single gallery (used both for previews and results)
161
- output_gallery = gr.Gallery(label="Gallery", show_label=True, columns=2, height=440)
162
-
163
- # Individual download buttons (same as before)
164
- download_sift = gr.File(label="Download SIFT Result")
165
- download_orb = gr.File(label="Download ORB Result")
166
- download_brisk = gr.File(label="Download BRISK Result")
167
- download_kaze = gr.File(label="Download KAZE Result")
168
- download_akaze = gr.File(label="Download AKAZE Result")
169
-
170
- run_btn = gr.Button("Run Homography")
171
-
172
- # Show previews in the SAME gallery as soon as files are uploaded/changed
173
- flat_input.change(
174
- preview_uploaded_gallery,
175
- inputs=[flat_input, persp_input],
176
- outputs=[output_gallery]
177
- )
178
- persp_input.change(
179
- preview_uploaded_gallery,
180
- inputs=[flat_input, persp_input],
181
- outputs=[output_gallery]
182
- )
183
-
184
- # On run, compute and replace gallery with detector results + populate downloads
185
- run_btn.click(
186
- fn=homography_all_detectors,
187
- inputs=[flat_input, persp_input, json_input],
188
- outputs=[output_gallery, download_sift, download_orb, download_brisk, download_kaze, download_akaze]
189
- )
190
-
191
- demo.launch()
 
71
  good.append(m)
72
  return kp1, kp2, good
73
 
74
+ # ---------------- Main Homography Function ----------------
75
  def homography_all_detectors(flat_file, persp_file, json_file):
76
  flat_img = cv2.imread(flat_file.name)
77
  persp_img = cv2.imread(persp_file.name)
 
122
  gallery_images.append((result_rgb, f"{method} Result"))
123
  download_files.append(file_name)
124
 
125
+ # return gallery + 5 download files (pad with None if less)
126
  while len(download_files) < 5:
127
  download_files.append(None)
128
 
 
129
  return [gallery_images] + download_files[:5]
130
 
131
+ # ---------------- Gradio UI ----------------
132
+ iface = gr.Interface(
133
+ fn=homography_all_detectors,
134
+ inputs=[
135
+ gr.File(label="Upload Flat Image", file_types=[".jpg",".png",".jpeg"]),
136
+ gr.File(label="Upload Perspective Image", file_types=[".jpg",".png",".jpeg"]),
137
+ gr.File(label="Upload mockup.json", file_types=[".json"])
138
+ ],
139
+ outputs=[
140
+ gr.Gallery(label="Results (per Detector)", show_label=True),
141
+ gr.File(label="Download SIFT Result"),
142
+ gr.File(label="Download ORB Result"),
143
+ gr.File(label="Download BRISK Result"),
144
+ gr.File(label="Download KAZE Result"),
145
+ gr.File(label="Download AKAZE Result")
146
+ ],
147
+ title="Homography ROI Projection with Multiple Feature Detectors",
148
+ description="Upload flat & perspective images with mockup.json. The system will project ROI using SIFT, ORB, BRISK, KAZE, and AKAZE. Each result can be viewed and downloaded."
149
+ )
150
+
151
+ iface.launch()