1.conda创建环境
conda create -n whisper python==3.9 conda activate whisper
安装pytorch
pip install torch==1.8.1+cu101 torchvision==0.9.1+cu101 torchaudio==0.8.1 -f https://download.pytorch.org/whl/torch_stable.html
安装whisper
pip install -U openai-whisper
2.准备模型和测试音频
因为我的电脑是GTX 1060 显存只有6G
最大运行的模型是medium
所以去官网下载medium.pt模型
连接在这里:
https://github.com/openai/whisper/blob/main/whisper/init.py
然后自己录制一段mp3格式的音频放在如下文件夹中
G:\desktop\whisper
3.语音转文字
3.1方法一:
打开conda窗口,进入该文件夹,
用command窗口执行的命令:
(whisper) G:\desktop\whisper>whisper test1.mp3 --model medium.pt Detecting language using up to the first 30 seconds. Use `--language` to specify the language Detected language: Chinese [00:00.000 --> 00:06.700] 我现在正在测试OpenAI Whisper的翻译效 果 [00:06.700 --> 00:08.000] 这里是中文 [00:08.000 --> 00:09.300] 现在是英文 [00:09.300 --> 00:10.200] English [00:10.200 --> 00:11.300] American [00:11.300 --> 00:12.000] China [00:12.000 --> 00:12.900] People [00:14.400 --> 00:16.900] 现在录制时长是15分钟 [00:16.900 --> 00:21.500] Now this is 18 second [00:22.000 --> 00:26.300] And we will wait for 1 minute [00:26.600 --> 00:28.800] 我们要一直等到1分钟 [00:28.800 --> 00:30.200] 这个视频结束 [00:30.200 --> 00:34.800] 来测试一下长视频的长音频的翻译效果 [00:39.100 --> 00:50.700] 37 38 40 41 42 43 44 45 46 47 48 49 50 [00:50.700 --> 00:59.700] 5 4 3 2 1 [00:59.700 --> 01:01.200] 现在是1分钟 [01:01.200 --> 01:03.200] This is 1 minute
3.2 方法二:
写一个python文件test.py调用:
test.py内容如下
import whisper model = whisper.load_model("medium.pt") result = model.transcribe("test1.mp3") print(result["text"])
运行方式
(whisper) G:\desktop\whisper>python test.py 我现在正在测试OpenAI Whisper的翻译效果这里是中文现在是英文EnglishAmericanChinaPeople现在录制时长是15分钟Now this is 18 secondsAnd we will wait for 1 minute我们要一直等到1分钟这个视频结束来测试一下长视频的长音频的翻译效果3738404142434445464748495054321现在是1分钟This is 1 minute
3.3 两者的区别
用command输出自动给你按照时间分类了,python代码要自己补充
command输出其他的文件,其中test1.txt是保存的识别的文字
祝大家开心!
whispercodeopenaipythonmediummp3pytorch文件夹长视频python代码createiva语音转文字htmlgitgithuburl