-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconvert_lmdb.py
More file actions
94 lines (77 loc) · 2.79 KB
/
convert_lmdb.py
File metadata and controls
94 lines (77 loc) · 2.79 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
"""a modified version of CRNN torch repository
https://github.com/bgshih/crnn/blob/master/tool/create_dataset.py"""
import os
import sys
import fire
import lmdb
import numpy as np
import cv2
def checkImageIsValid(imageBin) -> bool:
if imageBin is None:
return False
imageBuf = np.frombuffer(imageBin, dtype=np.uint8)
img = cv2.imdecode(imageBuf, cv2.IMREAD_GRAYSCALE)
imgH, imgW = img.shape[0], img.shape[1]
if imgH * imgW == 0:
return False
return True
def writeCache(env, cache) -> None:
with env.begin(write=True) as txn:
for k, v in cache.items():
txn.put(k, v)
def createDataset(inputPath, gtFile, outputPath, checkValid=True) -> None:
"""
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=1099511629)
cache = {}
cnt = 1
with open(gtFile, "r", encoding="utf-8") as data:
datalist = data.readlines()
nSamples = len(datalist)
for i in range(nSamples):
try:
imagePath, label = datalist[i].strip("\n").split("\t")
except:
print(datalist[i])
sys.exit(0)
imagePath = os.path.join(inputPath, imagePath)
# # only use alphanumeric data
# if re.search('[^a-zA-Z0-9]', label):
# continue
if not os.path.exists(imagePath):
print("%s does not exist" % imagePath)
continue
with open(imagePath, "rb") as f:
imageBin = f.read()
if checkValid:
try:
if not checkImageIsValid(imageBin):
print("%s is not a valid image" % imagePath)
continue
except:
print("error occured", i)
with open(outputPath + "/error_image_log.txt", "a") as log:
log.write("%s-th image data occured error\n" % str(i))
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)
print("Created dataset with %d samples" % nSamples)
if __name__ == "__main__":
fire.Fire(createDataset)