-
Notifications
You must be signed in to change notification settings - Fork 5
/
facial_recognition.py
96 lines (73 loc) · 3.12 KB
/
facial_recognition.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# import the necessary packages
# the basic packages
from time import time
import matplotlib.pyplot as plt
import numpy as np
import cPickle as pickle
# importing the machine learning packages
from sklearn.datasets import fetch_lfw_people
from sklearn.cross_validation import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix
# Downloading the data(labeled faces in the wild) from the servers and if the data is already present,
# in the current working directory then load then as a numpy array
lfw_people = fetch_lfw_people(min_faces_per_person=70)
# the basic height and the width of the images and the number of images in the dataset
n_samples, h, w = lfw_people.images.shape
# load the data into a variable and its target values or say expected values in another variable
X = lfw_people.data
y = lfw_people.target
n_images = X.shape[0]
n_features = X.shape[1]
person_names = lfw_people.target_names
n_classes = person_names.shape[0]
print person_names
# printing the information about the dataset
print "Total dataset size:"
print "# images: %d" % n_images
print "# features: %d" % n_features
print "# classes: %d" % n_classes
# split into a training and testing set by 70-30 ratio
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)
# Train a Logisticregression classification model
print "Fitting the classifier to the training set"
t0 = time()
logisticreg = LogisticRegression()
logisticreg = logisticreg.fit(X_train, y_train)
print "done in %0.3fs" % (time() - t0)
with open("model.pkl", "wb") as fh:
pickle.dump(logisticreg, fh)
# Quantitative evaluation of the model quality on the test set
print "Predicting people's names on the test set"
t0 = time()
y_pred = logisticreg.predict(X_test)
print "done in %0.3fs" % (time() - t0)
# calculating the accuracy of the current system
num_examples = y_pred.shape[0]
count = 0
for idx in xrange(num_examples):
if y_pred[idx] == y_test[idx]:
count += 1
print "Accuracy: %0.3fs" % (count*100.0/num_examples)
print confusion_matrix(y_test, y_pred, labels=range(n_classes))
# Qualitative evaluation of the predictions using matplotlib
def plot_gallery(images, titles, h, w, n_row=3, n_col=4):
"""Helper function to plot a gallery of portraits"""
plt.figure(figsize=(1.8 * n_col, 2.4 * n_row))
plt.subplots_adjust(bottom=0, left=.01, right=.99, top=.90, hspace=.35)
for i in xrange(n_row * n_col):
plt.subplot(n_row, n_col, i + 1)
plt.imshow(images[i].reshape((h, w)), cmap=plt.cm.gray)
plt.title(titles[i], size=12)
plt.xticks(())
plt.yticks(())
# plot the result of the prediction on a portion of the test set
def title(y_pred, y_test, target_names, i):
pred_name = target_names[y_pred[i]].rsplit(' ', 1)[-1]
true_name = target_names[y_test[i]].rsplit(' ', 1)[-1]
return 'predicted: %s\ntrue: %s' % (pred_name, true_name)
prediction_titles = [title(y_pred, y_test, person_names, i)
for i in range(y_pred.shape[0])]
plot_gallery(X_test, prediction_titles, h, w)
plt.show()