'AttributeError: module 'cv2.cv2' has no attribute 'CascadeClassifer' (OpenCV Face Detection)
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()
Solution 1:[1]
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()
Solution 2:[2]
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')
Solution 3:[3]
same error for me, I've solved this error by downgrading the openCV version.
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==
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Brendan Jowett |
Solution 2 | KetZoomer |
Solution 3 | A5H1Q |