Home > other >  Opencv AttributeError: 'module' object has no attribute 'data'
Opencv AttributeError: 'module' object has no attribute 'data'

Time:01-20

That's what happened when I tried to use facial recognition on my raspberry PI The following code:

#coding=utf-8
import cv2
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades 'haarcascade_frontalface_default.xml')

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

cap = cv2.VideoCapture(0)

while(True):
 
    ret, frame = cap.read()
    faces = face_cascade.detectMultiScale(frame, 1.3, 5)
    img = frame
    for (x,y,w,h) in faces:
        img = cv2.rectangle(img,(x,y),(x w,y h),(255,0,0),2)
        face_area = img[y:y h, x:x w]
        eyes = eye_cascade.detectMultiScale(face_area)
        for (ex,ey,ew,eh) in eyes:
            cv2.rectangle(face_area,(ex,ey),(ex ew,ey eh),(0,255,0),1)
    cv2.imshow('frame2',img)
    if cv2.waitKey(5) & 0xFF == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()

Error message:

Traceback (most recent call last):
  File "face_recognition1.py", line 4, in <module>
    face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades 'haarcascade_frontalface_default.xml')
AttributeError: 'module' object has no attribute 'data'

python version:3.7.5 opencv version:3.4.3

CodePudding user response:

Not sure where you got that, but you don't need them. Just delete those:

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

CodePudding user response:

Your package of OpenCV is too old. You say it's v3.4.3. That is from 2018, so 3.5 years old.

Back then, the data namespace probably didn't exist yet.

Update the package.

Use the official package from PyPI: pip3 install --upgrade opencv-python (or just pip if you aren't on linux)

Do not use "OpenCV" packages from conda. At best they lag behind by half a year (conda-forge). At worst they're abandoned or potentially dangerous.

  •  Tags:  
  • Related