-
Notifications
You must be signed in to change notification settings - Fork 293
/
read_data.py
51 lines (36 loc) · 1.54 KB
/
read_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
50
51
#-*-coding:utf8-*-
__author__ = '万壑'
import os
import cv2
import numpy as np
from read_img import endwith
#输入一个文件路径,对其下的每个文件夹下的图片读取,并对每个文件夹给一个不同的Label
#返回一个img的list,返回一个对应label的list,返回一下有几个文件夹(有几种label)
def read_file(path):
img_list = []
label_list = []
dir_counter = 0
IMG_SIZE = 128
#对路径下的所有子文件夹中的所有jpg文件进行读取并存入到一个list中
for child_dir in os.listdir(path):
child_path = os.path.join(path, child_dir)
for dir_image in os.listdir(child_path):
if endwith(dir_image,'jpg'):
img = cv2.imread(os.path.join(child_path, dir_image))
resized_img = cv2.resize(img, (IMG_SIZE, IMG_SIZE))
recolored_img = cv2.cvtColor(resized_img,cv2.COLOR_BGR2GRAY)
img_list.append(recolored_img)
label_list.append(dir_counter)
dir_counter += 1
# 返回的img_list转成了 np.array的格式
img_list = np.array(img_list)
return img_list,label_list,dir_counter
#读取训练数据集的文件夹,把他们的名字返回给一个list
def read_name_list(path):
name_list = []
for child_dir in os.listdir(path):
name_list.append(child_dir)
return name_list
if __name__ == '__main__':
img_list,label_lsit,counter = read_file('D:\myProject\pictures\dataset')
print counter