forked from SnShine/FaceRecognizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathface_recognition.py
78 lines (61 loc) · 2.3 KB
/
face_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
'''
Surya Teja Cheedella
shine123surya[at]gmail[dot]com
BITS Pilani, Hyderabad Campus
Usage: face_recognition.py <full/path/to/root/images/folder> <full/path/to/images/folder/to/predict>
Takes two arguments:
1. Input folder which contains sub-folders of subjects/ persons.
There should be images saved in subfolders which are used to train.
2. Path to a folder consists of images for which we are gonna predict subject.
Note: Images in here should be cropped and have only faces of subjects.
'''
import cv2
import numpy as np
from os import listdir
import sys, time
def get_images(path, size):
'''
path: path to a folder which contains subfolders of for each subject/person
which in turn cotains pictures of subjects/persons.
size: a tuple to resize images.
Ex- (256, 256)
'''
sub= 0
images, labels= [], []
people= []
for subdir in listdir(path):
for image in listdir(path+ "/"+ subdir):
#print(subdir, images)
img= cv2.imread(path+"/"+subdir+"/"+image, cv2.IMREAD_GRAYSCALE)
img= cv2.resize(img, size)
images.append(np.asarray(img, dtype= np.uint8))
labels.append(sub)
#cv2.imshow("win", img)
#cv2.waitKey(10)
people.append(subdir)
sub+= 1
return [images, labels, people]
if __name__== "__main__":
if len(sys.argv)!= 3:
print("Wrong number of arguments! See the usage.\n")
print("Usage: face_recognition.py <full/path/to/root/images/folder> <full/path/to/images/folder/to/predict>")
sys.exit()
[images, labels, people]= get_images(sys.argv[1], (256, 256))
#print([images, labels])
labels= np.asarray(labels, dtype= np.int32)
# initializing eigen_model and training
print("Initializing eigen FaceRecognizer and training...")
sttime= time.clock()
eigen_model= cv2.createEigenFaceRecognizer()
eigen_model.train(images, labels)
print("\tSuccessfully completed training in "+ str(time.clock()- sttime)+ " Secs!")
# starting to predict subject/ person
for image_name in listdir(sys.argv[2]):
try:
pre_image= cv2.imread(sys.argv[2]+ "/"+ image_name, cv2.IMREAD_GRAYSCALE)
pre_image= cv2.resize(pre_image, (256, 256))
except:
print("Couldn't read image. Please check the path to image file.")
sys.exit()
[predicted_label, predicted_conf]= eigen_model.predict(np.asarray(pre_image))
print("Predicted person in the image "+ image_name+ " : "+ people[predicted_label])