Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,65 +1,110 @@
|
|
| 1 |
-
import cv2
|
| 2 |
-
import numpy as np
|
| 3 |
-
import gradio as gr
|
| 4 |
-
|
| 5 |
-
def sift_ransac_matching(image, template):
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
if len(
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import numpy as np
|
| 3 |
+
import gradio as gr
|
| 4 |
+
|
| 5 |
+
def sift_ransac_matching(image, template, ratio_thresh=0.75, ransac_reproj_thresh=5.0, score_thresh=0.8, min_inliers=10):
|
| 6 |
+
"""
|
| 7 |
+
Returns:
|
| 8 |
+
- result_text (str)
|
| 9 |
+
- match_score (float, inlier_ratio = inliers / good_matches)
|
| 10 |
+
- detected (bool)
|
| 11 |
+
- annotated_image (numpy array, BGR)
|
| 12 |
+
"""
|
| 13 |
+
if image is None or template is None:
|
| 14 |
+
return "Invalid input images.", 0.0, False, image
|
| 15 |
+
|
| 16 |
+
# Convert to grayscale
|
| 17 |
+
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 18 |
+
gray_template = cv2.cvtColor(template, cv2.COLOR_BGR2GRAY)
|
| 19 |
+
|
| 20 |
+
# Initialize SIFT detector
|
| 21 |
+
sift = cv2.SIFT_create()
|
| 22 |
+
|
| 23 |
+
# Find keypoints and descriptors
|
| 24 |
+
kp_img, des_img = sift.detectAndCompute(gray_image, None)
|
| 25 |
+
kp_tmpl, des_tmpl = sift.detectAndCompute(gray_template, None)
|
| 26 |
+
|
| 27 |
+
if des_img is None or des_tmpl is None or len(kp_img) == 0 or len(kp_tmpl) == 0:
|
| 28 |
+
return "No features detected. Template not found.", 0.0, False, image
|
| 29 |
+
|
| 30 |
+
# KNN matches with Lowe's ratio test
|
| 31 |
+
bf = cv2.BFMatcher()
|
| 32 |
+
knn = bf.knnMatch(des_img, des_tmpl, k=2)
|
| 33 |
+
|
| 34 |
+
good_matches = []
|
| 35 |
+
for pair in knn:
|
| 36 |
+
if len(pair) < 2:
|
| 37 |
+
continue
|
| 38 |
+
m, n = pair
|
| 39 |
+
if m.distance < ratio_thresh * n.distance:
|
| 40 |
+
good_matches.append(m)
|
| 41 |
+
|
| 42 |
+
if len(good_matches) < 4:
|
| 43 |
+
return f"Not enough good matches ({len(good_matches)}). Template not found.", 0.0, False, image
|
| 44 |
+
|
| 45 |
+
# Build point arrays
|
| 46 |
+
src_pts = np.float32([kp_img[m.queryIdx].pt for m in good_matches]).reshape(-1, 1, 2) # image -> src
|
| 47 |
+
dst_pts = np.float32([kp_tmpl[m.trainIdx].pt for m in good_matches]).reshape(-1, 1, 2) # template -> dst
|
| 48 |
+
|
| 49 |
+
# Homography with RANSAC
|
| 50 |
+
M, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, ransac_reproj_thresh)
|
| 51 |
+
if M is None or mask is None:
|
| 52 |
+
return "Homography failed. Template not found.", 0.0, False, image
|
| 53 |
+
|
| 54 |
+
inliers = int(mask.ravel().sum())
|
| 55 |
+
match_score = float(inliers) / float(len(good_matches)) # inlier ratio
|
| 56 |
+
|
| 57 |
+
# Detection decision (both quality and absolute inlier count can help stability)
|
| 58 |
+
detected = (match_score >= score_thresh) and (inliers >= min_inliers)
|
| 59 |
+
|
| 60 |
+
# Annotate image by projecting template corners back to image space
|
| 61 |
+
annotated = image.copy()
|
| 62 |
+
if detected:
|
| 63 |
+
try:
|
| 64 |
+
# M maps image -> template; invert to map template -> image
|
| 65 |
+
Minv = np.linalg.inv(M)
|
| 66 |
+
h, w = gray_template.shape[:2]
|
| 67 |
+
tmpl_corners = np.float32([[0, 0], [w, 0], [w, h], [0, h]]).reshape(-1, 1, 2)
|
| 68 |
+
proj = cv2.perspectiveTransform(tmpl_corners, Minv)
|
| 69 |
+
proj = proj.astype(int)
|
| 70 |
+
|
| 71 |
+
# Draw polygon
|
| 72 |
+
cv2.polylines(annotated, [proj.reshape(-1, 2)], True, (0, 255, 0), 2)
|
| 73 |
+
# Put label
|
| 74 |
+
txt = f"Detected | score={match_score:.3f} | inliers={inliers}/{len(good_matches)}"
|
| 75 |
+
x, y = proj.reshape(-1, 2)[0]
|
| 76 |
+
cv2.putText(annotated, txt, (max(0, x), max(20, y)),
|
| 77 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)
|
| 78 |
+
except np.linalg.LinAlgError:
|
| 79 |
+
# If inversion fails, still return score/decision
|
| 80 |
+
pass
|
| 81 |
+
|
| 82 |
+
result_text = (
|
| 83 |
+
f"Template {'found' if detected else 'not found'} | "
|
| 84 |
+
f"score={match_score:.3f} | inliers={inliers}/{len(good_matches)} good matches"
|
| 85 |
+
)
|
| 86 |
+
return result_text, match_score, detected, annotated
|
| 87 |
+
|
| 88 |
+
# Gradio UI
|
| 89 |
+
iface = gr.Interface(
|
| 90 |
+
fn=sift_ransac_matching,
|
| 91 |
+
inputs=[
|
| 92 |
+
gr.Image(type="numpy", label="Target Image"),
|
| 93 |
+
gr.Image(type="numpy", label="Template Image"),
|
| 94 |
+
gr.Slider(0.5, 0.95, value=0.75, step=0.01, label="Lowe ratio threshold"),
|
| 95 |
+
gr.Slider(1.0, 10.0, value=5.0, step=0.5, label="RANSAC reprojection threshold"),
|
| 96 |
+
gr.Slider(0.1, 1.0, value=0.8, step=0.01, label="Match score threshold"),
|
| 97 |
+
gr.Slider(0, 100, value=10, step=1, label="Minimum inliers"),
|
| 98 |
+
],
|
| 99 |
+
outputs=[
|
| 100 |
+
gr.Text(label="Result"),
|
| 101 |
+
gr.Number(label="Match score (inlier ratio)"),
|
| 102 |
+
gr.Checkbox(label="Detected?"),
|
| 103 |
+
gr.Image(label="Annotated result"),
|
| 104 |
+
],
|
| 105 |
+
title="SIFT + RANSAC Template Search",
|
| 106 |
+
description="Uploads a target and a template. Shows detection decision, match score, and an annotated result."
|
| 107 |
+
)
|
| 108 |
+
|
| 109 |
+
if __name__ == "__main__":
|
| 110 |
+
iface.launch()
|