Error: (-215:Assertion failed) !ssize.empty() in function 'cv::resize' occur when runs code on Windows but works fine on Mac

folder_selected = filedialog.askdirectory()
images_path = glob.glob(folder_selected+"/*")
print("image_path: ",images_path)

layer_names = net.getLayerNames()
output_layers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()]
colors = np.random.uniform(0, 255, size=(len(classes), 3))

# Insert here the path of your images
random.shuffle(images_path)
# loop through all the images
for img_path in images_path:
    # Loading image
    img = cv2.imread(img_path)
    img = cv2.resize(img, None, fx=0.4, fy=0.4)
    height, width, channels = img.shape

the code works perfectly on Mac but when I run the code on Windows
problems occur with the path

  1. the path cannot be a pure string I have to put r on the path like this (r"path") ,so this problem solved
  2. but now it shows an error
img = cv2.resize(img, None, fx=0.4, fy=0.4)
cv2.error: OpenCV(4.5.1) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-6uw63ony\opencv\modules\imgproc\src\resize.cpp:4051: error: (-215:Assertion failed) !ssize.empty() in function 'cv::resize'

I did try to print the image path and I found that on Mac, it is performed correctly
but on the Windows
the path shows

C:/Users/Wor/Downloads/PPE_Detection/PPE_Detection/Sample_Input/had_no_email_in_folder/image_fol\\139.jpg

the last part of the path is double backslash while on mac is normally path
I searched on the internet and found that \ also can work
but actually it won’t

I did try copied the

C:/Users/Wor/Downloads/PPE_Detection/PPE_Detection/Sample_Input/had_no_email_in_folder/image_fol\\139.jpg

and pasted on the Windows Explorer (don’t know its name)
it also cannot find the image

but if I change double backslash to normal slash like this

C:/Users/Wor/Downloads/PPE_Detection/PPE_Detection/Sample_Input/had_no_email_in_folder/image_fol/139.jpg

it can find the image

How this problem occurs?

I cannot find anyone had problem like me

that’s the most blunt lie i ever heard.
it sems, every python noobs encounters this kind of error, this also has been answered like 500 times here and on SO

main problem is: you have to manually CHECK the output of imread() or cap.read()
else your NEXT operation will fail, something like:

    img = cv2.imread(img_path)
    if img is None:
          # print some err
          continue

btw:

\\ only works inside strings (inside a program), not in some filebox

and yes, even on windows, prefer single / as seperators

it cannot find the image at all but the path is generated by image in the folder

img = cv2.imread(img_path)
if img is None:
# print some err
continue

if I do this nothing would pass into detection

as I have said it works correctly on mac but not on windows

why the last part of the path have to be \ and as you have said it works on program why the error occurs

actually I have seen a ton of [Error: (-215:Assertion failed) but their case, they can’t even run their code but mine perform correctly on mac

I no longer had Windows to try any but I just want to know why this occur

(the code is written on my mac, I just want to run it on the other computer)

maybe you need some glob filter, like "/*.jpg"
there might be non-image files in your folder, like a thumbnails.db

the images had jpg, jpeg, or even png that why I put just *

I have set it to show all hidden file there are only image on the folder

or even change name of all images in folder to not contain any special symbol, I already did that

  • in the folder

sorry for poor grammar

Maybe try to get just the filename and then rebuild the path with os.path.join which will use the correct character depending on the OS.

Also, in my opinion you should do some checks before, like “os.path.isfile(path)” or “os.path.exists(path)”, that will save you some trouble

[EDIT] You also have built-in functions in os module like os.path.normcase

I’m on Windows, see :

import os
import glob
folder = "C:/Users/pomme/test"
subfolders = glob.glob(folder+"/*")
print(folder)
for foldername in subfolders:
    print(os.path.normcase(foldername))

Result

['C:/Users/pomme/test\\one', 'C:/Users/pomme/test\\two']

c:\users\pomme\test\one
c:\users\pomme\test\two

You can also fix the pathnames yourself by doing

filename.replace("\\","/")
filename.replace("\","/")