本文讲述本人使用python将文件中的语音转成文本时遇到的一些问题,希望可以帮助到一些人
1
根据github[https://github.com/openai/whisper]在安装whisper中我遇到了一些问题:我通过conda设置了一个名为whisper的虚拟环境,安装了Python 3.9.9 以及 PyTorch 1.10.1,然后直接使用pip install -U openai-whisper直接安装whisper。问题在于numpy的版本太高,导致报错,将numpy的版本由2.0.2下调至1.23.5,可以运行。
2
使用官方提供的代码生成文字时会报错,以下是我使用的代码:
import whisper
model = whisper.load_model("turbo")
# load audio and pad/trim it to fit 30 seconds
audio = whisper.load_audio("audio.mp3")
audio = whisper.pad_or_trim(audio)
# make log-Mel spectrogram and move to the same device as the model
mel = whisper.log_mel_spectrogram(audio).to(model.device)
# detect the spoken language
_, probs = model.detect_language(mel)
print(f"Detected language: {max(probs, key=probs.get)}")
# decode the audio
options = whisper.DecodingOptions()
result = whisper.decode(model, mel, options)
# print the recognized text
print(result.text)
报错信息为:
RuntimeError: Given groups=1, weight of size [1280, 128, 3], expected input[1, 80, 3000] to have 128 channels, but got 80 channels instead
将代码中的
mel = whisper.log_mel_spectrogram(audio).to(model.device)
改为
mel = whisper.log_mel_spectrogram(audio=audio, n_mels=128).to(model.device)
解决方案出自:https://github.com/openai/whisper/discussions/1778#discussioncomment-7520324
3
有时生成的文字不是中文,自动识别的语言类型出错了,所以想指定中文生成。
通过命令行调用时好像可以直接使用--language Chinese来约束
通过上述python代码的形式,也可以指定中文:
options = whisper.DecodingOptions()
改为
options = whisper.DecodingOptions(language='Chinese')
如此,在生成时就可以默认生成中文了。
2024/10/10