-
Notifications
You must be signed in to change notification settings - Fork 0
/
calFeature.py
58 lines (50 loc) · 1.73 KB
/
calFeature.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
# -*-coding:utf-8 -*-
'''
author: hrwhipser
date : Jun 5, 2015
function : calculate feature and save in ./data/feature.txt ./data/tag.txt
'''
import os
import numpy as np
from handwriting_tools import read_image, to_the_same_size
sameSize = 16
def get_train_data(dataPath):
train_input, desired_output = [], []
i = 0
for root, dirs, files in os.walk(dataPath):
for _file in files:
outCharacter = _file.split('_')[-2]
suffix = _file.split('.')[-1]
if outCharacter.isdigit() or suffix != 'bmp': continue
test_img = to_the_same_size(read_image(dataPath + '\\' + _file))
train_input.append(test_img.ravel())
desired_output.append(outCharacter)
print i, _file
i += 1
'''
test = to_the_same_size(read_image(dataPath+'\\'+file))
ax = plt.subplot(5,2,i+1)
ax.set_axis_off()
ax.imshow(test)
train_input.append(test)
'''
return train_input, desired_output
if __name__ == '__main__':
# dataPath = r'F:\handwritingData7'
dataPath = r'E:\sd_nineteen\HSF_7'
train_input, desired_output = get_train_data(dataPath)
print len(train_input)
with open('feature.txt', 'a+') as f:
np.savetxt(f, train_input)
with open('tag.txt', 'a+') as f:
f.write('\n'.join(desired_output))
f.write('\n')
# dataPath = r'F:\handwritingData0'
dataPath = r'E:\sd_nineteen\HSF_0'
train_input, desired_output = get_train_data(dataPath)
print len(train_input)
with open('feature.txt', 'a+') as f:
np.savetxt(f, train_input)
with open('tag.txt', 'a+') as f:
f.write('\n'.join(desired_output))
f.write('\n')