Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hackassin
GitHub Repository: hackassin/learnopencv
Path: blob/master/Efficient-image-loading/create_lmdb.py
3118 views
1
import os
2
from argparse import ArgumentParser
3
4
import cv2
5
import lmdb
6
import numpy as np
7
8
from tools import get_images_paths
9
10
11
def store_many_lmdb(images_list, save_path):
12
13
num_images = len(images_list) # number of images in our folder
14
15
file_sizes = [os.path.getsize(item) for item in images_list] # all file sizes
16
max_size_index = np.argmax(file_sizes) # the maximum file size index
17
18
# maximum database size in bytes
19
map_size = num_images * cv2.imread(images_list[max_size_index]).nbytes * 10
20
21
env = lmdb.open(save_path, map_size=map_size) # create lmdb environment
22
23
with env.begin(write=True) as txn: # start writing to environment
24
for i, image in enumerate(images_list):
25
with open(image, "rb") as file:
26
data = file.read() # read image as bytes
27
key = f"{i:08}" # get image key
28
txn.put(key.encode("ascii"), data) # put the key-value into database
29
30
env.close() # close the environment
31
32
33
if __name__ == "__main__":
34
parser = ArgumentParser()
35
parser.add_argument(
36
"--path",
37
"-p",
38
type=str,
39
required=True,
40
help="path to the images folder to collect",
41
)
42
parser.add_argument(
43
"--output",
44
"-o",
45
type=str,
46
required=True,
47
help='path to the output environment directory file i.e. "path/to/folder/env/"',
48
)
49
50
args = parser.parse_args()
51
if not os.path.exists(args.output):
52
os.makedirs(args.output)
53
54
images = get_images_paths(args.path)
55
store_many_lmdb(images, args.output)
56
57