读取图像以创建用于图像分类的数据集

时间:2018-11-26 07:51:32

标签: python pandas opencv tensorflow keras

我想基于我的图像数据训练基于keras和tensorflow的分类器。

图像位于文件夹内的子文件夹中:

data
    train
        terror
        no_terror
    test
        terror
        no_terror

这里的问题是,我拥有所有大小不同的图像。

我正在使用下面的代码。

TERROR_TRAIN_DIR = 'C://Users/shiva/OneDrive/Desktop/Dhivy/data/train/terror/'
TERROR_TEST_DIR = 'C://Users/shiva/OneDrive/Desktop/Dhivy/data/test/terror/'

NON_TERROR_TRAIN_DIR = 'C://Users/shiva/OneDrive/Desktop/Dhivy/data/train/non-terror/'
NON_TERROR_TEST_DIR = 'C://Users/shiva/OneDrive/Desktop/Dhivy/data/test/non-terror/'

ROWS = 64
COLS = 64
CHANNELS = 3

terror_train_images = [TERROR_TRAIN_DIR+i for i in os.listdir(TERROR_TRAIN_DIR)] 
terror_test_images = [TERROR_TEST_DIR+i for i in os.listdir(TERROR_TEST_DIR)] 

non_terror_train_images = [NON_TERROR_TRAIN_DIR+i for i in os.listdir(NON_TERROR_TRAIN_DIR)] 
non_terror_test_images = [NON_TERROR_TEST_DIR+i for i in os.listdir(NON_TERROR_TEST_DIR)] 



def read_image(file_path):
    img = cv2.imread(file_path, cv2.IMREAD_COLOR) #cv2.IMREAD_GRAYSCALE
    return cv2.resize(img, (ROWS, COLS), interpolation=cv2.INTER_CUBIC)


def prep_data(images):
    count = len(images)
    data = np.ndarray((count, CHANNELS, ROWS, COLS), dtype=np.uint8)

    for i, image_file in enumerate(images):
        image = read_image(image_file)
        data[i] = image.T
        if i%250 == 0: print('Processed {} of {}'.format(i, count))

    return data

terror_train_images = prep_data(terror_train_images)
terror_test_images = prep_data(terror_test_images)

non_terror_train_images = prep_data(non_terror_train_images)
non_terror_test_images = prep_data(non_terror_test_images)

我收到下面显示的错误。

<ipython-input-27-4eadf011e89d> in read_image(file_path)
      1 def read_image(file_path):
      2     img = cv2.imread(file_path, cv2.IMREAD_COLOR) #cv2.IMREAD_GRAYSCALE
----> 3     return cv2.resize(img, (ROWS, COLS), interpolation=cv2.INTER_CUBIC)
      4 
      5 

error: OpenCV(3.4.3) C:\projects\opencv-python\opencv\modules\imgproc\src\resize.cpp:4044: error: (-215:Assertion failed) !ssize.empty() in function 'cv::resize'

有没有办法,我也可以从文件夹结构本身创建带有标签的训练和测试数据集,并创建X_train,y_train,X_test和y_test,我可以将它们直接输入到VGG和Resnet中,我的主要目标是测试这些图像在这些预训练模型上的表现。

预先感谢

0 个答案:

没有答案