Complete the following code in Python (Biometrics for Voice Recognition) 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) for i in range(num_samples): input("Press Enter and start speaking...") os.makedirs(f"speakers/{speaker_name}", exist_ok=True) audio = audioBasicIO.read_audio_file(f"speakers/{speaker_name}/sample_{i + 1}.wav") audio = AudioSegment.from_wav(f"speakers/{speaker_name}/sample_{i + 1}.wav") audio.export(f"speakers/{speaker_name}/sample_{i + 1}.wav", format="wav") 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()
Complete the following code in Python (Biometrics for Voice Recognition) 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) for i in range(num_samples): input("Press Enter and start speaking...") os.makedirs(f"speakers/{speaker_name}", exist_ok=True) audio = audioBasicIO.read_audio_file(f"speakers/{speaker_name}/sample_{i + 1}.wav") audio = AudioSegment.from_wav(f"speakers/{speaker_name}/sample_{i + 1}.wav") audio.export(f"speakers/{speaker_name}/sample_{i + 1}.wav", format="wav") 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()
Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
Related questions
Question
Q/Complete the following code in Python (Biometrics for Voice Recognition)
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)
for i in range(num_samples):
input("Press Enter and start speaking...")
os.makedirs(f"speakers/{speaker_name}", exist_ok=True)
audio = audioBasicIO.read_audio_file(f"speakers/{speaker_name}/sample_{i + 1}.wav")
audio = AudioSegment.from_wav(f"speakers/{speaker_name}/sample_{i + 1}.wav")
audio.export(f"speakers/{speaker_name}/sample_{i + 1}.wav", format="wav")
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()
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)
for i in range(num_samples):
input("Press Enter and start speaking...")
os.makedirs(f"speakers/{speaker_name}", exist_ok=True)
audio = audioBasicIO.read_audio_file(f"speakers/{speaker_name}/sample_{i + 1}.wav")
audio = AudioSegment.from_wav(f"speakers/{speaker_name}/sample_{i + 1}.wav")
audio.export(f"speakers/{speaker_name}/sample_{i + 1}.wav", format="wav")
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
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
Step by step
Solved in 3 steps
Knowledge Booster
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.Recommended textbooks for you
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education