OpenCV's built in face recognizer is recognizing all wrong faces

So I have built a face recognition project with open cv’s built-in face recognizer, but the code is recognizing all the wrong faces.
My code-

import cv2 as cv
#import numpy as np

haar_cascade = cv.CascadeClassifier("haar_face.xml")
faces = ['geogre clooney', 'leonardo dicaprio', 'margot robbie', 'nawazuddin siddique', 'priyanka chopra']

#features = np.load("features.npy")
#labels = np.load("labels.npy")

face_recognizer = cv.face.LBPHFaceRecognizer_create()
face_recognizer.read("face_trained.yml")

# Testing
img = cv.imread("Z:/Projects.py/validation/margot.jpg")
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
cv.imshow("George", gray)

# Detecting the face of the image
faces_rect = haar_cascade.detectMultiScale(gray, 1.1, 4)

for (x,y,w,h) in faces_rect:
    faces_roi = gray[y:y+h, x:x+w]

    label, confidence = face_recognizer.predict(faces_roi)
    print(label)
    print(faces[label], ": ", confidence)

    cv.putText(img, str(faces[label]), (20,20), cv.FONT_HERSHEY_PLAIN, 1.0, (100, 50, 200), thickness=2)
    cv.rectangle(img, (x,y), (x+w,y+h), (100, 50, 200), thickness=2)

cv.imshow("Detected Image", img)

cv.waitKey(0)

If ya’ll need any more codeblocks please do tell me, I didn’t put them to make it look cleaner.

  • what are you trying to achieve here ?
    purpose of the face recognition ?
    those classes do a nearest neighbour retrieval from a db.
    is that really what you need ?
  • how many images do you have per person ? can you show a few ?
  • did you do any prepocessing / alignment for the training ?
  • this is all very old stuff. are you aware of more modern, cnn based methods (e.g. facenet) ?

So I’m a beginner in OpenCV and was following along a tutorial, where it was told that this code will recognize the face based on the dataset given.
So I don’t know exactly what are nearest neighbour retrieval other than the fact that they are one type of an algorithm.
Per person, around 15-20,


No, I did not do any prepocessing / alignment for the training .
No, I’m not aware of any modern cnn methods

1 Like

thanks for showing some images !

so, they show the upper body of persons, that’s far too much.
did you use those “as is” ?

for the training already, you need to crop out the face region, in the same way your program above does (Cascade) , and use only that, else you compare apples to pears.

that’s ok, you also want to learn something about it, right ?

but let me ask again:

those face reco classes can e.g. answer: “how many (known !) pupils of the school class are present ?” (identification)

but if your qestion is: “is this the same person as in the other image ?” (verification) or even: “is that me ?” (authentification) – they’re useless !!

So I have cropped the images, they are in the faces_roi variable.

So this is the difference between face detection and recognition right, what I wanted to achive was recognition

please show, how you train that thing.

not at all. those are 3 diffeent aspects of recognition

import cv2 as cv
import numpy as np

faces = ['geogre clooney', 'leonardo dicaprio', 'margot robbie', 'nawazuddin siddique', 'priyanka chopra']
DIR = "Z:/Projects.py/faces"
haar_cascade = cv.CascadeClassifier("haar_face.xml")

features = []
labels = []
print(faces)
# Obtain all the images
def create_train():
    for person in faces:
        path = os.path.join(DIR, person)
        label = faces.index(person)

        for img in os.listdir(path):

            img_path = os.path.join(path, img)
            img_array = cv.imread(img_path)
            gray = cv.cvtColor(img_array, cv.COLOR_BGR2GRAY)

            faces_rect = haar_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=4)

            for (x,y,w,h) in faces_rect:
                faces_roi = gray[y:y+h, x:x+w]
                features.append(faces_roi)
                labels.append(label)

create_train()
# Printing the number of images (features) and thier labesls (eg. 0 for leo, 1 for george)
print("-------------------------TRAINING DONE---------------------------")

# Converting list to numpy list
features = np.array(features, dtype="object")
labels = np.array(labels)

face_recognizer = cv.face.LBPHFaceRecognizer_create()

# Train the face recognizer with features and labels list
face_recognizer.train(features, labels)

# Saving the numpy lists and face recognizer
face_recognizer.save("face_trained.yml")
np.save("features.npy", features)
np.save("labels.npy", labels)