Home > other >  Image processing with OpenCV- AttributeError: module 'cv2' has no attribute 'face
Image processing with OpenCV- AttributeError: module 'cv2' has no attribute 'face

Time:11-23

I try to run following "trying.py" but get above error. How to fix it?

trying.py

import cv2, os
import numpy as np
from PIL import Image

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

# 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')

Error:

Traceback (most recent call last): File "C:\Users\HP\PycharmProjects\face_identificiation\trying.py", line 12, in recognizer = cv2.face.LBPHFaceRecognizer_create() AttributeError: module 'cv2' has no attribute 'face'

CodePudding user response:

Most probably, you installed the wrong OpenCV version.

Importing face will fail (I am using v4.6.0), since the module is not included in the "normal" install of OpenCv.

Try running pip list and check for the OpenCv Version. My guess is, that you installed normal OpenCv that will give you an entry like:

opencv-python 4.6.0.66

If that is the case, you should first uninstall opencv-python with pip uninstall opencv-python before you install OpenCv with contrib packages, using pip install opencv-contrib-python.

Your entry with pip list should then contain

opencv-contrib-python 4.6.0.66

With that, your code should work!

Edit: I should add, that you overwrote the keyword id in your code and you should rename your variable to something else, e.g. id_doc or so.

CodePudding user response:

Try this command:

pip install opencv-contrib-python --upgrade

If it didn't work, you can uninstall opencv-contrib-python using this command:

pip uninstall opencv-contrib-python

And install it again.

  • Related