加载自定义模型和字符集 Args: model_path: 模型文件路径 charset_path: 字符集文件路径 Returns: (推理会话, 字符集信息) Raises: ModelLoadError: 当加载失败时
(self, model_path: str, charset_path: str)
| 168 | raise ModelLoadError(f"检测模型加载失败: {str(e)}") from e |
| 169 | |
| 170 | def load_custom_model(self, model_path: str, charset_path: str) -> tuple: |
| 171 | """ |
| 172 | 加载自定义模型和字符集 |
| 173 | |
| 174 | Args: |
| 175 | model_path: 模型文件路径 |
| 176 | charset_path: 字符集文件路径 |
| 177 | |
| 178 | Returns: |
| 179 | (推理会话, 字符集信息) |
| 180 | |
| 181 | Raises: |
| 182 | ModelLoadError: 当加载失败时 |
| 183 | """ |
| 184 | try: |
| 185 | # 加载模型 |
| 186 | session = self.load_model(model_path) |
| 187 | |
| 188 | # 加载字符集信息 |
| 189 | if not os.path.exists(charset_path): |
| 190 | raise ModelLoadError(f"字符集文件不存在: {charset_path}") |
| 191 | |
| 192 | with open(charset_path, 'r', encoding="utf-8") as f: |
| 193 | charset_info = json.loads(f.read()) |
| 194 | |
| 195 | # 验证字符集信息格式 |
| 196 | required_keys = ['charset', 'word', 'image', 'channel'] |
| 197 | for key in required_keys: |
| 198 | if key not in charset_info: |
| 199 | raise ModelLoadError(f"字符集文件缺少必需字段: {key}") |
| 200 | |
| 201 | return session, charset_info |
| 202 | |
| 203 | except Exception as e: |
| 204 | raise ModelLoadError(f"自定义模型加载失败: {str(e)}") from e |
| 205 | |
| 206 | def validate_model_compatibility(self, session: onnxruntime.InferenceSession, |
| 207 | expected_input_shape: Optional[List[int]] = None) -> bool: |
no test coverage detected