-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathface_prepare.py
126 lines (111 loc) · 4.45 KB
/
face_prepare.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
#!D:/workplace/python
# -*- coding: utf-8 -*-
# @File : face_prepare.py
# @Author: WangYe
# @Date : 2019/4/17
# @Software: PyCharm
from PIL import Image
import numpy as np
import os
def readData():
image_dir = r"./face_data/"
image_data = []
image_lable = []
image_length = []
image_width = []
for root, dirs, files in os.walk(image_dir):
# root代表路径,dirs代表目录,files代表文件名
for _dir in dirs: # 若是目录,跳过
pass
for _file in files:
image_file = os.path.join(root, _file) #拼接目录
if "images" in root:
x = Image.open(image_file).convert("L") #打开图片
matrix = np.asarray(x,'f') #转换为矩阵
#te = matrix.reshape()
# print(matrix)
'''
这里得到的矩阵维度是不同的,比如
(854, 613, 3)
(651, 855, 3)
虽然都是3维,但是长和宽我们要进行填充
所以先找出长和宽的最大值
最大维度:1569*1000*3
image_length.append(temp_tuple[0])
image_width.append(temp_tuple[1])
'''
temp_tuple = matrix.shape#获取当前矩阵维度
#print(temp_tuple)
#print(type(matrix)) #<class 'numpy.ndarray'>
'''
开始填充矩阵
'''
length = temp_tuple[0] #读取长
width = temp_tuple[1] #读取宽
matrix = matrix.reshape(length,width,1)
pad_length_up = int((1750 -length) / 2) #长的向上填充,1559按1750算,整数
pad_length_down = int(1750 - length - pad_length_up)
pad_width_left = int((1000 - width) / 2)
pad_width_right = int(1000 - width - pad_width_left)
matrix_pad = np.pad(matrix,
pad_width=((pad_length_up, pad_length_down),
(pad_width_left, pad_width_right),
(0,0) #三维处理成一维之后就不用了
),
mode="constant", constant_values=(0, 0))
image_data.append(matrix_pad) #存储矩阵
elif "label" in root:
x = Image.open(image_file).convert('L') # 打开图片
matrix = np.asarray(x,'f') # 转换为矩阵
'''
所有标签都是(250, 250, 3)
'''
matrix = matrix.reshape(250,250,1)
#temp_tuple = matrix.shape
#print(temp_tuple)
# print(type(matrix)) #<class 'numpy.ndarray'>
matrix1 = np.ravel(matrix)
image_lable.append(matrix1) # 存储矩阵
# print(max(image_length))#1569
# print(max(image_width))#1000
data = np.asarray(image_data) #(1028, 1600, 1000, 3)
label = np.asarray(image_lable)#(1028,250, 250, 3)
return data,label
#readData()
if __name__ == '__main__':
readData()
'''
下面的这个代码是因为data和label有些的数据对不上,所以写了个它来处理有data
没label的数据。现在已经我手动处理过了,所以每个data都有属于自己的label
'''
# image_dir1 = r"./face_data/face_label"
# image_dir = r"./face_data/images"
# temp = []
# for root, dirs, files in os.walk(image_dir):
# # root代表路径,dirs代表目录,files代表文件名
# temp = []
# for _dir in dirs: # 若是目录,跳过
# pass
# for _file in files:
# #image_file = os.path.join(root, _file) # 拼接目录
# #print(_file)
# temp.append(_file)
# # print(temp)
#
# temp1 = []
# for root, dirs, files in os.walk(image_dir1):
# # root代表路径,dirs代表目录,files代表文件名
#
# for _dir in dirs: # 若是目录,跳过
# pass
# for _file in files:
# #image_file = os.path.join(root, _file) # 拼接目录
# #print(_file)
# if _file in temp:
# #print(_file)
# pass
# else:
# temp1.append(_file)
# pass
# print(len(temp1)) #11个
# print(temp1)