File size: 2,462 Bytes
d61adaf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import os
from ast import literal_eval

from segment_key import *
from matplotlib import pyplot as plt


def show_kps(contour):
    list1 = range(0, 310)
    list2 = list(zip(get_features_right(contour), list1))

    x_coords = [point[0] for point in list2]
    y_coords = [point[1] for point in list2]
    plt.scatter(x_coords, y_coords, c='red', marker='o', label='Keypoints')

    list2 = list(zip(get_features_left(contour), list1))
    x_coords = [point[0] for point in list2]
    y_coords = [point[1] for point in list2]
    plt.scatter(x_coords, y_coords, c='red', marker='o', label='Keypoints')

    list2 = list(zip(list1, get_features_up(contour)))
    x_coords = [point[0] for point in list2]
    y_coords = [point[1] for point in list2]
    plt.scatter(x_coords, y_coords, c='red', marker='o', label='Keypoints')

    list2 = list(zip(list1, get_features_down(contour)))
    x_coords = [point[0] for point in list2]
    y_coords = [point[1] for point in list2]
    plt.scatter(x_coords, y_coords, c='red', marker='o', label='Keypoints')

    plt.show()


def get_all_features():
    contours = []
    with open('prediction/database.txt', 'r') as file:
        lines = file.readlines()
        for line in lines:
            results = (line.split(';')[1])
            results = results.replace(",,", ",'',")
            results = literal_eval(results)
            results = np.array(results)
            contours.append((line.split(';')[0], results))

    return contours


def cos_similarity(feature1, feature2):
    return np.dot(feature1, feature2) / (np.linalg.norm(feature1) * np.linalg.norm(feature2))


def predict_match(image_path):
    main_name = os.path.basename(image_path)[:-11] + '.jpg'
    main_feature = final_features(image_path)
    contours = get_all_features()

    l = []

    for image in contours:
        feature = image[1]
        feature_similarity = 1 - cos_similarity(feature, main_feature)

        l.append([image[0], feature_similarity])

    l.sort(key=lambda x: x[1])

    print(l)
    print(l[0])
    index_in_list = -1
    for i in range(len(l)):
        if l[i][0] == main_name:
            index_in_list = i
    return l[0][0]


# Create the Gradio interface
iface = gr.Interface(
    fn=predict_match,
    inputs=gr.Image(type='numpy'),
    outputs=["text"],
    title="YOLOv8 Object Detection",
    description="Upload an image to detect objects using the YOLOv8 model.",
)

# Launch the interface
iface.launch()