-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake_train_data.py
75 lines (67 loc) · 2.22 KB
/
make_train_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
import numpy as np
from PIL import Image
import xlsxwriter
# 开始读取测试图片
def ImageToMatrix(filename):
im = Image.open(filename)
width, height = im.size
im = im.convert("L")
data = im.getdata()
data = np.matrix(data, dtype='float')/255.0
new_data = np.reshape(data, (height, width))
return new_data
def ImageToMatrix2(ima):
width, height = ima.size
ima = ima.convert("L")
data = ima.getdata()
data = np.matrix(data, dtype='float')/255.0
new_data = np.reshape(data, (height, width))
return new_data
def MatrixToImage(data):
data = data*255
new_im = Image.fromarray(data.astype(np.uint8))
return new_im
# 循环读取测试图片并写入
# 开始进行写excel的准备
book = xlsxwriter.Workbook(r'train_set.xlsx')
sheet1 = book.add_worksheet('train_input1')
sheet2 = book.add_worksheet('train_input2')
sheet3 = book.add_worksheet('train_input3')
sheet4 = book.add_worksheet('train_input4')
# 完成写excel的准备
for i in range(1, 1430+1):
test_pic = ImageToMatrix(str(i)+'.png')
# 完成测试图片读取
# -------------------------
# 开始处理测试图片
# 开始寻找图片四边
hang, lie = np.shape(test_pic)
for top in range(0, hang):
if np.min(test_pic[top, :]) != 1:
break
for bot in range(hang-1, 0, -1):
if np.min(test_pic[bot, :]) != 1:
break
for left in range(0, lie):
if np.min(test_pic[:, left]) != 1:
break
for right in range(lie - 1, 0, -1):
if np.min(test_pic[:, right]) != 1:
break
new_test_pic = test_pic[top:bot, left:right]
# 完成图片四边寻找
# 开始进行图片尺寸转换
pic = MatrixToImage(new_test_pic)
pic2 = pic.resize((28, 28))
test_datap = ImageToMatrix2(pic2)
test_data = np.reshape(test_datap, (1, 784))
# 完成图片尺寸转换
# 对行向量进行储存
for j in range(0, 200):
sheet1.write(i-1, j, test_data[0, j])
sheet2.write(i - 1, j, test_data[0, j+200])
sheet3.write(i - 1, j, test_data[0, j+400])
if j+600 <= 783:
sheet4.write(i - 1, j, test_data[0, j+600])
print(i)
book.close()