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

18,257

Solution 1

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

Solution 2

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\\"
Share:
18,257

Related videos on Youtube

kazakk
Author by

kazakk

Updated on June 04, 2022

Comments

  • kazakk
    kazakk almost 2 years

    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)
    
    • Croolman
      Croolman about 5 years
      Are you sure your images have loaded correctly? It seems they haven't.
    • Scott
      Scott about 5 years
      Could you please report what is the exact while you run your code?