AttributeError: module 'cv2.cv2' has no attribute 'CascadeClassifer' (OpenCV Face Detection)

Question:

I have tried running this code for the OpenCV face detection and upon running it I get the error (AttributeError: module ‘cv2.cv2’ has no attribute ‘CascadeClassifer’) Is there some sort of new code I need to use?

Error is on line 4 (face_cascade = cv2.CascadeClassifer(‘haarcascade_frontalface_default.xml’)

import cv2
import numpy as np

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

eye_cascade = cv2.CascadeClassifer('haarcascade_eye.xml')

cap = cv2.VideoCapture(0)

while True:
    ret, img = cap.read()
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    face = face_cascade.detectMultiScale(gray, 1.3, 5)
    for (x,y,w,h) in faces:
        cv2.rectangle(img, (x,y), (x+w, y+h), (255,0,0), 2)
        roi_gray = gray[y:y+h. x:x+w]
        roi_color = img[y:y+h. x:x+w]
        eye = eye_cascade.detectMultiScale(roi_gray)
        for (ex,ey,ew,eh) in eyes:
            cv2.rectangle(roi_color, (ex, ey), (ex+ew,ey+eh), (0,255,0), 2)

        cv2.imshow('img',img)
        k = cv2.waitKey(30) & 0xff
        if k == 27:
            break

cap.release()
cv2.destoryAllWindows()
Asked By: Brendan Jowett

||

Answers:

Assuming you want to load the classifier from a file.
Use the correct spellings

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

not

face_cascade = cv2.CascadeClassifer('haarcascade_frontalface_default.xml')
Answered By: AvidJoe

Nevermind, I have fixed the code if anyone is interested.

import numpy as np
import cv2

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

eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')

cap = cv2.VideoCapture(0)

while 1:
    ret, img = cap.read()
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)

    for (x,y,w,h) in faces:
        cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
        roi_gray = gray[y:y+h, x:x+w]
        roi_color = img[y:y+h, x:x+w]

        eyes = eye_cascade.detectMultiScale(roi_gray)
        for (ex,ey,ew,eh) in eyes:
            cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)

    cv2.imshow('img',img)
    k = cv2.waitKey(30) & 0xff
    if k == 27:
        break

cap.release()
cv2.destroyAllWindows()
Answered By: Brendan Jowett

same error for me, I’ve solved this error by downgrading the openCV version.

NOTE: The downgrading approach is not something I would recommend or use, but since i was unable to find a fix, downgrading to previous version helped my case.

Use pip install opencv-python== to get a list of openCV versions.
Try openCV versions below 4
first uninstall using pip uninstall opencv-python
then Install using pip install opencv-python==

EDIT: Consider downgrading as your last resort, if all other methods wont fix.

Answered By: A5H1Q
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.