File size: 2,187 Bytes
793804e
 
 
 
 
 
 
870f781
 
 
793804e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3ef3204
 
 
 
 
793804e
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
import cv2
import matplotlib.pyplot as plt
import gradio as gr
import os


def find_descriptors(img1, img2):
    if img1 is None or img2 is None:
        return None, None, None, None, None, None

    img1 = cv2.cvtColor(img1, cv2.IMREAD_GRAYSCALE)
    img2 = cv2.cvtColor(img2, cv2.IMREAD_GRAYSCALE)
    sift = cv2.SIFT_create()
    kp1, des1 = sift.detectAndCompute(img1, None)
    kp2, des2 = sift.detectAndCompute(img2, None)
    bf = cv2.BFMatcher()
    matches = bf.knnMatch(des1, des2, k=2)
    good = []
    for m, n in matches:
        if m.distance < 0.7 * n.distance:
            good.append([m])
    img3 = cv2.drawMatchesKnn(img1, kp1, img2, kp2, good, None, flags=cv2.DrawMatchesFlags_NOT_DRAW_SINGLE_POINTS)
    plt.imshow(img3), plt.show()
    score = (100 * len(good) / min(len(kp1), len(kp2)))
    if score >= 10:
        result = "Totally YES"
    elif score >= 3:
        result = "YES"
    elif score >= 1:
        result = "MAYBE"
    else:
        result = "NO"

    descriptors = len(good)
    keypoints1 = len(kp1)
    keypoints2 = len(kp2)

    return img3, descriptors, keypoints1, keypoints2, result, score


sift_result = gr.Interface(
    fn=find_descriptors,
    inputs=["image", "image"],
    outputs=["image", gr.Textbox(label="Descriptors"),
             gr.Textbox(label="First image keypoints"),
             gr.Textbox(label="Second image keypoints"),
             gr.Textbox(label="Are these places equals?"),
             gr.Textbox(label="Score")],
    examples=[[
        os.path.join(os.path.dirname(__file__), "images/1.jpg"),
        os.path.join(os.path.dirname(__file__), "images/2.jpg"),
    ], [os.path.join(os.path.dirname(__file__), "images/3.png"),
        os.path.join(os.path.dirname(__file__), "images/4.png")],
        [os.path.join(os.path.dirname(__file__), "images/5.jpg"),
         os.path.join(os.path.dirname(__file__), "images/6.jpg")],
        [os.path.join(os.path.dirname(__file__), "images/7.jpg"),
         os.path.join(os.path.dirname(__file__), "images/8.jpg")]],
    title="SIFT Image Compare",
    batch=False,
    description="A model that uses SIFT method for image compare.",
    live=True)

sift_result.launch()