-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtime_distributed_read_images.py
178 lines (137 loc) · 5.81 KB
/
time_distributed_read_images.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# -*- coding: utf-8 -*-
"""
Created on Mon Mar 11 22:44:41 2019
@author: Gireesh Sundaram
"""
import cv2
import numpy as np
import pandas as pd
import codecs
import math
from config import window_height, window_width, nb_labels, stride, max_image_width, max_label_len, no_of_frames, mini_batch_size
from keras.preprocessing import sequence
#%%
#reading the class files
data = {}
with codecs.open("Data/class.txt", 'r', encoding='utf-8') as cF:
data = cF.read().split('\n')
#%%
def returnClasses(string):
text = list(string)
text = ["<SPACE>"] + ["<SPACE>" if x==" " else x for x in text] + ["<SPACE>"]
classes = [data.index(x) if x in data else 2 for x in text]
classes = np.asarray(classes)
return classes
#%%
def labels_to_text(labels):
ret = []
for c in labels:
if c == len(data): # CTC Blank
ret.append("")
else:
ret.append(data[c])
output = "".join(ret)
output = output.replace("<SPACE>", " ").strip()
return output
#%%
def split_frames(path):
image = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
h, w = np.shape(image)
# print(np.shape(image))
if (h > window_height): factor = window_height/float(h)
else: factor = 1
image = cv2.resize(image, None, fx=factor, fy=factor, interpolation = cv2.INTER_CUBIC)
h, w = np.shape(image)
if (h < window_height): image = cv2.resize(image, (w, window_height))
h, w = np.shape(image)
# print(np.shape(image))
if w / window_width < math.ceil(w / window_width):
padding = np.full((window_height, math.ceil(w / window_width) * window_width - w), 255)
image = np.append(image, padding, axis = 1)
# print("++", str(np.shape(image)))
h, w = np.shape(image)
frames = np.full((no_of_frames, window_height, window_width, 1), 255)
slide = window_width
i = 0
while slide <= w:
end = slide
start = end - window_width
# print("Start and end are: ", str(start), str(end))
this_frame = image[:, start:end]
this_frame = np.expand_dims(this_frame, 2)
frames[i] = this_frame
slide = slide + stride
i = i + 1
return frames, i
#%%
def generate_arrays_from_file(infile):
while True:
# infile = pd.read_excel(path)
for record in range(0, len(infile)):
print("Reading file: " + str (record))
path = infile["Path"][record]
annotation = infile["Annotation"][record]
print(annotation)
no_of_frames = ((max_image_width // window_width) + 1) * 64 // 8 - 8 + 1
image_out = np.zeros((1, no_of_frames, window_height, window_width, 1))
image_out[0], width = split_frames(path)
# print(np.shape(image_out))
label_out = np.zeros((1, max_label_len))
annot = returnClasses(annotation)
if len(annot) < max_label_len:
padding = np.full((max_label_len - len(annot)), 91)
annot = np.append(annot, padding, axis = 0)
label_out[0] = annot
# print(np.shape(label_out))
input_length = np.zeros([1, 1])
input_length[0] = width
# print(input_length)
label_length = np.zeros((1, 1), dtype=np.float32)
label_length[0] = len(annotation)
inputs = {
'the_input': image_out, #Batch Size, h, w, no of channels
'the_labels': label_out, #Batch size, max len of labels - 91 for labels that are over the actual label
'input_length': input_length, #Batch size, 1
'label_length': label_length #Batch size, 1
}
outputs = {'ctc': np.zeros([1])} #Batch size, 1
yield(inputs, outputs)
#%%
def generate_val_arrays_from_file(infile):
while True:
# infile = pd.read_excel(path)
for record in range(0, len(infile)):
# print("Reading file: " + str (record))
path = infile["Path"][record]
annotation = infile["Annotation"][record]
# print(annotation)
image_out = np.zeros((1, no_of_frames, window_height, window_width, 1))
image_out[0], width = split_frames(path)
# print(np.shape(image_out))
label_out = np.zeros((1, max_label_len))
annot = returnClasses(annotation)
if len(annot) < max_label_len:
padding = np.full((max_label_len - len(annot)), 91)
annot = np.append(annot, padding, axis = 0)
label_out[0] = annot
# print(np.shape(label_out))
input_length = np.zeros([1, 1])
input_length[0] = width
# print(input_length)
label_length = np.zeros((1, 1), dtype=np.float32)
label_length[0] = len(annotation)
inputs = {
'the_input': image_out, #Batch Size, h, w, no of channels
'the_labels': label_out, #Batch size, max len of labels - 91 for labels that are over the actual label
'input_length': input_length, #Batch size, 1
'label_length': label_length #Batch size, 1
}
outputs = {'ctc': np.zeros([1])} #Batch size, 1
yield(inputs, outputs)
#%%
def image_out(infile, path):
image_out = np.zeros((1, no_of_frames, window_height, window_width, 1))
image_out[0], width = split_frames(path)
input_length = np.zeros([1, 1])
input_length[0] = width
return image_out, input_length