-
Notifications
You must be signed in to change notification settings - Fork 0
/
match.py
94 lines (69 loc) · 3.36 KB
/
match.py
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
87
88
89
90
91
92
93
94
import os
import pickle
import logging
class Match:
"""Represents a feature matches between two views"""
def __init__(self, view1, view2, match_path):
self.indices1 = [] # indices of the matched keypoints in the first view
self.indices2 = [] # indices of the matched keypoints in the second view
self.distances = [] # distance between the matched keypoints in the first view
self.root_path = view1.root_path # root directory containing the videos
self.inliers1 = [] # list to store the indices of the keypoints from the first view not removed using the fundamental matrix
self.inliers2 = [] # list to store the indices of the keypoints from the second view not removed using the fundamental matrix
self.view1 = view1
self.view2 = view2
if not match_path:
self.get_matches(view1, view2)
else:
self.read_matches()
def get_matches(self, view1, view2):
"""Extracts feature matches between two views"""
# store match components in their respective lists
for i in range(view1.led_count): #TODO LED Count
try:
idx1 = view1.descriptors.index(i)
idx2 = view2.descriptors.index(i)
self.indices1.append(idx1)
self.indices2.append(idx2)
self.distances.append(0)
except:
pass
logging.info("Computed matches between view %s and view %s", self.view1.name, self.view2.name)
self.write_matches()
def write_matches(self):
"""Writes a match to a pkl file in the root_path/matches directory"""
if not os.path.exists(os.path.join(self.root_path, 'matches')):
os.makedirs(os.path.join(self.root_path, 'matches'))
temp_array = []
for i in range(len(self.indices1)):
temp = (self.distances[i], self.indices1[i], self.indices2[i])
temp_array.append(temp)
matches_file = open(os.path.join(self.root_path, 'matches', self.view1.name + '_' + self.view2.name + '.pkl'), 'wb')
pickle.dump(temp_array, matches_file)
matches_file.close()
def read_matches(self):
"""Reads matches from file"""
try:
matches = pickle.load(
open(
os.path.join(self.root_path, 'matches', self.view1.name + '_' + self.view2.name + '.pkl'),
"rb"
)
)
logging.info("Read matches from file for view pair pair %s %s", self.view1.name, self.view2.name)
for point in matches:
self.distances.append(point[0])
self.indices1.append(point[1])
self.indices2.append(point[2])
except FileNotFoundError:
logging.error("Pkl file not found for match %s_%s. Computing from scratch", self.view1.name, self.view2.name)
self.get_matches(self.view1, self.view2)
def create_matches(views):
"""Computes matches between every possible pair of views and stores in a dictionary"""
root_path = views[0].root_path
match_path = os.path.exists(os.path.join(root_path, 'matches'))
matches = {}
for i in range(0, len(views) - 1):
for j in range(i+1, len(views)):
matches[(views[i].name, views[j].name)] = Match(views[i], views[j], match_path)
return matches