Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -88,6 +88,28 @@ def homography_all_detectors(flat_img, persp_img, json_file):
|
|
| 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
|
|
@@ -116,7 +138,37 @@ def homography_all_detectors(flat_img, persp_img, json_file):
|
|
| 116 |
file_name = f"result_{method.lower()}.png"
|
| 117 |
cv2.imwrite(file_name, result_rgb[:, :, ::-1]) # save as BGR
|
| 118 |
|
| 119 |
-
gallery_images.append(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
download_files[i] = file_name
|
| 121 |
|
| 122 |
return [gallery_images] + download_files
|
|
|
|
| 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] * 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 |
+
with open(json_file.name, 'r') as f:
|
| 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
|
|
|
|
| 138 |
file_name = f"result_{method.lower()}.png"
|
| 139 |
cv2.imwrite(file_name, result_rgb[:, :, ::-1]) # save as BGR
|
| 140 |
|
| 141 |
+
gallery_images.append(result_rgb) # ✅ only image
|
| 142 |
+
download_files[i] = file_name
|
| 143 |
+
|
| 144 |
+
return [gallery_images] + download_files
|
| 145 |
+
|
| 146 |
+
|
| 147 |
+
for i, method in enumerate(detectors):
|
| 148 |
+
kp1, kp2, good_matches = detect_and_match(flat_gray, persp_gray, method=method)
|
| 149 |
+
if kp1 is None or kp2 is None or len(good_matches) < 4:
|
| 150 |
+
continue
|
| 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 |
+
if H is None:
|
| 157 |
+
continue
|
| 158 |
+
|
| 159 |
+
roi_corners_flat = get_rotated_rect_corners(roi_x, roi_y, roi_w, roi_h, roi_rot_deg)
|
| 160 |
+
roi_corners_persp = cv2.perspectiveTransform(roi_corners_flat.reshape(-1,1,2), H).reshape(-1,2)
|
| 161 |
+
|
| 162 |
+
persp_debug = persp_bgr.copy()
|
| 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
|