def features(): path = 'C:/Verification/new_user' person = os.walk(path) for j in person: print(j) for file in j[2]: if file == 'filename1.wav': temp = audio_proc.mfcc(j[0] + '/' + file) else: temp = numpy.vstack((temp, audio_proc.mfcc(j[0] + '/' + file))) numpy.savetxt(j[0] + '/etalon.txt', temp)
def verify(): process = audio_proc.mfcc('C:/Verification/verification_file.wav') path_to_dir = 'C:/Verification/' folder = os.walk(path_to_dir) for i in folder: if i[1]: folders = i[1] rapid = 0.4 name = '' verified = False compare = float(numpy.inf) temp = 1 for i in range(0, len(folders)): help = False path = path_to_dir + folders[i] with open(path + '/etalon.txt') as fin: flag = True for line in fin: if flag: data = (line.rstrip('\n')).split(' ') flag = False else: data = numpy.vstack((data, (line.rstrip('\n')).split(' '))) data = data.astype(float) for k in range(data.shape[0]): dist = scipy.spatial.distance.correlation(data[k], process) #print(folders[i] + " " + dist.__str__()) if dist < rapid: help = True else: help = False if dist < temp: temp = dist if help and temp < compare: name = folders[i] compare = temp print(name + " " + temp.__str__()) verified = True if verified: f = open('C:/Verification/verified.txt', 'w') f.write("True") f.close() f = open('C:/Verification/name.txt', 'w') f.write(name) f.close() else: f = open('C:/Verification/verified.txt', 'w') f.write("False") f.close()
def registration(): how_to_say = ['НОРМАЛЬНО', 'БЫСТРО', 'МЕДЛЕННО', 'ТИХО', 'ГРОМКО'] name = input("Введите имя: ") path = 'C:/Verification/' + name os.mkdir(path) for i in range(0, len(how_to_say)): r = sr.Recognizer() with sr.Microphone() as source: print("Произнесите фразу " + how_to_say[i] + ": ЗЕЛЕНАЯ ТРАВА") r.adjust_for_ambient_noise(source) audio = r.listen(source) path_to_write = path + "/filename" + (i + 1).__str__() + ".wav" with open(path_to_write, "wb") as f: f.write(audio.get_wav_data()) if i == 0: array = audio_proc.mfcc(path_to_write) else: array = numpy.vstack((array, audio_proc.mfcc(path_to_write))) numpy.savetxt(path + '/etalon.txt', array)
def verify(): #data = numpy.genfromtxt('C:/Verification/Karina/etalon.txt', delimiter='\n', dtype=numpy.float) #data = audio_proc.mfcc('C:/Verification/Karina/filename1.wav') #process = numpy.loadtxt('C:/Verification/Karina/etalon.txt', delimiter='\n', dtype=numpy.float) with open('C:/Verification/etalon/etalon.txt') as fin: i = True for line in fin: if i: data = (line.rstrip('\n')).split(' ') i = False else: data = numpy.vstack((data, (line.rstrip('\n')).split(' '))) data = data.astype(float) process = audio_proc.mfcc('C:/Verification/Kamilya/filename3.wav') mark = 0.0 for i in range(data.shape[0]): dist = scipy.spatial.distance.euclidean(data[i], process) print(dist) '''CBD = 0 # city block distance
def verify(): r = sr.Recognizer() verification_file = 'C:/Verification/verification_file.wav' word_1 = [line.strip() for line in open('C:/Verification/1.txt', 'r')] word_2 = [line.strip() for line in open('C:/Verification/2.txt', 'r')] phrase = numpy.random.choice(word_1) + " " + numpy.random.choice(word_2) with sr.Microphone() as source: print("Произнесите фразу: " + phrase) r.adjust_for_ambient_noise(source) audio = r.listen(source, phrase_time_limit=5) print("Запись остановлена") try: phrase_recognition = r.recognize_google(audio, language="ru-RU") if phrase == phrase_recognition: with open(verification_file, "wb") as f: f.write(audio.get_wav_data()) process = audio_proc.mfcc(verification_file) path_to_dir = 'C:/Verification/' folder = os.walk(path_to_dir) for i in folder: if i[1]: folders = i[1] rapid = 0.5 name = '' verified = False compare = float(numpy.inf) temp = 1 for i in range(0, len(folders)): help = False path = path_to_dir + folders[i] with open(path + '/etalon.txt') as fin: flag = True for line in fin: if flag: data = (line.rstrip('\n')).split(' ') flag = False else: data = numpy.vstack( (data, (line.rstrip('\n')).split(' '))) data = data.astype(float) for k in range(data.shape[0]): dist = scipy.spatial.distance.correlation(data[k], process) #print(folders[i] + " " + dist.__str__()) if dist < rapid: help = True if dist < temp: temp = dist if help and temp < compare: name = folders[i] compare = temp print(name + " " + temp.__str__()) verified = True if verified: print('Привет, ' + name) else: print('Вы не были распознаны!') else: print('Произнесенная фраза неверна либо не распознана!') # error occurs when google could not understand what was said except sr.UnknownValueError: print("Google Speech Recognition could not understand audio") except sr.RequestError as e: print( "Could not request results from Google Speech Recognition service; {0}" .format(e))