我已将一个函数分配给多处理pool
:
with Pool(os.cpu_count() - 1) as p:
N = len(fpaths)
p.starmap(resample, zip(fpaths, new_paths, [sr] * N, ['WAV'] * N, [manifest] * N))
它正在更改一些音频文件。一个或两个文件已损坏,采样率为零。这将导致除以零的错误,如下所示:
multiprocessing.pool.RemoteTraceback:
"""
Traceback (most recent call last):
File "/opt/conda/lib/python3.7/multiprocessing/pool.py", line 121, in worker
result = (True, func(*args, **kwds))
File "/opt/conda/lib/python3.7/multiprocessing/pool.py", line 47, in starmapstar
return list(itertools.starmap(args[0], args[1]))
File "/home/jupyter/jn-kaggle/birdsong/who-said-what/wsw/preprocessing.py", line 35, in resample
audio, sr = librosa.load(old_path, sr=sr)
File "/opt/conda/lib/python3.7/site-packages/librosa/core/audio.py", line 172, in load
y = resample(y, sr_native, sr, res_type=res_type)
File "/opt/conda/lib/python3.7/site-packages/librosa/core/audio.py", line 553, in resample
ratio = float(target_sr) / orig_sr
ZeroDivisionError: float division by zero
"""
我想处理此错误,但是,我尝试执行的操作似乎无效:
def resample(old_path, new_path, sr, ext='WAV', manifest=None):
try:
with warnings.catch_warnings():
warnings.simplefilter("ignore", UserWarning)
print(f'Loading {old_path}',)
audio, sr = librosa.load(old_path, sr=sr)
except ZeroDivisionError:
audio, sr = librosa.load(old_path, sr=None)
print(f'Error loading file at {old_path}. file=sys.stderr)
我知道ZeroDivisionError是直接由librosa.load()
函数引起的,该函数从其自身的依赖项中获取了不正确的采样率0。我有一个修复程序,但我需要抓住错误。我该怎么办?
答案 0 :(得分:0)
这不是最终答案,但我不想在注释中压缩很多代码:
将异常设为一般,然后重试。我要确保调用了except块。
尝试以下代码:
def resample(old_path, new_path, sr, ext='WAV', manifest=None):
try:
print("Start Resample: ", old_path)
with warnings.catch_warnings():
warnings.simplefilter("ignore", UserWarning)
print(f'Loading {old_path}',)
audio, sr = librosa.load(old_path, sr=sr) # maybe error here
print("Done Resample: ", old_path)
except Exception as ex: # any exception
# audio, sr = librosa.load(old_path, sr=None) # remove for now
print('Error: ', ex)