-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload_image.py
135 lines (117 loc) · 3.99 KB
/
load_image.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
import os
import cv2
import numpy as np
import h5py
import tensorflow as tf
import matplotlib.pyplot as plt
from numpy import *
def get_file(file_dir):
one = []
label_one = []
two = []
label_two = []
three = []
label_three = []
four = []
label_four = []
five = []
label_five = []
ok = []
label_six = []
six = []
label_seven = []
seven = []
label_eight = []
eight = []
label_nine = []
nine = []
label_ten = []
ten = []
label_ok = []
good = []
label_good = []
for file in os.listdir(file_dir + '/ONE'):
one.append(file_dir + '/ONE' + '/' + file)
label_one.append(0)
for file in os.listdir(file_dir + '/TWO'):
two.append(file_dir + '/TWO' + '/' + file)
label_two.append(1)
for file in os.listdir(file_dir + '/THREE'):
three.append(file_dir + '/THREE' + '/' + file)
label_three.append(2)
for file in os.listdir(file_dir + '/FOUR'):
four.append(file_dir + '/FOUR' + '/' + file)
label_four.append(3)
for file in os.listdir(file_dir + '/FIVE'):
five.append(file_dir + '/FIVE' + '/' + file)
label_five.append(4)
for file in os.listdir(file_dir + '/SIX'):
six.append(file_dir + '/SIX' + '/' + file)
label_six.append(5)
for file in os.listdir(file_dir + '/SEVEN'):
seven.append(file_dir + '/SEVEN' + '/' + file)
label_seven.append(6)
for file in os.listdir(file_dir + '/EIGHT'):
eight.append(file_dir + '/EIGHT' + '/' + file)
label_eight.append(7)
for file in os.listdir(file_dir + '/NINE'):
nine.append(file_dir + '/NINE' + '/' + file)
label_nine.append(8)
for file in os.listdir(file_dir + '/TEN'):
ten.append(file_dir + '/TEN' + '/' + file)
label_ten.append(9)
for file in os.listdir(file_dir + '/OK'):
ok.append(file_dir + '/OK' + '/' + file)
label_ok.append(10)
for file in os.listdir(file_dir + '/GOOD'):
good.append(file_dir + '/GOOD' + '/' + file)
label_good.append(11)
image_list = np.hstack((one, two, three, four, five, six, seven, eight, nine, ten, ok, good))
label_list = np.hstack((label_one, label_two, label_three, label_four, label_five, label_six, label_seven,
label_eight, label_nine, label_ten, label_ok, label_good))
temp = np.array([image_list, label_list]) # 转换成2维矩阵
temp = temp.transpose() # 转置
np.random.shuffle(temp) # 按行随机打乱顺序函数
return image_list, label_list
def image_to_h5(X_dirs, Y):
counter = 0
X = []
for dirs in X_dirs:
counter = counter + 1
im = cv2.imread(dirs, 0)
print("正在处理第%d张照片" % counter)
# resize_im = cv2.resize(im,(40,40),interpolation= cv2.INTER_AREA)
# img_gray = cv2.cvtColor(resize_im,cv2.COLOR_RGB2GRAY)
mat = np.asarray(im) # image 转矩阵
X.append(mat)
aa = np.array(X)
num, _, _ = aa.shape
aa.reshape(num, 40, 40, 1)
print(aa.shape)
file = h5py.File("dataset//data_notwhite.h5", "w")
file.create_dataset('X', data=aa)
file.create_dataset('Y', data=np.array(Y))
file.close()
# test
# data = h5py.File("dataset//data.h5","r")
# X_data = data['X']
# print(X_data.shape)
# Y_data = data['Y']
# print(Y_data[123])
# image = Image.fromarray(X_data[123]) #矩阵转图片并显示
# image.show()
if __name__ == "__main__":
# print("start.....: " + str((time.strftime('%Y-%m-%d %H:%M:%S'))))
# resize_img()
# print("end....: " + str((time.strftime('%Y-%m-%d %H:%M:%S'))))
train_dir = 'E:/Project/DeepLearning/anph1a//hand_gesture_dataset'
train, train_label = get_file(train_dir)
image_to_h5(train, train_label)
# test
data = h5py.File("dataset//data_notwhite.h5", "r")
X_data = data['X']
print(X_data.shape)
Y_data = data['Y']
print(Y_data[1235])
# image = Image.fromarray(X_data[1235]) # 矩阵转图片并显示
# image.show()