-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataset.py
122 lines (87 loc) · 3.2 KB
/
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
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
from utils import preprocessor
import re, os
from config import *
def readIAMData(filePath, imgSize):
'''
This is the part of https://github.com/githubharald/SimpleHTR with simple modification
See License.
'''
data = {}
f = open(filePath, 'r')
for line in f:
# ignore comment line
if not line or line[0] == '#':
continue
lineSplit = line.strip().split(' ')
assert len(lineSplit) >= 9
fileName = 'lines/' + lineSplit[0] + '.jpg'
gtText = ' '.join(lineSplit[8:]).replace('|', ' ')
# gtText = gtText.lower()
gtText = re.sub(r'([\'?.!,¿])', r" \1 ", gtText)
gtText = re.sub(r'[^ ' + wordCharList + ']+', ' ', gtText)
gtText = re.sub(r'[" "]+', ' ', gtText)
lineSplit = list(gtText)
gtText = '|'.join(lineSplit[:])
gtText = gtText.rstrip().strip()
# if truncateLabel(gtText, maxTextLen) :
# print("maxlen must be changed")
# print(gtText)
# chars = chars.union(set(list(gtText)))
img = preprocessor(fileName, imgSize, False, False)
data[gtText] = img
return data
def readKHATTdata(filePath, imgSize):
data = {}
f = open(filePath, 'r')
for line in f:
# ignore comment line
if not line or line[0] == '#':
continue
lineSplit = line.strip().split(' ')
assert len(lineSplit) == 2
fileNameSplit = lineSplit[0].split('.')
fileName = 'lines/' + filePath.split('.')[0] + '/' + fileNameSplit[0] + '.jpg'
lineSplit = lineSplit[1].split(" ")
gtText = '|'.join(lineSplit[:])
gtText = gtText.rstrip().strip()
# if truncateLabel(gtText, maxTextLen) :
# print("maxlen must be changed")
# print(gtText)
# chars = chars.union(set(list(gtText)))
if os.path.isfile(fileName):
img = preprocessor(fileName, imgSize, False, False)
data[gtText] = img
else:
# print(fileName)
pass
return data
def readParzival_SaintGallData(filePath, imgSize):
data = {}
f = open(filePath, 'r')
for line in f:
# ignore comment line
if not line or line[0] == '#':
continue
lineSplit = line.strip().split(' ')
assert len(lineSplit) >= 2
fileName = 'lines/' + lineSplit[0] + '.jpg'
gtText = '|'.join(lineSplit[1:])
gtText = gtText.rstrip().strip()
# if truncateLabel(gtText, maxTextLen) :
# print("maxlen must be changed")
# print(gtText)
# chars = chars.union(set(list(gtText)))
if os.path.isfile(fileName):
img = preprocessor(fileName, imgSize, False, False)
data[gtText] = img
else:
# print(fileName)
pass
return data
if __name__ == "__main__":
data = readIAMData(train_path, (img_w, img_h))
print(len(data))
dataVal = readIAMData(validation_path, (img_w, img_h))
print(len(dataVal))
dataTest = readIAMData(test_path, (img_w, img_h))
print(len(dataTest))