{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.python.client import device_lib\nprint(device_lib.list_local_devices())\nprint(\"Num GPUs Available: \", len(tf.config.experimental.list_physical_devices('GPU')))\n","metadata":{"execution":{"iopub.status.busy":"2021-07-02T12:58:18.086246Z","iopub.execute_input":"2021-07-02T12:58:18.086663Z","iopub.status.idle":"2021-07-02T12:58:24.372375Z","shell.execute_reply.started":"2021-07-02T12:58:18.086625Z","shell.execute_reply":"2021-07-02T12:58:24.370810Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-07-02T12:58:24.377166Z","iopub.execute_input":"2021-07-02T12:58:24.377470Z","iopub.status.idle":"2021-07-02T12:58:24.383871Z","shell.execute_reply.started":"2021-07-02T12:58:24.377440Z","shell.execute_reply":"2021-07-02T12:58:24.383167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from os.path import isdir, join\nfrom pathlib import Path\nimport pandas as pd\nimport tensorflow as tf\n# Math\nimport numpy as np\nfrom scipy.fftpack import fft\nfrom scipy import signal\nfrom scipy.io import wavfile\nimport librosa\n\nfrom sklearn.decomposition import PCA\n\n# Visualization\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport IPython.display as ipd\nimport librosa.display\n\nimport plotly.offline as py\npy.init_notebook_mode(connected=True)\nimport plotly.graph_objs as go\nimport plotly.tools as tls\nimport pandas as pd\n\nimport warnings\nwarnings.filterwarnings(\"ignore\")\n%matplotlib inline","metadata":{"execution":{"iopub.status.busy":"2021-07-02T12:58:24.385064Z","iopub.execute_input":"2021-07-02T12:58:24.385517Z","iopub.status.idle":"2021-07-02T12:58:26.753903Z","shell.execute_reply.started":"2021-07-02T12:58:24.385485Z","shell.execute_reply":"2021-07-02T12:58:26.752625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import keras\nprint(keras.__version__)","metadata":{"execution":{"iopub.status.busy":"2021-07-02T12:58:26.755625Z","iopub.execute_input":"2021-07-02T12:58:26.755925Z","iopub.status.idle":"2021-07-02T12:58:26.824548Z","shell.execute_reply.started":"2021-07-02T12:58:26.755896Z","shell.execute_reply":"2021-07-02T12:58:26.823380Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!apt-get install -y p7zip-full\n!7z x ../input/tensorflow-speech-recognition-challenge/train.7z\n","metadata":{"execution":{"iopub.status.busy":"2021-07-02T12:58:26.828326Z","iopub.execute_input":"2021-07-02T12:58:26.828673Z","iopub.status.idle":"2021-07-02T13:00:36.204906Z","shell.execute_reply.started":"2021-07-02T12:58:26.828640Z","shell.execute_reply":"2021-07-02T13:00:36.203507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_audio_path = 'train/audio/'\nprint(os.listdir(train_audio_path))","metadata":{"execution":{"iopub.status.busy":"2021-07-02T13:00:36.208793Z","iopub.execute_input":"2021-07-02T13:00:36.209185Z","iopub.status.idle":"2021-07-02T13:00:36.216022Z","shell.execute_reply.started":"2021-07-02T13:00:36.209150Z","shell.execute_reply":"2021-07-02T13:00:36.214368Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dirs = [f for f in os.listdir(train_audio_path) if isdir(join(train_audio_path, f))]\ndirs.sort()\nprint('Number of labels: ' + str(len(dirs[1:])))\nprint(dirs)","metadata":{"execution":{"iopub.status.busy":"2021-07-02T13:00:36.218200Z","iopub.execute_input":"2021-07-02T13:00:36.218717Z","iopub.status.idle":"2021-07-02T13:00:36.229661Z","shell.execute_reply.started":"2021-07-02T13:00:36.218667Z","shell.execute_reply":"2021-07-02T13:00:36.228376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels=os.listdir(train_audio_path)\n\n#find count of each label and plot bar graph\nno_of_recordings=[]\nfor label in labels:\n    waves = [f for f in os.listdir(train_audio_path + '/'+ label) if f.endswith('.wav')]\n    no_of_recordings.append(len(waves))\n    \n#plot\nplt.figure(figsize=(30,5))\nindex = np.arange(len(labels))\nplt.bar(index, no_of_recordings)\nplt.xlabel('Commands', fontsize=12)\nplt.ylabel('No of recordings', fontsize=12)\nplt.xticks(index, labels, fontsize=15, rotation=60)\nplt.title('No. of recordings for each command')\nplt.show()\n\nlabels=[\"up\",\"down\",\"left\",\"right\"]","metadata":{"execution":{"iopub.status.busy":"2021-07-02T13:00:36.231722Z","iopub.execute_input":"2021-07-02T13:00:36.232237Z","iopub.status.idle":"2021-07-02T13:00:36.705431Z","shell.execute_reply.started":"2021-07-02T13:00:36.232189Z","shell.execute_reply":"2021-07-02T13:00:36.704395Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#resampling to 8000Hz\nall_wave = []\nall_label = []\nfor label in labels:\n    print(label)\n    waves = [f for f in os.listdir(train_audio_path + '/'+ label) if f.endswith('.wav')]\n    for wav in waves:\n        samples, sample_rate = librosa.load(train_audio_path + '/' + label + '/' + wav, sr = 16000)\n       # samples = librosa.resample(samples, sample_rate, 8000)\n        if(len(samples)== 16000) : \n            all_wave.append(samples)\n            all_label.append(label)\n","metadata":{"execution":{"iopub.status.busy":"2021-07-02T13:00:36.707016Z","iopub.execute_input":"2021-07-02T13:00:36.707348Z","iopub.status.idle":"2021-07-02T13:00:38.933742Z","shell.execute_reply.started":"2021-07-02T13:00:36.707318Z","shell.execute_reply":"2021-07-02T13:00:38.932439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def log_specgram(audio, sample_rate, window_size=20,\n                 step_size=10, eps=1e-10):\n    nperseg = int(round(window_size * sample_rate / 1e3))\n    noverlap = int(round(step_size * sample_rate / 1e3))\n    freqs, times, spec = signal.spectrogram(audio,\n                                    fs=sample_rate,\n                                    window='hann',\n                                    nperseg=nperseg,\n                                    noverlap=noverlap,\n                                    detrend=False)\n    return freqs, times, np.log(spec.T.astype(np.float32) + eps)","metadata":{"execution":{"iopub.status.busy":"2021-07-02T13:00:38.935593Z","iopub.execute_input":"2021-07-02T13:00:38.936058Z","iopub.status.idle":"2021-07-02T13:00:38.943691Z","shell.execute_reply.started":"2021-07-02T13:00:38.936014Z","shell.execute_reply":"2021-07-02T13:00:38.942397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"raw_file=np.array(all_wave)\nfreqs, times, spectrogram =log_specgram(raw_file, 16000)\n","metadata":{"execution":{"iopub.status.busy":"2021-07-02T13:00:38.945530Z","iopub.execute_input":"2021-07-02T13:00:38.945966Z","iopub.status.idle":"2021-07-02T13:00:46.431700Z","shell.execute_reply.started":"2021-07-02T13:00:38.945923Z","shell.execute_reply":"2021-07-02T13:00:46.430567Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.shape(spectrogram)","metadata":{"execution":{"iopub.status.busy":"2021-07-02T13:00:46.433517Z","iopub.execute_input":"2021-07-02T13:00:46.433940Z","iopub.status.idle":"2021-07-02T13:00:46.441315Z","shell.execute_reply.started":"2021-07-02T13:00:46.433899Z","shell.execute_reply":"2021-07-02T13:00:46.440370Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"duration_of_recordings=[]\nfor label in labels:\n    waves = [f for f in os.listdir(train_audio_path + '/'+ label) if f.endswith('.wav')]\n    for wav in waves:\n        sample_rate, samples = wavfile.read(train_audio_path + '/' + label + '/' + wav)\n        duration_of_recordings.append(float(len(samples)/sample_rate))\n    \nplt.hist(np.array(duration_of_recordings))","metadata":{"execution":{"iopub.status.busy":"2021-07-02T13:00:46.443839Z","iopub.execute_input":"2021-07-02T13:00:46.444352Z","iopub.status.idle":"2021-07-02T13:00:46.993180Z","shell.execute_reply.started":"2021-07-02T13:00:46.444305Z","shell.execute_reply":"2021-07-02T13:00:46.992087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"L = 8000\ndef pad_audio(samples):\n    if len(samples) >= L:\n        return samples\n    else:\n        return np.pad(samples, pad_width=(L - len(samples), 0), mode='constant', constant_values=(0, 0))","metadata":{"execution":{"iopub.status.busy":"2021-07-02T13:00:46.994602Z","iopub.execute_input":"2021-07-02T13:00:46.994897Z","iopub.status.idle":"2021-07-02T13:00:47.000614Z","shell.execute_reply.started":"2021-07-02T13:00:46.994868Z","shell.execute_reply":"2021-07-02T13:00:46.999465Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"po=pad_audio(samples)\nplr=[]\nplr.append(float(len(po)/sample_rate))\nplt.hist(plr)","metadata":{"execution":{"iopub.status.busy":"2021-07-02T13:00:47.002139Z","iopub.execute_input":"2021-07-02T13:00:47.002460Z","iopub.status.idle":"2021-07-02T13:00:47.160239Z","shell.execute_reply.started":"2021-07-02T13:00:47.002430Z","shell.execute_reply":"2021-07-02T13:00:47.158874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from __future__ import print_function, division\nfrom builtins import range, input\n\n\nfrom keras.layers import Input, Lambda, Dense, Flatten\nfrom keras.models import Model\nfrom keras.applications.vgg16 import VGG16\nfrom keras.applications.vgg16 import preprocess_input\nfrom keras.preprocessing import image\nfrom keras.preprocessing.image import ImageDataGenerator\n\nfrom sklearn.metrics import confusion_matrix\nimport numpy as np\nimport matplotlib.pyplot as plt\n%matplotlib inline\nfrom glob import glob","metadata":{"execution":{"iopub.status.busy":"2021-07-02T13:00:47.161935Z","iopub.execute_input":"2021-07-02T13:00:47.162403Z","iopub.status.idle":"2021-07-02T13:00:47.175056Z","shell.execute_reply.started":"2021-07-02T13:00:47.162357Z","shell.execute_reply":"2021-07-02T13:00:47.174009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#changing dimensions\ntemp3=spectrogram.reshape((spectrogram.shape[2],spectrogram.shape[1],spectrogram.shape[0],1))\nprint(np.shape(temp3))\n\n\n\n#plotting spectrogram\ntemp = spectrogram[:,:,2061];\nfig = plt.figure(figsize=(14, 8))\nax2 = fig.add_subplot(111)\nax2.imshow(temp, aspect='auto', origin='lower')\nnp.shape(temp)\n","metadata":{"execution":{"iopub.status.busy":"2021-07-02T13:00:47.176594Z","iopub.execute_input":"2021-07-02T13:00:47.176970Z","iopub.status.idle":"2021-07-02T13:00:47.919903Z","shell.execute_reply.started":"2021-07-02T13:00:47.176938Z","shell.execute_reply":"2021-07-02T13:00:47.918646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#converting 1 ch spectrogram to 3 channel\nx=np.zeros((8534,161,99,3))\nx[:,:,:,0]=temp3[:,:,:,0]\nx[:,:,:,1]=temp3[:,:,:,0]\nx[:,:,:,2]=temp3[:,:,:,0]\nx.shape","metadata":{"execution":{"iopub.status.busy":"2021-07-02T13:22:05.026698Z","iopub.execute_input":"2021-07-02T13:22:05.027154Z","iopub.status.idle":"2021-07-02T13:22:07.885241Z","shell.execute_reply.started":"2021-07-02T13:22:05.027096Z","shell.execute_reply":"2021-07-02T13:22:07.884268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#training initials\nfrom sklearn.preprocessing import LabelEncoder\nle = LabelEncoder()\ny=le.fit_transform(all_label)\nclasses= list(le.classes_)\nfrom keras.utils import np_utils\ny=np_utils.to_categorical(y, num_classes=len(labels))","metadata":{"execution":{"iopub.status.busy":"2021-07-02T13:22:16.061015Z","iopub.execute_input":"2021-07-02T13:22:16.061460Z","iopub.status.idle":"2021-07-02T13:22:16.074209Z","shell.execute_reply.started":"2021-07-02T13:22:16.061430Z","shell.execute_reply":"2021-07-02T13:22:16.072800Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#SPLITTING DATA FOR TRAINING\nfrom sklearn.model_selection import train_test_split\nx_tr, x_val, y_tr, y_val = train_test_split(np.array(x),np.array(y),stratify=y,test_size = 0.2,random_state=777,shuffle=True)","metadata":{"execution":{"iopub.status.busy":"2021-07-02T13:22:18.620298Z","iopub.execute_input":"2021-07-02T13:22:18.620644Z","iopub.status.idle":"2021-07-02T13:22:25.476511Z","shell.execute_reply.started":"2021-07-02T13:22:18.620616Z","shell.execute_reply":"2021-07-02T13:22:25.475523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow.keras as K\nimport tensorflow as tf\nfrom keras.regularizers import l2\ninput_t = K.Input(shape=(161, 99, 3))\nres_model = K.applications.ResNet50(include_top=False, weights=\"imagenet\",\n                                        input_tensor=input_t)\n\nfor layer in res_model.layers[:143]:        layer.trainable = False\n    # Check the freezed was done ok\nfor i, layer in enumerate(res_model.layers):\n        print(i, layer.name, \"-\", layer.trainable)\n\nto_res = (161, 99)\n\nmodel = K.models.Sequential()\nmodel.add(K.layers.Lambda(lambda image: tf.image.resize(image, to_res)))\nmodel.add(res_model)\nmodel.add(K.layers.Flatten())\nmodel.add(K.layers.BatchNormalization())\nmodel.add(K.layers.Dense(256, activation='relu'))\nmodel.add(K.layers.Dropout(0.5))\nmodel.add(K.layers.BatchNormalization())\nmodel.add(K.layers.Dense(128, activation='relu'))\nmodel.add(K.layers.Dropout(0.5))\nmodel.add(K.layers.BatchNormalization())\nmodel.add(K.layers.Dense(64, activation='relu'))\nmodel.add(K.layers.Dropout(0.5))\nmodel.add(K.layers.BatchNormalization())\nmodel.add(K.layers.Dense(4, activation='softmax'))\n\n\n#model.summary()\n\n\n","metadata":{"execution":{"iopub.status.busy":"2021-07-02T13:22:34.767781Z","iopub.execute_input":"2021-07-02T13:22:34.768202Z","iopub.status.idle":"2021-07-02T13:22:37.153056Z","shell.execute_reply.started":"2021-07-02T13:22:34.768168Z","shell.execute_reply":"2021-07-02T13:22:37.151951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Trainn\nfrom keras.callbacks import EarlyStopping, ModelCheckpoint\nmodel.compile(loss=K.losses.categorical_crossentropy,\n             optimizer=K.optimizers.Adam(lr = 0.0001),\n             metrics=['accuracy'])\n\nes = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=10, min_delta=0.00001) \nmc = ModelCheckpoint('best_model.hdf5', monitor='val_acc', verbose=1, save_best_only=True, mode='max')\nhistory = model.fit(x_tr, y_tr, validation_data=(x_val, y_val),\n          batch_size=32, \n          epochs=20,\n          callbacks=[es,mc],            \n          verbose=1)","metadata":{"execution":{"iopub.status.busy":"2021-07-02T13:22:42.664381Z","iopub.execute_input":"2021-07-02T13:22:42.664778Z","iopub.status.idle":"2021-07-02T13:23:09.747563Z","shell.execute_reply.started":"2021-07-02T13:22:42.664747Z","shell.execute_reply":"2021-07-02T13:23:09.745830Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Trainn\n\nfrom keras.callbacks import EarlyStopping, ModelCheckpoint\nmodel.compile(loss='categorical_crossentropy',optimizer='sgd',metrics=['accuracy'])\nes = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=10, min_delta=0.00001) \nmc = ModelCheckpoint('best_model.hdf5', monitor='val_acc', verbose=1, save_best_only=True, mode='max')\nhistory=model.fit(x_tr, y_tr ,epochs=20, callbacks=[es,mc],  batch_size=32, validation_data=(x_val,y_val))","metadata":{"execution":{"iopub.status.busy":"2021-07-02T13:00:48.315876Z","iopub.status.idle":"2021-07-02T13:00:48.316567Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary() ","metadata":{"execution":{"iopub.status.busy":"2021-07-02T13:00:48.318074Z","iopub.status.idle":"2021-07-02T13:00:48.318599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from matplotlib import pyplot \npyplot.plot(history.history['loss'], label='train') \npyplot.plot(history.history['val_loss'], label='test') \npyplot.legend()\npyplot.show()","metadata":{"execution":{"iopub.status.busy":"2021-07-02T13:00:48.319344Z","iopub.status.idle":"2021-07-02T13:00:48.319749Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save_weights(\"weights.h5\")\nmodel.load_weights(\"weights.h5\")","metadata":{"execution":{"iopub.status.busy":"2021-07-02T13:00:48.320459Z","iopub.status.idle":"2021-07-02T13:00:48.320871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save('tfsrfyp_model.h5')","metadata":{"execution":{"iopub.status.busy":"2021-07-02T13:00:48.322065Z","iopub.status.idle":"2021-07-02T13:00:48.322629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import load_model\nnew_model = load_model('tfsrfyp_model.h5')\nnew_model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-07-02T13:00:48.323717Z","iopub.status.idle":"2021-07-02T13:00:48.324156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save_weights('./ tfsrfyp_model.pt') ","metadata":{"execution":{"iopub.status.busy":"2021-07-02T13:00:48.325229Z","iopub.status.idle":"2021-07-02T13:00:48.325718Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save_weights('my_model_weights.h5')","metadata":{"execution":{"iopub.status.busy":"2021-07-02T13:00:48.326612Z","iopub.status.idle":"2021-07-02T13:00:48.327139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.load_weights('my_model_weights.h5')","metadata":{"execution":{"iopub.status.busy":"2021-07-02T13:00:48.328043Z","iopub.status.idle":"2021-07-02T13:00:48.328487Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow import keras\nmodel = keras.models.load_model('./checkpoint')","metadata":{"execution":{"iopub.status.busy":"2021-07-02T13:00:48.329291Z","iopub.status.idle":"2021-07-02T13:00:48.329774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import soundfile as sd\nimport matplotlib.pyplot as plt\nimport time\n#import tensorflow.keras.backend as K\nimport numpy as np \nfrom scipy.io.wavfile import write\nfrom scipy.io.wavfile import read\nfrom scipy.io import wavfile\nfrom pydub import AudioSegment","metadata":{"execution":{"iopub.status.busy":"2021-07-02T13:00:48.330842Z","iopub.status.idle":"2021-07-02T13:00:48.331281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" pip install pyaudio ","metadata":{"execution":{"iopub.status.busy":"2021-07-02T13:00:48.332382Z","iopub.status.idle":"2021-07-02T13:00:48.332814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pip install sounddevice","metadata":{"execution":{"iopub.status.busy":"2021-07-02T13:00:48.334027Z","iopub.status.idle":"2021-07-02T13:00:48.334527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import sounddevice as sd\nfrom scipy.io.wavfile import write\n\nfs = 44100  # Sample rate\nseconds = 3  # Duration of recording\n\nmyrecording = sd.rec(int(seconds * fs), samplerate=fs, channels=2)\nsd.wait()  # Wait until recording is finished\nwrite('output.wav', fs, myrecording)  # Save as WAV file ","metadata":{"execution":{"iopub.status.busy":"2021-07-02T13:00:48.335368Z","iopub.status.idle":"2021-07-02T13:00:48.335965Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pyaudio\nimport wave\n \nFORMAT = pyaudio.paInt16\nCHANNELS = 2\nRATE = 44100\nCHUNK = 1024\nRECORD_SECONDS = 1\nWAVE_OUTPUT_FILENAME = \"file.wav\"\n \naudio = pyaudio.PyAudio()\n \n# start Recording\nstream = audio.open(format=FORMAT, channels=CHANNELS,\n                rate=RATE, input=True,\n                frames_per_buffer=CHUNK)\nprint (\"recording\")\nframes = []\n \nfor i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):\n    data = stream.read(CHUNK)\n    frames.append(data)\nprint (\"finished recording\")\n \n \n# stop Recording\nstream.stop_stream()\nstream.close()\naudio.terminate()\n \nwaveFile = wave.open(WAVE_OUTPUT_FILENAME, 'wb')\nwaveFile.setnchannels(CHANNELS)\nwaveFile.setsampwidth(audio.get_sample_size(FORMAT))\nwaveFile.setframerate(RATE)\nwaveFile.writeframes(b''.join(frames))\nwaveFile.close()","metadata":{"execution":{"iopub.status.busy":"2021-07-02T13:00:48.336953Z","iopub.status.idle":"2021-07-02T13:00:48.337409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#test data\ntest_audio_path = r'D:\\IST\\FYP\\dataset\\test\\audio\\clip_0000adecb.wav'\nprint(test_audio_path)\nsamples, sample_rate = librosa.load(test_audio_path )\nsamples = librosa.resample(samples, sample_rate, 8000)","metadata":{"execution":{"iopub.status.busy":"2021-07-02T13:00:48.338433Z","iopub.status.idle":"2021-07-02T13:00:48.338918Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def predict(test_audio_path):\n    prob=model.predict(test_audio_path)\n    index=np.argmax(prob[0])\n    return classes[index]\n\nimport random\nindex=random.randint(0,len(x_val)-1)\nsamples=x_val[index].ravel()\nprint(\"Audio:\",classes[np.argmax(y_val[index])])\nipd.Audio(samples, rate=8000)\nprint(\"Text:\",predict(samples))","metadata":{"execution":{"iopub.status.busy":"2021-07-02T13:00:48.339792Z","iopub.status.idle":"2021-07-02T13:00:48.340242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ntestwav[], testsr = librosa.load(test_audio_path)\n        ","metadata":{"execution":{"iopub.status.busy":"2021-07-02T13:00:48.341141Z","iopub.status.idle":"2021-07-02T13:00:48.341577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test=model.predict(testwav)","metadata":{"execution":{"iopub.status.busy":"2021-07-02T13:00:48.342395Z","iopub.status.idle":"2021-07-02T13:00:48.342828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import sys\nprint(sys.version)","metadata":{"execution":{"iopub.status.busy":"2021-07-02T13:00:48.343714Z","iopub.status.idle":"2021-07-02T13:00:48.344272Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf;\nprint(tf.__version__)","metadata":{"execution":{"iopub.status.busy":"2021-07-02T13:00:48.345195Z","iopub.status.idle":"2021-07-02T13:00:48.345618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import keras; \nprint(keras.__version__)","metadata":{"execution":{"iopub.status.busy":"2021-07-02T13:00:48.346932Z","iopub.status.idle":"2021-07-02T13:00:48.347432Z"},"trusted":true},"execution_count":null,"outputs":[]}]}