加载图像数据集

#path:数据路径
#txt:每条数据对应的标签
 1 def generateds(path, txt):
 2     f = open(txt, 'r')  # 以只读形式打开txt文件
 3     contents = f.readlines()  # 读取文件中所有行
 4     f.close()  # 关闭txt文件
 5     x, y_ = [], []  # 建立空列表
 6     for content in contents:  # 逐行取出
 7         value = content.split()  # 以空格分开,图片路径为value[0] , 标签为value[1] , 存入列表
 8         img_path = path + value[0]+' '+value[1]  # 拼出图片路径和文件名
 9         print(img_path)
10         # img = Image.open(img_path)  # 读入图片
11         # img1 = np.array(img.convert('L'))  # 图片变为8位宽灰度值的np.array格式
12         img=cv2.imread('img_path',0)
13         img = np.array(img, dtype =float) ##Mat转为numpy
14         img = img / 255.  # 数据归一化 (实现预处理)
15         x.append(img)  # 归一化后的数据,贴到列表x
16         print(value[2])
17         y_.append(value[2])  # 标签贴到列表y_
18         print('loading : ' + content)  # 打印状态提示
19     x = np.array(x)  # 变为np.array格式
20     y_ = np.array(y_)  # 变为np.array格式
21     y_ = y_.astype(np.int64)  # 变为64位整型
22     return x, y_  # 返回输入特征x,返回标签y_

猜你喜欢

转载自www.cnblogs.com/hsy1941/p/12942844.html