-
Notifications
You must be signed in to change notification settings - Fork 59
/
add_user.py
209 lines (151 loc) · 5.68 KB
/
add_user.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import pyaudio
import wave
import cv2
import os
import pickle
import time
from scipy.io.wavfile import read
from IPython.display import Audio, display, clear_output
from main_functions import *
def add_user():
name = input("Enter Name:")
# check for existing database
if os.path.exists('./face_database/embeddings.pickle'):
with open('./face_database/embeddings.pickle', 'rb') as database:
db = pickle.load(database)
if name in db or name == 'unknown':
print("Name Already Exists! Try Another Name...")
return
else:
#if database not exists than creating new database
db = {}
cap = cv2.VideoCapture(0)
cap.set(3, 640)
cap.set(4, 480)
#detecting only frontal face using haarcascade
face_cascade = cv2.CascadeClassifier('./haarcascades/haarcascade_frontalface_default.xml')
i = 3
face_found = False
while True:
_, frame = cap.read()
frame = cv2.flip(frame, 1, 0)
#time.sleep(1.0)
cv2.putText(frame, 'Keep Your Face infront of Camera', (100, 200), cv2.FONT_HERSHEY_SIMPLEX,
0.8, (255, 255, 255), 2)
cv2.putText(frame, 'Starting', (260, 270), cv2.FONT_HERSHEY_SIMPLEX,
0.8, (255, 255, 255), 2)
cv2.putText(frame, str(i), (290, 330), cv2.FONT_HERSHEY_SIMPLEX,
1.3, (255, 255, 255), 3)
i-=1
cv2.imshow('frame', frame)
cv2.waitKey(1000)
if i < 0:
break
start_time = time.time()
img_path = './saved_image/1.jpg'
## Face recognition
while True:
curr_time = time.time()
_, frame = cap.read()
frame = cv2.flip(frame, 1, 0)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
face = face_cascade.detectMultiScale(gray, 1.3, 5)
if len(face) == 1:
for(x, y, w, h) in face:
roi = frame[y-10:y+h+10, x-10:x+w+10]
fh, fw = roi.shape[:2]
#make sure the face roi is of required height and width
if fh < 20 and fw < 20:
continue
face_found = True
#cv2.imwrite(img_path, roi)
cv2.rectangle(frame, (x-10,y-10), (x+w+10, y+h+10), (255, 200, 200), 2)
if curr_time - start_time >= 3:
break
cv2.imshow('frame', frame)
cv2.waitKey(1)
cap.release()
cv2.destroyAllWindows()
if face_found:
img = cv2.resize(roi, (96, 96))
db[name] = img_to_encoding(img)
with open('./face_database/embeddings.pickle', "wb") as database:
pickle.dump(db, database, protocol=pickle.HIGHEST_PROTOCOL)
elif len(face) > 1:
print("More than one faces found. Try again...")
return
else:
print('There was no face found in the frame. Try again...')
return
os.system('cls' if os.name == 'nt' else 'clear')
#Voice authentication
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
CHUNK = 1024
RECORD_SECONDS = 3
source = "./voice_database/" + name
os.mkdir(source)
for i in range(3):
audio = pyaudio.PyAudio()
if i == 0:
j = 3
while j>=0:
time.sleep(1.0)
os.system('cls' if os.name == 'nt' else 'clear')
print("Speak your name in {} seconds".format(j))
j-=1
elif i ==1:
time.sleep(2.0)
print("Speak your name one more time")
time.sleep(0.8)
else:
time.sleep(2.0)
print("Speak your name one last time")
time.sleep(0.8)
# start Recording
stream = audio.open(format=FORMAT, channels=CHANNELS,
rate=RATE, input=True,
frames_per_buffer=CHUNK)
print("recording...")
frames = []
for _ in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
frames.append(data)
# stop Recording
stream.stop_stream()
stream.close()
audio.terminate()
# saving wav file of speaker
waveFile = wave.open(source + '/' + str((i+1)) + '.wav', 'wb')
waveFile.setnchannels(CHANNELS)
waveFile.setsampwidth(audio.get_sample_size(FORMAT))
waveFile.setframerate(RATE)
waveFile.writeframes(b''.join(frames))
waveFile.close()
print("Done")
dest = "./gmm_models/"
count = 1
for path in os.listdir(source):
path = os.path.join(source, path)
features = np.array([])
# reading audio files of speaker
(sr, audio) = read(path)
# extract 40 dimensional MFCC & delta MFCC features
vector = extract_features(audio,sr)
if features.size == 0:
features = vector
else:
features = np.vstack((features, vector))
# when features of 3 files of speaker are concatenated, then do model training
if count == 3:
gmm = GMM(n_components = 16, n_iter = 200, covariance_type='diag',n_init = 3)
gmm.fit(features)
# saving the trained gaussian model
pickle.dump(gmm, open(dest + name + '.gmm', 'wb'))
print(name + ' added successfully')
features = np.asarray(())
count = 0
count = count + 1
if __name__ == '__main__':
add_user()