-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
172 lines (129 loc) · 5.35 KB
/
model.py
File metadata and controls
172 lines (129 loc) · 5.35 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
from absl import logging
import matplotlib.pyplot as plt
import numpy as np
from scipy.spatial import cKDTree
from skimage.feature import plot_matches
from skimage.measure import ransac
from skimage.transform import AffineTransform
from six import BytesIO
import tensorflow as tf
import tensorflow_hub as hub
from six.moves.urllib.request import urlopen
import scipy.io
from time import time
from utils import getImage_and_resize, load_saved_delf_data
delf = hub.load('https://tfhub.dev/google/delf/1').signatures['default']
def run_delf(np_image):
# np_image = np.array(image)
float_image = tf.image.convert_image_dtype(np_image, tf.float32)
return delf(
image=float_image,
score_threshold=tf.constant(100.0),
image_scales=tf.constant([0.25, 0.3536, 0.5, 0.7071, 1.0, 1.4142, 2.0]),
max_feature_num=tf.constant(1000))
def change_data_range(min_latitude=None, min_longitude=None, max_latitude=None, max_longitude=None):
pass
#@title TensorFlow is not needed for this post-processing and visualization
def match_images(result1, result2, gps, label, image1=None, image2=None):
distance_threshold = 0.8
inliers_num=0
# Read features.
num_features_1 = result1['locations'].shape[0]
# print("Loaded image 1's %d features" % num_features_1)
num_features_2 = result2['locations'].shape[0]
# print("Loaded image 2's %d features" % num_features_2)
# Find nearest-neighbor matches using a KD tree. # 여기서 시간 줄일 수 있으려나
d1_tree = cKDTree(result1['descriptors'])
_, indices = d1_tree.query(
result2['descriptors'],
distance_upper_bound=distance_threshold)
# print(indices)
# Select feature locations for putative matches.
locations_2_to_use = result2['locations'].numpy()[np.where(indices != num_features_1)]
# locations_2_to_use = np.array([
# result2['locations'][i,]
# for i in range(num_features_2)
# if indices[i] != num_features_1
# ])
locations_1_to_use = result1['locations'].numpy()[np.where(indices != num_features_1)]
# locations_1_to_use = np.array([
# result1['locations'][indices[i],]
# for i in range(num_features_2)
# if indices[i] != num_features_1
# ])
# Perform geometric verification using RANSAC.
try:
start=time()
_, inliers = ransac(
(locations_1_to_use, locations_2_to_use),
AffineTransform,
min_samples=3,
residual_threshold=20,
max_trials=100,
stop_probability=0.99)
end=time()
# print(end-start)
inliers_num=sum(inliers)
#print(type(inliers))
#print(inliers)
print('Found %d inliers' % inliers_num)
except:
# print('None')
pass
# Visualize correspondences.
# _, ax = plt.subplots()
# inlier_idxs = np.nonzero(inliers)[0]
# plot_matches(
# ax,
# image1,
# image2,
# locations_1_to_use,
# locations_2_to_use,
# np.column_stack((inlier_idxs, inlier_idxs)),
# matches_color='b')
# ax.axis('off')
# ax.set_title('DELF correspondences')
# plt.show()
return [inliers_num, label] + list(gps)
def run_model(url):
img_path = tf.keras.utils.get_file(url.split('/')[-1], url)
input_data_image, input_data_array = getImage_and_resize(img_path)
input_data = run_delf(input_data_array)
compare_data = data[:]
start = time()
inliers=[]
for i in range(0,len(compare_data),5):
inliers.append(match_images(result1=input_data,
result2=compare_data[i],
gps=gps_compass[labels[i]],
label=labels[i],
image1=input_data_array.astype(dtype='uint8')),
image2=images[i].astype(dtype='uint8')))
end = time()
inliers_sorted = sorted(inliers, key=lambda x: x[0], reverse=True)
print('run time: ', end-start)
return inliers_sorted
# if __name__ == '__main__':
# data, labels, images = load_saved_delf_data() # main server 실행 시 미리 실행 필요
# gps = scipy.io.loadmat('Dataset/GPS_Long_Lat_Compass.mat')
# gps_compass = gps['GPS_Compass']
# florida_idx=np.where(gps_compass[:,0]<=32.5)[0]
# # gps_florida = gps_compass[florida_idx]
# url = 'https://upload.wikimedia.org/wikipedia/commons/2/28/Bridge_of_Sighs%2C_Oxford.jpg'
# input_data_path = tf.keras.utils.get_file(url.split('/')[-1], url)
# input_data_image, input_data_array = getImage_and_resize(input_data_path)
# input_data = run_delf(input_data_array)
# compare_data = data[:10]
# start = time()
# inliers=[]
# for i in range(0,len(compare_data),5):
# inliers.append(match_images(result1=input_data,
# result2=compare_data[i],
# gps=gps_compass[labels[i]],
# label=labels[i],
# image1=input_data_array.astype(dtype='uint8'),
# image2=images[i].astype(dtype='uint8')))
# end = time()
# inliers_sorted = sorted(inliers, key=lambda x: x[0], reverse=True)
# print('run time: ', end-start)
# # print(inliers_sorted)