优化部分报红代码

This commit is contained in:
yueliuli 2026-04-29 09:45:51 +08:00
parent 651e949cfa
commit a4311f824d
2 changed files with 12 additions and 4 deletions

View File

@ -184,6 +184,10 @@ class ASRService:
print(f"正在识别: {audio_path}")
# 执行识别
# 确保模型已正确加载
if self._model is None:
raise RuntimeError("模型加载失败,无法执行识别")
result = self._model.generate(
input=str(audio_path),
batch_size_s=batch_size_s,
@ -330,7 +334,11 @@ def recognize_audio(
List[Sentence]: 识别结果
"""
service = ASRService(model_name=model_name, device=device)
return service.recognize(audio_path)
result = service.recognize(audio_path)
# 如果返回的是字典return_raw=True的情况则解析为Sentence列表
if isinstance(result, dict):
return service._parse_result([result])
return result
if __name__ == "__main__":

View File

@ -59,11 +59,11 @@ def test_single_audio(audio_path: str, model_name: str = "paraformer-zh"):
# 导出 JSON
json_path = f"{base_name}_result.json"
service.export_to_json(sentences, json_path)
service.export_to_json(sentences, json_path) # type: ignore
# 导出 SRT 字幕
srt_path = f"{base_name}_result.srt"
service.export_to_srt(sentences, srt_path)
service.export_to_srt(sentences, srt_path) # type: ignore
print("\n" + "=" * 70)
print("📁 输出文件:")
@ -103,7 +103,7 @@ def test_batch(audio_dir: str, model_name: str = "paraformer-zh"):
# 导出
base_name = audio_path.stem
service.export_to_json(sentences, f"{base_name}_result.json")
service.export_to_json(sentences, f"{base_name}_result.json") # type: ignore
except Exception as e:
print(f" ✗ 失败: {e}")