读取图像数据:通过append方法

原来通过np.concatenate方法拼接加载的数据,发现比较耗内存,现在改成通过append方法加载图像数据。
需要注意:通过append方法加载后的数据类型是list,要想喂给卷积神经网络训练,需要转换为np.ndarray类型。

import cv2
import numpy as np
'''ROI数据集'''
open_path = 'C:/Users/panansi/Desktop/easyAI'
read_path = 'C:/Users/panansi/Desktop/easyAI'

x_test = []
y_test = []

for line in open('{}/{}'.format(open_path,'/test/sick/sick.txt'), 'r'):
    file_name = line[:-3].replace('img','bmp')
    img = cv2.imread('{}/test/sick/{}'.format(read_path, file_name))#, flags=-1)
    img = img / 255.0
    img = img.astype(np.float32)
    x_test.append(img)
    y_test.append(1)
    #cv2.imwrite('{}/train/sick/{}'.format(write_path, file_name), img)
#############################################################################
i=0
for line in open('{}/{}'.format(open_path,'/test/unsick/unsick.txt'), 'r'):
    file_name = line[:-3].replace('img','bmp')
    img = cv2.imread('{}/test/unsick/{}'.format(read_path, file_name))#, flags=-1)
    img = img / 255.0
    img = img.astype(np.float32)
    x_test.append(img)
    y_test.append(0)

for line in open('{}/{}'.format(open_path,'/train/sick/sick.txt'), 'r'):
    file_name = line[:-3].replace('img','bmp')
    img = cv2.imread('{}/train/sick/{}'.format(read_path, file_name))#, flags=-1)
    img = img / 255.0
    img = img.astype(np.float32)
    x_test.append(img)
    y_test.append(1)
    #cv2.imwrite('{}/train/sick/{}'.format(write_path, file_name), img)
#############################################################################
i=0
for line in open('{}/{}'.format(open_path,'/train/unsick/unsick.txt'), 'r'):
    file_name = line[:-3].replace('img','bmp')
    img = cv2.imread('{}/train/unsick/{}'.format(read_path, file_name))#, flags=-1)
    img = img / 255.0
    img = img.astype(np.float32)
    x_test.append(img)
    y_test.append(0)

x_test = np.reshape(x_test, (len(x_test), x_test[1].shape[0], x_test[1].shape[1], x_test[1].shape[2]))
y_test = np.reshape(y_test, (len(y_test), 1))
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值