SpeechRecognition/CLEANUP_SUMMARY.md

108 lines
2.8 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 代码清理总结
## 清理目标
`main.py` 的流程为主,删除其他文件中的未使用代码。
## 当前流程main.py
```
阶段 1: 说话人分离 (3D-Speaker)
清理 CUDA 缓存
阶段 2: ASR 识别 + 合并结果
```
## 清理内容
### 1. asr_service.py
**删除的功能**:
-`use_3d_speaker` 参数及相关逻辑(已在 main.py 中手动处理)
-`_merge_diarization_segments()` 方法(未使用)
-`_map_asr_to_speaker()` 方法(已在 main.py 中内联实现)
**保留的功能**:
-`recognize()` - 基础 ASR 识别
-`_parse_result()` - 解析识别结果
-`export_to_json()` / `export_to_srt()` - 导出功能
**修改说明**:
- ASR 识别结果中的默认说话人统一设为 `speaker_0`
- 不再在 ASR 服务内部调用 3D-Speaker
### 2. map_speaker.py
**删除的功能**:
-`find_speaker()` 函数(已在 main.py 中内联实现)
-`main()` 函数(过时的示例代码)
**保留的功能**:
-`load_json()` - 加载 JSON 文件
-`save_json()` - 保存 JSON 文件
### 3. example_usage.py
**修改内容**:
- 更新输出示例中的说话人格式:`SPEAKER_00` → `speaker_0`
- 保持示例代码的参考价值
### 4. 删除的文件
-`test_staged.py` - 临时测试脚本
-`test_model_load.py` - 临时测试脚本
## 核心逻辑main.py
### 阶段 1: 说话人分离
```python
diar_service = DiarizationService()
diar_service._load_model()
for video in videos:
wav = extract_wav(video)
segments = diar_service.diarize(wav)
save_json(temp_file, {"segments": segments})
```
### 阶段 2: ASR 识别 + 合并
```python
asr_service = ASRService()
asr_service._load_model()
for video in videos:
asr_sentences = asr_service.recognize(wav)
# 合并说话人(只使用 3D-Speaker 结果)
for sentence in asr_sentences:
matched_speaker = 查找最大重叠的说话人
if matched_speaker:
sentence.speaker = matched_speaker
else:
sentence.speaker = "speaker_0"
export_to_json(output_file, asr_sentences)
```
## 优势
1. **逻辑清晰**: 只在一个地方main.py处理说话人合并
2. **避免重复**: 删除了多处重复的说话人对齐逻辑
3. **易于维护**: 核心流程集中在 main.py服务类只负责基础功能
4. **统一格式**: 所有说话人标签统一为 `speaker_0`, `speaker_1`, ...
## 文件依赖关系
```
main.py
├── asr_service.py (基础 ASR 识别)
├── diarization_service.py (说话人分离)
└── map_speaker.py (JSON 工具函数)
```
## 未清理的文件
- `server.py` - Web API 服务(独立功能)
- `test_asr.py` - 测试脚本(可保留)
- `example_usage.py` - 示例代码(已更新)