Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

CEP Report:

Roll No: 2020-EE-503


2020-EE-510
2020-EE-519
2020-EE-523
Report: Gender Recognition from Voice
In this code, the objective is to recognize the gender of a person based on their voice. The code
consists of several steps, including data preprocessing, model training and evaluation, and voice
analysis. Let's break down the code and discuss each part in detail.
1. Data Exploration and Preprocessing:
 The code starts by importing necessary libraries such as numpy, pandas,
matplotlib, seaborn, and scikit-learn.
 It reads a CSV file named "voice.csv" using pandas, which presumably contains
the dataset for training the gender recognition model.
 Some initial exploratory data analysis (EDA) is performed to gain insights into
the dataset using pandas and seaborn. This includes summary statistics, checking
for missing values, and visualization of correlations and distributions of certain
features.
2. Model Training and Evaluation:
 The code proceeds to split the dataset into training and testing sets using the
train_test_split function from scikit-learn.
 Support Vector Machines (SVM), K-Nearest Neighbors (KNN), Random Forest,
and Decision Tree classifiers are trained and evaluated using the training and
testing sets.
 For SVM and KNN, hyperparameter tuning is performed using GridSearchCV to
find the best combination of hyperparameters.
 The classification reports, confusion matrices, and accuracy scores are displayed
for each model.
3. Voice Analysis:
 The code utilizes the sounddevice library to record audio for a specified duration.
 The recorded audio is reshaped into a one-dimensional array.
 A simple analysis is performed on the audio by calculating the average amplitude.
If the average amplitude exceeds a threshold (0.1 in this case), the voice is
classified as male.
 Depending on the gender classification, the code performs system-specific
actions. For a male voice, it locks the PC using the appropriate system command.
Reasoning for Algorithm Selection:
 Support Vector Machines (SVM), K-Nearest Neighbors (KNN), Random Forest, and
Decision Tree classifiers are used in this code.
 SVM is a powerful algorithm that can handle complex classification tasks and works well
with high-dimensional data. It aims to find an optimal hyperplane that separates the
classes.
 KNN is a simple but effective algorithm that classifies new instances based on the
majority vote of their nearest neighbors. It is suitable for this task as it can capture the
local characteristics of the data.
 Random Forest is an ensemble learning method that combines multiple decision trees to
make predictions. It can handle nonlinear relationships and is robust against overfitting.
 Decision Trees are intuitive and easy to interpret. They make decisions based on feature
values and create hierarchical structures. Decision Tree models are used to establish a
baseline for comparison.
Reasoning for not using other algorithms:
 Although the chosen algorithms are commonly used and effective for classification tasks,
other algorithms could also be considered. The selection of algorithms depends on
various factors such as dataset size, complexity, interpretability, and computational
requirements.
 Some potential alternative algorithms for gender recognition could include Naive Bayes,
Logistic Regression, Gradient Boosting, and Neural Networks. These algorithms have
different strengths and weaknesses and may yield different performance depending on the
specific dataset.
Code:
import numpy as np

import pandas as pd

import matplotlib.pyplot as plt

import seaborn as sns

from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay, classification_report import os


for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename))df = pd.read_csv("C:\\Users\\hp\\Videos\\Free
YouTube Downloader\\voice.csv")
df.head()
df.describe().T df.isnull().sum() df.info() plt.figure(figsize=(16,10),
dpi=200)
sns.heatmap(df.corr(), annot=True, cmap="mako", fmt='.2f', linewidths=0.5);plt.figure(figsize=(10,6))
sns.scatterplot(data=df, x="dfrange", y="meanfreq", hue="label");
#dfrange:
range of dominant frequency measured across acoustic signal #meanfreq: mean frequency (in
kHz)#sp.ent: spectral entropy plt.figure(figsize=(10,6)) sns.histplot(data=df,x="sp.ent", hue="label",
color="orange"); #sfm: spectral flatness plt.figure(figsize=(10,6)) sns.histplot(data=df,x="sfm",
hue="label");
from sklearn.model_selection import train_test_split X = df.drop("label", axis=1)
y = df["label"]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=47) from sklearn.svm
import SVC svc = SVC()
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()scaled_X_train = scaler.fit_transform(X_train)
scaled_X_test = scaler.transform(X_test)svc.get_params().keys() param_grid = {"C":
[0,0.1,1,10,100,1000],
"degree":[2,3,4,5]
, "gamma": ["scale","auto"],
"kernel": ['linear', 'poly', 'rbf']
}from sklearn.model_selection import GridSearchCV
grid = GridSearchCV(svc,param_grid, cv=5, scoring="accuracy")
grid.fit(scaled_X_train, y_train) grid.best_params_ y_preds = grid.predict(scaled_X_test)
print(classification_report(y_test, y_preds)) cm = confusion_matrix(y_test, y_preds, labels=grid.classes_)
cm1 = ConfusionMatrixDisplay(confusion_matrix=cm,display_labels=grid.classes_)
cm1.plot()
plt.show() from sklearn.metrics import accuracy_score acc = round(accuracy_score(y_test, y_preds) *
100, 3) acc from sklearn.neighbors
import KNeighborsClassifier knn = KNeighborsClassifier()
knn.get_params().keys() operations = [("scaler",scaler),
("knn",knn)] from sklearn.pipeline import Pipeline pipe = Pipeline(operations)
k_values = list(range(1,30)) param_grid = {"knn__n_neighbors": k_values} grid = GridSearchCV(pipe,
param_grid, cv=5, scoring="accuracy") grid.fit(X_train, y_train)
grid.best_estimator_.get_params() knn7 = KNeighborsClassifier(n_neighbors=7) operations =
[('scaler',scaler),('knn7',knn7)] pipe = Pipeline(operations)
pipe.fit(X_train,y_train) y_pred = pipe.predict(X_test) print(classification_report(y_test, y_pred)) cm =
confusion_matrix(y_test, y_pred, labels=pipe.classes_)
cm1 = ConfusionMatrixDisplay(confusion_matrix=cm,display_labels=pipe.classes_)
cm1.plot() plt.show() acc2 = round(accuracy_score(y_test, y_pred) * 100, 3) acc2 from sklearn.ensemble
import RandomForestClassifier rfc = RandomForestClassifier()
rfc.fit(X_train, y_train) y_pred1 = rfc.predict(X_test)
print(classification_report(y_test, y_pred1)) cm = confusion_matrix(y_test, y_pred1, labels=rfc.classes_)
cm1 = ConfusionMatrixDisplay(confusion_matrix=cm,display_labels=rfc.classes_)
cm1.plot() plt.show() acc3 = round(accuracy_score(y_test, y_pred1) * 100, 3) acc3 from sklearn.tree
import DecisionTreeClassifier dt = DecisionTreeClassifier() dt.fit(X_train, y_train) y_pred2 =
dt.predict(X_test) cm = confusion_matrix(y_test, y_pred2, labels=dt.classes_)
cm1 = ConfusionMatrixDisplay(confusion_matrix=cm,display_labels=dt.classes_) cm1.plot() plt.show()
acc4 = round(accuracy_score(y_test, y_pred2) * 100,3) acc4 models = pd.DataFrame({ 'Model': ['Support
Vector Machines', 'KNN', 'Random Forest','Decision Tree'],
'Score': [acc, acc2, acc3, acc4]})
import sounddevice as sd
import numpy as np
import os
# Constants for recording duration = 20
# Duration in seconds sample_rate = 44100
# Sample rate (number of samples per second) num_samples = duration * sample_rate # Record audio
print("Recording...")
audio = sd.rec(int(num_samples), samplerate=sample_rate, channels=1) sd.wait() # Wait until recording
is complete # Reshape the audio data audio = np.reshape(audio, -1)
# Perform analysis on the recorded audio #
(Here, we assume a simple analysis to determine if the audio is male or not)
# Replace this section with your own analysis based on the specific features of male voice
# Dummy analysis:
Check the average amplitude of the audio average_amplitude = np.mean(np.abs(audio)) is_male =
average_amplitude > 0.1
# Adjust the threshold as needed # Lock PC if voice is determined to be male if is_male: print("Voice
detected as male. Locking PC...")
# Add your system-specific code here to lock the PC # Example for Windows: os.system("rundll32.exe
user32.dll,LockWorkStation")
# Example for macOS:
# os.system("/System/Library/CoreServices/Menu\ Extras/User.menu/Contents/Resources/CGSession -
suspend")
# Print the result if is_male:
print("The voice is male.") else:
print("The voice is not male.")

Conclusion:
In conclusion, this code demonstrates gender recognition from voice analysis using machine
learning algorithms. SVM, KNN, Random Forest, and Decision Tree classifiers are utilized for
classification tasks. The choice of algorithms is based on their suitability for gender recognition
and their performance in similar studies. The code provides a starting point for further research
and exploration in the field of gender recognition from voice analysis.

References:

1. Support Vector Machines: https://scikit-learn.org/stable/modules/svm.html


2. k-Nearest Neighbors: https://scikit-learn.org/stable/modules/neighbors.html
3. Random Forest:
https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.RandomForestClassi
fier.html
4. Decision Tree: https://scikit-learn.org/stable/modules/tree.html

You might also like