196 lines
5.0 KiB
Markdown
196 lines
5.0 KiB
Markdown
# 模型文件路径修复方案
|
||
|
||
## 🎯 **问题分析**
|
||
|
||
您遇到的"初始化失败,检查模型文件是否存在"问题是因为:
|
||
1. 应用尝试从项目的 `assets/models` 路径加载模型
|
||
2. 但模型文件实际在库内部的 `packages/yx_asr/assets/models` 路径
|
||
|
||
## ✅ **已完成的修复**
|
||
|
||
### 1. 更新库的初始化逻辑
|
||
修改了 `lib/src/yx_asr_service.dart` 中的路径处理:
|
||
|
||
```dart
|
||
// 修改前
|
||
final modelPath = config['modelPath'] as String? ?? 'assets/models';
|
||
|
||
// 修改后
|
||
final modelPath = config['modelPath'] as String? ?? 'packages/yx_asr/assets/models';
|
||
```
|
||
|
||
### 2. 更新便捷初始化方法
|
||
```dart
|
||
// 修改前
|
||
Future<bool> initializeWithDefaultModel([String modelPath = 'assets/models'])
|
||
|
||
// 修改后
|
||
Future<bool> initializeWithDefaultModel([String? modelPath]) async {
|
||
final defaultPath = modelPath ?? 'packages/yx_asr/assets/models';
|
||
return await initialize({'modelPath': defaultPath});
|
||
}
|
||
```
|
||
|
||
### 3. 简化 example 项目配置
|
||
- ✅ 移除了 `example/pubspec.yaml` 中的 assets 配置
|
||
- ✅ 更新了 `example/lib/main.dart` 使用 `initializeWithDefaultModel()`
|
||
|
||
## 🚀 **使用方法**
|
||
|
||
### 方法 1:使用默认模型(推荐)
|
||
```dart
|
||
final service = YxAsrService();
|
||
final initialized = await service.initializeWithDefaultModel();
|
||
```
|
||
|
||
### 方法 2:显式指定库内模型路径
|
||
```dart
|
||
final service = YxAsrService();
|
||
final initialized = await service.initialize({
|
||
'modelPath': 'packages/yx_asr/assets/models'
|
||
});
|
||
```
|
||
|
||
### 方法 3:使用自定义模型路径(如果项目有自己的模型)
|
||
```dart
|
||
final service = YxAsrService();
|
||
final initialized = await service.initialize({
|
||
'modelPath': 'assets/my_custom_models'
|
||
});
|
||
```
|
||
|
||
## 📁 **文件结构说明**
|
||
|
||
### 库内模型文件位置
|
||
```
|
||
yx_asr/
|
||
├── assets/models/
|
||
│ ├── encoder-epoch-99-avg-1.int8.onnx
|
||
│ ├── decoder-epoch-99-avg-1.int8.onnx
|
||
│ ├── joiner-epoch-99-avg-1.int8.onnx
|
||
│ └── tokens.txt
|
||
└── pubspec.yaml (配置了 assets)
|
||
```
|
||
|
||
### 运行时访问路径
|
||
```
|
||
packages/yx_asr/assets/models/encoder-epoch-99-avg-1.int8.onnx
|
||
packages/yx_asr/assets/models/decoder-epoch-99-avg-1.int8.onnx
|
||
packages/yx_asr/assets/models/joiner-epoch-99-avg-1.int8.onnx
|
||
packages/yx_asr/assets/models/tokens.txt
|
||
```
|
||
|
||
## 🔧 **验证修复**
|
||
|
||
### 1. 重新构建应用
|
||
```bash
|
||
cd example
|
||
flutter clean
|
||
flutter pub get
|
||
flutter build ios --no-codesign
|
||
```
|
||
|
||
### 2. 运行应用测试
|
||
```bash
|
||
flutter run -d "iPhone 15 Pro"
|
||
```
|
||
|
||
### 3. 检查初始化状态
|
||
应用启动后应该显示:
|
||
- ✅ 初始化成功
|
||
- ✅ 录音按钮可用
|
||
- ✅ 无"检查模型文件是否存在"错误
|
||
|
||
## 🐛 **故障排除**
|
||
|
||
### 如果仍然提示模型文件不存在
|
||
|
||
#### 检查 1:确认库的 pubspec.yaml 配置
|
||
```yaml
|
||
flutter:
|
||
assets:
|
||
- assets/models/
|
||
```
|
||
|
||
#### 检查 2:确认模型文件存在
|
||
```bash
|
||
ls -la assets/models/
|
||
# 应该看到:
|
||
# encoder-epoch-99-avg-1.int8.onnx
|
||
# decoder-epoch-99-avg-1.int8.onnx
|
||
# joiner-epoch-99-avg-1.int8.onnx
|
||
# tokens.txt
|
||
```
|
||
|
||
#### 检查 3:验证初始化代码
|
||
```dart
|
||
// 确保使用正确的初始化方法
|
||
final service = YxAsrService();
|
||
final initialized = await service.initializeWithDefaultModel();
|
||
|
||
if (!initialized) {
|
||
print('初始化失败');
|
||
} else {
|
||
print('初始化成功');
|
||
}
|
||
```
|
||
|
||
### 如果需要调试路径问题
|
||
|
||
#### 添加调试日志
|
||
```dart
|
||
Future<bool> initializeWithModel(String modelPath, {int sampleRate = 16000}) async {
|
||
print('🔍 尝试加载模型路径: $modelPath');
|
||
print('🔍 完整文件路径:');
|
||
print(' - encoder: $modelPath/encoder-epoch-99-avg-1.int8.onnx');
|
||
print(' - decoder: $modelPath/decoder-epoch-99-avg-1.int8.onnx');
|
||
print(' - joiner: $modelPath/joiner-epoch-99-avg-1.int8.onnx');
|
||
print(' - tokens: $modelPath/tokens.txt');
|
||
|
||
// 原有的初始化代码...
|
||
}
|
||
```
|
||
|
||
## 📱 **测试清单**
|
||
|
||
### 基础功能测试
|
||
- [ ] 应用启动无错误
|
||
- [ ] 初始化成功(无模型文件错误)
|
||
- [ ] 录音按钮可点击
|
||
- [ ] 权限申请正常
|
||
|
||
### 语音识别测试
|
||
- [ ] 点击录音按钮开始录音
|
||
- [ ] 说话时有实时反馈
|
||
- [ ] 停止录音后显示最终结果
|
||
- [ ] 多次录音测试稳定性
|
||
|
||
## 🎉 **预期结果**
|
||
|
||
修复后,您应该看到:
|
||
1. ✅ 应用启动时初始化成功
|
||
2. ✅ 无"检查模型文件是否存在"错误
|
||
3. ✅ 录音功能正常工作
|
||
4. ✅ 语音识别结果正确显示
|
||
|
||
## 💡 **最佳实践**
|
||
|
||
### 对于库开发者
|
||
1. **内置模型** - 将常用模型打包在库内
|
||
2. **灵活配置** - 支持自定义模型路径
|
||
3. **清晰文档** - 说明模型文件的使用方法
|
||
|
||
### 对于应用开发者
|
||
1. **使用默认模型** - 优先使用库内置模型
|
||
2. **错误处理** - 添加初始化失败的处理逻辑
|
||
3. **用户反馈** - 提供清晰的状态提示
|
||
|
||
## 🔄 **版本兼容性**
|
||
|
||
这个修复保持了向后兼容性:
|
||
- ✅ 现有的自定义路径仍然有效
|
||
- ✅ 新的默认行为使用库内模型
|
||
- ✅ 不需要修改现有的应用代码
|
||
|
||
**修复完成!现在您的应用应该能够正确加载库内置的模型文件了。** 🎊
|