Ubuntu下部署阿里的FunASR说话人语音识别项目
项目地址:
https://github.com/modelscope/FunASR
------------------------------------------
1、创建虚拟环境并激活:
conda create -n funasr python=3.10 -y && conda activate funasr
2、安装pytorch(需要科学上网/使用配置了alvin9999发布的vless节点旁路由)
pip install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu121
3、安装funasr
pip install -U funasr -i https://pypi.tuna.tsinghua.edu.cn/simple
4、安装modelscope和huggingface_hub
pip install -U modelscope huggingface_hub -i https://pypi.tuna.tsinghua.edu.cn/simple
第一次运行app.py前,要运行:
export HF_ENDPOINT=https://hf-mirror.com
5、创建 app.py 文件
import os
import json
import datetime
from funasr import AutoModel
def format_time(ms: float) -> str:
"""将毫秒数转成 分秒 格式"""
if ms is None:
return ""
seconds = int(ms // 1000) # 毫秒转秒
m, s = divmod(seconds, 60)
return f"{m}分{s}秒"
model = AutoModel(
model="paraformer-zh",
vad_model="fsmn-vad",
punc_model="ct-punc",
spk_model="cam++"
)
# 输入文件路径
audio_file = os.path.join("input", "duihua.mp3")
res = model.generate(
input=audio_file,
batch_size_s=300,
hotword='魔搭'
)
# 处理结果,只保留 spk + 时间戳(分秒) + text
output_data = []
for r in res:
for sentence in r.get("sentence_info", []):
start_time = format_time(sentence.get("start"))
end_time = format_time(sentence.get("end"))
output_data.append({
"spk": sentence.get("spk"),
"start": start_time,
"end": end_time,
"text": sentence.get("text")
})
# 按开始时间顺序排序
output_data.sort(key=lambda x: x["start"])
# 输出文件路径:output/output_年月日时分秒.json
output_dir = "output"
os.makedirs(output_dir, exist_ok=True)
timestamp = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
output_file = os.path.join(output_dir, f"output_{timestamp}.json")
with open(output_file, "w", encoding="utf-8") as f:
json.dump(output_data, f, ensure_ascii=False, indent=2)
print(f"结果已保存到 {output_file},共 {len(output_data)} 条记录")
6、运行程序
python app.py
评论
发表评论