OpenCV(3.4.1) Error: Assertion failed

asked 2018-03-25 14:14:58 -0600

pooja127 gravatar image

updated 2018-04-13 07:44:50 -0600

Marko5280 gravatar image

While executing the program i am getting following error:

OpenCV(3.4.1) Error: Assertion failed (!empty()) in cv::CascadeClassifier::detectMultiScale, file C:\build\master_winpack-bindings-win32-vc14-static\opencv\modules\objdetect\src\cascadedetect.cpp, line 1698 Traceback (most recent call last): File "new24.py", line 20, in <module> flags=cv2.CASCADE_SCALE_IMAGE cv2.error: OpenCV(3.4.1) C:\build\master_winpack-bindings-win32-vc14-static\open cv\modules\objdetect\src\cascadedetect.cpp:1698: error: (-215) !empty() in function cv::CascadeClassifier::detectMultiScale

What could be the reason for the error,how should i react to fix it? Thank you for your time and advise,

regards

pooja

i am trying to execute following program

import cv2
import sys

cascPath = "C:\opencv\sources\data\haarcascades\haarcascade_frontalface_alt2.xml"
faceCascade = cv2.CascadeClassifier(cascPath)

video_capture = cv2.VideoCapture(0)

while True:
    ret, frame = video_capture.read()

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

    faces = faceCascade.detectMultiScale(
        gray,
        scaleFactor=1.1,
        minNeighbors=5,
        minSize=(30, 30),
        flags=cv2.CASCADE_SCALE_IMAGE
        )
    for (x, y, w, h) in faces:
        cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
    cv2.imshow('Video', frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
video_capture.release()
cv2.destroyAllWindows()
edit retag flag offensive close merge delete

Comments

Without any code it is difficult to answer but it seems that cascade classifier is empty : are you sure that path to cascade file is good?

LBerger gravatar imageLBerger ( 2018-03-25 14:20:31 -0600 )edit

i am trying to execute following program:

import cv2
import sys

cascPath = "C:\opencv\sources\data\haarcascades\haarcascade_frontalface_alt2.xml"

faceCascade = cv2.CascadeClassifier(cascPath)
video_capture = cv2.VideoCapture(0)

while True:
    ret, frame = video_capture.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    faces = faceCascade.detectMultiScale(
        gray,
        scaleFactor=1.1,
        minNeighbors=5,
        minSize=(30, 30),
        flags=cv2.CASCADE_SCALE_IMAGE
        )
    for (x, y, w, h) in faces:
        cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
    cv2.imshow('Video', frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
pooja127 gravatar imagepooja127 ( 2018-03-25 14:36:32 -0600 )edit

you must use double backslash \ \ and not \ . for opencv filename you can use / it is easier

LBerger gravatar imageLBerger ( 2018-03-25 14:40:54 -0600 )edit
2

The error is saying you're not passing a valid file to the classifier. Double check your path or this could be because your path contains "\", which in python are used to escape text characters.

Try this:

cascPath = r"C:\opencv\sources\data\haarcascades\haarcascade_frontalface_alt2.xml"

Notice the "r" in front of the path. This tells python to treat the string as raw, and not interpret character escapes.

dan01 gravatar imagedan01 ( 2018-03-26 05:19:41 -0600 )edit