Error Message: 'cv2' has no attribute 'face'!

i am trying to do something like this :

import os
import cv2
from PIL import Image pip install pillow
import numpy as np # pip install numpy

def train_classifier(data_dir):
path = [os.path.join(data_dir, f) for f in os.listdir(data_dir)]

faces = []
ids = []

for image in path:
    img ='L')
    imageNp = np.array(img, 'uint8')
    id = int(os.path.split(image)[1].split(".")[1])
ids = np.array(ids)

# Train and save classifier
clf = cv2.face.LBPHFaceRecognizer_create()


But there is error message coming is like this:

AttributeError Traceback (most recent call last)
Cell In[52], line 26
24 clf.train(faces,ids)
25 clf.write(“classifier.xml”)
—> 26 train_classifier(“data”)

Cell In[52], line 23, in train_classifier(data_dir)
20 ids = np.array(ids)
22 # Train and save classifier
—> 23 clf = cv2.face.LBPHFaceRecognizer_create()
24 clf.train(faces,ids)
25 clf.write(“classifier.xml”)

AttributeError: module ‘cv2’ has no attribute ‘face’

I have opencv version of 4.9.0
and I have tried uninstalling and then again installing opencv but nothing worked for me!

what should I do now?

your cv2 does not contain the face module

uninstall & reinstall:

pip install opencv-contrib-python
1 Like

No I have done it already,as I have also written in description.

You did exactly that? And it didn’t help? Then there is nothing more we can do for you.

Actually after restarting the jupyter notebook it worked for me.