opencv / opencv

Open Source Computer Vision Library
https://opencv.org
Apache License 2.0
76.24k stars 55.63k forks source link

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

Closed sungjinp11 closed 6 years ago

sungjinp11 commented 6 years ago

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 "training.py", line 13, in recognizer = cv2.face.createLBPHFaceRecognizer() AttributeError: 'module' object has no attribute 'face'

error 2

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

error3

Traceback (most recent call last): File "training.py", line 13, in help(cv2.face.createLBPHFaceRecognizer) 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
faceSamples=[]

# Initialize empty id
ids = []

# Loop all the file path
for imagePath in imagePaths:

    # Get the image and convert it to grayscale
    PIL_img = Image.open(imagePath).convert('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])
    print(id)

    # 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
        faceSamples.append(img_numpy[y:y+h,x:x+w])

        # Add the ID to IDs
        ids.append(id)

# 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

recognizer.save('trainer/trainer.yml')

rrajnidhi commented 4 years ago

If uou are using python3 use pip3 instead of using pip for: pip3 install opencv-contrib-python.

then you can simply do: recognizer = cv2.face.LBPHFaceRecognizer_create()

amazingily worked

TumeloMosepele commented 3 years ago

i long solved it but thanks buddy.

On Sun, Jun 14, 2020 at 8:14 PM rahul06101997 notifications@github.com wrote:

i am getting error recoginizer = cv2.face.createLBPHFaceRecognizer() AttributeError: 'module' object has no attribute 'face' first pip install opencv-contrib-python and try like this

recoginizer = cv2.face.LBPHFaceRecognizer_create()

and for training data (if get any error for recoginizer.load(....yml)) use recoginizer.read(....yml)

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/opencv/opencv/issues/10516#issuecomment-643802262, or unsubscribe https://github.com/notifications/unsubscribe-auth/AIGXLV4JZNCPGFKLRAVQWPTRWUHRXANCNFSM4EKQPHJQ .