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)
|
|
@@ -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 |
-
#
|
| 71 |
-
def
|
| 72 |
-
flat_img
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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(
|
| 84 |
-
persp_gray = preprocess_gray_clahe(
|
| 85 |
|
| 86 |
-
|
| 87 |
-
|
|
|
|
| 88 |
|
| 89 |
-
for method in
|
| 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 =
|
| 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 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|