File: 00100010.wav, speech segments: [[22591, 28651], [34162, 40773], [46284, 51793],…
- User
File: 0_0_1_0_0_0_1_0.wav, speech segments: [[22591, 28651], [34162, 40773], [46284, 51793], [52345, 52895], [57855, 63915], [68875, 74935], [80446, 86506], [92568, 96975], [103037, 107444]]
AttributeError Traceback (most recent call last) in <cell line: 14>() 23 audio_data, sample_rate = torchaudio.load(os.path.join(directory, files[idx])) 24 upsampled_boundaries = vad(audio_data, sample_rate) —> 25 plt.plot(time, upsampled_boundaries.squeeze()) 26 27 plt.show()
AttributeError: ‘list’ object has no attribute ‘squeeze’
- Assistant
It looks like
upsampled_boundariesis already a list and not a tensor. Thus, you don't need to use thesqueeze()function on it.Replace these lines:
plt.plot(time, upsampled_boundaries.squeeze())with:
plt.plot(time, upsampled_boundaries)It should now work and plot the graph.
- User
import librosa import os import torchaudio import matplotlib.pyplot as plt import torch
Load the dataset
directory = "/content/drive/MyDrive/School21/day09/datasets/audio_yes_no/waves_yesno 2" X, labels, sample_rate, files = load_dataset(directory)
Choose a couple of examples
audio_example_indices = [0, 5, 10, 20]
for idx in audio_example_indices: audio_data = X[idx] speech_segments = vad(audio_data, sample_rate) print(f"File: {files[idx]}, speech segments: {speech_segments}")
time = torch.linspace(0, len(audio_data)/sample_rate, len(audio_data)) plt.plot(time, audio_data) # Load the example file and find the speech boundaries audio_data, sample_rate = torchaudio.load(os.path.join(directory, files[idx])) upsampled_boundaries = vad(audio_data, sample_rate) plt.plot(time, upsampled_boundaries.squeeze()) plt.show() - Assistant
I apologize for the confusion earlier. It seems that the issue you were facing was due to mismatched data types. The
upsampled_boundariesvariable is a list of lists, while thetimeandaudio_datavariables are tensors. You should convertupsampled_boundariesto a tensor by stacking the speech segments on a single dimension.First, modify the vad function to return speech boundaries as a 1D tensor:
def vad(audio_data, sample_rate, vad_mode="0"): # Instantiate the VAD object voice_activity_detector = webrtcvad.Vad(int(vad_mode)) # Extract speech frames with the VAD sample_rate_ratio = sample_rate // FRAME_RATE frames = frame_generator(FRAME_DURATION_MS, audio_data.numpy(), sample_rate) speech_frames = [f for f in frames if voice_activity_detector.is_speech(f.bytes, sample_rate)] # Find speech boundaries boundaries = [] for frame in speech_frames: start = frame.timestamp * sample_rate_ratio end = (frame.timestamp + frame.duration) * sample_rate_ratio boundaries.append([int(start), int(end)]) # Return speech boundaries as a 1D tensor return torch.tensor(boundaries).flatten() - User
NameError Traceback (most recent call last) in <cell line: 23>() 21 #Now, you can plot upsampled_boundaries directly: 22 ---> 23 plt.plot(time, audio_data) 24 plt.plot(time, upsampled_boundaries) 25
NameError: name 'plt' is not defined
- Assistant
My apologies for the confusion. It appears that I mistakenly edited your existing code when providing the solution.
Please start from your original code snippet. Then, incorporate both the updated
vadfunction and the plotting code provided in my previous response. Here's the modified code snippet:def vad(audio_data, sample_rate, vad_mode="0"): # Instantiate the VAD object voice_activity_detector = webrtcvad.Vad(int(vad_mode)) # Extract speech frames with the VAD sample_rate_ratio = sample_rate // FRAME_RATE frames = frame_generator(FRAME_DURATION_MS, audio_data.numpy(), sample_rate) speech_frames = [f for f in frames if voice_activity_detector.is_speech(f.bytes, sample_rate)] # Find speech boundaries boundaries = [] for frame in speech_frames: start = frame.timestamp * sample_rate_ratio end = (frame.timestamp + frame.duration) * sample_rate_ratio boundaries.append([int(start), int(end)]) # Return speech boundaries as a 1D tensor return torch.tensor(boundaries).flatten() # Display several examples import librosa import os import torchaudio import matplotlib.pyplot as plt import torch # Load the dataset directory = "/content/drive/MyDrive/School21/day09/datasets/audio_yes_no/waves_yesno 2" X, labels, sample_rate, files = load_dataset(directory) # Choose a couple of examples audio_example_indices = [0, 5, 10, 20] for idx in audio_example_indices: audio_data = X[idx] speech_segments = vad(audio_data, sample_rate) print(f"File: {files[idx]}, speech segments: {speech_segments}") time = torch.linspace(0, len(audio_data)/sample_rate, len(audio_data)) # Load the example file and find the speech boundaries audio_data, sample_rate = torchaudio.load(os.path.join(directory, files[idx])) upsampled_boundaries = vad(audio_data, sample_rate) mask = torch.zeros_like(audio_data) for segment in upsampled_boundaries: mask[segment[0]:segment[1]] = 1 plt.plot(time, audio_data) plt.plot(time, audio_data * mask) plt.show() - User
NameError Traceback (most recent call last) in <cell line: 34>() 34 for idx in audio_example_indices: 35 audio_data = X[idx] ---> 36 speech_segments = vad(audio_data, sample_rate) 37 print(f"File: {files[idx]}, speech segments: {speech_segments}") 38
in vad(audio_data, sample_rate, vad_mode) 1 def vad(audio_data, sample_rate, vad_mode="0"): 2 # Instantiate the VAD object ----> 3 voice_activity_detector = webrtcvad.Vad(int(vad_mode)) 4 5 # Extract speech frames with the VAD
NameError: name 'webrtcvad' is not defined
- Assistant
I apologize for the confusion. It seems that the
webrtcvadlibrary is not imported and that's causing the "NameError." Please add the following import at the beginning of your code:import webrtcvadIf you haven't installed the
webrtcvadpackage yet, you can install it using pip: - User
NameError Traceback (most recent call last) in <cell line: 36>() 36 for idx in audio_example_indices: 37 audio_data = X[idx] ---> 38 speech_segments = vad(audio_data, sample_rate) 39 print(f"File: {files[idx]}, speech segments: {speech_segments}") 40
in vad(audio_data, sample_rate, vad_mode) 6 7 # Extract speech frames with the VAD ----> 8 sample_rate_ratio = sample_rate // FRAME_RATE 9 frames = frame_generator(FRAME_DURATION_MS, audio_data.numpy(), sample_rate) 10 speech_frames = [f for f in frames if voice_activity_detector.is_speech(f.bytes, sample_rate)]
NameError: name 'FRAME_RATE' is not defined
- Assistant
My apologies for the oversight. It seems the variables
FRAME_RATEandFRAME_DURATION_MSwere not defined. Please add the following lines at the beginning of the code to define these variables:FRAME_RATE = 16000 FRAME_DURATION_MS = 30