Q/Complete the following code in Python language (biometrics) for voice recognition and apply the code, mentioning the approved source if it exists import os import numpy as np from pyAudioAnalysis import audioBasicIO, audioFeatureExtraction, audioTrainTest from pydub import AudioSegment # Function to capture and save voice samples def capture_voice_samples(num_samples, speaker_name): os.makedirs("speakers", exist_ok=True) os.makedirs(f"speakers/{speaker_name}", exist_ok=True) for i in range(num_samples): input(f"Press Enter and start speaking for sample {i + 1}...") # Recording audio using pyAudioAnalysis audio = audioBasicIO.record_audio(4, 44100) filepath = f"speakers/{speaker_name}/sample_{i + 1}.wav" audioBasicIO.write_audio_file(filepath, audio, 44100) print(f"Sample {i + 1} saved for {speaker_name}") # Function to extract features from voice samples def extract_features(): speakers = [d for d in os.listdir("speakers") if os.path.isdir(os.path.join("speakers", d))] all_features = [] all_labels = [] for i, speaker in enumerate(speakers): features = [] labels = [] for filename in os.listdir(f"speakers/{speaker}"): if filename.endswith(".wav"): filepath = os.path.join(f"speakers/{speaker}", filename) print(f"Extracting features from {filepath}") [Fs, x] = audioBasicIO.read_audio_file(filepath) F, f_names = audioFeatureExtraction.stFeatureExtraction(x[:, 0], Fs, 0.050 * Fs, 0.025 * Fs) features.append(F.T) labels.append(i) all_features.extend(features) all_labels.extend(labels) return np.array(all_features), np.array(all_labels) # Function to perform speaker identification def identify_speaker(): features, labels = extract_features() model = audioTrainTest.gmm_train(features, labels) while True: filepath = input("Enter the path of the voice sample to identify (or 'exit' to quit): ") if filepath.lower() == "exit": break [Fs, x] = audioBasicIO.read_audio_file(filepath) F, _ = audioFeatureExtraction.stFeatureExtraction(x[:, 0], Fs, 0.050 * Fs, 0.025 * Fs) winner, _, _ = audioTrainTest.gmm_classify(model, F.T) identified_speaker = os.listdir("speakers")[winner] print(f"The identified speaker is: {identified_speaker}") # Main function def main(): num_samples = int(input("Enter the number of voice samples to capture per speaker: ")) num_speakers = int(input("Enter the number of speakers: ")) for i in range(num_speakers): speaker_name = input(f"Enter the name of speaker {i + 1}: ") capture_voice_samples(num_samples, speaker_name) # Identify speaker from a given voice sample identify_speaker() if __name__ == "__main__": main()

Systems Architecture
7th Edition
ISBN:9781305080195
Author:Stephen D. Burd
Publisher:Stephen D. Burd
Chapter10: Application Development
Section: Chapter Questions
Problem 15VE
icon
Related questions
Topic Video
Question

Q/Complete the following code in Python language (biometrics) for voice recognition and apply the code, mentioning the approved source if it exists

import os
import numpy as np
from pyAudioAnalysis import audioBasicIO, audioFeatureExtraction, audioTrainTest
from pydub import AudioSegment


# Function to capture and save voice samples
def capture_voice_samples(num_samples, speaker_name):
os.makedirs("speakers", exist_ok=True)
os.makedirs(f"speakers/{speaker_name}", exist_ok=True)

for i in range(num_samples):
input(f"Press Enter and start speaking for sample {i + 1}...")

# Recording audio using pyAudioAnalysis
audio = audioBasicIO.record_audio(4, 44100)
filepath = f"speakers/{speaker_name}/sample_{i + 1}.wav"
audioBasicIO.write_audio_file(filepath, audio, 44100)

print(f"Sample {i + 1} saved for {speaker_name}")


# Function to extract features from voice samples
def extract_features():
speakers = [d for d in os.listdir("speakers") if os.path.isdir(os.path.join("speakers", d))]
all_features = []
all_labels = []

for i, speaker in enumerate(speakers):
features = []
labels = []

for filename in os.listdir(f"speakers/{speaker}"):
if filename.endswith(".wav"):
filepath = os.path.join(f"speakers/{speaker}", filename)
print(f"Extracting features from {filepath}")
[Fs, x] = audioBasicIO.read_audio_file(filepath)
F, f_names = audioFeatureExtraction.stFeatureExtraction(x[:, 0], Fs, 0.050 * Fs, 0.025 * Fs)
features.append(F.T)
labels.append(i)

all_features.extend(features)
all_labels.extend(labels)

return np.array(all_features), np.array(all_labels)


# Function to perform speaker identification
def identify_speaker():
features, labels = extract_features()
model = audioTrainTest.gmm_train(features, labels)

while True:
filepath = input("Enter the path of the voice sample to identify (or 'exit' to quit): ")
if filepath.lower() == "exit":
break

[Fs, x] = audioBasicIO.read_audio_file(filepath)
F, _ = audioFeatureExtraction.stFeatureExtraction(x[:, 0], Fs, 0.050 * Fs, 0.025 * Fs)
winner, _, _ = audioTrainTest.gmm_classify(model, F.T)
identified_speaker = os.listdir("speakers")[winner]
print(f"The identified speaker is: {identified_speaker}")


# Main function
def main():
num_samples = int(input("Enter the number of voice samples to capture per speaker: "))
num_speakers = int(input("Enter the number of speakers: "))

for i in range(num_speakers):
speaker_name = input(f"Enter the name of speaker {i + 1}: ")
capture_voice_samples(num_samples, speaker_name)

# Identify speaker from a given voice sample
identify_speaker()


if __name__ == "__main__":
main()
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Knowledge Booster
Instruction Format
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Systems Architecture
Systems Architecture
Computer Science
ISBN:
9781305080195
Author:
Stephen D. Burd
Publisher:
Cengage Learning