AttributeError: 'module' object has no attribute 'face' error even after installing opencv-contrib

Question:

I was trying to implement a face recognition using Python, OpenCv2 and LBPH
(Which is downloaded from HERE)

My python version is 2.7.14
PIP version is 9.0.3
and OpenCV version is 3.4.0

and my code is

import cv2
import numpy as np
import NameFind

# --- import the Haar cascades for face and eye ditection
face_cascade = cv2.CascadeClassifier('Haar/haarcascade_frontalcatface.xml')
eye_cascade = cv2.CascadeClassifier('Haar/haarcascade_eye.xml')
spec_cascade = cv2.CascadeClassifier('Haar/haarcascade_eye_tree_eyeglasses.xml')

help(cv2.face)
# FACE RECOGNISER OBJECT
LBPH = cv2.face.LBPHFaceRecognizer_create(2, 2, 7, 7, 20)
EIGEN = cv2.face.createEigenFaceRecognizer(10, 5000)
FISHER = cv2.face.createFisherFaceRecognizer(5, 500)

# Load the training data from the trainer to recognise the faces
LBPH.load("Recogniser/trainingDataLBPH.xml")
EIGEN.load("Recogniser/trainingDataEigan.xml")
FISHER.load("Recogniser/trainingDataFisher.xml")

# ------------------------------------  PHOTO INPUT  -----------------------------------------------------

img = cv2.imread('Me4.jpg')                  # ------->>> THE ADDRESS TO THE PHOTO

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)                # Convert the Camera to gray
faces = face_cascade.detectMultiScale(gray, 1.3, 4)         # Detect the faces and store the positions
print(faces)

for (x, y, w, h) in faces:                                  # Frames  LOCATION X, Y  WIDTH, HEIGHT

    Face = cv2.resize((gray[y: y+h, x: x+w]), (110, 110))   # The Face is isolated and cropped

    ID, conf = LBPH.predict(Face)                           # LBPH RECOGNITION
    print ID
    NAME = NameFind.ID2Name(ID, conf)
    NameFind.DispID(x, y, w, h, NAME, gray)

    ID, conf = EIGEN.predict(Face)                          # EIGEN FACE RECOGNITION
    NAME = NameFind.ID2Name(ID, conf)
    NameFind.DispID3(x, y, w, h, NAME, gray)

    ID, conf = FISHER.predict(Face)                         # FISHER FACE RECOGNITION
    NAME = NameFind.ID2Name(ID, conf)
    NameFind.DispID2(x, y, w, h, NAME, gray)

cv2.imshow('LBPH Face Recognition System', gray)           # IMAGE DISPLAY
cv2.waitKey(0)
cv2.destroyAllWindows()

I’m getting This error when i run any face recognition files like Recogniser_Image_All_Algorithms.py

Traceback (most recent call last): File
“Recogniser_Image_All_Algorithms.py”, line 11, in
LBPH = cv2.face.LBPHFaceRecognizer_create(2, 2, 7, 7, 20) AttributeError: ‘module’ object has no attribute ‘face’

I’ve googled the error and found the same answers like THIS ONE

after checking those feeds i tried to install opencv-contrib using python -m pip install opencv-contrib-python

it says

Requirement already satisfied: opencv-contrib-python in
c:usersrakanaconda3libsite-packages Requirement already
satisfied: numpy>=1.11.3 in c:usersrakanaconda3libsite-packages
(from opencv-contrib-python)

but the error remains same, how to fix this error. plz help

Asked By: rakcode

||

Answers:

I find out the problem myself! The problem was I had opencv-python installed, i uninstalled opencv-python and ran pip install opencv-contrib-python it worked.

Answered By: rakcode

I have installed opencv for python using sudo apt install python-opencv
& received error “AttributeError: ‘module’ object has no attribute ‘face'”.
My system configuration is OS Ubuntu 16.04 LTS & Python 2.7.12. I used following command to resolve the issue:
sudo apt remove python-opencv
And installed same library using pip
sudo pip install opencv-contrib-python

Now opencv is working absolutely fine.

Answered By: user2712873

In my Rpi
python version is 3.5 and 2.7 both. opencv version is 3.3.0
to change python platform from 2.7.x to 3.5.x…
go to program /python idle/ properties,desktop entry / browse/ select idle-python3.5
in face recognition module has no attribute “face”. after changing python platform this problem will be solve.

Answered By: juhi
recognizer = cv2.createLBPHFaceRecognizer()

try this out

Answered By: rohit kaswan