Source empty in function cvtColor

Hi there, I am new here and new to openCV so I apologize if this has already been covered elsewhere but I couldn’t find an answer.

I followed this course and when I try to execute my program, I get an error code for line 35:
pp:182: error: (-215:Assertion failed) !_src.empty() in function ‘cvtColor’

It seems to be reading the files fine, but I can’t figure out what else is going wrong.
Any help is appreciated!

Here is the code:

import cv2 as cv
import numpy as np
import os

people = ['Brian', 'Gaga','Justin', 'Rupaul']

DIR = r'/Users/name/Desktop/FaceRecognition/Faces'

haar_cascade = cv.CascadeClassifier('haar_faces.xml')

#training set:
features = []
#image arrays of faces
labels = []
#for every face in this features list, what is its corresponding label? (whose face is it?)

def create_train():
    for person in people:
        path = os.path.join(DIR, person) 
        label = people.index(person) 
      
        for img in os.listdir(path):
            img_path = os.path.join(path, img) 

            img_array = cv.imread(img_path) 
            gray = cv.cvtColor(img_array, cv.COLOR_BGR2GRAY)

            #detects faces:
            faces_rect = haar_cascade.detectMultiScale(gray, scaleFactor = 1.1, minNeighbors=4)

            for (x,y,w,h) in faces_rect: #loops over every face in faces_rect
                 faces_roi = gray[y:y+h, x:x+w] #grab the faces region of interest and crop it
                 features.append(faces_roi) #appends the region of interest to the features list
                 labels.append(label) #appends the corresponding label to the labels list

create_train()

print(f'length of the features list = {len(features)}')
print(f'length of the labels list = {len(labels)}')

Hi,
How do you know this in your code?

You have to check it :
https://docs.opencv.org/4.x/db/deb/tutorial_display_image.html

import cv2 as cv

import sys

img = cv.imread("starry_night.jpg")
if img is None:
    sys.exit("Could not read the image.")
1 Like