opencv configuration_error ai_generated true

cv2.error: (-215:Assertion failed) !empty() in function 'detectMultiScale'

ID: opencv/cascade-classifier-empty

Also available as: JSON · Markdown
92%Fix Rate
90%Confidence
3Evidence
2023-01-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
4 active

Root Cause

CascadeClassifier is empty because XML model file was not loaded. Wrong file path.

generic

Workarounds

  1. 95% success Use cv2.data.haarcascades for bundled cascades
    cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')

    Sources: https://docs.opencv.org/4.x/

  2. 90% success Check if cascade loaded before using
    if face_cascade.empty(): raise FileNotFoundError('Cascade not found')
  3. 85% success Use absolute path via __file__
    os.path.join(os.path.dirname(__file__), 'models', 'my_cascade.xml')

Dead Ends

Common approaches that don't work:

  1. Download XML from random GitHub repo 72% fail

    Third-party files may be incompatible. Use official OpenCV data files.

  2. Use relative path assuming cwd is script location 80% fail

    Python cwd is where you ran the command, not where the script lives