Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -78,6 +78,13 @@ def add_heading(img, text):
|
|
| 78 |
cv2.putText(canvas, text, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,0), 2, cv2.LINE_AA)
|
| 79 |
return canvas
|
| 80 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
# ---------------- Main Function ----------------
|
| 82 |
def homography_all_detectors(flat_file, persp_file, json_file, xml_file):
|
| 83 |
flat_img = cv2.imread(flat_file)
|
|
@@ -101,12 +108,22 @@ def homography_all_detectors(flat_file, persp_file, json_file, xml_file):
|
|
| 101 |
for method in methods:
|
| 102 |
kp1,kp2,good_matches = detect_and_match(flat_gray,persp_gray,method)
|
| 103 |
if kp1 is None or kp2 is None or len(good_matches)<4:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
continue
|
| 105 |
|
| 106 |
src_pts = np.float32([kp1[m.queryIdx].pt for m in good_matches]).reshape(-1,1,2)
|
| 107 |
dst_pts = np.float32([kp2[m.trainIdx].pt for m in good_matches]).reshape(-1,1,2)
|
| 108 |
H,_ = cv2.findHomography(src_pts,dst_pts,cv2.RANSAC,5.0)
|
| 109 |
if H is None:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
continue
|
| 111 |
|
| 112 |
# 1. Flat image with ROI (from JSON)
|
|
@@ -119,7 +136,7 @@ def homography_all_detectors(flat_file, persp_file, json_file, xml_file):
|
|
| 119 |
# 2. Perspective with Homography ROI
|
| 120 |
roi_corners_persp = cv2.perspectiveTransform(roi_corners_flat.reshape(-1,1,2),H).reshape(-1,2)
|
| 121 |
persp_roi = persp_img.copy()
|
| 122 |
-
cv2.polylines(persp_roi,[roi_corners_persp.astype(int)],True,(0,255,0),5)
|
| 123 |
for px,py in roi_corners_persp:
|
| 124 |
cv2.circle(persp_roi,(int(px),int(py)),5,(255,0,0),5)
|
| 125 |
|
|
|
|
| 78 |
cv2.putText(canvas, text, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,0), 2, cv2.LINE_AA)
|
| 79 |
return canvas
|
| 80 |
|
| 81 |
+
# ---------------- Placeholder if matching fails ----------------
|
| 82 |
+
def get_placeholder(method):
|
| 83 |
+
canvas = np.ones((600, 1800, 3), dtype=np.uint8) * 255
|
| 84 |
+
cv2.putText(canvas, f"{method}: Not enough feature matching", (50, 300),
|
| 85 |
+
cv2.FONT_HERSHEY_SIMPLEX, 2, (0,0,255), 3, cv2.LINE_AA)
|
| 86 |
+
return canvas
|
| 87 |
+
|
| 88 |
# ---------------- Main Function ----------------
|
| 89 |
def homography_all_detectors(flat_file, persp_file, json_file, xml_file):
|
| 90 |
flat_img = cv2.imread(flat_file)
|
|
|
|
| 108 |
for method in methods:
|
| 109 |
kp1,kp2,good_matches = detect_and_match(flat_gray,persp_gray,method)
|
| 110 |
if kp1 is None or kp2 is None or len(good_matches)<4:
|
| 111 |
+
placeholder = get_placeholder(method)
|
| 112 |
+
fname = f"{method.lower()}_placeholder.png"
|
| 113 |
+
cv2.imwrite(fname, placeholder)
|
| 114 |
+
gallery_paths.append(fname)
|
| 115 |
+
download_files.append(fname)
|
| 116 |
continue
|
| 117 |
|
| 118 |
src_pts = np.float32([kp1[m.queryIdx].pt for m in good_matches]).reshape(-1,1,2)
|
| 119 |
dst_pts = np.float32([kp2[m.trainIdx].pt for m in good_matches]).reshape(-1,1,2)
|
| 120 |
H,_ = cv2.findHomography(src_pts,dst_pts,cv2.RANSAC,5.0)
|
| 121 |
if H is None:
|
| 122 |
+
placeholder = get_placeholder(method)
|
| 123 |
+
fname = f"{method.lower()}_placeholder.png"
|
| 124 |
+
cv2.imwrite(fname, placeholder)
|
| 125 |
+
gallery_paths.append(fname)
|
| 126 |
+
download_files.append(fname)
|
| 127 |
continue
|
| 128 |
|
| 129 |
# 1. Flat image with ROI (from JSON)
|
|
|
|
| 136 |
# 2. Perspective with Homography ROI
|
| 137 |
roi_corners_persp = cv2.perspectiveTransform(roi_corners_flat.reshape(-1,1,2),H).reshape(-1,2)
|
| 138 |
persp_roi = persp_img.copy()
|
| 139 |
+
cv2.polylines(persp_roi,[roi_corners_persp.astype(int)],True,(0,255,0),5)
|
| 140 |
for px,py in roi_corners_persp:
|
| 141 |
cv2.circle(persp_roi,(int(px),int(py)),5,(255,0,0),5)
|
| 142 |
|