# Source empty in function cvtColor

**URL:** <https://forum.opencv.org/t/source-empty-in-function-cvtcolor/10586>\
**Category:** Python\
**Tags:** programming\
**Created:** [October 18, 2022, 2:23am UTC](https://forum.opencv.org/t/source-empty-in-function-cvtcolor/10586 "2022-10-18T02:23:39Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Finn](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/finn/32/6297_2.png) [@Finn](https://forum.opencv.org/u/Finn)\
**Post date:** [October 18, 2022, 2:23am UTC](https://forum.opencv.org/t/source-empty-in-function-cvtcolor/10586/1 "2022-10-18T02:23:39Z")

</div>

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](https://www.youtube.com/watch?v=oXlwWbU8l2o) 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:

```auto
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)}')

```

---

<div class="post-metadata">

**Author:** ![laurent.berger](https://avatars.discourse-cdn.com/v4/letter/l/ec9cab/32.png) [@laurent.berger](https://forum.opencv.org/u/laurent.berger)\
**Post date:** [October 18, 2022, 7:14am UTC](https://forum.opencv.org/t/source-empty-in-function-cvtcolor/10586/2 "2022-10-18T07:14:55Z")

</div>

Hi,  
How do you know this in your code?

> [@Finn](#):
>
> It seems to be reading the files fine, but I can’t figure out what else is going wrong.

You have to check it :  
[https://docs.opencv.org/4.x/db/deb/tutorial\_display\_image.html](https://docs.opencv.org/4.x/db/deb/tutorial_display_image.html)

```auto
import cv2 as cv

import sys

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

```
