【发布时间】:2022-02-01 15:55:45
【问题描述】:
我正在做语音情感识别方面的大学项目,我正在尝试运行这 3 个代码块,但我收到一长串错误,请帮助我
这里是 google colab 的链接 https://colab.research.google.com/drive/1yHyLBbwVhsqiF8bUvd7_7YFDvOhaqzgz#scrollTo=UVbUUU2ueuYC
def extract_feature(file_name, mfcc, chroma, mel):
with soundfile.SoundFile(file_name) as sound_file:
X = sound_file.read(dtype="float32")
sample_rate=sound_file.samplerate
if chroma:
stft=np.abs(librosa.stft(X))
result=np.array([])
if mfcc:
mfccs=np.mean(librosa.feature.mfcc(y=X, sr=sample_rate, n_mfcc=40).T, axis=0)
result=np.hstack((result, mfccs))
if chroma:
chroma=np.mean(librosa.feature.chroma_stft(S=stft, sr=sample_rate).T,axis=0)
result=np.hstack((result, chroma))
if mel:
mel=np.mean(librosa.feature.melspectrogram(X, sr=sample_rate).T,axis=0)
result=np.hstack((result, mel))
return result
emotions={
'01':'neutral',
'02':'calm',
'03':'happy',
'04':'sad',
'05':'angry',
'06':'fearful',
'07':'disgust',
'08':'surprised'
}
observed_emotions=['calm', 'happy', 'fearful', 'disgust']
def load_data(test_size=0.2):
x,y=[],[]
from google.colab import drive
drive.mount('/gdrive', force_remount=True)
for file in glob.glob(f"/gdrive/MyDrive/datatset/Actor_*/*.wav"):
file_name=os.path.basename(file)
emotion=emotions[file_name.split("-")[2]]
if emotion not in observed_emotions:
continue
feature=extract_feature(file, mfcc=True, chroma=True, mel=True)
x.append(feature)
y.append(emotion)
return train_test_split(np.array(x), y, test_size=test_size, random_state=9)
x_train,x_test,y_train,y_test=load_data(test_size=0.25)
以下是我收到的错误
Mounted at /gdrive
---------------------------------------------------------------------------
ParameterError Traceback (most recent call last)
<ipython-input-78-03a67f23ae57> in <module>()
----> 1 x_train,x_test,y_train,y_test=load_data(test_size=0.25)
3 frames
<ipython-input-77-e0a93a2717f9> in load_data(test_size)
8 if emotion not in observed_emotions:
9 continue
---> 10 feature=extract_feature(file, mfcc=True, chroma=True, mel=True)
11 x.append(feature)
12 y.append(emotion)
<ipython-input-66-c2424d41c241> in extract_feature(file_name, mfcc, chroma, mel)
4 sample_rate=sound_file.samplerate
5 if chroma:
----> 6 stft=np.abs(librosa.stft(X))
7 result=np.array([])
8 if mfcc:
/usr/local/lib/python3.7/dist-packages/librosa/core/spectrum.py in stft(y, n_fft, hop_length, win_length, window, center, dtype, pad_mode)
215
216 # Check audio is valid
--> 217 util.valid_audio(y)
218
219 # Pad the time series so that frames are centered
/usr/local/lib/python3.7/dist-packages/librosa/util/utils.py in valid_audio(y, mono)
293 raise ParameterError(
294 "Invalid shape for monophonic audio: "
--> 295 "ndim={:d}, shape={}".format(y.ndim, y.shape)
296 )
297
ParameterError: Invalid shape for monophonic audio: ndim=2, shape=(172972, 2)
【问题讨论】:
标签: python google-colaboratory speech-recognition