-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathgender_train_data.py
51 lines (36 loc) · 1.18 KB
/
gender_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
#coding=utf-8
import os
import numpy as np
import cv2
def get_img_list(dirname,flag=0):
rootdir= os.path.abspath('./data/'+dirname+'/')
list = os.listdir(rootdir) #列出文件夹下所有的目录与文件
files=[]
for i in range(0,len(list)):
path = os.path.join(rootdir,list[i])
if os.path.isfile(path):
files.append(path)
return files
images=[]
labels=[]
def read_img(list,flag=0):
for i in range(len(list)-1):
if os.path.isfile(list[i]):
images.append(cv2.imread(list[i]).flatten())
labels.append(flag)
read_img(get_img_list('male'),[0,1])
read_img(get_img_list('female'),[1,0])
images = np.array(images)
labels = np.array(labels)
#重新打乱
permutation = np.random.permutation(labels.shape[0])
all_images = images[permutation,:]
all_labels = labels[permutation,:]
#训练集与测试集比例 8:2
train_total = all_images.shape[0]
train_nums= int(all_images.shape[0]*0.8)
test_nums = all_images.shape[0]-train_nums
images = all_images[0:train_nums,:]
labels = all_labels[0:train_nums,:]
test_images = all_images[train_nums:train_total,:]
test_labels = all_labels[train_nums:train_total,:]