Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -12,6 +12,47 @@ import os
|
|
12 |
import gradio as gr
|
13 |
|
14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
def cleanup_text(text):
|
16 |
return "".join([c if ord(c) < 128 else "" for c in text]).strip()
|
17 |
|
|
|
12 |
import gradio as gr
|
13 |
|
14 |
|
15 |
+
def align_images(image, template, maxFeatures=500, keepPercent=0.2,debug=False):
|
16 |
+
# convert both the input image and template to grayscale
|
17 |
+
imageGray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
18 |
+
templateGray = cv2.cvtColor(template, cv2.COLOR_BGR2GRAY)
|
19 |
+
orb = cv2.ORB_create(maxFeatures)
|
20 |
+
(kpsA, descsA) = orb.detectAndCompute(imageGray, None)
|
21 |
+
(kpsB, descsB) = orb.detectAndCompute(templateGray, None)
|
22 |
+
# match the features
|
23 |
+
method = cv2.DESCRIPTOR_MATCHER_BRUTEFORCE_HAMMING
|
24 |
+
matcher = cv2.DescriptorMatcher_create(method)
|
25 |
+
matches = matcher.match(descsA, descsB, None)
|
26 |
+
matches = sorted(matches, key=lambda x:x.distance)
|
27 |
+
# keep only the top matches
|
28 |
+
keep = int(len(matches) * keepPercent)
|
29 |
+
matches = matches[:keep]
|
30 |
+
# check to see if we should visualize the matched keypoints
|
31 |
+
if debug:
|
32 |
+
matchedVis = cv2.drawMatches(image, kpsA, template, kpsB,matches, None)
|
33 |
+
matchedVis = imutils.resize(matchedVis, width=1000)
|
34 |
+
cv2_imshow(matchedVis)
|
35 |
+
cv2.waitKey(0)
|
36 |
+
# allocate memory for the keypoints (x, y)-coordinates from the
|
37 |
+
# top matches -- we'll use these coordinates to compute our
|
38 |
+
# homography matrix
|
39 |
+
ptsA = np.zeros((len(matches), 2), dtype="float")
|
40 |
+
ptsB = np.zeros((len(matches), 2), dtype="float")
|
41 |
+
# loop over the top matches
|
42 |
+
for (i, m) in enumerate(matches):
|
43 |
+
# indicate that the two keypoints in the respective images
|
44 |
+
# map to each other
|
45 |
+
ptsA[i] = kpsA[m.queryIdx].pt
|
46 |
+
ptsB[i] = kpsB[m.trainIdx].pt
|
47 |
+
# compute the homography matrix between the two sets of matched
|
48 |
+
# points
|
49 |
+
(H, mask) = cv2.findHomography(ptsA, ptsB, method=cv2.RANSAC)
|
50 |
+
# use the homography matrix to align the images
|
51 |
+
(h, w) = template.shape[:2]
|
52 |
+
aligned = cv2.warpPerspective(image, H, (w, h))
|
53 |
+
# return the aligned image
|
54 |
+
return aligned
|
55 |
+
|
56 |
def cleanup_text(text):
|
57 |
return "".join([c if ord(c) < 128 else "" for c in text]).strip()
|
58 |
|