Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 ----------------
|
| 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,38 +122,70 @@ 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
|
| 126 |
while len(download_files) < 5:
|
| 127 |
download_files.append(None)
|
| 128 |
|
| 129 |
-
return
|
| 130 |
-
|
| 131 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
with gr.Blocks() as demo:
|
| 133 |
-
gr.Markdown("
|
|
|
|
| 134 |
|
| 135 |
with gr.Row():
|
| 136 |
-
flat_input = gr.File(label="Upload Flat Image", file_types=[".jpg",".png",".jpeg"])
|
| 137 |
-
persp_input = gr.File(label="Upload Perspective Image", file_types=[".jpg",".png",".jpeg"])
|
| 138 |
json_input = gr.File(label="Upload mockup.json", file_types=[".json"])
|
| 139 |
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
persp_preview = gr.Image(label="Perspective Image Preview")
|
| 143 |
-
|
| 144 |
-
output_gallery = gr.Gallery(label="Detector Results", show_label=True, columns=2, height=400)
|
| 145 |
|
| 146 |
-
|
| 147 |
-
|
|
|
|
| 148 |
download_brisk = gr.File(label="Download BRISK Result")
|
| 149 |
-
download_kaze
|
| 150 |
download_akaze = gr.File(label="Download AKAZE Result")
|
| 151 |
|
| 152 |
run_btn = gr.Button("Run Homography")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
run_btn.click(
|
| 154 |
-
fn=homography_all_detectors,
|
| 155 |
inputs=[flat_input, persp_input, json_input],
|
| 156 |
-
outputs=[
|
| 157 |
)
|
| 158 |
|
| 159 |
demo.launch()
|
|
|
|
| 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 |
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()
|