Args: config (dict, optional): 配置信息。默认为None。 mode (str, optional): 模式,'server' 或 'mobile'。默认为'mobile'。 backend (str): 'torch' 或 'onnx' onnx_model_path (str): ONNX模型路径(仅当backend='onnx'时需要) use_gpu (str, optional): GPU使用策略,可选值为'auto'
(self,
config=None,
mode='mobile',
backend='torch',
onnx_model_path=None,
use_gpu='auto',
numId=0)
| 193 | class OpenRecognizer: |
| 194 | |
| 195 | def __init__(self, |
| 196 | config=None, |
| 197 | mode='mobile', |
| 198 | backend='torch', |
| 199 | onnx_model_path=None, |
| 200 | use_gpu='auto', |
| 201 | numId=0): |
| 202 | """ |
| 203 | Args: |
| 204 | config (dict, optional): 配置信息。默认为None。 |
| 205 | mode (str, optional): 模式,'server' 或 'mobile'。默认为'mobile'。 |
| 206 | backend (str): 'torch' 或 'onnx' |
| 207 | onnx_model_path (str): ONNX模型路径(仅当backend='onnx'时需要) |
| 208 | use_gpu (str, optional): GPU使用策略,可选值为'auto'/'true'/'false'。默认为'auto'。 |
| 209 | numId (int, optional): 设备编号。默认为0。 |
| 210 | """ |
| 211 | |
| 212 | # Auto-switch backend for server mode |
| 213 | if mode == 'server' and backend != 'torch': |
| 214 | logger.warning( |
| 215 | f"Server mode only supports 'torch' backend, got '{backend}'. " |
| 216 | f"Automatically switching to 'torch' backend. " |
| 217 | f"Please make sure 'torch' and 'torchvision' are installed: " |
| 218 | f"pip install torch torchvision") |
| 219 | backend = 'torch' |
| 220 | |
| 221 | if config is None: |
| 222 | config_file = DEFAULT_CFG_PATH_REC_SERVER if mode == 'server' else DEFAULT_CFG_PATH_REC |
| 223 | config = Config(config_file).cfg |
| 224 | |
| 225 | # Parse use_gpu parameter |
| 226 | if use_gpu == 'auto': |
| 227 | try: |
| 228 | import torch |
| 229 | device = 'gpu' if torch.cuda.is_available() else 'cpu' |
| 230 | except: |
| 231 | device = 'cpu' |
| 232 | elif use_gpu == 'true': |
| 233 | device = 'gpu' |
| 234 | elif use_gpu == 'false': |
| 235 | device = 'cpu' |
| 236 | else: |
| 237 | raise ValueError(f"use_gpu must be 'auto', 'true', or 'false', got '{use_gpu}'") |
| 238 | |
| 239 | config['Global']['device'] = device |
| 240 | |
| 241 | self.cfg = config |
| 242 | # 公共初始化 |
| 243 | self._init_common() |
| 244 | backend = backend if config['Global'].get( |
| 245 | 'backend', None) is None else config['Global']['backend'] |
| 246 | self.backend = backend |
| 247 | if backend == 'torch': |
| 248 | import torch |
| 249 | self.torch = torch |
| 250 | self._init_torch_model(numId) |
| 251 | elif backend == 'onnx': |
| 252 | from tools.infer.onnx_engine import ONNXEngine |
nothing calls this directly
no test coverage detected