一 语音识别代码
import pyaudio
import wave
from datetime import datetime
import json
from vosk import Model, KaldiRecognizer
from funasr import AutoModel
from audio_tools.lfasr_new_python_demo.Ifasr_new import xunfei_api
def record_audio(record_seconds):
# 设置录音参数
FORMAT = pyaudio.paInt16 # 采样格式
CHANNELS = 1 # 单声道
RATE = 44100 # 采样率
CHUNK = 1024 # 数据块大小
# 生成文件名,格式为 年月日_时分.wav
current_time = datetime.now().strftime("%Y%m%d_%H%M")
output_filename = f"{current_time}.wav"
# 初始化PyAudio
p = pyaudio.PyAudio()
# 打开录音流
stream = p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK)
print("开始录音...")
frames = []
# 录音
for i in range(0, int(RATE / CHUNK * record_seconds)):
data = stream.read(CHUNK)
frames.append(data)
print("录音结束。")
# 停止录音流
stream.stop_stream()
stream.close()
p.terminate()
# 保存录音为WAV文件
with wave.open(output_filename, 'wb') as wf:
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
print(f"录音已保存为 {output_filename}")
return output_filename
def audio_transform_str(use_model, output_filename, model_name, RATE=44100):
start_time = datetime.now()
recognizer = KaldiRecognizer(use_model, RATE)
# 读取音频文件
with wave.open(output_filename, "rb") as wf:
audio_data = wf.readframes(wf.getnframes())
# 识别音频数据
text = ""
if recognizer.AcceptWaveform(audio_data):
result = recognizer.Result()
text = json.loads(result).get("text", "")
final_result = recognizer.FinalResult()
text += json.loads(final_result).get("text", "")
print(f"\n\n使用vosk {model_name} 识别时长:{(datetime.now() - start_time).total_seconds()}秒,识别结果: \n{text}\n\n")
return text
def use_ali_model(ali_model, output_filename, model_name):
start_time = datetime.now()
rec = ali_model.generate(input=output_filename)
text = rec[0]['text']
print(f"\n\n使用{model_name} 识别时长:{(datetime.now() - start_time).total_seconds()}秒, 识别结果: \n{text}\n\n")
return text
def use_all_model_process():
"""执行所有模型"""
print(F'\n\n----------------> 开始执行所有模型 语音识别, 录音文件:{radio_dir}')
audio_transform_str(small_model, radio_dir, 'small_model_50M')
audio_transform_str(large_model, radio_dir, 'large_model_2G')
# 阿里模型
use_ali_model(ali_speech_paraformer, radio_dir, model_name='ali_speech_paraformer')
# 阿里模型
use_ali_model(ali_model_large_v3, radio_dir, model_name='ali_model_large_v3')
# 讯飞模型
xunfei_api(radio_dir)
pass
if __name__ == '__main__':
# 调用函数,录制10秒的音频并进行识别, 录音文件目录
radio_dir = '20250324_1106.wav' # test
# radio_dir = record_audio(20) # 录音
# 开始识别
start_time1 = datetime.now()
# 进行音频识别
small_model = Model("models/vosk-model-small-cn-0.22") # 加载语音识别模型
print(f"\n\n加载 small_model 时长, 识别时长:{(datetime.now() - start_time1).total_seconds()}秒")
start_time1 = datetime.now()
large_model = Model("models/vosk-model-cn-0.22") # 加载语音识别模型
print(f"\n\n加载 large_model 时长:{(datetime.now() - start_time1).total_seconds()}秒")
# https://www.xfyun.cn/services/voicedictation#anchor742544 # 讯飞
# 阿里模型
start_time1 = datetime.now()
ali_speech_paraformer = AutoModel(model="models/speech_paraformer_large_asr_mtl-16k-common-vocab11666-pytorch",
model_revision="master",
disable_update=True)
print(f"\n\n加载 ali_model 时长:{(datetime.now() - start_time1).total_seconds()}秒")
# 阿里模型二
start_time1 = datetime.now()
ali_model_large_v3 = AutoModel(model="models/Whisper-large-v3-turbo",
model_revision="master",
disable_update=True)
print(f"\n\n加载 ali_model_large_v3 时长:{(datetime.now() - start_time1).total_seconds()}秒")
use_all_model_process()
print(f'\n\n\n\n\n--------------> 暂定选择模型:{"ali_speech_paraformer"}')
加载 large_model 时长:12.360683秒
funasr version: 1.2.6.
加载 ali_model 时长:1.427374秒
funasr version: 1.2.6.
Detect model requirements, begin to install it: models/Whisper-large-v3-turbo\requirements.txt
install model requirements successfully
加载 ali_model_large_v3 时长:6.196327秒
----------------> 开始执行所有模型 语音识别, 录音文件:20250324_1106.wav
使用vosk small_model_50M 识别时长:2.778595秒,识别结果:
给 二号 精髓 更 深层 一个 维修工 单 内容 是 设备 次序 能耗 高 可能 与 精髓 不 稳定 有关 发送 给 王宁
使用vosk large_model_2G 识别时长:2.992114秒,识别结果:
给 二 号 金水 帮 生成 一个 维修 工单 内容 是 设备 次序 能耗 高 可能 与 进水 不 稳定 有关 发送 给 王宁
rtf_avg: 0.032: 100%|██████████| 1/1 [00:00<00:00, 1.58it/s]
使用ali_speech_paraformer 识别时长:0.637585秒, 识别结果:
给二号进水泵生成一个维修工单内容是设备持续能耗高可能与进水不稳定有关发送给王宁
rtf_avg: 2.375: 100%|██████████| 1/1 [00:47<00:00, 47.47s/it]
使用ali_model_large_v3 识别时长:47.470227秒, 识别结果:
给二号进水泵生成一个维修工单内容是设备持续人耗高可能与进水不稳定有关发送给王林
--->使用讯飞api, 识别时长:11.045245秒, 识别结果:
嗯啊给二号进水泵生成一个维修工单 内容是设备持续能耗高 可能与进水不稳定有关 发送给王林--------------> 暂定选择模型:ali_speech_paraformer
3万+

被折叠的 条评论
为什么被折叠?



