-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdb_manager.py
More file actions
executable file
·343 lines (246 loc) · 10.3 KB
/
db_manager.py
File metadata and controls
executable file
·343 lines (246 loc) · 10.3 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
(c) Research Group CAMMA, University of Strasbourg, France
Website: http://camma.u-strasbg.fr
Code author: Armine Vardazaryan
"""
import pandas as pd
import numpy as np
import os
import pickle
from glob import glob1
header = {
'video_id',
'frame_id',
'frame_id_int',
'instr_in_roi',
'comment',
'difficult',
'out_of_body',
'seen',
'post_view',
'cvs_cri_1',
'cvs_cri_2',
'cvs_cri_3',
'anatomical_variation',
'roi_not_seen',
'artifact',
'roi_visible_partially'
}
def load_from_dataset(p):
d = []
for root, dirs, files in os.walk(p):
if root==p:
continue
vidname = os.path.basename(root)
for framename in files:
if not framename.lower().endswith(('.png', '.jpg', '.jpeg')):
continue
frame_id, fileformat = framename.split('.')
frame_id_int = int(frame_id)
d.append([vidname, frame_id, frame_id_int])
d = np.vstack(d)
df = pd.DataFrame(columns=header)
df['video_id'] = d[:,0]
df['video_id'] = df['video_id'].astype(str)
df['frame_id'] = d[:,1]
df['frame_id'] = df['frame_id'].astype(str)
df['frame_id_int'] = d[:,2]
df['frame_id_int'] = df['frame_id_int'].astype(int)
cols = ['video_id', 'frame_id', 'frame_id_int', 'comment']
for c in cols:
df[c].fillna("", inplace=True)
cols = ['instr_in_roi', 'difficult', 'out_of_body', 'seen', 'post_view',
'cvs_cri_1', 'cvs_cri_2', 'cvs_cri_3', 'anatomical_variation',
'roi_not_seen', 'artifact','roi_visible_partially']
for c in cols:
df[c].fillna(0, inplace=True)
df = df.sort_values(['video_id','frame_id_int'])
df = df.reset_index(drop=True)
return df, fileformat
def find_file_extension(p):
""" takes a path with a partial file name. Returns full path of file """
dirpath = os.path.dirname(p)
filename = os.path.basename(p)
files = glob1(dirpath, '*.png')
files.extend(glob1(dirpath, '*.jpg'))
files.extend(glob1(dirpath, '*.jpeg'))
found_filename = [f for f in files if filename in f]
assert(len(found_filename) > 0)
extension = found_filename[0].split('.')[1]
return extension
class DB_Manager:
def __init__(self, frames_path, annotation_path):
self.frames_path = frames_path
self.annotation_path = annotation_path
try:
self.database = pd.read_pickle(self.annotation_path)
self.fileformat = find_file_extension(os.path.join(self.frames_path, self.database.iloc[0]['video_id'], self.database.iloc[0]['frame_id']))
except (OSError, IOError) as e:
self.database, self.fileformat = load_from_dataset(self.frames_path)
self.save_database()
assert(len(self.database) > 0)
self._current_frame_idx = 0
self.n_frames = len(self.database)
self.shuffled_indices = np.arange(self.n_frames)
np.random.seed(42)
np.random.shuffle(self.shuffled_indices)
self.shuffled = False
self.skip_seen = False
self.only_seen = False
self.only_difficult = False
#load history
self.history_path = os.path.join(os.path.dirname(os.path.abspath(self.annotation_path)), 'history.pickle')
if os.path.exists(self.history_path):
with open(self.history_path, 'rb') as handle:
self.history = pickle.load(handle)
else:
self.history = []
def save_history(self, before_frame, after_frame):
if np.all(before_frame == after_frame):
return
self.history.append((list(before_frame), list(after_frame)))
with open(self.history_path, 'wb') as handle:
pickle.dump(self.history, handle)
def save_database(self):
self.database.to_pickle(self.annotation_path)#, index=False)
self.database.to_csv(self.annotation_path.split('.')[0]+'.csv')
def get_frame(self):
if self.shuffled:
idx = self.shuffled_indices[self._current_frame_idx]
else:
idx = self._current_frame_idx
return self.database.iloc[idx]
def get_id(self):
if self.shuffled:
idx = self.shuffled_indices[self._current_frame_idx]
else:
idx = self._current_frame_idx
return (self.database.iloc[idx]['video_id'], self.database.iloc[idx]['frame_id_int'])
def get_comment(self):
if self.shuffled:
idx = self.shuffled_indices[self._current_frame_idx]
else:
idx = self._current_frame_idx
comment = self.database.iloc[idx]['comment']
if not comment:
return ''
return comment
def get_progress(self):
return ((self._current_frame_idx + 1), self.n_frames)
def get_frame_path(self):
if self.shuffled:
idx = self.shuffled_indices[self._current_frame_idx]
else:
idx = self._current_frame_idx
vid_dir = self.database.iloc[idx]['video_id']
file_name = self.database.iloc[idx]['frame_id'] +'.'+self.fileformat
return os.path.join(self.frames_path, vid_dir, file_name)
def next_frame(self):
counter = 0
while True:
self._current_frame_idx += 1
self._current_frame_idx %= self.n_frames
if self.shuffled:
idx = self.shuffled_indices[self._current_frame_idx]
else:
idx = self._current_frame_idx
seen = self.database.loc[idx, 'seen']
difficult = self.database.loc[idx, 'difficult']
if not (self.skip_seen and seen) \
and not (self.only_difficult and not difficult) \
and not (self.only_seen and not seen):
break
counter += 1
if counter >= self.n_frames:
break
return self.database.iloc[idx]
def prev_frame(self):
counter = 0
while True:
self._current_frame_idx -= 1
self._current_frame_idx %= self.n_frames
if self.shuffled:
idx = self.shuffled_indices[self._current_frame_idx]
else:
idx = self._current_frame_idx
seen = self.database.loc[idx, 'seen']
difficult = self.database.loc[idx, 'difficult']
if not (self.skip_seen and seen) \
and not (self.only_difficult and not difficult) \
and not (self.only_seen and not seen):
break
counter += 1
if counter >= self.n_frames:
break
return self.database.iloc[idx]
def get_labels(self):
if self.shuffled:
idx = self.shuffled_indices[self._current_frame_idx]
else:
idx = self._current_frame_idx
c1 = self.database.iloc[idx]['cvs_cri_1']
c2 = self.database.iloc[idx]['cvs_cri_2']
c3 = self.database.iloc[idx]['cvs_cri_3']
return [c1, c2, c3]
def get_flags(self):
return self.get_frame()
def update_value(self, field_name, new_value):
old_frame_data = self.get_frame().copy()
if self.shuffled:
idx = self.shuffled_indices[self._current_frame_idx]
else:
idx = self._current_frame_idx
self.database.loc[idx, field_name] = new_value
new_frame_data = self.get_frame().copy()
self.save_history(old_frame_data, new_frame_data)
self.save_database()
def set_labels(self, new_labels):
if len(new_labels) != 3:
return
self.update_value('cvs_cri_1', new_labels[0])
self.update_value('cvs_cri_2', new_labels[1])
self.update_value('cvs_cri_3', new_labels[2])
def set_instr_flag(self, new_state):
self.update_value('instr_in_roi', new_state)
def set_diff_flag(self, new_state):
self.update_value('difficult', new_state)
def set_oob_flag(self, new_state):
self.update_value('out_of_body', new_state)
def set_seen_flag(self, new_state):
self.update_value('seen', new_state)
def set_post_view_flag(self, new_state):
self.update_value('post_view', new_state)
def set_roi_not_seen_flag(self, new_state):
self.update_value('roi_not_seen', new_state)
def set_artifact_flag(self, new_state):
self.update_value('artifact', new_state)
def set_roi_visible_partially_flag(self, new_state):
self.update_value('roi_visible_partially', new_state)
def set_anatomical_variation_flag(self, new_state):
self.update_value('anatomical_variation', new_state)
def set_comment(self, comment):
self.update_value('comment', comment)
def goto_frame(self, vid_id, frame_id):
try:
frame_id_int = int(frame_id)
except:
print('ERROR: frame id should only contain numbers ',frame_id)
return
found_idx = np.where((self.database['frame_id_int']==frame_id_int) & (self.database['video_id']==vid_id))[0]
if len(found_idx) == 0:
return
found_idx = found_idx[0]
if self.shuffled:
self._current_frame_idx = np.where(self.shuffled_indices==found_idx)[0][0]
else:
self._current_frame_idx = found_idx
def toggle_shuffle(self, shuffle=False):
self.shuffled = shuffle
def toggle_skip_seen(self, skip=False):
self.skip_seen = skip
def toggle_only_seen(self, only_seen=False):
self.only_seen = only_seen
def toggle_only_difficult(self, only_difficult=False):
self.only_difficult = only_difficult