diff --git a/asr_service.py b/asr_service.py index 672953f..2ad5d6d 100644 --- a/asr_service.py +++ b/asr_service.py @@ -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__": diff --git a/test_asr.py b/test_asr.py index b4ff203..65ddef7 100644 --- a/test_asr.py +++ b/test_asr.py @@ -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}")