shivanshg's picture
Upload 5 files
4571b9b
raw
history blame
2.77 kB
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import os
import librosa
import joblib
import gradio as gr
## Voice Data Feature Extraction
### extract the features from the audio files using mfcc
def feature_extracter(fileName):
audio,sample_rate = librosa.load(fileName,sr=None, mono=True, dtype=np.float32,res_type='kaiser_fast')
mfcc_features = librosa.feature.mfcc(y=audio,sr=sample_rate,n_mfcc=30)
mfccs_scaled_features = np.mean(mfcc_features.T, axis=0)
return list(mfccs_scaled_features)
def prediction_age_gender(fileName):
col_name = ['Feature_1', 'Feature_2', 'Feature_3', 'Feature_4', 'Feature_5','Feature_6', 'Feature_7', 'Feature_8', 'Feature_9', 'Feature_10','Feature_11', 'Feature_12', 'Feature_13', 'Feature_14', 'Feature_15','Feature_16', 'Feature_17', 'Feature_18', 'Feature_19', 'Feature_20','Feature_21', 'Feature_22', 'Feature_23', 'Feature_24', 'Feature_25','Feature_26', 'Feature_27', 'Feature_28', 'Feature_29', 'Feature_30']
observation = [feature_extracter(fileName)]
observation = pd.DataFrame(observation, columns = col_name)
## scaling the observation
scaler = joblib.load('/content/scaler.pkl')
scaled_observation = scaler.transform(observation)
scaled_observation = pd.DataFrame(scaled_observation, columns = col_name)
### Gender classification model
gender_model = joblib.load('/content/KNN_gender_detection.pkl')
gender_predict = gender_model.predict_proba(scaled_observation)
## considering the labels 1 = male 0 = female
gender_dict = {}
gender_dict['Female'] = gender_predict[0][0]
gender_dict['Male'] = gender_predict[0][1]
### Age classification model
age_model = joblib.load('/content/KNN_age_model.pkl')
age_predict = age_model.predict_proba(scaled_observation)
age_dict = {}
age_dict['Eighties'] = age_predict[0][0]
age_dict['Fifties'] = age_predict[0][1]
age_dict['Fourties'] = age_predict[0][2]
age_dict['Seventies'] = age_predict[0][3]
age_dict['Sixties'] = age_predict[0][4]
age_dict['Teens'] = age_predict[0][5]
age_dict['Thirties'] = age_predict[0][6]
age_dict['Twenties'] = age_predict[0][7]
age_dict['Other'] = 1 - age_dict['Eighties'] - age_dict['Fifties'] - age_dict['Fourties'] - age_dict['Seventies'] - age_dict['Sixties'] - age_dict['Teens'] - age_dict['Thirties'] - age_dict['Twenties']
#final = "The person is a: " + gender + " of the age group: " + age
return gender_dict, age_dict
demo = gr.Interface(
prediction_age_gender,
inputs = [gr.Audio(sources=["microphone","upload"], type = 'filepath')],
outputs = [gr.Label(num_top_classes=2, label = 'Gender'), gr.Label(num_top_classes=9, label = 'Age Class')],
#gr.Text()
).launch(share=True, debug = True)