-
Notifications
You must be signed in to change notification settings - Fork 1
/
face_recognizer_self_dataset.py
80 lines (62 loc) · 1.91 KB
/
face_recognizer_self_dataset.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
import cv2
import numpy as np
face_height = 65
face_width = 65
def get_trainer_data():
face_image = []
name = []
for i in range(1,3):
for j in range(1,11):
img = cv2.imread('FACES_self_dataset/s'+str(i)+'/'+str(j)+'.jpg',0)
img = cv2.resize(img,(500,250))
img = np.array(img,'uint8')
img = np.transpose(img)
#cv2.imshow('image',img)
#cv2.waitKey(50)
#print img
faceCascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
faces = faceCascade.detectMultiScale(img)
for (x, y, w, h) in faces:
print x,y,w,h
face_only = img[y: y + h, x: x + w]
face_only = cv2.resize(face_only,(face_height,face_width))
face_image.append(face_only)
#face_image = face_image.reshape(face_image.size,1)
#print face_image
name.append(i)
#print name
#face_image.append(face_only)
#name.append(i)
cv2.imshow("image",img)
cv2.imshow("Adding faces to traning set...", img[y: y + h, x: x + w])
cv2.waitKey(50)
print 'saving data_Set....',i
return face_image,name
def get_trainer_data_without_facedetect():
face_image = []
name = []
for i in range(1,9):
for j in range(1,11):
face_only = cv2.imread('FACES_self_dataset/s'+str(i)+'/1 ('+str(j)+').jpg',0)
print face_only.shape
face_only = cv2.resize(face_only,(face_height,face_width))
face_image.append(face_only)
name.append(i)
cv2.imshow("image",face_only)
cv2.waitKey(500)
print 'saving data_Set....',i
return face_image,name
#face_image, name = get_trainer_data()
face_image, name = get_trainer_data_without_facedetect()
#face_image = np.array(face_image,'float32')
#print face_image
#print face_image.shape
np.save('face_image_self.npy', face_image)
np.save('name_self.npy', name)
print face_image#,face_image.shape
print
print name
recogniser = cv2.createLBPHFaceRecognizer()
recogniser.train(face_image, np.array(name))
cv2.waitKey(0)
cv2.destroyAllWindows()