
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
expand_more
expand_more
format_list_bulleted
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 stepSolved 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.Similar questions
- # python demo script to illustrate dictionary usage to replace words in a sentenceInput is word pairs exactly with same whitespaces as here: Hi Hello Thanks Thank-you Bye Goodbye (one space between pairs, two spaces between every pairs)In a user's input sentence example as follows: Hi there, Thanks, Bye using the above given pairs as a dictionary to replace corresponding words in the input sentence, and the output becomes:Hello there, Thank-you, Goodbye Read input from user, first word pairs, then the sentence, split, loop over elements to replace what's entered from pair words into entered sentence. Comments are very important so I can understand. Thanksarrow_forwardIn python don't import librariesarrow_forwardFilename: runlength_decoder.py Starter code for data: hw4data.py Download hw4data.py Download hw4data.pyYou must provide a function named decode() that takes a list of RLE-encoded values as its single parameter, and returns a list containing the decoded values with the “runs” expanded. If we were to use Python annotations, the function signature would look similar to this:def decode(data : list) -> list: Run-length encoding (RLE) is a simple, “lossless” compression scheme in which “runs” of data. The same value in consecutive data elements are stored as a single occurrence of the data value, and a count of occurrences for the run. For example, using Python lists, the initial list: [‘P’,‘P’,‘P’,‘P’,‘P’,‘Y’,‘Y’,‘Y’,‘P’,‘G’,‘G’] would be encoded as: ['P', 5, 'Y', 3, ‘P’, 1, ‘G’, 2] So, instead of using 11 “units” of space (if we consider the space for a character/int 1 unit), we only use 8 “units”. In this small example, we don’t achieve much of a savings (and indeed the…arrow_forward
- Computer sciencearrow_forwardNeed some help on this Python Quesionarrow_forwardThe mapped list pattern Our second pattern is the mapped list pattern, described in video 4 3 mapped list pattern. Often we need to write a function that takes a list as a parameter and returns a new list in which each item in the original list is "mapped" to a new item in the result list. For example, the following function takes a list of numbers as a parameter and returns a list of all the numbers squared, e.g. squares ( [1, 3, 7]) returns [1, 9, 49]. def squares (nums): "Returns the squares of the given numbers""" result = [] for num in nums: result.append (num * num) return result Although this is just a special case of the accumulator pattern, it is so common that we give it its own name: the mapped list pattern. Consider the following function: def squares(nums): ""Returns the squares of the given numbers""" result = [] for num in nums: result.append (num * num) return result If the main program calls print(squares ( [5, -3, 2, 7]) what is the state table for the function…arrow_forward
- In Python IDLE: How would I write a function for the problem in the attached image?arrow_forwardUpload answer sheets Compare the effectiveness of input statements putchar(), fgets() and gets() when working with character arrays. Discuss the concepts with an examplearrow_forwardMATLAB. write code for all partsarrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education

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