Ask Your Question
0

How to solve error: (-215:Assertion failed) !empty() in function 'cv::CascadeClassifier::de

asked 2020-04-02 00:39:55 -0600

sabe gravatar image

updated 2020-04-02 03:36:47 -0600

berak gravatar image

When I run the programm, it shows

Traceback (most recent call last):
  File "F:/pycharm file/webcam.py", line 18, in <module>
    faces = face_cascade.detectMultiScale(gray, 1.1, 5, cv2.CASCADE_SCALE_IMAGE)
cv2.error: OpenCV(4.1.2) C:\projects\opencv-python\opencv\modules\objdetect\src\cascadedetect.cpp:1689: error: (-215:Assertion failed) !empty() in function 'cv::CascadeClassifier::de

My code is here :

import cv2


face_cascade = cv2.CascadeClassifier('F:/pycharm file/webcam.py/haarcascade_frontalface_default.xml')

cap = cv2.VideoCapture(0)



while True:
    # Read the frame
    _, img = cap.read()
    #ret, im = cap.read()
    # Convert to grayscale
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    # Detect the faces
    faces = face_cascade.detectMultiScale(gray, 1.1, 5, cv2.CASCADE_SCALE_IMAGE)
    for (x, y, w, h) in faces:
        cv2.rectangle(img, (x, y), (x+w, y+h), (255, 255, 255), 3)
    # Display
    cv2.imshow('img', img)
    # Stop if escape key is pressed
    k = cv2.waitKey(30) & 0xff
    if k==27:
        break

cap.release()

I tried every solution available on the internet. Most of the solutions say to locate the xml file properly. I located that path properly. Then another group of solution says to check the webcam. I checked the webcam through another code and it properly gives grayscale image when asked. I'm using python 3.7 and opencv 4.1.2

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2020-04-02 02:15:25 -0600

berak gravatar image

updated 2020-04-02 03:21:55 -0600

it might be hard to understand, what a file path is:

if F:/pycharm file/webcam.py is your python script, then

F:/pycharm file/webcam.py/haarcascade_frontalface_default.xml

is most likely invalid (webcam.py is not a directory)

if it's in the same folder as your script, i'm guessing you wanted

F:/pycharm file/haarcascade_frontalface_default.xml
edit flag offensive delete link more

Comments

When I made correction, the code ran and showed some error. It is

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "F:/pycharm file/webcam.py", line 4, in <module>
    face_cascade = cv2.CascadeClassifier('F:/pycharm file/haarcascade_frontalface_default.xml')
SystemError: <class 'cv2.CascadeClassifier'> returned a result with an error set

Process finished with exit code 1

sabe gravatar imagesabe ( 2020-04-02 08:35:57 -0600 )edit

If you are in currently folder. You set exactly like that ..

face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

Another thing is or replace..

faces = face_cascade.detectMultiScale(gray, 1.1, 5)
supra56 gravatar imagesupra56 ( 2020-04-02 09:32:43 -0600 )edit

after applying the corrections,

SystemError: <class 'cv2.cascadeclassifier'=""> returned a result with an error set

sabe gravatar imagesabe ( 2020-04-02 10:56:06 -0600 )edit

As @berak pointing this F:/pycharm file/haarcascade_frontalface_default.xml. It should be like this F:/pycharm file/haarcascades/haarcascade_frontalface_default.xml instead of webcam.py. Actually, where did you put xml file?

supra56 gravatar imagesupra56 ( 2020-04-02 12:52:03 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2020-04-02 00:39:55 -0600

Seen: 6,006 times

Last updated: Apr 02 '20