-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathface_reco.py
96 lines (74 loc) · 2.35 KB
/
face_reco.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
# -*- coding: utf-8 -*-
"""Face_reco.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/gist/AtulJoshi1/979e4652fbdf56d86335f089cbc6f133/face_reco.ipynb
**Face Recognition using Transfer Learning from FaceNet**
"""
from google.colab import drive
drive.mount('/content/drive')
from keras_facenet import FaceNet
embedder = FaceNet()
import matplotlib.image as mpimg
import cv2
import numpy as np
import pandas as pd
def load_train_data(n):
lst = []
for i in range(1,n+1):
filename = str(i)+'.jpg'
x = mpimg.imread('/content/drive/My Drive/face reco/train/'+filename)
x = cv2.resize(x,(160,160))
lst.append(x)
df = np.array(lst)
y_train = pd.read_csv('/content/drive/My Drive/face reco/train/y_train.csv',header=None)
y_train = y_train.values
return df,y_train
def load_test_data(n):
lst = []
for i in range(1,n+1):
filename = str(i)+'.jpg'
x = mpimg.imread('/content/drive/My Drive/face reco/Test/'+filename)
x = cv2.resize(x,(160,160))
lst.append(x)
df = np.array(lst)
y_test = pd.read_csv('/content/drive/My Drive/face reco/Test/y_test.csv',header=None)
y_test = y_test.values
return df,y_test
'''
Loading the data set
'''
X_train_orig,y_train = load_train_data(24)
X_test_orig,y_test = load_test_data(7)
print('X_Train data shape=',X_train_orig.shape)
print('X_Test data shape=',X_test_orig.shape)
print('y_Train data shape=',y_train.shape)
print('y_Test data shape=',y_test.shape)
X_train = embedder.embeddings(X_train_orig)
X_test = embedder.embeddings(X_test_orig)
print('Train embed shape=',X_train.shape)
print('Test embed shape=',X_test.shape)
from sklearn.svm import SVC
from sklearn.linear_model import LogisticRegression
model = LogisticRegression()
model.fit(X_train,y_train)
#Predicted faces
model.predict(X_test)
#True faces
y_test
model.score(X_test,y_test)
'''
To recognise face of a given input file with path and filename.
Returns one of the six classes if face is from the training data faces,
else 'Face not found !' message is returned .
'''
def predict(path, filename):
x = mpimg.imread(path+'/'+filename)
x = cv2.resize(x,(160,160))
x = x.reshape(1,160,160,3)
x = embedder.embeddings(x)
if(max(model.predict_proba(x)[0])<0.25): #0.25 is the threshold
print("Face not found !")
return
else:
return model.predict(x)