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()

Keras批量读取图片_第1张图片

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

你可能感兴趣的:(Keras)