-
Notifications
You must be signed in to change notification settings - Fork 2
/
storage.py
175 lines (145 loc) · 5.24 KB
/
storage.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
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
import pickle
import shutil
from io import BytesIO
from pathlib import Path
import h5py
import lmdb
import numpy as np
from PIL import Image
# From https://github.com/jinyu121/video2frame/blob/master/storage.py
class Storage:
def __init__(self, path):
self.path = Path(path)
self.database = None
self.display_info = self.__display_info
def put(self, video_key, clip_tmp_dir, frame_files):
raise NotImplementedError()
def close(self):
pass
def __display_info(self):
self.__class__.display_info(self.path)
@staticmethod
def info(path):
"""Returns a meta info dict and the last Image"""
raise NotImplementedError()
@classmethod
def display_info(cls, path):
print(path, ":")
meta, img = cls.info(path)
for size, count in meta.items():
print(f" Image size (W,H): {size} count: {count}")
class LMDBStorage(Storage):
map_size = 1 << 40
def __init__(self, path):
super().__init__(path)
self.database = lmdb.open(str(path), map_size=LMDBStorage.map_size)
def put(self, video_key, clip_tmp_dir, frame_files):
with self.database.begin(write=True, buffers=True) as txn:
for ith_frame, (frame_id, frame_path) in enumerate(frame_files):
data = (clip_tmp_dir / frame_path).open("rb").read()
key = f"{video_key}/{ith_frame:08d}"
txn.put(key.encode(), data)
def close(self):
self.database.sync()
self.database.close()
@staticmethod
def info(path):
path = Path(path)
info = {}
db = lmdb.open(str(path), map_size=LMDBStorage.map_size)
with db.begin(write=False) as txn:
cur = txn.cursor()
f = cur.first()
while f:
k,v = cur.key(), cur.value()
try:
img = Image.open(BytesIO(v))
ik = str(img.size)
info[ik] = info.get(ik, 0) + 1
except Exception as e:
print(f"reading failed for {k}: {e}")
f = cur.next()
return info, img
class HDF5Storage(Storage):
def __init__(self, path):
super().__init__(path)
self.database = h5py.File(str(path), 'w-') # fail if exists
def put(self, video_key, clip_tmp_dir, frame_files):
for ith_frame, (frame_id, frame_path) in enumerate(frame_files):
data = (clip_tmp_dir / frame_path).open("rb").read()
key = f"{video_key}/{ith_frame:08d}"
self.database[key] = np.void(data)
def close(self):
self.database.close()
@staticmethod
def info(path):
path = Path(path)
info = {}
frame_db = h5py.File(str(path), 'r')
if len(frame_db.keys()) == 0:
print("No video frames found")
for vid in frame_db:
db_vid = frame_db[vid]
for fid in db_vid:
b = np.asarray(db_vid[fid]).tobytes()
try:
img = Image.open(BytesIO(b))
ik = str(img.size)
info[ik] = info.get(ik, 0) + 1
except:
print(f"reading failed for {vid}/{fid}")
frame_db.close()
return info, img
class PKLStorage(Storage):
pickle_path = "frames.pkl"
def __init__(self, path):
super().__init__(path)
def put(self, video_key, clip_tmp_dir, frame_files):
save_dir = self.path / video_key
save_dir.mkdir(exist_ok=True, parents=True)
frame_data = []
for ith_frame, (frame_id, frame_path) in enumerate(frame_files):
data = (clip_tmp_dir / frame_path).open("rb").read()
frame_data.append(data)
pickle.dump(frame_data, (save_dir / PKLStorage.pickle_path).open("wb"))
@staticmethod
def info(path):
path = Path(path)
info = {}
for frame_dir in path.iterdir():
with (frame_dir / PKLStorage.pickle_path).open('rb') as f:
frame_data = pickle.load(f)
for frame in frame_data:
try:
img = Image.open(BytesIO(frame))
ik = str(img.size)
info[ik] = info.get(ik, 0) + 1
except Exception as e:
print(f"reading failed for {frame_dir}: {e}")
return info, img
class FileStorage(Storage):
def __init__(self, path):
super().__init__(path)
def put(self, video_key, clip_tmp_dir, frame_files):
save_dir = self.path / video_key
save_dir.mkdir(exist_ok=True, parents=True)
for ith_frame, (frame_id, frame_path) in enumerate(frame_files):
shutil.copy(str((clip_tmp_dir / frame_path)), str(save_dir / "{:08d}.jpg".format(ith_frame)))
@staticmethod
def info(path):
path = Path(path)
info = {}
for f in path.glob('**/*.jpg'):
try:
img = Image.open(f)
ik = str(img.size)
info[ik] = info.get(ik, 0) + 1
except:
print(f"reading failed for {f}")
return info, img
STORAGE_TYPES = {
"HDF5": HDF5Storage,
"LMDB": LMDBStorage,
"FILE": FileStorage,
"PKL": PKLStorage
}