OpenCV Error: Assertion Failed Even Though File Exists

565 views Asked by At

I'm using this algorithm for gender and age prediction with the Wiki image set. Getting the following error. I have researched here extensively and found that this error usually means the image doesn't exist. I added a print statement to show the name of each photo. The photo that throws an error, 10_0_NozomiÌhashi.jpg, does exist, per the photo below.

enter image description here 0_1_MarkPorter.jpg

0_1_OleksiyKartunov.jpg

10_0_AmiraWillighagen.jpg

10_0_AngelikaHellmann.jpg

10_0_CandyHsu.jpg

10_0_Catharina-Amalia,PrincessofOrange.jpg

10_0_EmilyAlynLind.jpg

10_0_KarenPendleton.jpg

10_0_LadyLouiseWindsor.jpg

10_0_NozomiÌhashi.jpg

Traceback (most recent call last):
  File "preprocess.py", line 246, in <module>
    align_faces(clean = True)
  File "preprocess.py", line 172, in align_faces
    aligned = FL.getAligns(picname)
  File "C:\Users\abc\Downloads\Age-Gender-Pred-master\align_faces.py", line 45, in getAligns
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.error: OpenCV(4.1.1) C:\projects\opencv-python\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'

I considered that perhaps the foreign character in the file name was causing the error, so I edited all the file names with a regex replace (^[a-zA-z]+$) and the script was able to run through a similar number of photos before failing with the same error on a photo without any foreign characters. I then removed that string replacement because obviously that wasn't the issue. I am now at a loss for what could be causing this.

Code that's failing:

def getAligns(self,
                img,
                use_cnn = False,
                savepath = None,
                return_info = False):
    """
    get face alignment picture
    :param img: original BGR image or a path to it
    :param use_cnn: using CNN to extract aligned faces, if set, dlib 
                    be compiled with cuda support
    :param savepath: savepath, format "xx/xx/xx.png"
    :param return_info: if set, return face positinos [(x, y, w, h)] 
    :return: aligned faces, (opt) rects
    """
    if type(img) == str:
      img = cv2.imread(img)

    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

The final line fails.

Running on an Azure VM, Windows 10, Python 3.7, 64GB ram. Thank you in advance for any insights.

1

There are 1 answers

0
Ryan Park On

Check that the cv2.imread is performing as you expected; i.e., it might be returning None or some other non-image value.

It might be that the image is not being read properly, or the file itself is corrupted.

See also: OpenCV !_src.empty() in function 'cvtColor' error