train multiple images per person find and recognize faces in an image using a svc with scikitlearn structure testimage jpg traindir person1 person1face1 jpg person1face2 jpg person1facen jpg person2 person2face1 jpg person2face2 jpg person2facen jpg personn personnface1 jpg personnface2 jpg personnfacen jpg training the svc classifier the training data would be all the face encodings from all the known images and the labels are their names training directory loop through each person in the training directory loop through each training image for the current person get the face encodings for the face in each image file if training image contains exactly one face add face encoding for current image with corresponding label name to the training data create and train the svc classifier load the test image with unknown faces into a numpy array find all the faces in the test image using the default hogbased model predict all the faces in the test image using the trained classifier train multiple images per person find and recognize faces in an image using a svc with scikit learn structure test_image jpg train_dir person_1 person_1_face 1 jpg person_1_face 2 jpg person_1_face n jpg person_2 person_2_face 1 jpg person_2_face 2 jpg person_2_face n jpg person_n person_n_face 1 jpg person_n_face 2 jpg person_n_face n jpg training the svc classifier the training data would be all the face encodings from all the known images and the labels are their names training directory loop through each person in the training directory loop through each training image for the current person get the face encodings for the face in each image file if training image contains exactly one face add face encoding for current image with corresponding label name to the training data create and train the svc classifier load the test image with unknown faces into a numpy array find all the faces in the test image using the default hog based model predict all the faces in the test image using the trained classifier
import face_recognition
from sklearn import svm
import os




encodings = []
names = []


train_dir = os.listdir('/train_dir/')


for person in train_dir:
    pix = os.listdir("/train_dir/" + person)

    
    for person_img in pix:
        
        face = face_recognition.load_image_file("/train_dir/" + person + "/" + person_img)
        face_bounding_boxes = face_recognition.face_locations(face)

        
        if len(face_bounding_boxes) == 1:
            face_enc = face_recognition.face_encodings(face)[0]
            
            encodings.append(face_enc)
            names.append(person)
        else:
            print(person + "/" + person_img + " was skipped and can't be used for training")


clf = svm.SVC(gamma='scale')
clf.fit(encodings,names)


test_image = face_recognition.load_image_file('test_image.jpg')


face_locations = face_recognition.face_locations(test_image)
no = len(face_locations)
print("Number of faces detected: ", no)


print("Found:")
for i in range(no):
    test_image_enc = face_recognition.face_encodings(test_image)[i]
    name = clf.predict([test_image_enc])
    print(*name)