0

I am trying to create a dataset for training a neural network to authenticate signatures. I encountered an error whlie resizing the images in my dataset.

This is my code so far

import cv2
import numpy as np
import os
from random import shuffle
from tqdm import tqdm
from PIL import Image

TRAIN_DIR = '../DATASET/TRAIN/'
TEST_DIR = '../DATASET/TEST/'
IMG_BREDTH = 250
IMG_HEIGHT = 100
LR = 1e-3

MODEL_NAME = 'signature-{}-{}.model'.format(LR, '2conv-basic')

def label_img(img):
    word_label = img.split('.')[-2]
    if (word_label == '1') or (word_label == '2'): return [1,0]
    elif word_label == 'F': return [0,1]

def create_training_set():
    training_data = []

    for img in  tqdm(os.listdir(TRAIN_DIR)):
        label = label_img(img)
        path = os.path.join(TRAIN_DIR, img)  
        img = cv2.resize(cv2.imread(path, cv2.IMREAD_GRAYSCALE), (IMG_BREDTH, IMG_HEIGHT))
        training_data.append([np.array(img), np.array(label)])

    shuffle(training_data)
    np.save('training_data.npy', training_data)

    return training_data

I am getting this error

 14%|█▍        | 105/730 [00:00<00:00, 1040.64it/s]

---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
<ipython-input-87-57e881a5e615> in <module>()
----> 1 create_training_set()

<ipython-input-86-ed607a429014> in create_training_set()
     20         path = os.path.join(TRAIN_DIR, img)
     21 #         img = rotateImage(img, 90)
---> 22         img = cv2.resize(cv2.imread(path, cv2.IMREAD_GRAYSCALE), (IMG_BREDTH, IMG_HEIGHT))
     23         training_data.append([unq_num, np.array(img), np.array(label)])
     24         unq_num += 3

error: /Users/travis/build/skvark/opencv-python/opencv/modules/imgproc/src/resize.cpp:4044: error: (-215) ssize.width > 0 && ssize.height > 0 in function resize

What am I doing wrong? Can someone explain the error?

Thanks in advance.

1 Answers1

0

This often means that imread failed to read an image, which is mentioned in the docs

Warning Even if the image path is wrong, it won’t throw any error, but print img will give you None

So it might not be your arguments to resize that are wrong, but actually a failure to read the image (maybe a wrong path?) resulting in None being passed as the image.

Your path involves using the ../ convention for a parent path, but this might not be resolving to the right directory (depending on the directory from which you invoke the interpreter).

For example, look here (second answer) or here for a solution regarding how to properly reference parent paths from a given file, and as noted here about why a naive use of '..' for parent path might not work as expected (because it can be based on the path from which you invoke the interpreter, not relative to the file unless you base it on __file__).

If it is getting the right directory, then likely there are corrupted images, non-image files, or other files that are causing imread to return None .. you might want to add a try ... except statement in the loop and print out file paths for the case that causes the error.

ely
  • 74,674
  • 34
  • 147
  • 228
  • you were right. some of the files were not labeled correctly. thank you so much –  Mar 14 '18 at 04:34