-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
109 lines (76 loc) · 2.7 KB
/
utils.py
File metadata and controls
109 lines (76 loc) · 2.7 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import PIL
from PIL import Image
import pickle
import os
from sklearn.model_selection import train_test_split
# resize images
def create_new_img(path, base_path, new_base, height):
base_width = height
img = Image.open(base_path+path)
wpercent = (base_width / float(img.size[1]))
hsize = int((float(img.size[0]) * float(wpercent)))
img = img.resize((hsize, base_width), PIL.Image.ANTIALIAS)
img.save(new_base+path)
print(new_base+path)
def resize_images():
base_path = 'EnglishHnd/EnglishHnd/English/Hnd/Img/'
file = open(base_path+'all.txt~')
new_file = "";
# 50x66 - height/width
base_width = 50
new_base = 'dataset/'
# samples sample
i, j = 1, 1
for line in file:
if j == 56:
j = 1
# create folder
if j == 1:
print()
os.mkdir(new_base+'Sample{0:0>3}'.format(i))
i += 1
j += 1
create_new_img(line[:-1], base_path, new_base, base_width)
def img2lsit(path):
res = []
img = Image.open(path)
for i in range(img.size[1]):
res.append([])
for j in range(img.size[0]):
res[i].append([(img.getpixel((j, i))[0] - 127.5) / 255])
return res
def create_dataset():
res = []
y = []
for i in range(62):
for j in range(55):
res.append(img2lsit("dataset/Sample{0:0>3}/img{0:0>3}-{1:0>3}.png"
.format(i+1, j+1)))
y.append([0 for x in range(62)])
y[i*55+j][i] = 1
return res, y
def y2letter(i):
letters = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
]
return letters[i]
def pickle_dump_dataset():
X, y = create_dataset()
with open('x.txt', 'wb') as pickle_file:
pickle.dump(X, pickle_file)
with open('y.txt', 'wb') as pickle_file:
pickle.dump(y, pickle_file)
def pickle_read_dataset():
with open('x.txt', 'rb') as pickle_file:
X = pickle.load(pickle_file)
with open('y.txt', 'rb') as pickle_file:
y = pickle.load(pickle_file)
return X, y
def get_samples():
X, y = pickle_read_dataset()
X_train, X_test, Y_train, Y_test = train_test_split(X, y, test_size=0.1,
random_state=241)
return X_train, X_test, Y_train, Y_test