Spaces:
Running
Running
Rename test.py to app.py
Browse files- test.py β app.py +86 -82
test.py β app.py
RENAMED
@@ -1,82 +1,86 @@
|
|
1 |
-
import os
|
2 |
-
from ast import literal_eval
|
3 |
-
|
4 |
-
from segment_key import *
|
5 |
-
from matplotlib import pyplot as plt
|
6 |
-
|
7 |
-
|
8 |
-
def show_kps(contour):
|
9 |
-
list1 = range(0, 310)
|
10 |
-
list2 = list(zip(get_features_right(contour), list1))
|
11 |
-
|
12 |
-
x_coords = [point[0] for point in list2]
|
13 |
-
y_coords = [point[1] for point in list2]
|
14 |
-
plt.scatter(x_coords, y_coords, c='red', marker='o', label='Keypoints')
|
15 |
-
|
16 |
-
list2 = list(zip(get_features_left(contour), list1))
|
17 |
-
x_coords = [point[0] for point in list2]
|
18 |
-
y_coords = [point[1] for point in list2]
|
19 |
-
plt.scatter(x_coords, y_coords, c='red', marker='o', label='Keypoints')
|
20 |
-
|
21 |
-
list2 = list(zip(list1, get_features_up(contour)))
|
22 |
-
x_coords = [point[0] for point in list2]
|
23 |
-
y_coords = [point[1] for point in list2]
|
24 |
-
plt.scatter(x_coords, y_coords, c='red', marker='o', label='Keypoints')
|
25 |
-
|
26 |
-
list2 = list(zip(list1, get_features_down(contour)))
|
27 |
-
x_coords = [point[0] for point in list2]
|
28 |
-
y_coords = [point[1] for point in list2]
|
29 |
-
plt.scatter(x_coords, y_coords, c='red', marker='o', label='Keypoints')
|
30 |
-
|
31 |
-
plt.show()
|
32 |
-
|
33 |
-
|
34 |
-
def get_all_features():
|
35 |
-
contours = []
|
36 |
-
with open('prediction/database.txt', 'r') as file:
|
37 |
-
lines = file.readlines()
|
38 |
-
for line in lines:
|
39 |
-
results = (line.split(';')[1])
|
40 |
-
results = results.replace(",,", ",'',")
|
41 |
-
results = literal_eval(results)
|
42 |
-
results = np.array(results)
|
43 |
-
contours.append((line.split(';')[0], results))
|
44 |
-
|
45 |
-
return contours
|
46 |
-
|
47 |
-
|
48 |
-
def cos_similarity(feature1, feature2):
|
49 |
-
return np.dot(feature1, feature2) / (np.linalg.norm(feature1) * np.linalg.norm(feature2))
|
50 |
-
|
51 |
-
|
52 |
-
def predict_match(image_path):
|
53 |
-
main_name = os.path.basename(image_path)[:-11] + '.jpg'
|
54 |
-
main_feature = final_features(image_path)
|
55 |
-
contours = get_all_features()
|
56 |
-
|
57 |
-
l = []
|
58 |
-
|
59 |
-
for image in contours:
|
60 |
-
feature = image[1]
|
61 |
-
feature_similarity = 1 - cos_similarity(feature, main_feature)
|
62 |
-
|
63 |
-
l.append([image[0], feature_similarity])
|
64 |
-
|
65 |
-
l.sort(key=lambda x: x[1])
|
66 |
-
|
67 |
-
print(l)
|
68 |
-
print(l[0])
|
69 |
-
index_in_list = -1
|
70 |
-
for i in range(len(l)):
|
71 |
-
if l[i][0] == main_name:
|
72 |
-
index_in_list = i
|
73 |
-
return l[0][0]
|
74 |
-
|
75 |
-
|
76 |
-
#
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from ast import literal_eval
|
3 |
+
|
4 |
+
from segment_key import *
|
5 |
+
from matplotlib import pyplot as plt
|
6 |
+
|
7 |
+
|
8 |
+
def show_kps(contour):
|
9 |
+
list1 = range(0, 310)
|
10 |
+
list2 = list(zip(get_features_right(contour), list1))
|
11 |
+
|
12 |
+
x_coords = [point[0] for point in list2]
|
13 |
+
y_coords = [point[1] for point in list2]
|
14 |
+
plt.scatter(x_coords, y_coords, c='red', marker='o', label='Keypoints')
|
15 |
+
|
16 |
+
list2 = list(zip(get_features_left(contour), list1))
|
17 |
+
x_coords = [point[0] for point in list2]
|
18 |
+
y_coords = [point[1] for point in list2]
|
19 |
+
plt.scatter(x_coords, y_coords, c='red', marker='o', label='Keypoints')
|
20 |
+
|
21 |
+
list2 = list(zip(list1, get_features_up(contour)))
|
22 |
+
x_coords = [point[0] for point in list2]
|
23 |
+
y_coords = [point[1] for point in list2]
|
24 |
+
plt.scatter(x_coords, y_coords, c='red', marker='o', label='Keypoints')
|
25 |
+
|
26 |
+
list2 = list(zip(list1, get_features_down(contour)))
|
27 |
+
x_coords = [point[0] for point in list2]
|
28 |
+
y_coords = [point[1] for point in list2]
|
29 |
+
plt.scatter(x_coords, y_coords, c='red', marker='o', label='Keypoints')
|
30 |
+
|
31 |
+
plt.show()
|
32 |
+
|
33 |
+
|
34 |
+
def get_all_features():
|
35 |
+
contours = []
|
36 |
+
with open('prediction/database.txt', 'r') as file:
|
37 |
+
lines = file.readlines()
|
38 |
+
for line in lines:
|
39 |
+
results = (line.split(';')[1])
|
40 |
+
results = results.replace(",,", ",'',")
|
41 |
+
results = literal_eval(results)
|
42 |
+
results = np.array(results)
|
43 |
+
contours.append((line.split(';')[0], results))
|
44 |
+
|
45 |
+
return contours
|
46 |
+
|
47 |
+
|
48 |
+
def cos_similarity(feature1, feature2):
|
49 |
+
return np.dot(feature1, feature2) / (np.linalg.norm(feature1) * np.linalg.norm(feature2))
|
50 |
+
|
51 |
+
|
52 |
+
def predict_match(image_path):
|
53 |
+
main_name = os.path.basename(image_path)[:-11] + '.jpg'
|
54 |
+
main_feature = final_features(image_path)
|
55 |
+
contours = get_all_features()
|
56 |
+
|
57 |
+
l = []
|
58 |
+
|
59 |
+
for image in contours:
|
60 |
+
feature = image[1]
|
61 |
+
feature_similarity = 1 - cos_similarity(feature, main_feature)
|
62 |
+
|
63 |
+
l.append([image[0], feature_similarity])
|
64 |
+
|
65 |
+
l.sort(key=lambda x: x[1])
|
66 |
+
|
67 |
+
print(l)
|
68 |
+
print(l[0])
|
69 |
+
index_in_list = -1
|
70 |
+
for i in range(len(l)):
|
71 |
+
if l[i][0] == main_name:
|
72 |
+
index_in_list = i
|
73 |
+
return l[0][0]
|
74 |
+
|
75 |
+
|
76 |
+
# Create the Gradio interface
|
77 |
+
iface = gr.Interface(
|
78 |
+
fn=predict_match,
|
79 |
+
inputs=gr.Image(type='numpy'),
|
80 |
+
outputs=["text"],
|
81 |
+
title="YOLOv8 Object Detection",
|
82 |
+
description="Upload an image to detect objects using the YOLOv8 model.",
|
83 |
+
)
|
84 |
+
|
85 |
+
# Launch the interface
|
86 |
+
iface.launch()
|