-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
executable file
·230 lines (186 loc) · 7 KB
/
utils.py
File metadata and controls
executable file
·230 lines (186 loc) · 7 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#!/bin/python3
import sys
from typing import Generator, Any
import os
from tqdm import tqdm
import struct
from math import sqrt
import numpy as np
import heapq
import cv2 as cv
import functools
import numpy.linalg as la
import heapq as hp
LoadError = Exception()
DEFAULT_N_FEATURES = 256
@functools.total_ordering
class Image:
def __init__(
self, path: str, name: str | None = None, descr_path: str | None = None
) -> None:
self.path = os.path.abspath(path)
self.name = self.path.split("/")[-1].split(".")[0]
self.descr = np.empty(0)
self.descr_path = descr_path
self.nb_descr = DEFAULT_N_FEATURES
self.group_id = int(self.name[:4])
self.id = int(self.name[4:])
def __hash__(self) -> int:
return hash(self.path)
def __eq__(self, other) -> bool:
if not isinstance(other, Image):
return NotImplemented
return self.path == other.path and self.nb_descr == other.nb_descr
def __lt__(self, other):
if not isinstance(other, Image):
return NotImplemented
return self.path < other.path and self.nb_descr < other.nb_descr
def has_same_group(self, other):
if not isinstance(other, Image):
return NotImplemented
return self.group_id == other.group_id
def load_descr(self, buffer_size=256):
if self.descr_path == None:
print("Cannot load, no descr_path provided")
raise LoadError
else:
with open(self.descr_path, "rb") as file:
lg = file.read(4)
self.nb_descr = struct.unpack("<l", lg)[0]
# nombre de descripteurs de l'image
data = np.empty((self.nb_descr, 128))
i = 0
for i in range(0, self.nb_descr):
chunk = file.read(4 * 128)
descriptor = struct.unpack("<" + ("f" * 128), chunk)
data[i] = descriptor
self.descr = data
def compute_descr(self, save=False, outfile="", nfeatures=DEFAULT_N_FEATURES):
outfile = os.path.abspath(outfile)
img = cv.imread(self.path)
grayscale = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
sift = cv.SIFT_create(nfeatures=nfeatures)
_, des = sift.detectAndCompute(grayscale, None)
nbr_effectif_features = min(len(des), nfeatures)
self.descr = np.array(des[:nbr_effectif_features])
self.nb_descr = nbr_effectif_features
self.descr.reshape((self.nb_descr, 128)) # risque de bugs dans ce coin
if save:
# format de sortie : n : nbr de descripteur : 4 bytes, entier signé, little endiean, suivit de 128 * n flottants, chacun sur 4 bytes
with open(outfile, "wb") as outfile:
outfile.write(struct.pack("<l", self.nb_descr))
for d in des:
for f in d:
outfile.write(
struct.pack("<f", f)
) # c'est très probablement des flottants 32 bits donc f est ok
class Database:
def __init__(
self,
dir_path: str = "",
auto_init=True,
verbose=False,
nb_descr_per_img=DEFAULT_N_FEATURES,
) -> None:
self.dir_path = os.path.abspath(dir_path)
self.images = np.empty(0)
# nombre avec lequel ça a été calculé, ça peut être moins si y'a pas assez de features
self.nb_descr_per_img = nb_descr_per_img
self.name = dir_path.split("/")[-1]
self.descr_path = (
self.dir_path + "/../" + f"_descr_{self.nb_descr_per_img}_" + self.name
)
if auto_init:
self.auto_init(verbose=verbose)
def load_images(self):
assert self.dir_path != None
files_paths = [
self.dir_path + "/" + f
for f in os.listdir(self.dir_path)
if os.path.isfile(self.dir_path + "/" + f)
]
self.images = np.empty(len(files_paths), dtype=Image)
descr_dir_exist = os.path.isdir(self.descr_path)
for i, f_path in enumerate(files_paths):
self.images[i] = Image(f_path)
if descr_dir_exist:
self.images[i].descr_path = self.descr_path + "/" + self.images[i].name
def load_descriptors(self, verbose=False):
# assert self.images != np.empty(0)
assert self.dir_path != None
if verbose:
it = tqdm(range(len(self.images)), desc="Chargement des descripteurs")
else:
it = range(len(self.images))
for i in it:
self.images[i].load_descr()
def compute_descr(self, save: bool = False, verbose=False):
# assert self.images != np.empty(0)
if verbose:
it = tqdm(self.images, desc="Calcul des descripteurs")
else:
it = self.images
os.mkdir(self.descr_path)
for im in it:
outfile = self.descr_path + "/" + im.name
im.compute_descr(
outfile=outfile, save=save, nfeatures=self.nb_descr_per_img
)
def auto_init(self, verbose=False):
self.load_images()
if os.path.isdir(self.descr_path):
self.load_descriptors(verbose=verbose)
else:
self.compute_descr(save=True, verbose=verbose)
def iter_descr(self) -> Generator[tuple[int, Image], Any, None]:
for im in self.images:
for d in im.descr:
yield (d, im)
@functools.cache
def taille_nuage(self):
return sum(x.nb_descr for x in self.images)
def to_array(self):
tot_nb_descr = sum(x.nb_descr for x in self.images)
arr = np.empty((tot_nb_descr, 128), dtype=np.float32)
for i, (d, _) in enumerate(self.iter_descr()):
arr[i] = d
return arr
# détermine l'image associée au descripteur indexé ind (dans le tableau généré par to_array)
def image_of_descr_index(self, ind):
s = self.images[0].nb_descr
i = 0
while s <= ind:
s += self.images[i].nb_descr
i += 1
return self.images[i]
def basic_search_base(point_set, query_point, k: int):
h = []
if len(point_set) == 0:
print("Empty Pointset")
for d, im in point_set:
# distance euclidiènne entre les deux vecteurs
dist = la.norm(query_point - d)
if len(h) < k:
hp.heappush(h, (-dist, im))
else:
hp.heappushpop(h, (-dist, im))
return [(-x, y) for x, y in h]
if __name__ == "__main__":
# test01()
args = sys.argv
nfeatures = DEFAULT_N_FEATURES
if len(args) >= 2:
entree = args[1]
entree = os.path.abspath(entree)
entree_type = os.path.isfile(entree)
else:
print("No input provided")
exit(1)
if len(args) == 3:
nfeatures = int(args[2])
if entree_type:
im = Image(entree)
im.compute_descr()
else:
d = Database(entree, auto_init=True, verbose=True, nb_descr_per_img=nfeatures)
a = d.to_array()