forked from Rid7/Table-OCR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.py
executable file
·161 lines (148 loc) · 6.82 KB
/
data.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import numpy as np
import os
import warnings
import random
from keras.preprocessing.image import ImageDataGenerator
from PIL import ImageEnhance, Image
warnings.filterwarnings("ignore")
# If you want to distinguish vertical, horizontal, invisible lines, encodes them differently and feed to the DICT.
# Thus, label process should change relevantly. Since it's simple but time consuming, I won't discuss in this repo.
BackGround = [255, 255, 255]
Line = [0, 0, 0]
COLOR_DICT = np.array([Line, BackGround])
class data_preprocess:
def __init__(self, train_path=None, image_folder=None, label_folder=None,
valid_path=None, valid_image_folder =None,valid_label_folder = None,
test_path=None,
save_path=None,
img_rows=512, img_cols=512,
flag_multi_class=False, # If you want to distinguish vertical, horizontal, invisible lines should be
num_classes=2): # True and num_classes varies too.
self.img_rows = img_rows
self.img_cols = img_cols
self.train_path = train_path
self.image_folder = image_folder
self.label_folder = label_folder
self.valid_path = valid_path
self.valid_image_folder = valid_image_folder
self.valid_label_folder = valid_label_folder
self.test_path = test_path
self.save_path = save_path
self.data_gen_args = dict(rotation_range=5,
width_shift_range=0.5,
height_shift_range=0.5,
shear_range=0.5,
zoom_range=0.5,
vertical_flip=True,
horizontal_flip=True,
fill_mode='nearest')
self.image_color_mode = "rgb"
self.label_color_mode = 'grayscale'
self.flag_multi_class = flag_multi_class
self.num_class = num_classes
self.target_size = (800, 800)
self.img_type = 'jpg'
def adjustData(self, img, label):
if (self.flag_multi_class):
img = img / 255.
label = label[:, :, :, 0] if (len(label.shape) == 4) else label[:, :, 0]
new_label = np.zeros(label.shape + (self.num_class,))
for i in range(self.num_class):
new_label[label == i, i] = 1
label = new_label
elif (np.max(img) > 1):
img = img / 255.
# label = label[:, :, :, 1] if (len(label.shape) == 4) else label[:, :, 1]
label = label / 255.
label = 1.0 - label
label[label > 0.5] = 1
label[label <= 0.5] = 0
# print(label.shape)
# print(len(np.argwhere(label==0)))
return (img, label)
def trainGenerator(self, batch_size, image_save_prefix="image", label_save_prefix="label",
save_to_dir=None, seed=7):
'''
can generate image and label at the same time
use the same seed for image_datagen and label_datagen to ensure the transformation for image and label is the same
if you want to visualize the results of generator, set save_to_dir = "your path"
'''
image_datagen = ImageDataGenerator(**self.data_gen_args)
label_datagen = ImageDataGenerator(**self.data_gen_args)
image_generator = image_datagen.flow_from_directory(
self.train_path,
classes=[self.image_folder],
class_mode=None,
color_mode=self.image_color_mode,
target_size=self.target_size,
batch_size=batch_size,
save_to_dir=save_to_dir,
save_prefix=image_save_prefix,
seed=seed)
label_generator = label_datagen.flow_from_directory(
self.train_path,
classes=[self.label_folder],
class_mode=None,
color_mode=self.label_color_mode,
target_size=self.target_size,
batch_size=batch_size,
save_to_dir=save_to_dir,
save_prefix=label_save_prefix,
seed=seed)
train_generator = zip(image_generator, label_generator)
for (img, label) in train_generator:
img = img.astype(np.uint8)
img = Image.fromarray(img[0]).convert('RGB')
img = ImageEnhance.Contrast(img).enhance(random.randint(1, 4)*0.5)
img = np.array(img, dtype=np.float32)
img = np.expand_dims(img, axis=0)
img, label = self.adjustData(img, label)
yield (img, label)
def testGenerator(self):
filenames = os.listdir(self.test_path)
for filename in filenames:
img = Image.open(os.path.join(self.test_path, filename))
img = img / 255.
img = img.resize(img, self.target_size, mode='constant')
img = np.reshape(img, img.shape + (1,)) if (not self.flag_multi_class) else img
img = np.reshape(img, (1,) + img.shape)
yield img
def validLoad(self, batch_size,seed=7):
image_datagen = ImageDataGenerator(**self.data_gen_args)
label_datagen = ImageDataGenerator(**self.data_gen_args)
image_generator = image_datagen.flow_from_directory(
self.valid_path,
classes=[self.valid_image_folder],
class_mode=None,
color_mode=self.image_color_mode,
target_size=self.target_size,
batch_size=batch_size,
seed=seed)
label_generator = label_datagen.flow_from_directory(
self.valid_path,
classes=[self.valid_label_folder],
class_mode=None,
color_mode=self.label_color_mode,
target_size=self.target_size,
batch_size=batch_size,
seed=seed)
train_generator = zip(image_generator, label_generator)
for (img, label) in train_generator:
img, label = self.adjustData(img, label)
yield (img, label)
def saveResult(self, npyfile):
for i, item in enumerate(npyfile):
img = item
img_std = np.zeros((img.shape[0], img.shape[1], 3), dtype=np.uint8)
# img_std += 1
if self.flag_multi_class:
for row in range(len(img)):
for col in range(len(img[row])):
num = np.argmax(img[row][col])
img_std[row][col] = COLOR_DICT[num]
else:
img = np.squeeze(img, axis=-1)
img_std[img > 0.3] = 255
if self.save_path:
result = Image.fromarray(img_std)
result.save(os.path.join(self.save_path, ("%s." + self.img_type) % (i)))