Keras批量读取图片

import os
from keras.preprocessing.image import load_img, img_to_array
import matplotlib.pyplot as plt
import numpy as np
import cv2 as cv


left = os.getcwd() + '/new_data/left'
left_image = os.listdir(left)
left_image.sort()

right = os.getcwd() + '/new_data/right'
right_image = os.listdir(right)
right_image.sort()

disparity = os.getcwd() + '/new_data/disparity'
disparity_image = os.listdir(disparity)
disparity_image.sort()


with open('data_path.txt', 'wb') as file:
    for left_each, right_each, disparity_each in zip(left_image, right_image, disparity_image):
        left_final_path = os.path.join(left + '/' + left_each)
        right_final_path = os.path.join(right + '/' + right_each)
        disparity_final_path = os.path.join(disparity + '/' + disparity_each)
        file.write(left_final_path + ' ')
        file.write(right_final_path + ' ')
        file.write(disparity_final_path + '\n')


def generate_batch_data_random(path, shuffle=True, batch_size=2):
    path_list = []
    with open(path, 'rb') as file:
        a = file.readlines()
    for i in a:
        path_list.append(i.strip().split(' '))

    while True:
        if shuffle:
            np.random.shuffle(path_list)

        for i in range(0, len(path_list), batch_size):
            left_lable_right = path_list[i: i + batch_size]

            left_batch = np.empty(shape=[batch_size, 192, 384, 3], dtype='float32')
            right_batch = np.empty(shape=[batch_size, 192, 384, 3], dtype='float32')
            disparity_batch = np.empty(shape=[batch_size, 192, 384, 1], dtype='float32')

            for j in range(batch_size):
                img0 = load_img(left_lable_right[j][0])
                img1 = load_img(left_lable_right[j][1])
                img2 = load_img(left_lable_right[j][2], color_mode='grayscale')
                img0 = img_to_array(img0)
                img1 = img_to_array(img1)
                img2 = img_to_array(img2)

                img0 = cv.resize(img0, (384, 192))
                img1 = cv.resize(img1, (384, 192))
                img2 = np.expand_dims(cv.resize(img2, (384, 192)), -1)

                left_batch[j] = img0
                right_batch[j] = img1
                disparity_batch[j] = img2

            yield [left_batch, right_batch], disparity_batch


if __name__ == '__main__':
    for i, j in generate_batch_data_random('data_path.txt'):
        i = np.array(i).astype('uint8')
        j = j.astype('uint8')

        plt.subplot(311)
        plt.imshow(i[0][0])
        plt.subplot(312)
        plt.imshow(i[1][0])
        plt.subplot(313)
        plt.imshow(j[0].reshape([192, 384]), cmap='gray')
        plt.show()

data_path.txt格式:一共2万多张
/home/dell/device/image_processing/new_data/left/0000.png /home/dell/device/image_processing/new_data/right/0000.png /home/dell/device/image_processing/new_data/disparity/0000.png
/home/dell/device/image_processing/new_data/left/0001.png /home/dell/device/image_processing/new_data/right/0001.png /home/dell/device/image_processing/new_data/disparity/0001.png
/home/dell/device/image_processing/new_data/left/0002.png /home/dell/device/image_processing/new_data/right/0002.png /home/dell/device/image_processing/new_data/disparity/0002.png
/home/dell/device/image_processing/new_data/left/0003.png /home/dell/device/image_processing/new_data/right/0003.png /home/dell/device/image_processing/new_data/disparity/0003.png
/home/dell/device/image_processing/new_data/left/0004.png /home/dell/device/image_processing/new_data/right/0004.png /home/dell/device/image_processing/new_data/disparity/0004.png
/home/dell/device/image_processing/new_data/left/0005.png /home/dell/device/image_processing/new_data/right/0005.png /home/dell/device/image_processing/new_data/disparity/0005.png
/home/dell/device/image_processing/new_data/left/0006.png /home/dell/device/image_processing/new_data/right/0006.png /home/dell/device/image_processing/new_data/disparity/0006.png
/home/dell/device/image_processing/new_data/left/0007.png /home/dell/device/image_processing/new_data/right/0007.png /home/dell/device/image_processing/new_data/disparity/0007.png
/home/dell/device/image_processing/new_data/left/0008.png /home/dell/device/image_processing/new_data/right/0008.png /home/dell/device/image_processing/new_data/disparity/0008.png
/home/dell/device/image_processing/new_data/left/0009.png /home/dell/device/image_processing/new_data/right/0009.png /home/dell/device/image_processing/new_data/disparity/0009.png
/home/dell/device/image_processing/new_data/left/0010.png /home/dell/device/image_processing/new_data/right/0010.png /home/dell/device/image_processing/new_data/disparity/0010.png
/home/dell/device/image_processing/new_data/left/0011.png /home/dell/device/image_processing/new_data/right/0011.png /home/dell/device/image_processing/new_data/disparity/0011.png
/home/dell/device/image_processing/new_data/left/0012.png /home/dell/device/image_processing/new_data/right/0012.png /home/dell/device/image_processing/new_data/disparity/0012.png
/home/dell/device/image_processing/new_data/left/0013.png /home/dell/device/image_processing/new_data/right/0013.png /home/dell/device/image_processing/new_data/disparity/0013.png

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值