优化部分报红代码
This commit is contained in:
parent
651e949cfa
commit
a4311f824d
|
|
@ -184,6 +184,10 @@ class ASRService:
|
||||||
print(f"正在识别: {audio_path}")
|
print(f"正在识别: {audio_path}")
|
||||||
|
|
||||||
# 执行识别
|
# 执行识别
|
||||||
|
# 确保模型已正确加载
|
||||||
|
if self._model is None:
|
||||||
|
raise RuntimeError("模型加载失败,无法执行识别")
|
||||||
|
|
||||||
result = self._model.generate(
|
result = self._model.generate(
|
||||||
input=str(audio_path),
|
input=str(audio_path),
|
||||||
batch_size_s=batch_size_s,
|
batch_size_s=batch_size_s,
|
||||||
|
|
@ -330,7 +334,11 @@ def recognize_audio(
|
||||||
List[Sentence]: 识别结果
|
List[Sentence]: 识别结果
|
||||||
"""
|
"""
|
||||||
service = ASRService(model_name=model_name, device=device)
|
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__":
|
if __name__ == "__main__":
|
||||||
|
|
|
||||||
|
|
@ -59,11 +59,11 @@ def test_single_audio(audio_path: str, model_name: str = "paraformer-zh"):
|
||||||
|
|
||||||
# 导出 JSON
|
# 导出 JSON
|
||||||
json_path = f"{base_name}_result.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 字幕
|
||||||
srt_path = f"{base_name}_result.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("\n" + "=" * 70)
|
||||||
print("📁 输出文件:")
|
print("📁 输出文件:")
|
||||||
|
|
@ -103,7 +103,7 @@ def test_batch(audio_dir: str, model_name: str = "paraformer-zh"):
|
||||||
|
|
||||||
# 导出
|
# 导出
|
||||||
base_name = audio_path.stem
|
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:
|
except Exception as e:
|
||||||
print(f" ✗ 失败: {e}")
|
print(f" ✗ 失败: {e}")
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue