forked from ageitgey/face_recognition
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Find all the faces in an image then recognize them using a SVM with scikit-learn This allows you to train multiple images per person
- Loading branch information
1 parent
d386681
commit 0b1d879
Showing
1 changed file
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# Find all the faces in an image then recognize them using a SVM with scikit-learn | ||
# This allows you to train multiple images per person | ||
|
||
""" | ||
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 | ||
""" | ||
|
||
# Install scikit-learn if you haven't already with pip | ||
# $ pip3 install scikit-learn | ||
|
||
import face_recognition | ||
from sklearn import svm | ||
import os | ||
|
||
# Training the SVC classifier | ||
|
||
# The training data would be all the face encodings from all the known images and the labels are their names | ||
encodings = [] | ||
names = [] | ||
|
||
# Training directory | ||
train_dir = os.listdir('/train_dir/') | ||
|
||
# Loop through each person in the training directory | ||
for person in train_dir: | ||
pix = os.listdir("/train_dir/" + person) | ||
|
||
# Loop through each training image for the current person | ||
for person_img in pix: | ||
# Get the face encodings for the face in each image file | ||
face = face_recognition.load_image_file("/train_dir/" + person + "/" + person_img) | ||
face_enc = face_recognition.face_encodings(pic)[0] | ||
|
||
# Add face encoding for current image with corresponding label (name) to the training data | ||
encodings.append(face_enc) | ||
names.append(person) | ||
|
||
# Create and train the SVC classifier | ||
clf = svm.SVC(gamma='scale') | ||
clf.fit(encodings,names) | ||
|
||
# Load the test image with unknown faces into a numpy array | ||
test_image = face_recognition.load_image_file('test_image.jpg') | ||
|
||
# Find all the faces in the test image using the default HOG-based model | ||
face_locations = face_recognition.face_locations(test_image) | ||
no = len(face_locations) | ||
print("Number of faces detected: ", no) | ||
|
||
# Predict all the faces in the test image using the trained classifier | ||
print("Found: \n") | ||
for i in range(no): | ||
test_image_enc = face_recognition.face_encodings(test_image)[i] | ||
name = clf.predict([test_image_enc]) | ||
print(*name) |