Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,9 +2,8 @@ import cv2
|
|
| 2 |
import numpy as np
|
| 3 |
import json
|
| 4 |
import gradio as gr
|
| 5 |
-
import matplotlib.pyplot as plt
|
| 6 |
|
| 7 |
-
#
|
| 8 |
def get_rotated_rect_corners(x, y, w, h, rotation_deg):
|
| 9 |
rot_rad = np.deg2rad(rotation_deg)
|
| 10 |
cos_r = np.cos(rot_rad)
|
|
@@ -27,13 +26,11 @@ def get_rotated_rect_corners(x, y, w, h, rotation_deg):
|
|
| 27 |
corners = rotated_corners + np.array([cx, cy])
|
| 28 |
return corners.astype(np.float32)
|
| 29 |
|
| 30 |
-
# === Preprocessing: Grayscale + CLAHE ===
|
| 31 |
def preprocess_gray_clahe(img):
|
| 32 |
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
| 33 |
clahe = cv2.createCLAHE(clipLimit=3.0, tileGridSize=(8, 8))
|
| 34 |
return clahe.apply(gray)
|
| 35 |
|
| 36 |
-
# === Detect and match features ===
|
| 37 |
def detect_and_match(img1_gray, img2_gray, method="SIFT", ratio_thresh=0.78):
|
| 38 |
if method == "SIFT":
|
| 39 |
sift = cv2.SIFT_create(nfeatures=5000)
|
|
@@ -70,11 +67,11 @@ def detect_and_match(img1_gray, img2_gray, method="SIFT", ratio_thresh=0.78):
|
|
| 70 |
good.append(m)
|
| 71 |
return kp1, kp2, good
|
| 72 |
|
| 73 |
-
#
|
| 74 |
def homography_demo(flat_file, persp_file, json_file):
|
| 75 |
-
flat_img = cv2.
|
| 76 |
-
persp_img = cv2.
|
| 77 |
-
mockup = json.load(json_file)
|
| 78 |
|
| 79 |
roi_data = mockup["printAreas"][0]["position"]
|
| 80 |
roi_x = roi_data["x"]
|
|
@@ -91,38 +88,31 @@ def homography_demo(flat_file, persp_file, json_file):
|
|
| 91 |
|
| 92 |
for method in methods:
|
| 93 |
kp1, kp2, good_matches = detect_and_match(flat_gray, persp_gray, method=method)
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
dst_pts = np.float32([kp2[m.trainIdx].pt for m in good_matches]).reshape(-1, 1, 2)
|
| 97 |
-
|
| 98 |
H, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0)
|
| 99 |
|
| 100 |
roi_corners_flat = get_rotated_rect_corners(roi_x, roi_y, roi_w, roi_h, roi_rot_deg)
|
| 101 |
-
roi_corners_persp = cv2.perspectiveTransform(roi_corners_flat.reshape(-1,
|
| 102 |
|
| 103 |
persp_debug = persp_img.copy()
|
| 104 |
-
cv2.polylines(persp_debug, [roi_corners_persp.astype(int)], True, (0,
|
| 105 |
for (px, py) in roi_corners_persp:
|
| 106 |
-
cv2.circle(persp_debug, (int(px), int(py)), 5, (255,
|
| 107 |
|
| 108 |
-
|
| 109 |
-
persp_debug_rgb = cv2.cvtColor(persp_debug, cv2.COLOR_BGR2RGB)
|
| 110 |
-
outputs.append((persp_debug_rgb, method))
|
| 111 |
|
| 112 |
return outputs
|
| 113 |
|
| 114 |
-
#
|
| 115 |
with gr.Blocks() as demo:
|
| 116 |
gr.Markdown("## Homography ROI Demo with Multiple Feature Detectors")
|
| 117 |
with gr.Row():
|
| 118 |
-
flat_input = gr.File(label="Upload Flat Image")
|
| 119 |
-
persp_input = gr.File(label="Upload Perspective Image")
|
| 120 |
-
json_input = gr.File(label="Upload mockup.json")
|
| 121 |
-
output_gallery = gr.Gallery(label="Perspective ROI Results",
|
| 122 |
run_btn = gr.Button("Run Homography")
|
| 123 |
-
|
| 124 |
-
run_btn.click(homography_demo,
|
| 125 |
-
inputs=[flat_input, persp_input, json_input],
|
| 126 |
-
outputs=output_gallery)
|
| 127 |
|
| 128 |
demo.launch()
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
import json
|
| 4 |
import gradio as gr
|
|
|
|
| 5 |
|
| 6 |
+
# Helper functions remain same
|
| 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)
|
|
|
|
| 26 |
corners = rotated_corners + np.array([cx, cy])
|
| 27 |
return corners.astype(np.float32)
|
| 28 |
|
|
|
|
| 29 |
def preprocess_gray_clahe(img):
|
| 30 |
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
| 31 |
clahe = cv2.createCLAHE(clipLimit=3.0, tileGridSize=(8, 8))
|
| 32 |
return clahe.apply(gray)
|
| 33 |
|
|
|
|
| 34 |
def detect_and_match(img1_gray, img2_gray, method="SIFT", ratio_thresh=0.78):
|
| 35 |
if method == "SIFT":
|
| 36 |
sift = cv2.SIFT_create(nfeatures=5000)
|
|
|
|
| 67 |
good.append(m)
|
| 68 |
return kp1, kp2, good
|
| 69 |
|
| 70 |
+
# Main function
|
| 71 |
def homography_demo(flat_file, persp_file, json_file):
|
| 72 |
+
flat_img = cv2.imread(flat_file.name)
|
| 73 |
+
persp_img = cv2.imread(persp_file.name)
|
| 74 |
+
mockup = json.load(open(json_file.name))
|
| 75 |
|
| 76 |
roi_data = mockup["printAreas"][0]["position"]
|
| 77 |
roi_x = roi_data["x"]
|
|
|
|
| 88 |
|
| 89 |
for method in methods:
|
| 90 |
kp1, kp2, good_matches = detect_and_match(flat_gray, persp_gray, method=method)
|
| 91 |
+
src_pts = np.float32([kp1[m.queryIdx].pt for m in good_matches]).reshape(-1,1,2)
|
| 92 |
+
dst_pts = np.float32([kp2[m.trainIdx].pt for m in good_matches]).reshape(-1,1,2)
|
|
|
|
|
|
|
| 93 |
H, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0)
|
| 94 |
|
| 95 |
roi_corners_flat = get_rotated_rect_corners(roi_x, roi_y, roi_w, roi_h, roi_rot_deg)
|
| 96 |
+
roi_corners_persp = cv2.perspectiveTransform(roi_corners_flat.reshape(-1,1,2), H).reshape(-1,2)
|
| 97 |
|
| 98 |
persp_debug = persp_img.copy()
|
| 99 |
+
cv2.polylines(persp_debug, [roi_corners_persp.astype(int)], True, (0,255,0), 2)
|
| 100 |
for (px, py) in roi_corners_persp:
|
| 101 |
+
cv2.circle(persp_debug, (int(px), int(py)), 5, (255,0,0), -1)
|
| 102 |
|
| 103 |
+
outputs.append(cv2.cvtColor(persp_debug, cv2.COLOR_BGR2RGB))
|
|
|
|
|
|
|
| 104 |
|
| 105 |
return outputs
|
| 106 |
|
| 107 |
+
# Gradio UI
|
| 108 |
with gr.Blocks() as demo:
|
| 109 |
gr.Markdown("## Homography ROI Demo with Multiple Feature Detectors")
|
| 110 |
with gr.Row():
|
| 111 |
+
flat_input = gr.File(label="Upload Flat Image", file_types=[".jpg",".png",".jpeg"])
|
| 112 |
+
persp_input = gr.File(label="Upload Perspective Image", file_types=[".jpg",".png",".jpeg"])
|
| 113 |
+
json_input = gr.File(label="Upload mockup.json", file_types=[".json"])
|
| 114 |
+
output_gallery = gr.Gallery(label="Perspective ROI Results", columns=2, height=400)
|
| 115 |
run_btn = gr.Button("Run Homography")
|
| 116 |
+
run_btn.click(homography_demo, inputs=[flat_input, persp_input, json_input], outputs=output_gallery)
|
|
|
|
|
|
|
|
|
|
| 117 |
|
| 118 |
demo.launch()
|