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