我有以下代码
import os
from os import *
from multiprocessing import freeze_support
import matplotlib.pyplot as plt
import numpy as np
import librosa
import librosa.display
import pandas as pd
from pandarallel import pandarallel
pandarallel.initialize()
Base_Directory = "UltraSound/"
PATH = os.path.abspath(Base_Directory)
def feature_extractor(file_name):
file_name = os.path.join(PATH, file_name)
audio,sample_rate = librosa.load(file_name, res_type='kaiser_fast')
mfccs_features = librosa.feature.mfcc(y=audio, sr=sample_rate, n_mfcc=40)
mfccs_scaled_features = np.mean(mfccs_features.T, axis=0)
return mfccs_scaled_features
"""
audio_file ='UltraSound/100263-2-0-3.wav'
signal,sample_rate =librosa.load(audio_file)
plt.figure(figsize=(12, 4))
plt.plot(signal)
plt.show()
"""
metadata =pd.read_csv('UltraSound/UrbanSound8K.csv')
if __name__ == '__main__':
freeze_support()
metadata['mfccs_scaled_features'] = metadata['slice_file_name'].parallel_apply(feature_extractor)
print(metadata.head())
尽管操作系统库是导入的,但它显示了以下错误:
NameError: name 'os' is not defined
我不知道原因,我是否应该在if语句中找到所有的进口?或者原因是什么?以下是完整的输出:
INFO: Pandarallel will run on 6 workers.
INFO: Pandarallel will use standard multiprocessing data transfer (pipe) to transfer data between the main process and workers.
WARNING: You are on Windows. If you detect any issue with pandarallel, be sure you checked out the Troubleshooting page:
https://nalepae.github.io/pandarallel/troubleshooting/
INFO: Pandarallel will run on 6 workers.
INFO: Pandarallel will use standard multiprocessing data transfer (pipe) to transfer data between the main process and workers.
WARNING: You are on Windows. If you detect any issue with pandarallel, be sure you checked out the Troubleshooting page:
https://nalepae.github.io/pandarallel/troubleshooting/
INFO: Pandarallel will run on 6 workers.
INFO: Pandarallel will use standard multiprocessing data transfer (pipe) to transfer data between the main process and workers.
WARNING: You are on Windows. If you detect any issue with pandarallel, be sure you checked out the Troubleshooting page:
https://nalepae.github.io/pandarallel/troubleshooting/
INFO: Pandarallel will run on 6 workers.
INFO: Pandarallel will use standard multiprocessing data transfer (pipe) to transfer data between the main process and workers.
WARNING: You are on Windows. If you detect any issue with pandarallel, be sure you checked out the Troubleshooting page:
https://nalepae.github.io/pandarallel/troubleshooting/
INFO: Pandarallel will run on 6 workers.
INFO: Pandarallel will use standard multiprocessing data transfer (pipe) to transfer data between the main process and workers.
WARNING: You are on Windows. If you detect any issue with pandarallel, be sure you checked out the Troubleshooting page:
https://nalepae.github.io/pandarallel/troubleshooting/
INFO: Pandarallel will run on 6 workers.
INFO: Pandarallel will use standard multiprocessing data transfer (pipe) to transfer data between the main process and workers.
WARNING: You are on Windows. If you detect any issue with pandarallel, be sure you checked out the Troubleshooting page:
https://nalepae.github.io/pandarallel/troubleshooting/
INFO: Pandarallel will run on 6 workers.
INFO: Pandarallel will use standard multiprocessing data transfer (pipe) to transfer data between the main process and workers.
WARNING: You are on Windows. If you detect any issue with pandarallel, be sure you checked out the Troubleshooting page:
https://nalepae.github.io/pandarallel/troubleshooting/
INFO: Pandarallel will run on 6 workers.
INFO: Pandarallel will use standard multiprocessing data transfer (pipe) to transfer data between the main process and workers.
WARNING: You are on Windows. If you detect any issue with pandarallel, be sure you checked out the Troubleshooting page:
https://nalepae.github.io/pandarallel/troubleshooting/
multiprocessing.pool.RemoteTraceback:
"""
Traceback (most recent call last):
File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\multiprocessing\pool.py", line 125, in worker
result = (True, func(*args, **kwds))
File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\multiprocessing\pool.py", line 51, in starmapstar
return list(itertools.starmap(args[0], args[1]))
File "C:\Users\User\PycharmProjects\DataScience\venv\lib\site-packages\pandarallel\core.py", line 158, in __call__
results = self.work_function(
File "C:\Users\User\PycharmProjects\DataScience\venv\lib\site-packages\pandarallel\data_types\series.py", line 26, in work
return data.apply(
File "C:\Users\User\PycharmProjects\DataScience\venv\lib\site-packages\pandas\core\series.py", line 4433, in apply
return SeriesApply(self, func, convert_dtype, args, kwargs).apply()
File "C:\Users\User\PycharmProjects\DataScience\venv\lib\site-packages\pandas\core\apply.py", line 1082, in apply
return self.apply_standard()
File "C:\Users\User\PycharmProjects\DataScience\venv\lib\site-packages\pandas\core\apply.py", line 1137, in apply_standard
mapped = lib.map_infer(
File "pandas\_libs\lib.pyx", line 2870, in pandas._libs.lib.map_infer
File "C:\Users\User\PycharmProjects\DataScience\glass_csv.py", line 15, in feature_extractor
audio,sample_rate = librosa.load(file_name, res_type='kaiser_fast')
NameError: name 'os' is not defined
"""
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\User\PycharmProjects\DataScience\glass_csv.py", line 29, in <module>
metadata['mfccs_scaled_features'] = metadata['slice_file_name'].parallel_apply(feature_extractor)
File "C:\Users\User\PycharmProjects\DataScience\venv\lib\site-packages\pandarallel\core.py", line 433, in closure
results = results_promise.get()
File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\multiprocessing\pool.py", line 771, in get
raise self._value
NameError: name 'os' is not defined
Process finished with exit code 1
发布于 2022-05-12 10:27:18
我通过反复试验解决了这个问题(我的意思是从if语句中取出一些库,如下所示)
"""
audio_file ='UltraSound/100263-2-0-3.wav'
signal,sample_rate =librosa.load(audio_file)
plt.figure(figsize=(12, 4))
plt.plot(signal)
plt.show()
"""
import os
from os.path import join, abspath
Base_Directory = "UltraSound/"
# import os
# from os.path import join,abspath
PATH = abspath(Base_Directory)
import numpy as np
import librosa
import librosa.display
import pandas as pd
if __name__ == '__main__':
from multiprocessing import freeze_support
import matplotlib.pyplot as plt
from pandarallel import pandarallel
pandarallel.initialize()
freeze_support()
Base_Directory = "UltraSound/"
#import os
#from os.path import join,abspath
#PATH = abspath(Base_Directory)
def feature_extractor(file_name):
file_name = join(PATH, file_name)
audio, sample_rate = librosa.load(file_name, res_type='kaiser_fast')
mfccs_features = librosa.feature.mfcc(y=audio, sr=sample_rate, n_mfcc=40)
mfccs_scaled_features = np.mean(mfccs_features.T, axis=0)
return mfccs_scaled_features
metadata = pd.read_csv('UltraSound/UrbanSound8K.csv')
metadata['mfccs_scaled_features'] = metadata['slice_file_name'].parallel_apply(feature_extractor)
print(metadata.head())
现在一切都很好:
C:\Users\User\PycharmProjects\DataScience\venv\Scripts\python.exe C:/Users/User/PycharmProjects/DataScience/parallel_processing_audio.py
INFO: Pandarallel will run on 6 workers.
INFO: Pandarallel will use standard multiprocessing data transfer (pipe) to transfer data between the main process and workers.
WARNING: You are on Windows. If you detect any issue with pandarallel, be sure you checked out the Troubleshooting page:
https://nalepae.github.io/pandarallel/troubleshooting/
C:\Users\User\PycharmProjects\DataScience\venv\lib\site-packages\librosa\util\decorators.py:88: UserWarning: n_fft=2048 is too small for input signal of length=1323
return f(*args, **kwargs)
C:\Users\User\PycharmProjects\DataScience\venv\lib\site-packages\librosa\util\decorators.py:88: UserWarning: n_fft=2048 is too small for input signal of length=1103
return f(*args, **kwargs)
C:\Users\User\PycharmProjects\DataScience\venv\lib\site-packages\librosa\util\decorators.py:88: UserWarning: n_fft=2048 is too small for input signal of length=1323
return f(*args, **kwargs)
C:\Users\User\PycharmProjects\DataScience\venv\lib\site-packages\librosa\util\decorators.py:88: UserWarning: n_fft=2048 is too small for input signal of length=1523
return f(*args, **kwargs)
slice_file_name ... mfccs_scaled_features
0 100032-3-0-0.wav ... [-218.18938, 71.38549, -131.49442, -52.25892, ...
1 100263-2-0-117.wav ... [-425.05234, 110.67095, -54.192833, 62.045406,...
2 100263-2-0-121.wav ... [-459.82623, 122.82864, -47.908062, 53.302677,...
3 100263-2-0-126.wav ... [-414.82184, 102.94826, -36.65685, 54.170742, ...
4 100263-2-0-137.wav ... [-447.60776, 115.08627, -53.74607, 61.55309, 1...
[5 rows x 9 columns]
Process finished with exit code 0
它完成得太快了(在google中,一名工人花了50分钟,这里只花了2分钟)
https://stackoverflow.com/questions/72213601
复制相似问题