-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathlmdbutils.py
57 lines (42 loc) · 943 Bytes
/
lmdbutils.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
"""
LF-Font
Copyright (c) 2020-present NAVER Corp.
MIT license
"""
import io
import os
import lmdb
import json
from PIL import Image, ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True
def load_lmdb(lmdb_path):
lmdb_path = os.path.join(lmdb_path)
env = lmdb.open(
lmdb_path,
max_readers=32,
readonly=True,
lock=False,
readahead=False,
meminit=False,
)
return env
def load_json(json_path):
with open(json_path) as f:
meta = json.load(f)
return meta
def read_data_from_lmdb(env, lmdb_key):
with env.begin(write=False) as txn:
data = txn.get(lmdb_key.encode())
data = deserialize_data(data)
return data
def deserialize_data(data):
if data is None:
return None
buf = io.BytesIO()
buf.write(data)
buf.seek(0)
img = Image.open(buf)
unpacked_data = {
"img": img
}
return unpacked_data