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

**URL:** <https://forum.opencv.org/t/opencvs-built-in-face-recognizer-is-recognizing-all-wrong-faces/4946>\
**Category:** Python\
**Tags:** face\
**Created:** [August 27, 2021, 8:18am UTC](https://forum.opencv.org/t/opencvs-built-in-face-recognizer-is-recognizing-all-wrong-faces/4946 "2021-08-27T08:18:30Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Avinier](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/avinier/32/3027_2.png) [@Avinier](https://forum.opencv.org/u/Avinier)\
**Post date:** [August 27, 2021, 8:18am UTC](https://forum.opencv.org/t/opencvs-built-in-face-recognizer-is-recognizing-all-wrong-faces/4946/1 "2021-08-27T08:18:30Z")

</div>

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-

```auto
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.

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [August 27, 2021, 8:41am UTC](https://forum.opencv.org/t/opencvs-built-in-face-recognizer-is-recognizing-all-wrong-faces/4946/2 "2021-08-27T08:41:30Z")

</div>

- 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) ?

---

<div class="post-metadata">

**Author:** ![Avinier](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/avinier/32/3027_2.png) [@Avinier](https://forum.opencv.org/u/Avinier)\
**Post date:** [August 28, 2021, 10:28am UTC](https://forum.opencv.org/t/opencvs-built-in-face-recognizer-is-recognizing-all-wrong-faces/4946/3 "2021-08-28T10:28:57Z")

</div>

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,

 ![image](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/0/0cc17d5c2a6ac01f30ef8daa8b0ee0366a6cecba.jpeg)  
No, I did not do any prepocessing / alignment for the training .  
No, I’m not aware of any modern cnn methods

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [August 28, 2021, 11:01am UTC](https://forum.opencv.org/t/opencvs-built-in-face-recognizer-is-recognizing-all-wrong-faces/4946/4 "2021-08-28T11:01:00Z")

</div>

thanks for showing some images !

> [@Avinier](#):
>
> No, I did not do any prepocessing / alignment for the training .

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.

> [@Avinier](#):
>
> So I’m a beginner in OpenCV

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

but let me ask again:

> [@berak](#):
>
> what are you trying to achieve here ?  
> purpose of the face recognition ?

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 !!

---

<div class="post-metadata">

**Author:** ![Avinier](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/avinier/32/3027_2.png) [@Avinier](https://forum.opencv.org/u/Avinier)\
**Post date:** [August 28, 2021, 3:12pm UTC](https://forum.opencv.org/t/opencvs-built-in-face-recognizer-is-recognizing-all-wrong-faces/4946/5 "2021-08-28T15:12:34Z")

</div>

> [@berak](#):
>
> 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.

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

> [@berak](#):
>
> those face reco classes can e.g. answer: “how many (known !) pupils of the school class are present ?” ( _identification_ )
> 
> but if your question is: “is this the same person as in the other image ?” ( _verification_ ) or even: “is that me ?” ( _authentification_ ) – they’re useless !!

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

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [August 28, 2021, 3:18pm UTC](https://forum.opencv.org/t/opencvs-built-in-face-recognizer-is-recognizing-all-wrong-faces/4946/6 "2021-08-28T15:18:17Z")

</div>

> [@Avinier](#):
>
> So I have cropped the images, they are in the `faces_roi` variable.

please show, how you **train** that thing.

> [@Avinier](#):
>
> So this is the difference between face detection and recognition right,

not at all. those are 3 diffeent aspects of recognition

---

<div class="post-metadata">

**Author:** ![Avinier](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/avinier/32/3027_2.png) [@Avinier](https://forum.opencv.org/u/Avinier)\
**Post date:** [August 28, 2021, 3:34pm UTC](https://forum.opencv.org/t/opencvs-built-in-face-recognizer-is-recognizing-all-wrong-faces/4946/7 "2021-08-28T15:34:03Z")

</div>

> [@berak](#):
>
> please show, how you **train** that thing.

```import
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)

```
