-
Notifications
You must be signed in to change notification settings - Fork 0
/
face_recon.py
167 lines (100 loc) · 3.32 KB
/
face_recon.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import numpy as np
import cv2
import os
from PIL import Image
try:
os.mkdir("user_txt")
print("user_txt is create")
except:
print("user_txt is exsit!")
try:
os.mkdir("dataset")
print("dataset is create")
except:
print("dataset is exsit!")
try:
os.mkdir("recognizer")
print("recognizer is create")
except:
print("recognizer is exsit!")
#######################################
def data_set(user_id,name):
f = open("user_txt/"+user_id+".spw","w")
f.write(name)
f.close()
face = cv2.CascadeClassifier('face.xml')
cam = cv2.VideoCapture(0)
#u_id = input("enter user id : ")
#os.mkdir("dataset/user/"+user_id)
s_code = 0
while (1):
_,img = cam.read()
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
faces = face.detectMultiScale(gray,1.3,5)
for (x,y,w,h) in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(0,0,255),2)
img2 = gray[y:y+h,x:x+w]
cv2.imwrite("dataset/_"+user_id+"_"+str(s_code)+".jpg",img2)
#cv2.imshow("face",img2)
s_code = s_code+1
cv2.imshow("my_face",img)
if (cv2.waitKey(1) == ord('q')):
break
cam.release()
cv2.destroyAllWindows()
def train():
recon = cv2.face.LBPHFaceRecognizer_create();
path = "dataset"
faces = []
IDs = []
allimagepath = [os.path.join(path,f) for f in os.listdir(path)]
#print (allimagepath)
for imagepath in allimagepath:
face_img = Image.open(imagepath).convert('L')
face_np = np.array(face_img,'uint8')
ID = int(os.path.split(imagepath)[-1].split("_")[1])
faces.append(face_np)
IDs.append(ID)
# print(IDs)
cv2.imshow("face_train",face_np)
cv2.waitKey(10)
# cv2.destroyAllWindows()
#print(faces)
recon.train(faces,np.array(IDs))
recon.save("recognizer/train_data.yml")
cv2.destroyAllWindows()
#return faces,IDs
# print(faces)
def detector():
face = cv2.CascadeClassifier('face.xml')
cam = cv2.VideoCapture(0)
rec = cv2.face.LBPHFaceRecognizer_create();
try:
rec.read('recognizer/train_data.yml')
except:
print("train data not exsit!")
Id =0
# font
font = cv2.FONT_HERSHEY_SIMPLEX
while (1):
_,img = cam.read()
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
faces = face.detectMultiScale(gray,1.3,5)
for (x,y,w,h) in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)
#ana wakiya
Id,conf = rec.predict(gray[y:y+h,x:x+w])
#
# set id
cv2.putText(img,"ID : "+str(Id),(x,y+h+25),font,0.8,(255,0,0),2,cv2.LINE_AA)
#
f = open("user_txt/"+str(Id)+".spw","r")
user_name = f.readline()
f.close()
cv2.putText(img,"name : "+user_name,(x,y+h+45),font,0.8,(255,0,0),2,cv2.LINE_AA)
cv2.imshow("my_face",img)
if (cv2.waitKey(1) == ord('q')):
#cam.release()
break
cam.release()
cv2.destroyAllWindows()