Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -5,15 +5,6 @@ import gradio as gr
|
|
5 |
import os
|
6 |
import xml.etree.ElementTree as ET
|
7 |
|
8 |
-
def get_rotated_rect_corners(x, y, w, h, rotation_deg):
|
9 |
-
rot_rad = np.deg2rad(rotation_deg)
|
10 |
-
cos_r, sin_r = np.cos(rot_rad), np.sin(rot_rad)import cv2
|
11 |
-
import numpy as np
|
12 |
-
import json
|
13 |
-
import gradio as gr
|
14 |
-
import os
|
15 |
-
import xml.etree.ElementTree as ET
|
16 |
-
|
17 |
def get_rotated_rect_corners(x, y, w, h, rotation_deg):
|
18 |
rot_rad = np.deg2rad(rotation_deg)
|
19 |
cos_r, sin_r = np.cos(rot_rad), np.sin(rot_rad)
|
@@ -126,115 +117,4 @@ iface = gr.Interface(
|
|
126 |
description="Flat + Perspective images with mockup.json & XML. Shows 4 views per detector. Original resolution kept."
|
127 |
)
|
128 |
|
129 |
-
iface.launch()
|
130 |
-
|
131 |
-
R = np.array([[cos_r, -sin_r], [sin_r, cos_r]])
|
132 |
-
cx, cy = x + w/2, y + h/2
|
133 |
-
local_corners = np.array([[-w/2,-h/2],[w/2,-h/2],[w/2,h/2],[-w/2,h/2]])
|
134 |
-
rotated_corners = np.dot(local_corners, R.T)
|
135 |
-
return (rotated_corners + np.array([cx,cy])).astype(np.float32)
|
136 |
-
|
137 |
-
def preprocess_gray_clahe(img):
|
138 |
-
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
139 |
-
clahe = cv2.createCLAHE(clipLimit=3.0, tileGridSize=(8,8))
|
140 |
-
return clahe.apply(gray)
|
141 |
-
|
142 |
-
def detect_and_match(img1_gray, img2_gray, method="SIFT", ratio_thresh=0.78):
|
143 |
-
if method=="SIFT": detector=cv2.SIFT_create(nfeatures=5000); matcher=cv2.BFMatcher(cv2.NORM_L2)
|
144 |
-
elif method=="ORB": detector=cv2.ORB_create(5000); matcher=cv2.BFMatcher(cv2.NORM_HAMMING)
|
145 |
-
elif method=="BRISK": detector=cv2.BRISK_create(); matcher=cv2.BFMatcher(cv2.NORM_HAMMING)
|
146 |
-
elif method=="KAZE": detector=cv2.KAZE_create(); matcher=cv2.BFMatcher(cv2.NORM_L2)
|
147 |
-
elif method=="AKAZE": detector=cv2.AKAZE_create(); matcher=cv2.BFMatcher(cv2.NORM_HAMMING)
|
148 |
-
else: return None,None,[]
|
149 |
-
|
150 |
-
kp1, des1 = detector.detectAndCompute(img1_gray,None)
|
151 |
-
kp2, des2 = detector.detectAndCompute(img2_gray,None)
|
152 |
-
if des1 is None or des2 is None: return None,None,[]
|
153 |
-
|
154 |
-
raw_matches = matcher.knnMatch(des1,des2,k=2)
|
155 |
-
good = [m for m,n in raw_matches if m.distance < ratio_thresh*n.distance]
|
156 |
-
return kp1, kp2, good
|
157 |
-
|
158 |
-
def parse_xml_points(xml_file):
|
159 |
-
tree = ET.parse(xml_file)
|
160 |
-
root = tree.getroot()
|
161 |
-
points=[]
|
162 |
-
for pt_type in ["TopLeft","TopRight","BottomLeft","BottomRight"]:
|
163 |
-
elem=root.find(f".//point[@type='{pt_type}']")
|
164 |
-
points.append([float(elem.get("x")), float(elem.get("y"))])
|
165 |
-
return np.array(points,dtype=np.float32).reshape(-1,2)
|
166 |
-
|
167 |
-
def homography_all_detectors(flat_file, persp_file, json_file, xml_file):
|
168 |
-
flat_img = cv2.imread(flat_file)
|
169 |
-
persp_img = cv2.imread(persp_file)
|
170 |
-
mockup = json.load(open(json_file.name))
|
171 |
-
roi_data = mockup["printAreas"][0]["position"]
|
172 |
-
roi_x, roi_y = roi_data["x"], roi_data["y"]
|
173 |
-
roi_w, roi_h = mockup["printAreas"][0]["width"], mockup["printAreas"][0]["height"]
|
174 |
-
roi_rot_deg = mockup["printAreas"][0]["rotation"]
|
175 |
-
|
176 |
-
flat_gray = preprocess_gray_clahe(flat_img)
|
177 |
-
persp_gray = preprocess_gray_clahe(persp_img)
|
178 |
-
xml_points = parse_xml_points(xml_file.name)
|
179 |
-
|
180 |
-
methods = ["SIFT","ORB","BRISK","KAZE","AKAZE"]
|
181 |
-
gallery_paths = []
|
182 |
-
download_files = []
|
183 |
-
|
184 |
-
for method in methods:
|
185 |
-
kp1,kp2,good_matches = detect_and_match(flat_gray,persp_gray,method)
|
186 |
-
if kp1 is None or kp2 is None or len(good_matches)<4: continue
|
187 |
-
|
188 |
-
match_img = cv2.drawMatches(flat_img,kp1,persp_img,kp2,good_matches,None,flags=2)
|
189 |
-
|
190 |
-
src_pts = np.float32([kp1[m.queryIdx].pt for m in good_matches]).reshape(-1,1,2)
|
191 |
-
dst_pts = np.float32([kp2[m.trainIdx].pt for m in good_matches]).reshape(-1,1,2)
|
192 |
-
H,_ = cv2.findHomography(src_pts,dst_pts,cv2.RANSAC,5.0)
|
193 |
-
if H is None: continue
|
194 |
-
|
195 |
-
roi_corners_flat = get_rotated_rect_corners(roi_x,roi_y,roi_w,roi_h,roi_rot_deg)
|
196 |
-
roi_corners_persp = cv2.perspectiveTransform(roi_corners_flat.reshape(-1,1,2),H).reshape(-1,2)
|
197 |
-
persp_roi = persp_img.copy()
|
198 |
-
cv2.polylines(persp_roi,[roi_corners_persp.astype(int)],True,(0,255,0),2)
|
199 |
-
for px,py in roi_corners_persp: cv2.circle(persp_roi,(int(px),int(py)),5,(255,0,0),-1)
|
200 |
-
|
201 |
-
xml_gt_img = persp_img.copy()
|
202 |
-
xml_mapped = cv2.perspectiveTransform(xml_points.reshape(-1,1,2),H).reshape(-1,2)
|
203 |
-
for px,py in xml_mapped: cv2.circle(xml_gt_img,(int(px),int(py)),5,(0,0,255),-1)
|
204 |
-
|
205 |
-
# Merge 2x2 grid (original size)
|
206 |
-
top = np.hstack([flat_img, match_img])
|
207 |
-
bottom = np.hstack([persp_roi, xml_gt_img])
|
208 |
-
combined_grid = np.vstack([top, bottom])
|
209 |
-
|
210 |
-
# Save combined grid
|
211 |
-
base_name = os.path.splitext(os.path.basename(persp_file))[0]
|
212 |
-
file_name = f"{base_name}_{method.lower()}.png"
|
213 |
-
cv2.imwrite(file_name, combined_grid)
|
214 |
-
gallery_paths.append(file_name)
|
215 |
-
download_files.append(file_name)
|
216 |
-
|
217 |
-
while len(download_files)<5: download_files.append(None)
|
218 |
-
return gallery_paths, download_files[0], download_files[1], download_files[2], download_files[3], download_files[4]
|
219 |
-
|
220 |
-
iface = gr.Interface(
|
221 |
-
fn=homography_all_detectors,
|
222 |
-
inputs=[
|
223 |
-
gr.Image(label="Upload Flat Image",type="filepath"),
|
224 |
-
gr.Image(label="Upload Perspective Image",type="filepath"),
|
225 |
-
gr.File(label="Upload mockup.json",file_types=[".json"]),
|
226 |
-
gr.File(label="Upload XML file",file_types=[".xml"])
|
227 |
-
],
|
228 |
-
outputs=[
|
229 |
-
gr.Gallery(label="Results per Detector",show_label=True),
|
230 |
-
gr.File(label="Download SIFT Result"),
|
231 |
-
gr.File(label="Download ORB Result"),
|
232 |
-
gr.File(label="Download BRISK Result"),
|
233 |
-
gr.File(label="Download KAZE Result"),
|
234 |
-
gr.File(label="Download AKAZE Result")
|
235 |
-
],
|
236 |
-
title="Homography ROI Projection with Feature Matching & XML GT",
|
237 |
-
description="Flat + Perspective images with mockup.json & XML. Shows 4 views per detector. Original resolution kept."
|
238 |
-
)
|
239 |
-
|
240 |
iface.launch()
|
|
|
5 |
import os
|
6 |
import xml.etree.ElementTree as ET
|
7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
def get_rotated_rect_corners(x, y, w, h, rotation_deg):
|
9 |
rot_rad = np.deg2rad(rotation_deg)
|
10 |
cos_r, sin_r = np.cos(rot_rad), np.sin(rot_rad)
|
|
|
117 |
description="Flat + Perspective images with mockup.json & XML. Shows 4 views per detector. Original resolution kept."
|
118 |
)
|
119 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
120 |
iface.launch()
|