Ask Your Question

AttributeError: 'module' object has no attribute 'face'

asked 2018-01-07 18:37:44 -0500

sj.park gravatar image

updated 2018-01-08 03:35:44 -0500

I got an error when running opencv in Python on raspberry pi.

I tried to find and apply it to fix the error, but it did not work out. I also confirmed that the module "face" is in the file opencv_contrib-3.3.0. I do not know why for some reason.

error 1

Traceback (most recent call last):
File "", line 13, in 
recognizer = cv2.face.createLBPHFaceRecognizer()
AttributeError: 'module' object has no attribute 'face'

error 2

Traceback (most recent call last):
File "", line 13, in 
AttributeError: 'module' object has no attribute 'face'


Traceback (most recent call last):
File "", line 13, in 
AttributeError: 'module' object has no attribute 'face'

python : 3.5.3 opencv-3.3.0 opencv_contrib-3.3.0

source code

# Import OpenCV2 for image processing
# Import os for file path
import cv2, os

# Import numpy for matrix calculation
import numpy as np

# Import Python Image Library (PIL)
from PIL import Image

# Create Local Binary Patterns Histograms for face recognization
recognizer = cv2.face.createLBPHFaceRecognizer()

# Using prebuilt frontal face training model, for face detection
detector = cv2.CascadeClassifier("haarcascade_frontalface_default.xml");

# Create method to get the images and label data
def getImagesAndLabels(path):

    # Get all file path
    imagePaths = [os.path.join(path,f) for f in os.listdir(path)] 

    # Initialize empty face sample

    # Initialize empty id
    ids = []

    # Loop all the file path
    for imagePath in imagePaths:

        # Get the image and convert it to grayscale
        PIL_img ='L')

        # PIL image to numpy array
        img_numpy = np.array(PIL_img,'uint8')

        # Get the image id
        id = int(os.path.split(imagePath)[-1].split(".")[1])

        # Get the face from the training images
        faces = detector.detectMultiScale(img_numpy)

        # Loop for each face, append to their respective ID
        for (x,y,w,h) in faces:

            # Add the image to face samples

            # Add the ID to IDs

    # Pass the face array and IDs array
    return faceSamples,ids

# Get the faces and IDs
faces,ids = getImagesAndLabels('dataset')

# Train the model using the faces and IDs
recognizer.train(faces, np.array(ids))

# Save the model into trainer.yml'trainer/trainer.yml')
edit retag flag offensive close merge delete


It seems your installation cannot find the desired modules. Can you try installing through pip install opencv-contrib-python?

StevenPuttemans gravatar imageStevenPuttemans ( 2018-01-08 03:37:00 -0500 )edit

@StevenPuttemans. That not issue problem. The problem is cv2.face.createLBPHFaceRecognizer()....the variable face is missing.

supra56 gravatar imagesupra56 ( 2018-01-08 08:58:03 -0500 )edit

@supra56 Seriously? Please refrain yourself of adding these useless comments ... I asked the author a question. cv2 will always be available, but cv2.face will only be available IF the user installed the contrib python library correctly...

StevenPuttemans gravatar imageStevenPuttemans ( 2018-01-09 03:10:23 -0500 )edit

Thank you for your reply.

sj.park gravatar imagesj.park ( 2018-01-09 04:05:25 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2018-01-09 09:07:35 -0500

supra56 gravatar image

updated 2018-01-09 09:12:03 -0500

Apologised. Kindly, ignore my comment as above.

recognizer = cv2.face.LBPHFaceRecognizer_create()
edit flag offensive delete link more


Thank you :)

sj.park gravatar imagesj.park ( 2018-01-09 18:26:50 -0500 )edit

Bro tell me the solution if you solve that

varu gravatar imagevaru ( 2019-04-01 01:54:26 -0500 )edit

please give me reply, I am eagerly waiting......

varu gravatar imagevaru ( 2019-04-01 01:55:24 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2018-01-07 18:37:44 -0500

Seen: 6,204 times

Last updated: Jan 09 '18