0

I'm working with the following Code:

import cv2
tfd=cv2.CascadeClassifier(r"C:\Users\shlok\F\ml\opencv-master\data\haarcascades_cuda")
img=cv2.imread('RDJ.jpg')
gi=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
fc=tfd.detectMultiScale(gi)
print(fc)
cv2.imshow('Face detector',gi)  
cv2.waitKey()

However when I run it, it yields the following error:

cv2.error: OpenCV(4.5.3) C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-sn_xpupm\opencv\modules\objdetect\src\cascadedetect.cpp:1689: error: (-215:Assertion failed) !empty() in function 'cv::CascadeClassifier::detectMultiScale'
Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
bonk
  • 9
  • 1
  • The error indicates that your image object (`gi)` is empty. Are you sure your image is in the directory you expect? Have you printed `img` and `gi` to make sure they are valid? – Tim Roberts Sep 20 '21 at 06:15
  • your classifier "tfd" is initialized incorrectly. You need to pass a valid filepath – Miki Sep 20 '21 at 07:21
  • The call `cv2.imread('RDJ.jpg')` uses a relative path. That looks in the current working directory, which is very often not where novices expect. Start by providing a full path to the `.jpg` file. – BoarGules Sep 20 '21 at 07:22
  • if the image wasn't loaded correctly, the error would have been on the "cvtColor" line. – Miki Sep 20 '21 at 07:23
  • 1
    @TimRoberts actually no, the assertion calls `CascadeClassifier::empty()`, which checks that the xml file was loaded correctly. this assertion is not about the array argument. – Christoph Rackwitz Sep 20 '21 at 11:38

0 Answers0