kazakk
kazakk

Reputation: 33

cv2.error: OpenCV(4.0.0) (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'

I'm trying to read images from the file but it's giving me this error-

cv2.error: OpenCV(4.0.0) C:\projects\opencv-python\opencv\modules\imgproc\src\color.cpp:181: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'

I've also tried changing the "/" on src_path to "\" but that didn't work. Any ideas?

Here is the code-

def create_imageset(excludeHardClasses=True):
    #Variables
    images_path = "C:/Users/bipin/Anaconda3/ASLConvNet-master/src/images/"

    #Read for images folder 
    image_files = [f for f in listdir(images_path) if isfile(join(images_path, f))]

    #processing and reading image files
    image_set = [] #contains all images

    for i in image_files : 
        #split 
        info = i.split('_')
        if excludeHardClasses and info[1] in exclude_label_list:
            continue
        if info[1] == 'o':
            info[1] = '0'
        if info[1] == 'v':
            info[1] = '2'

        matrix = cv2.imread(images_path + '/' + i)
        RGB_img = cv2.cvtColor(matrix, cv2.COLOR_BGR2RGB)

Upvotes: 2

Views: 19109

Answers (2)

Haree.H
Haree.H

Reputation: 53

Try to use \\ instead of using \. Because in Python \ is used to escape and to get \ you have to use \\. That indicates the backslash option.

According to your code it should be

images_path = "C:\\Users\\bipin\\Anaconda3\\ASLConvNet-master\\src\\images\\"

Upvotes: 1

Sounak
Sounak

Reputation: 4968

Remember isfile(join(images_path, f)) returns a list of all regular files, not only images.

I am suspecting you have read a file which is not an image. That makes your matrix variable None. When you call cv2.cvtColor with a None value you see this error.

If you are trying to read png files you can try

image_files = [f for f in listdir(images_path) if f.endswith(".png")]

Also, you may want to change

matrix = cv2.imread(images_path + '/' + i)

to

matrix = cv2.imread(join(images_path, i))

Upvotes: 3

Related Questions