Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,7 @@ import cv2
|
|
| 2 |
import numpy as np
|
| 3 |
import json
|
| 4 |
import gradio as gr
|
|
|
|
| 5 |
|
| 6 |
# ---------------- Helper functions ----------------
|
| 7 |
def get_rotated_rect_corners(x, y, w, h, rotation_deg):
|
|
@@ -87,7 +88,8 @@ def homography_all_detectors(flat_file, persp_file, json_file):
|
|
| 87 |
persp_gray = preprocess_gray_clahe(persp_img)
|
| 88 |
|
| 89 |
methods = ["SIFT", "ORB", "BRISK", "KAZE", "AKAZE"]
|
| 90 |
-
|
|
|
|
| 91 |
|
| 92 |
for method in methods:
|
| 93 |
kp1, kp2, good_matches = detect_and_match(flat_gray, persp_gray, method=method)
|
|
@@ -113,27 +115,37 @@ def homography_all_detectors(flat_file, persp_file, json_file):
|
|
| 113 |
# Convert BGR -> RGB for display
|
| 114 |
result_rgb = cv2.cvtColor(persp_debug, cv2.COLOR_BGR2RGB)
|
| 115 |
|
| 116 |
-
|
|
|
|
|
|
|
| 117 |
|
| 118 |
-
|
|
|
|
| 119 |
|
| 120 |
-
#
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
with gr.Row():
|
| 125 |
-
flat_input = gr.File(label="Upload Flat Image", file_types=[".jpg",".png",".jpeg"])
|
| 126 |
-
persp_input = gr.File(label="Upload Perspective Image", file_types=[".jpg",".png",".jpeg"])
|
| 127 |
-
json_input = gr.File(label="Upload mockup.json", file_types=[".json"])
|
| 128 |
|
| 129 |
-
|
| 130 |
-
label="Perspective ROI Results",
|
| 131 |
-
columns=2,
|
| 132 |
-
height=400,
|
| 133 |
-
show_label=True
|
| 134 |
-
)
|
| 135 |
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
import json
|
| 4 |
import gradio as gr
|
| 5 |
+
import os
|
| 6 |
|
| 7 |
# ---------------- Helper functions ----------------
|
| 8 |
def get_rotated_rect_corners(x, y, w, h, rotation_deg):
|
|
|
|
| 88 |
persp_gray = preprocess_gray_clahe(persp_img)
|
| 89 |
|
| 90 |
methods = ["SIFT", "ORB", "BRISK", "KAZE", "AKAZE"]
|
| 91 |
+
gallery_images = []
|
| 92 |
+
download_files = []
|
| 93 |
|
| 94 |
for method in methods:
|
| 95 |
kp1, kp2, good_matches = detect_and_match(flat_gray, persp_gray, method=method)
|
|
|
|
| 115 |
# Convert BGR -> RGB for display
|
| 116 |
result_rgb = cv2.cvtColor(persp_debug, cv2.COLOR_BGR2RGB)
|
| 117 |
|
| 118 |
+
# Save result for download
|
| 119 |
+
file_name = f"result_{method.lower()}.png"
|
| 120 |
+
cv2.imwrite(file_name, cv2.cvtColor(result_rgb, cv2.COLOR_RGB2BGR))
|
| 121 |
|
| 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()
|