Skip to content
This repository was archived by the owner on Jun 28, 2022. It is now read-only.
This repository was archived by the owner on Jun 28, 2022. It is now read-only.

Error when trying to run # convert BGR image to grayscale gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) #33

@rabiesofany

Description

@rabiesofany

I receive this error when trying to run this function to convert images to grayscale, this is not my code, this was provided by Udacity.

convert BGR image to grayscale

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


error Traceback (most recent call last)
in
1 # convert BGR image to grayscale
----> 2 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

error: OpenCV(4.1.0) /Users/travis/build/skvark/opencv-python/opencv/modules/imgproc/src/color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cvtColor'

That is the whole cell in the detecting human's pictures section

import cv2
import matplotlib.pyplot as plt
%matplotlib inline

extract pre-trained face detector

face_cascade = cv2.CascadeClassifier('haarcascades/haarcascade_frontalface_alt.xml')

load color (BGR) image

img = cv2.imread(human_files[3])

convert BGR image to grayscale

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

find faces in image

faces = face_cascade.detectMultiScale(gray)

print number of faces detected in the image

print('Number of faces detected:', len(faces))

get bounding box for each detected face

for (x,y,w,h) in faces:
# add bounding box to color image
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)

convert BGR image to RGB for plotting

cv_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

display the image, along with bounding box

plt.imshow(cv_rgb)
plt.show()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions