-
Notifications
You must be signed in to change notification settings - Fork 1
/
CNN_PREDICT.py
137 lines (105 loc) · 3.82 KB
/
CNN_PREDICT.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
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, Conv2D , MaxPooling2D
from keras.utils import to_categorical
import librosa
import os
from sklearn.model_selection import train_test_split
from keras.utils import to_categorical
import numpy as np
from tqdm import tqdm
names=["nishant","rajat","shreekar","shruthi"]
#--------------------------------------
#EXTRACTING MFCC AND PADDING ZEROES THEN SAVING IT AS .NPY FILES
def wav2mfcc(path):
max_len=13
wave,sr=librosa.load(path,sr=None)
#wave=wave[::3] #downsampling
mfcc=librosa.feature.mfcc(wave,sr=16000)
if (max_len>mfcc.shape[1]):
pad_width=max_len-mfcc.shape[1]
mfcc=np.pad(mfcc,pad_width=((0,0),(0,pad_width)),
mode='constant')
else:
mfcc=mfcc[:,:max_len]
return mfcc
#---------------------------------------
#RECORD VOICE AND SAVE IT IN .WAV FILE
import pyaudio
import wave
import os
import librosa
import numpy as np
from hmmlearn.hmm import GMMHMM
import pickle
from sklearn.externals import joblib
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
CHUNK = 1024
RECORD_SECONDS = 5
WAVE_OUTPUT_FILENAME = "C:/Anaconda codes/speaker reco/something new/samples/file_out.wav"
audio = pyaudio.PyAudio()
# start Recording
stream = audio.open(format=FORMAT, channels=CHANNELS,
rate=RATE, input=True,
frames_per_buffer=CHUNK)
print("recording...")
frames = []
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
frames.append(data)
print("finished recording")
# stop Recording
stream.stop_stream()
stream.close()
audio.terminate()
waveFile = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
waveFile.setnchannels(CHANNELS)
waveFile.setsampwidth(audio.get_sample_size(FORMAT))
waveFile.setframerate(RATE)
waveFile.writeframes(b''.join(frames))
waveFile.close()
import tensorflow as tf
from keras.models import load_model
model = load_model("speaker_reco.model")
g="C:/Anaconda codes/speaker reco/something new/samples/file_out.wav"
#g='C:/Anaconda codes/digits speech recognition/free-spoken-digit-dataset-master/7.wav'
#sample=wav2mfcc('C:/Anaconda codes/digits speech recognition/free-spoken-digit-dataset-master/recordings/5/5_jackson_5.wav')
sample=wav2mfcc(g)
sample=sample.reshape(1,20,13,1)
#print(sample)
predictions=model.predict(sample)
c=np.argmax(model.predict(sample))
print(predictions)
print(c)
y=np.argmax(model.predict(sample))
p=names[y]
print(p)
import speech_recognition as sr
# obtain path to "Daily_English_Conversation_02_Do_you_speak_English.wav" in the same folder as this script
from os import path
AUDIO_FILE = ( "C:/Anaconda codes/speaker reco/something new/samples/file_out.wav")
# use the audio file as the audio source
r = sr.Recognizer()
with sr.AudioFile(AUDIO_FILE) as source:
#print("Say something!")
audio = r.record(source) # read the entire audio file
try:
# for testing purposes, we're just using the default API key
# to use another API key, use `r.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY")`
# instead of `r.recognize_google(audio)`
print("Google Speech Recognition thinks you said : " + r.recognize_google(audio))
number=r.recognize_google(audio)
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))
number=int(number)
print(number)
if (p=="rajat" and number==127):
print("attendance given to rajat")
elif (p=="nishant" and number==102):
print("attendance given to nishanth")
else:
print("dont try to give proxy")