|
楼主 |
发表于 2020-4-15 21:44:02
|
显示全部楼层
def loadDataSet(k): # k代表在10张图片中选择几张作为训练集
dataSetDir = r'D:\tempproject\FaceDB_orl'
# 显示文件夹内容
choose = random.permutation(10) + 1 # 随机排序1-10 (0-9)+1
train_face = zeros((40 * k, 112 * 92))
train_face_number = zeros(40 * k)
test_face = zeros((40 * (10 - k), 112 * 92))
test_face_number = zeros(40 * (10 - k))
for i in range(40): # 40个人
people_num = i + 1
for j in range(10): # 每个人有10个不同的脸
if j < k: # 测试集
filename = dataSetDir + '/s' + str(people_num) + '/' + str(choose[j]) + '.png'
img = img2vector(filename)
train_face[i * k + j, :] = img
train_face_number[i * k + j] = people_num
else:
filename = dataSetDir + '/s' + str(people_num) + '/' + str(choose[j]) + '.png'
img = img2vector(filename)
test_face[i * (10 - k) + (j - k), :] = img
test_face_number[i * (10 - k) + (j - k)] = people_num
return train_face, train_face_number, test_face, test_face_number
# 将图片转换成矩阵
def img2vector(filename):
img = cv2.imread(filename, 0) # 读入灰度值
rows, cols = img.shape
imgVector = zeros((1, rows * cols))
imgVector = reshape(img, (1, rows * cols)) # 将2维转成1维
return imgVector
这个是文件路径:D:\tempproject\FaceDB_orl\s1 每个s*文件夹下面有10张图 |
|