-
Notifications
You must be signed in to change notification settings - Fork 1
/
create_lmdb_dataset.py
78 lines (64 loc) · 2.33 KB
/
create_lmdb_dataset.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
#!/usr/bin/env python3
""" a modified version of CRNN torch repository https://github.com/bgshih/crnn/blob/master/tool/create_dataset.py """
import io
import os
import fire
import lmdb
import numpy as np
from PIL import Image
def checkImageIsValid(imageBin):
if imageBin is None:
return False
img = Image.open(io.BytesIO(imageBin)).convert('RGB')
return np.prod(img.size) > 0
def writeCache(env, cache):
with env.begin(write=True) as txn:
for k, v in cache.items():
txn.put(k, v)
def createDataset(inputPath, gtFile, outputPath, checkValid=True):
"""
Create LMDB dataset for training and evaluation.
ARGS:
inputPath : input folder path where starts imagePath
outputPath : LMDB output path
gtFile : list of image path and label
checkValid : if true, check the validity of every image
"""
os.makedirs(outputPath, exist_ok=True)
env = lmdb.open(outputPath, map_size=1099511627776)
cache = {}
cnt = 1
with open(gtFile, 'r', encoding='utf-8') as f:
data = f.readlines()
nSamples = len(data)
for i, line in enumerate(data):
imagePath, label = line.strip().split(maxsplit=1)
imagePath = os.path.join(inputPath, imagePath)
with open(imagePath, 'rb') as f:
imageBin = f.read()
if checkValid:
try:
img = Image.open(io.BytesIO(imageBin)).convert('RGB')
except IOError as e:
with open(outputPath + '/error_image_log.txt', 'a') as log:
log.write('{}-th image data occured error: {}, {}\n'.format(i, imagePath, e))
continue
if np.prod(img.size) == 0:
print('%s is not a valid image' % imagePath)
continue
imageKey = 'image-%09d'.encode() % cnt
labelKey = 'label-%09d'.encode() % cnt
cache[imageKey] = imageBin
cache[labelKey] = label.encode()
if cnt % 1000 == 0:
writeCache(env, cache)
cache = {}
print('Written %d / %d' % (cnt, nSamples))
cnt += 1
nSamples = cnt - 1
cache['num-samples'.encode()] = str(nSamples).encode()
writeCache(env, cache)
env.close()
print('Created dataset with %d samples' % nSamples)
if __name__ == '__main__':
fire.Fire(createDataset)