Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,7 +3,7 @@ import numpy as np
|
|
| 3 |
import json
|
| 4 |
import gradio as gr
|
| 5 |
|
| 6 |
-
# ----------------
|
| 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)
|
|
@@ -58,7 +58,10 @@ def detect_and_match(img1_gray, img2_gray, method="SIFT", ratio_thresh=0.78):
|
|
| 58 |
kp2, des2 = akaze.detectAndCompute(img2_gray, None)
|
| 59 |
matcher = cv2.BFMatcher(cv2.NORM_HAMMING)
|
| 60 |
else:
|
| 61 |
-
return None, None,
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
raw_matches = matcher.knnMatch(des1, des2, k=2)
|
| 64 |
good = []
|
|
@@ -67,16 +70,11 @@ 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 |
-
# ----------------
|
| 71 |
-
def homography_all_detectors(
|
| 72 |
-
|
| 73 |
-
|
| 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,40 +83,18 @@ def homography_all_detectors(flat_img, persp_img, json_file):
|
|
| 85 |
roi_h = mockup["printAreas"][0]["height"]
|
| 86 |
roi_rot_deg = mockup["printAreas"][0]["rotation"]
|
| 87 |
|
| 88 |
-
flat_gray = preprocess_gray_clahe(
|
| 89 |
-
persp_gray = preprocess_gray_clahe(
|
| 90 |
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
download_files = [None] * 5def homography_all_detectors(flat_img, persp_img, json_file):
|
| 94 |
-
if flat_img is None or persp_img is None:
|
| 95 |
-
return [None] * 6
|
| 96 |
-
|
| 97 |
-
flat_bgr = cv2.cvtColor(flat_img, cv2.COLOR_RGB2BGR)
|
| 98 |
-
persp_bgr = cv2.cvtColor(persp_img, cv2.COLOR_RGB2BGR)
|
| 99 |
|
| 100 |
-
|
| 101 |
-
mockup = json.load(f)
|
| 102 |
-
|
| 103 |
-
roi_data = mockup["printAreas"][0]["position"]
|
| 104 |
-
roi_x = roi_data["x"]
|
| 105 |
-
roi_y = roi_data["y"]
|
| 106 |
-
roi_w = mockup["printAreas"][0]["width"]
|
| 107 |
-
roi_h = mockup["printAreas"][0]["height"]
|
| 108 |
-
roi_rot_deg = mockup["printAreas"][0]["rotation"]
|
| 109 |
-
|
| 110 |
-
flat_gray = preprocess_gray_clahe(flat_bgr)
|
| 111 |
-
persp_gray = preprocess_gray_clahe(persp_bgr)
|
| 112 |
-
|
| 113 |
-
detectors = ["SIFT", "ORB", "BRISK", "KAZE", "AKAZE"]
|
| 114 |
-
gallery_images = []
|
| 115 |
-
download_files = [None] * 5
|
| 116 |
-
|
| 117 |
-
for i, method in enumerate(detectors):
|
| 118 |
kp1, kp2, good_matches = detect_and_match(flat_gray, persp_gray, method=method)
|
|
|
|
| 119 |
if kp1 is None or kp2 is None or len(good_matches) < 4:
|
| 120 |
-
continue
|
| 121 |
-
|
| 122 |
src_pts = np.float32([kp1[m.queryIdx].pt for m in good_matches]).reshape(-1,1,2)
|
| 123 |
dst_pts = np.float32([kp2[m.trainIdx].pt for m in good_matches]).reshape(-1,1,2)
|
| 124 |
H, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0)
|
|
@@ -129,68 +105,35 @@ def homography_all_detectors(flat_img, persp_img, json_file):
|
|
| 129 |
roi_corners_flat = get_rotated_rect_corners(roi_x, roi_y, roi_w, roi_h, roi_rot_deg)
|
| 130 |
roi_corners_persp = cv2.perspectiveTransform(roi_corners_flat.reshape(-1,1,2), H).reshape(-1,2)
|
| 131 |
|
| 132 |
-
persp_debug =
|
| 133 |
cv2.polylines(persp_debug, [roi_corners_persp.astype(int)], True, (0,255,0), 2)
|
| 134 |
for (px, py) in roi_corners_persp:
|
| 135 |
cv2.circle(persp_debug, (int(px), int(py)), 5, (255,0,0), -1)
|
| 136 |
|
|
|
|
| 137 |
result_rgb = cv2.cvtColor(persp_debug, cv2.COLOR_BGR2RGB)
|
| 138 |
-
file_name = f"result_{method.lower()}.png"
|
| 139 |
-
cv2.imwrite(file_name, result_rgb[:, :, ::-1]) # save as BGR
|
| 140 |
|
| 141 |
-
|
| 142 |
-
download_files[i] = file_name
|
| 143 |
|
| 144 |
-
return
|
| 145 |
|
|
|
|
|
|
|
|
|
|
| 146 |
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
src_pts = np.float32([kp1[m.queryIdx].pt for m in good_matches]).reshape(-1,1,2)
|
| 153 |
-
dst_pts = np.float32([kp2[m.trainIdx].pt for m in good_matches]).reshape(-1,1,2)
|
| 154 |
-
H, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0)
|
| 155 |
|
| 156 |
-
|
| 157 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
|
| 159 |
-
|
| 160 |
-
|
| 161 |
|
| 162 |
-
|
| 163 |
-
cv2.polylines(persp_debug, [roi_corners_persp.astype(int)], True, (0,255,0), 2)
|
| 164 |
-
for (px, py) in roi_corners_persp:
|
| 165 |
-
cv2.circle(persp_debug, (int(px), int(py)), 5, (255,0,0), -1)
|
| 166 |
-
|
| 167 |
-
result_rgb = cv2.cvtColor(persp_debug, cv2.COLOR_BGR2RGB)
|
| 168 |
-
file_name = f"result_{method.lower()}.png"
|
| 169 |
-
cv2.imwrite(file_name, result_rgb[:, :, ::-1]) # save as BGR
|
| 170 |
-
|
| 171 |
-
gallery_images.append(result_rgb) # ✅ only image
|
| 172 |
-
download_files[i] = file_name
|
| 173 |
-
|
| 174 |
-
return [gallery_images] + download_files
|
| 175 |
-
|
| 176 |
-
# ---------------- Gradio Interface ---------------- #
|
| 177 |
-
iface = gr.Interface(
|
| 178 |
-
fn=homography_all_detectors,
|
| 179 |
-
inputs=[
|
| 180 |
-
gr.Image(type="numpy", label="Image 1 (Flat)"),
|
| 181 |
-
gr.Image(type="numpy", label="Image 2 (Perspective)"),
|
| 182 |
-
gr.File(type="filepath", label="JSON File")
|
| 183 |
-
],
|
| 184 |
-
outputs=[
|
| 185 |
-
gr.Gallery(label="Results"),
|
| 186 |
-
gr.File(label="Download SIFT Result"),
|
| 187 |
-
gr.File(label="Download ORB Result"),
|
| 188 |
-
gr.File(label="Download BRISK Result"),
|
| 189 |
-
gr.File(label="Download KAZE Result"),
|
| 190 |
-
gr.File(label="Download AKAZE Result")
|
| 191 |
-
],
|
| 192 |
-
title="Homography ROI Projection with Multiple Feature Detectors",
|
| 193 |
-
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."
|
| 194 |
-
)
|
| 195 |
-
|
| 196 |
-
iface.launch()
|
|
|
|
| 3 |
import json
|
| 4 |
import gradio as gr
|
| 5 |
|
| 6 |
+
# ---------------- Helper functions ----------------
|
| 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)
|
|
|
|
| 58 |
kp2, des2 = akaze.detectAndCompute(img2_gray, None)
|
| 59 |
matcher = cv2.BFMatcher(cv2.NORM_HAMMING)
|
| 60 |
else:
|
| 61 |
+
return None, None, []
|
| 62 |
+
|
| 63 |
+
if des1 is None or des2 is None:
|
| 64 |
+
return None, None, []
|
| 65 |
|
| 66 |
raw_matches = matcher.knnMatch(des1, des2, k=2)
|
| 67 |
good = []
|
|
|
|
| 70 |
good.append(m)
|
| 71 |
return kp1, kp2, good
|
| 72 |
|
| 73 |
+
# ---------------- Main Homography Function ----------------
|
| 74 |
+
def homography_all_detectors(flat_file, persp_file, json_file):
|
| 75 |
+
flat_img = cv2.imread(flat_file.name)
|
| 76 |
+
persp_img = cv2.imread(persp_file.name)
|
| 77 |
+
mockup = json.load(open(json_file.name))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
roi_data = mockup["printAreas"][0]["position"]
|
| 80 |
roi_x = roi_data["x"]
|
|
|
|
| 83 |
roi_h = mockup["printAreas"][0]["height"]
|
| 84 |
roi_rot_deg = mockup["printAreas"][0]["rotation"]
|
| 85 |
|
| 86 |
+
flat_gray = preprocess_gray_clahe(flat_img)
|
| 87 |
+
persp_gray = preprocess_gray_clahe(persp_img)
|
| 88 |
|
| 89 |
+
methods = ["SIFT", "ORB", "BRISK", "KAZE", "AKAZE"]
|
| 90 |
+
outputs = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
+
for method in methods:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
kp1, kp2, good_matches = detect_and_match(flat_gray, persp_gray, method=method)
|
| 94 |
+
|
| 95 |
if kp1 is None or kp2 is None or len(good_matches) < 4:
|
| 96 |
+
continue # skip if no matches
|
| 97 |
+
|
| 98 |
src_pts = np.float32([kp1[m.queryIdx].pt for m in good_matches]).reshape(-1,1,2)
|
| 99 |
dst_pts = np.float32([kp2[m.trainIdx].pt for m in good_matches]).reshape(-1,1,2)
|
| 100 |
H, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0)
|
|
|
|
| 105 |
roi_corners_flat = get_rotated_rect_corners(roi_x, roi_y, roi_w, roi_h, roi_rot_deg)
|
| 106 |
roi_corners_persp = cv2.perspectiveTransform(roi_corners_flat.reshape(-1,1,2), H).reshape(-1,2)
|
| 107 |
|
| 108 |
+
persp_debug = persp_img.copy()
|
| 109 |
cv2.polylines(persp_debug, [roi_corners_persp.astype(int)], True, (0,255,0), 2)
|
| 110 |
for (px, py) in roi_corners_persp:
|
| 111 |
cv2.circle(persp_debug, (int(px), int(py)), 5, (255,0,0), -1)
|
| 112 |
|
| 113 |
+
# Convert BGR -> RGB for display
|
| 114 |
result_rgb = cv2.cvtColor(persp_debug, cv2.COLOR_BGR2RGB)
|
|
|
|
|
|
|
| 115 |
|
| 116 |
+
outputs.append((result_rgb, f"{method} Result"))
|
|
|
|
| 117 |
|
| 118 |
+
return outputs
|
| 119 |
|
| 120 |
+
# ---------------- Gradio UI ----------------
|
| 121 |
+
with gr.Blocks() as demo:
|
| 122 |
+
gr.Markdown("## Homography ROI Demo with Multiple Feature Detectors")
|
| 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 |
+
output_gallery = gr.Gallery(
|
| 130 |
+
label="Perspective ROI Results",
|
| 131 |
+
columns=2,
|
| 132 |
+
height=400,
|
| 133 |
+
show_label=True
|
| 134 |
+
)
|
| 135 |
|
| 136 |
+
run_btn = gr.Button("Run Homography")
|
| 137 |
+
run_btn.click(homography_all_detectors, inputs=[flat_input, persp_input, json_input], outputs=output_gallery)
|
| 138 |
|
| 139 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|